Getting started with open source 3D Game Engine irrlicht

Source: Internet
Author: User

Getting started with open source 3D Game Engine irrlicht

 

Zhao Gang

 

 

Irrlicht (Chinese name: Ghost fire) is one of the famous 3D open source engines. The 3D engine has a clear structure, high execution efficiency, and easy to use. No matter how beginners learn 3D game engines, it is a good choice to develop small-scale 3D applications.

 

This article uses irrlicht 1.7.2 (the latest version) as an example to explain its usage in windows.

 

First you need to download irrlicht SDK in http://irrlicht.sourceforge.net/downloads.html, 1.7.2 SDK size is 24.6 MB, very lightweight, can be downloaded to the local in a few minutes.

 

After sdkis downloaded to the folder, it is an irrlicht-1.7.2.zip file, decompress the file (you can right-click the zip in the pop-up menu and select "decompress to the current folder", if the WinRAR software is installed, get a directory named irrlicht-1.7.2, enter the directory to obtain the following structure:

In bin, The Win32-VisualStudio directory already contains the compiled DLL, of course, if you need to make some modifications to the engine, you need to re-compile the SDK to generate the DLL, compile the SDK is very easy, go to the irrlicht directory under the source directory and find the solution files of Visual Studio, which are available in versions 7.1, 8.0, and 9.0. Taking Visual Studio 2008 as an example, you can double-click the irrlicht9.0.sln file to open the solution.

 

The solutions in Visual Studio 2008 are as follows:

You can try to generate a solution without modifying any code, and the compilation process should pass smoothly (there will be a lot of warning c4819, you can ignore them ), A new irrlicht is found under the Win32-VisualStudio directory in the bin. DLL.

 

If fatalError c1083 occurs during the compilation process: the file including "d3dx9shader" cannot be opened. H "indicates that you have not installed the DirectX SDK. If you must use DirectX as the underlying rendering API, you can

Http://msdn.microsoft.com/zh-cn/directx/ download a installation, DirectX SDK size is large 500 MB +, if not particularly fond of Microsoft products, You can do not use DirectX SDK directly with OpenGL, but before compilation to tell irrlicht, hi! I don't need DirectX. can I ignore it! Otherwise, irrlicht will persistently report errors, making compilation unsuccessful.

 

How can I tell irrlicht? Very simple,

Open the irrcompileconfig. h file under the include directory,

Find the # define_irr_compile_with_direct3d_9 _ sentence and comment it out.

 

The compilation will succeed. If the compilation fails, it will be unfortunate. I don't know why. I have compiled several versions and can compile them smoothly, check the problem of Visual Studio.

 

In fact, it is no pleasure to compile successfully, because it will be compiled successfully. If you modify a lot of code in the engine and compile it successfully, it is worth the joy.

 

If you want to see the irrlicht engine effect first, you can go to

Win32-visualstudio contains many. EXE files, the first of which is

01.helloworld.exe

Double-click to run the task. The following figure is displayed immediately:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

It looks ugly, but don't worry. This screen does not represent the strength of irrlicht. It only indicates that irrlicht runs normally on your computer, provide beautiful models and texture images.

 

There are also many other demos that can be used for testing, such as 02.quake3map.exe

The following images are much more beautiful than the helloworld images.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

When running 02.quake3map.exe, a console window is displayed first.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Select a, indicating that OpenGL is used as the underlying rendering API.

 

The mouse is hidden during running, so you cannot close the window by clicking "X" in the window. to close the running window and exit the program, you can press Alt + F4.

 

Be sure to see how to use irrlicht to write the simplest program: helloworld! Now, irrlicht comes with a helloworld as the Getting Started example. Let's open it and check it out.

 

Enter the examples directory to see the 01. helloworld directory. Double-click

Helloworld_vc9.vcproj can open the helloworld project. There is only one main. cpp file in the helloworld project. The file content is as follows (excluding the English comments ):

 

# Include
<Irrlicht. h>

Using namespace IRR;

Using namespace core;

Using namespace scene;

Using namespace video;

Using namespace IO;

Using namespace GUI;

# Ifdef _ irr_windows _

# Pragma
Comment (Lib, "irrlicht. lib ")

# Pragma
Comment (linker, "/subsystem: Windows/entry: maincrtstartup ")

# Endif

 

Int main ()

{

Irrlichtdevice * Device =

Createdevice (Video: edt_software, dimension2d <u32> (640,480 ),

16, false,
False, false, 0 );

 

If (! Device)

Return 1;

 

Device-> setwindowcaption (L "Hello world! -Irrlicht engine Demo ");

 

Ivideodriver * driver = device-> getvideodriver ();

Iscenemanager * smgr = device-> getscenemanager ();

Iguienvironment * guienv = device-> getguienvironment ();

Guienv-> addstatictext (L "Hello world! This is the irrlicht Software

Renderer! ", Rect <s32> (10, 10,), true );

 

Ianimatedmesh * mesh = smgr-> getmesh (".../../Media/sysydney. md2 ");

If (! Mesh)

{

Device-> drop ();

Return 1;

}

 

Ianimatedmeshscenenode * node =

Smgr-> addanimatedmeshscenenode (mesh );

 

If (node)

{

Node-> setmaterialflag (emf_lighting, false );

Node-> setmd2animation (Scene: emat_stand );

Node-> setmaterialtexture (0,

Driver-> gettexture (".../../Media/sydney.bmp "));

}

Smgr-> addcamerascenenode (0, vector3df (0, 30,-40 ),

Vector3df (0, 5, 0 ));

 

While (device-> Run ())

{

Driver-> beginscene (true,
True, scolor (255,100,101,140 ));

Smgr-> drawall ();

Guienv-> drawall ();

Driver-> endscene ();

}

 

Device-> drop ();

Return 0;

}

 

 

Before main (), it contains objects and uses namespaces and other life types:

# Pragma comment (linker, "/subsystem: Windows/entry: maincrtstartup") tells the compiler that when the program is running, do not bring up a console window and comment it out, if commented out, a black Console window will pop up.

 

After entering main (), the first thing to do is to create a device:

Irrlichtdevice * Device =

Createdevice (Video: edt_software, dimension2d <u32> (640,480 ),

16, false, 0 );

An irrlicht device is displayed as a window. If the device fails to be created, the program will have nothing to do next. Only one error can be reported and exited.

 

If (! Device)

Return 1;

 

If the device is successfully created, a window is displayed. The window size is 640*480. The video: edt_software parameter tells irrlicht to create a window using the software Renderer, if you want to use OpenGL as the underlying rendering API, you can change video: edt_software to video: edt_opengl.

 

The following sentence:

Device-> setwindowcaption (L "Hello world! -Irrlicht engine Demo "); set the window name, you can change it to the name you want.

 

Ivideodriver * driver = device-> getvideodriver ();

Iscenemanager * smgr = device-> getscenemanager ();

Iguienvironment * guienv = device-> getguienvironment ();

These three statements store the video driver object, scenario management object, and image User Interface object in the zero-hour variable for future use.

 

Guienv-> addstatictext (L "Hello world! This is the irrlicht Software

Renderer! ", Rect <s32> (10, 10,), true );

Displays a line of text at 10 or 10 points on the screen.

 

Ianimatedmesh * mesh = smgr-> getmesh (".../../Media/sysydney. md2 ");

To read a 3D model file and create a grid object with a skeleton animation.

 

If the creation fails (for example, the disk does not contain the so-called sysydney. if the md2 file is not deleted, the file exists and is successfully created ). Then there is nothing to show about the program.

 

If (! Mesh)

{

Device-> drop ();

Return 1;

}

 

Run device-> drop (); to release the device object before exiting. In fact, the device object will be released after the program is exited, but as a C ++ programmer, it is a good habit to release useless objects in time.

 

The following sentence:

Ianimatedmeshscenenode * node =

Smgr-> addanimatedmeshscenenode (mesh );

To understand why, all the objects visible in the scenario in irrlicht appear in scenenode mode, the mesh object just created from the disk is only an isolated data in the memory. It should be displayed by irrlicht, for example, adding it to the scene tree as a scenario node.

 

After the scene node is created, you can set some parameters for it:

If (node)

{

Node-> setmaterialflag (emf_lighting, false );

Node-> setmd2animation (Scene: emat_stand );

Node-> setmaterialtexture (0,

Driver-> gettexture (".../../Media/sydney.bmp "));

}

 

In these periods, we will set the material quality of the node to a closed light, stand up, and use sydney.bmp as the texture.

 

Smgr-> addcamerascenenode (0, vector3df (0, 30,-40 ),

Vector3df (0, 5, 0 ));

This tells the camera in the irrlicht scenario to be in the world coordinate (,-40). The orientation of the camera is (, 0)

 

Now that the rendered window is ready, the rendered object (3D model) is ready, and the camera is aligned, it is time to enter the endless rendering loop:

 

While (device-> Run ())

{

Driver-> beginscene (true,
True, scolor (255,100,101,140 ));

Smgr-> drawall ();

Guienv-> drawall ();

Driver-> endscene ();

}

 

The above while loop continues until you click X in the window or press Alt + F4 to exit the program.

 

After the loop ends, the device objects must be released before exiting the program to maintain a good C ++ programming style.

 

Run the program and you will see an ugly woman standing in the window, turning her body and looking at you from time to time. If you want to get rid of the program, it depends on your skill, no, it doesn't matter. I will continue to write irrlicht usage.

 

Write it here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.