This tutorial demonstrates how to load a Raytheon hammer 3 map into the engine, create a scenenode to optimize rendering speed, and create a user-controlled camera. Please note that you should first understand the basic knowledge of the engine before starting this tutorial. If you have not yet understood it, please first check the first tutorial, 1. helloworld.
The result of this example is as follows:
First, let's look at the example of helloworld: We include irrlicht header files and an additional file so that you can use some program classes on the console.
As mentioned in the helloworld example, all classes in the irrlicht engine are included in the namespace "IRR. In order not to add IRR: before each class name, we will tell the compiler to include the IRR namespace at the beginning.
This includes five namespaces: 'core', 'scene', 'video', 'IO ', and 'GU'. Therefore, we do not
You need to include these five namespaces like helloworld. Of course, if you like it, you can do the same as in the previous example. Just like the following code:
Next, in order for our program to use the irrlicht DLL file, we need to connect to the irrlicht. Lib library. We can set this option in project settings, but we use Pragma
Comment preprocessing will be simpler:
#pragma comment(lib, "Irrlicht.lib") |
Well, next we will start with the main () method. Here we will not use winmain (), because main is shorter.
Like in the helloworld example, we use createdevice () to create an irrlichtdevice. The difference is that we require the user to select a hardware acceleration driver. Of course, software devices can also draw a huge "Thunder hammer 3: map, but it is too slow. However, we provide methods we can choose to learn.
// Ask the user to select the driver video: e_driver_type drivertype; printf ("Please select the driver you want for this example: \ n" \ "(a) OpenGL 1.5 \ n (B) direct3d 9.0c \ n (c) direct3d 8.1 \ n "\" (d) Burning's software Renderer \ n (e) software Renderer \ n "\" (f) nulldevice \ n (otherkey) Exit \ n "); char I; STD: CIN> I; Switch (I) {Case 'A': drivertype = video :: edt_opengl; break; Case 'B': drivertype = video: edt_direct3d9; break; Case 'C': drivertype = video: edt_direct3d8; break; Case 'D ': drivertype = video: edt_burningsvideo; break; Case 'E': drivertype = video: edt_software; break; Case 'F': drivertype = video: edt_null; break; default: return 1;} // create a device and exit irrlichtdevice * Device = createdevice (drivertype, core: dimension2d <u32> (640,480) If creation fails; If (device = 0) return 1;
Get the pointer to the video driver and scenario management, so that we do not need to always write device-> getvideodriver () and device-> getscenemanager ().
video::IVideoDriver* driver = device->getVideoDriver();scene::ISceneManager* smgr = device->getSceneManager(); |
To display a map of Raytheon hammer 3, we need to load it first. You can see that A. ZIP file is packaged into A. pk3 format file.
Now we add the. pk3 file to our file system. After adding the file, we can read the map file from the package file as we store it on the hard disk.
device->getFileSystem()->addZipFileArchive("../../media/map-20kdm2.pk3"); |
Now, we can call getmesh () to load the mesh. We return an ianimatedmesh pointer. As you know, Raytheon hammer 3: maps are not real animations, but Static geometric blocks with additional materials.
Therefore, the ianimated mesh consists of only one frame. Therefore, we will use the quake level to obtain the "first frame" of the "Animation" and use addocttreescenenode () to create an octree scene node.
Octtree (octotree) Optimizes the scenario by drawing only the currently visible geometric shapes. If you use animatedmeshscenenode to replace the octtree here, the engine will not have any optimized mesh ry.
Try again: replace addocttreescenenode with addanimatedmeshscenenode, and then compare the two using the video driver (here we can use the ivideodriver class method getprimitivecountdrawed ), note that REE can only be used to draw a huge grid composed of multiple ry.
scene::IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");scene::ISceneNode* node = 0;if (mesh) node = smgr->addOctTreeSceneNode(mesh->getMesh(0)); |
Since the grid is not based on (0, 0, 0), we will convert it here.
if (node) node->setPosition(core::vector3df(-1300,-144,-1249)); |
Now we only need a camera to view the map of Raytheon 3. We want to create a user-controlled camera. The irrlicht engine provides different types. For example, the Maya camera can control the rotation of the camera by pressing the left mouse button. When two buttons are pressed, the camera is enlarged and right-clicked to move the camera. This can be created through addcamerascenenodemaya. But in this example, we want to create a camera behavior like in the first-person shooter game (FPS:
smgr->addCameraSceneNodeFPS(); |
The mouse and cursor need to be hidden, so here we set it to invisible.
device->getCursorControl()->setVisible(false); |
Well, we have done everything we need. Now we need to draw it out. Then we draw the FPS information of each frame on the window title. Here, 'If (device-> iswindowactive () 'is optional. However, to prevent other programs from being activated, engine rendering is required to change the cursor position.
Int lastfps =-1; while (device-> Run () {If (device-> iswindowactive () {driver-> beginscene (true, true, video :: scolor (255,200,200,200); smgr-> drawall (); driver-> endscene (); int FPS = driver-> getfps (); If (lastfps! = FPS) {core: stringw STR = l "irrlicht engine-quake 3 map example ["; STR + = driver-> getname (); STR + = "] FPS: "; STR + = FPS; device-> setwindowcaption (Str. c_str (); lastfps = FPS ;}} else device-> yield (); // IRR: irrlichtdevice: yield () will avoid window inactive, busy cycles consume all CPU cycles. }Finally, delete the irrlicht device.
device->drop(); return 0;} |
That's it. Compile and test this program.