Game Programming With Irrlicht Pdf Download
Download directx 9 3d or read online here in PDF or EPUB. Introduction to 3D Game Programming with DirectX 9.0c: A Shader Approach presents an introduction to programming interactive computer graphics, with an emphasis on game development, using real-time shaders with DirectX 9.0. The book is divided into three parts that explain basic. Advanced UNIX Programming PDF Online. Animate that! - The principles in depth - An essential book for novice animators PDF Download.
Game Programming With Irrlicht Pdf Download Windows 10
Irrlicht 1.7 Realtime 3D Engine Beginner’s Guide
Aung Sithu Kyaw Johannes Stein
Chapter No.3 'Loading Meshes'
In this package, you will find: A Biography of the author of the book A preview chapter from the book, Chapter NO.3 'Loading Meshes' A synopsis of the book’s content Information on where to buy this book
About the Author Aung Sithu Kyaw is originally from Myanmar (Burma). He has been a developer in the software development industry for over seven years and has a great passion for graphics programming, creating video games, writing, and sharing knowledge with others. He holds a M.Sc (Digital Media Technology) from Nanyang Technological University (NTU), Singapore. Aung is currently the CEO of a digital media company, Rival Edge Pte Ltd, Singapore, which he cofounded in 2011 with two other engineers. Visit http://rivaledge.sg for more information. He can be followed on twitter @aungsithu and his LinkedIn profile http://www. linkedin.com/in/aungsithu.
Irrlicht 1.7 Realtime 3D Engine Beginner’s Guide Irrlicht 1.7 Realtime 3D Engine Beginner's Guide will teach you to master all that is required to create 2D and 3D applications using Irrlicht, beginning right from installation, and proceeding step-by-step to deployment. Beginning with installation, this book guides you through creating a basic template application, followed by using meshes, creating overlays, and UI. You will then scan through data types, nodes, scenes, camera, lights, and particle systems. Finally, you will learn about some advanced concepts such as handling data, files, and shaders, followed by the last stage—deployment—in an appendix. This book is a step-by-step guide to Irrlicht that starts at an easy level for beginners and then gradually works to more advanced topics through clear code examples and a number of demos, which illustrate theoretical concepts.
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
What This Book Covers Chapter 1, Installing Irrlicht, shows how to get and set up Irrlicht across different platforms such as Windows, Max, and Linux. Chapter 2, Creating a Basic Template Application, explains how to set up and create a basic Irrlicht application using different Integrated Development Environments (IDEs) such as Microsoft Visual Studio, XCode, CodeBlocks, and so on. Chapter 3, Loading Meshes, shows how to add 3D meshes to our application, apply materials, and animate them. Chapter 4, Overlays and User Interface, explores the 2D capabilities of Irrlicht, creating graphical user interface (GUI), loading, and rendering 2D images. Chapter 5, Understanding Data Types, provides a primer on C++ templates as well as some mathematical concepts such as vectors. Chapter 6, Managing Scenes, introduces using CopperCube, which is a visual 3D scene editor for Irrlicht available for both free and commercial use. Chapter 7, Using Nodes—The Basic Objects of the Irrlicht 3D engine, explains what the nodes in Irrlicht are and how to use animators to do some basic animations. Chapter 8, Managing Cameras, shows how to use camera scene nodes and touches a little bit on creating a simple terrain to test the walkthrough camera. Chapter 9, Lightening the Scene, introduces lighting concepts, materials, shadows, and using CopperCube/IrrEdit. Chapter 10, Creating Eye Candy Effects with Particle Systems, shows how to implement particle systems both in the visual editor and from the code. Chapter 11, Handling Data and Files, explains how to save and load game-related data files such as player save files. Chapter 12, Using Shaders in Irrlicht, introduces how the graphics rendering pipeline works and practical usage of shaders in Irrlicht. The Appendix, Deploying an Irrlicht Application, explains the procedures and essential dependencies to be included while packaging an Irrlicht application for different platforms such as Windows, Mac, or Linux.
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
3
Loading Meshes Now that we know how to set up our Irrlicht development environment, it is me to get started with programming our first applica on with Irrlicht.
In this chapter, we shall:
Learn what a polygon mesh is
Load a mesh from Irrlicht's media folder
Apply a texture to our mesh
Load and access our mesh in our applica on
Animate our mesh
Model a mesh using Blender
So, let's take a look at it...
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
What is a mesh? A polygon mesh is basically a construct of ver ces, faces, and edges that defines the shape of an object which are then rendered on the screen by one of the chosen renderers that Irrlicht provides:
You might already know that there are different file formats for storing mesh data, for example, OBJ, MD2, MD3, and so on. Irrlicht supports 15 different file formats out of the box, so you should not run into problems when expor ng your own model. These formats are 3DS, B3D, BSP, IRRMESH, LMTS, LWO, OBJ, MD2, MD3, MS3D, MY3D, OGRE, PLY, STL, and the X file format. Frequently used file formats are explained later in this chapter. If you want to import a format that Irrlicht doesn't support, you can always write your own mesh file loader by extending the IMeshLoader interface that Irrlicht has provided. And hopefully, you can contribute back to the community again by distribu ng your newlycreated loader class for Irrlicht. It's always a good idea to involve yourself in forums if you need that kind of support. Somebody probably must have already wri en what you need and posted it on the forum.
[ 50 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Chapter 3
Time for action – loading a mesh Now, we are going to load and display a mesh in our applica on. Let's take the template applica on, we made in Chapter 2, Crea ng a Basic Template Applica on and take it a bit further:
1.
Open the media folder of your extracted Irrlicht package.
2.
Copy sydney.md2 and sydney.bmp to where the executable of your new applica on is: Note for Xcode users: Right-click on the Xcode project and Add | Exis ng Files... and select sydney.md2 and sydney.bmp. Check the op on Copy items into des na on group's folder (if needed). Make sure those two files are listed in Copy Bundle Resources in your build target.
3.
Add the line ISceneManager* smgr = device->getSceneManager(); directly a er where we create our instance to the video driver.
4.
To actually load a mesh, insert IMesh* mesh = smgr->getMesh('sydney.md2').
5.
Now that our mesh is loaded, we s ll can't see our mesh. We need a node to display the mesh in our scene. Let's create one by adding this code: IMeshSceneNode* node = smgr->addMeshSceneNode(mesh).
6.
Our node is added to the scene, but we need to adjust the camera to be able to actually see the mesh. Add this line: smgr->addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0)).
7.
Add the line smgr->drawAll(); into our 'game loop' between the beginScene() and endScene() func on.
[ 51 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
Your complete source code should look like this: #include using using using using
namespace namespace namespace namespace
irr; core; video; scene;
#if defined(_MSC_VER) #pragma comment(lib, 'Irrlicht.lib') #endif int main() { IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2d(640, 480), 16, false, false, false, 0); if (!device) return 1; IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IMesh* mesh = smgr->getMesh('sydney.md2'); IMeshSceneNode* node = smgr->addMeshSceneNode(mesh); smgr->addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0)); while (device->run()) { driver->beginScene(true, true, SColor(255, 255, 255, 255)); smgr->drawAll(); driver->endScene(); } device->drop(); return 0; }
[ 52 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Chapter 3
What just happened? We just copied a ready-to-go mesh from the Irrlicht media folder to demonstrate some of the possibili es of what we can do with meshes. In step 3, we retrieved the instance of a scene manager from our Irrlicht device object. A scene manager is responsible for everything inside our scene, including the meshes we want to render. It holds several child nodes, some of which are drawable objects while others are used just for references such as camera nodes. A er that we load our mesh file and store it in an IMesh object. But, to actually draw this mesh on screen, we need to add this mesh as a child node under our scene manager so that it knows about the existence of our mesh node. Step 5 shows how to add that node to the scene manager with the ini al posi on and rota on vectors. In step 6, we set up a camera to be able to see the mesh. The first parameter is the parent scene node, which we leave at 0 at the moment, because this camera does not have a parent node. We could a ach our mesh scene node as the parent to make our camera follow wherever our mesh goes. The second parameter is the posi on of the camera in the scene, while the third parameter is the point where the camera looks at. The drawAll() func on is the most important line in this code snippet, it makes sure that everything we have added to the scene manager will be drawn.
[ 53 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
Differences between mesh formats In this example, we added a mesh with the extension MD2. Let's take a quick look at the most popular mesh formats and their advantages and disadvantages:
OBJ The OBJ file format has been developed by Wavefront Technologies. It is one of the simplest mesh formats and stores a list of ver ces, normals, and texture coordinates. There can be a reference to a material file, but the material or any other data, besides the object geometry itself, is not stored in this file format. An OBJ file can be viewed and edited in any text editor.
MD2/MD3 These file formats were created by ID So ware for Quake 2 and Quake 3 respec vely. They are very common in game development and can store addi onally to the geometry itself as an anima on data.
COLLADA COLLADA is a rela vely new mesh format that uses XML file specifica ons and can be easily edited using any text editor. COLLADA files can store not only a single mesh, but also the whole scene with mul ple meshes together with lights, cameras, and so on. Irrlicht loader for COLLADA files can set up a scene as described in the file.
X This is the mesh file format developed by Microso along with DirectX. It can be saved as text or binary and can also store anima ons and skeleton informa on as well, in addi on to the geometry data.
Using textures At the moment, we just have a blackish mesh on a white background. This is because MD2 and MD3 formats don't have material informa on on which textures to load. So let's try to change that by loading the required texture manually. If we use a mesh file format that contains material informa on such as ninja.b3d from media directory, it would load the texture automa cally with the mesh.
[ 54 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Chapter 3
Time for action – applying texture to a mesh Our mesh looks a bit dull at the moment, now we are going to try to add a texture. Think of textures as clothes for meshes.
1.
Go to the line where you added the mesh to the scene node.
2.
Add the following code directly a er that: if (node) { node->setMaterialFlag(EMF_LIGHTING, false); node->setMaterialTexture(0, driver->getTexture('sydney.bmp')); }
[ 55 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
And this is what it should look like: #include using using using using
namespace namespace namespace namespace
irr; core; video; scene;
#if defined(_MSC_VER) #pragma comment(lib, 'Irrlicht.lib') #endif int main() { IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2d(640, 480), 16, false, false, false, 0); if (!device) return 1; IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IMesh* mesh = smgr->getMesh('sydney.md2'); IMeshSceneNode* node = smgr->addMeshSceneNode(mesh); if (node) { node->setMaterialFlag(EMF_LIGHTING, false); node->setMaterialTexture(0, driver>getTexture('sydney.bmp')); } smgr->addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0)); while (device->run()) { driver->beginScene(true, true, SColor(255, 255, 255, 255)); smgr->drawAll(); driver->endScene(); } device->drop(); return 0; }
[ 56 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Chapter 3
What just happened? At first, we are going to check if the mesh node object is successfully loaded and if so con nue with the actual code. The first line sets a material flag. Because we don't have any lights set in the scene, we are going to disable ligh ng on materials. If this flag would s ll be set to true, the whole model would s ll be displayed in black. In the second line, we assign a texture as a material for this node. The first parameter stands for the layer of the texture and as we don't have any layered textures, we just set it to zero. The second parameter requires a texture interface pointer.
Time for action – manipulating our mesh Now that we have a textured mesh object on the screen, let's try to rotate, scale, and update the posi on of our mesh:
1.
Go to the line where we check if our node has been assigned.
2.
Add node->setRotation(vector3df(0.0f, -70.0f, 0.0f)); a er we set the material of the mesh.
3.
Add node->setPosition(vector3df(10.0f, -10.0f, 0.0f));.
4.
Add the line node->setScale(vector3df(0.5f, 1.4f, 1.0f));:
[ 57 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
If you have had any problem, take a look at the full source code of this example: #include using using using using
namespace namespace namespace namespace
irr; core; video; scene;
#if defined(_MSC_VER) #pragma comment(lib, 'Irrlicht.lib') #endif int main() { IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2d(640, 480), 16, false, false, false, 0); if (!device) return 1; IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IMesh* mesh = smgr->getMesh('sydney.md2'); IMeshSceneNode* node = smgr->addMeshSceneNode(mesh); if (node) { node->setMaterialFlag(EMF_LIGHTING, false); node->setMaterialTexture(0, driver>getTexture('sydney.bmp')); node->setRotation(vector3df(0.0f, -70.0f, 0.0f)); node->setPosition(vector3df(10.0f, -10.0f, 0.0f)); node->setScale(vector3df(0.5f, 1.4f, 1.0f)); } smgr->addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0)); while (device->run()) { driver->beginScene(true, true, SColor(255, 255, 255, 255)); smgr->drawAll(); driver->endScene(); } device->drop(); return 0; } [ 58 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Chapter 3
What just happened? Now we have mesh that rotated -70 degrees around the Y-axis, hideously scaled, and moved 10 units on the X-axis and -10 units on the Y-axis. The most important thing here is that we don't actually manipulate the mesh itself, but the scene node with the mesh. Each of these func ons we have been introduced to require a 3D vector as a parameter. Create one using the vector3df() func on, whose parameters are the X, Y, and Z values.
Time for action – animating our mesh Let's take our code that we used before we accessed and manipulated our mesh. Fortunately, the exemplary sydney.md2 file already contains some anima ons for us to play with. We are going to use an animated mesh instead of our sta c mesh and add an anima on:
1.
Change all references from IMesh to IAnimatedMesh.
2.
Change all references from IMeshSceneNode to IAnimatedMeshSceneNode.
3.
Change all references from addMeshSceneNode to addAnimatedMeshSceneNode.
4.
Add node->setMD2Animation(scene::EMAT_ATTACK); a er the place where we check if the node has been assigned.
5.
If you think the anima on is running too fast, you can adjust it by using node>setAnimationSpeed(25); method and passing the speed you want to use.
[ 59 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
6.
Compile and run the applica on to enjoy our first animated mesh:
The complete source code should look something like this: #include using using using using
namespace namespace namespace namespace
irr; core; video; scene;
#if defined(_MSC_VER) #pragma comment(lib, 'Irrlicht.lib') #endif int main() { IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2d(640, 480), 16, false, false, false, 0); [ 60 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Chapter 3 if (!device) return 1; IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IAnimatedMesh* mesh = smgr->getMesh('sydney.md2'); IAnimatedMeshSceneNode* node = smgr>addAnimatedMeshSceneNode(mesh); if (node) { node->setMD2Animation(EMAT_ATTACK); node->setAnimationSpeed(25); node->setMaterialFlag(EMF_LIGHTING, false); node->setMaterialTexture(0, driver>getTexture('sydney.bmp')); } smgr->addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0)); while (device->run()) { driver->beginScene(true, true, SColor(255, 255, 255, 255)); smgr->drawAll(); driver->endScene(); } device->drop(); return 0; }
What just happened? An animated mesh is a derived object from IMesh. To be able to use anima ons, we have to use an animated scene node. We need to set the anima on we want to play using setMD2Animation. The Quake 2 mesh file is able to store up to nearly a dozen different anima ons from standing and running to a acking and dying. If you use a different mesh format than MD2, you need to call the node->setFrameLoop(0, 15); method specifying the posi on of frames for the par cular anima on of that mesh file.
[ 61 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
Have a go hero – switching animations In the above example, we play the a ack anima on with an infinite loop. Now try to play the stand anima on. Disable the infinite loop and switch to run anima on once the stand anima on has finished playing. Hint: you'll need to use setLoopMode() and setAnimationEndCallback() methods of the animated mesh scene node. Plus, you'll also need to implement your own AnimationEndCallback class extending from IAnimationEndCallback.
Pop quiz 1. You want to use a mesh file format that Irrlicht doesn't support currently. To import this format into the engine, you can implement your own reader class deriving from: a. IMeshLoader b. IAnimatedMesh c.
ISceneManager
2. You have an animated mesh loaded into the scene. Which of the following is responsible for manipula ng that animated mesh's a ributes such as posi on, rota on, scale, and so on? a. IMesh b. IMeshSceneNode c.
IAnimatedMesh
Summary In this chapter, we explored different types of mesh and how to work with them in Irrlicht. In par cular, we've covered:
Loading and manipula ng a mesh in Irrlicht
Dressing up meshes with textures
File formats
Anima ng a mesh
Now that we've learned how to load meshes, we should get deeper into 3D graphics programming. To do so, we need to understand more about vectors, points, and other data types available in Irrlicht. We'll look into that in the next chapter. [ 62 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Where to buy this book You can buy Irrlicht 1.7 Realtime 3D Engine Beginner’s Guide from the Packt Publishing website: http://www.packtpub.com/irrlicht-171-realtime-3dengine-beginners-guide/book. Free shipping to the US, UK, Europe and selected Asian countries. For more information, please read our shipping policy.
Alternatively, you can buy the book from Amazon, BN.com, Computer Manuals and most internet book retailers.
www.PacktPub.com
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Aung Sithu Kyaw Johannes Stein
Chapter No.3 'Loading Meshes'
In this package, you will find: A Biography of the author of the book A preview chapter from the book, Chapter NO.3 'Loading Meshes' A synopsis of the book’s content Information on where to buy this book
About the Author Aung Sithu Kyaw is originally from Myanmar (Burma). He has been a developer in the software development industry for over seven years and has a great passion for graphics programming, creating video games, writing, and sharing knowledge with others. He holds a M.Sc (Digital Media Technology) from Nanyang Technological University (NTU), Singapore. Aung is currently the CEO of a digital media company, Rival Edge Pte Ltd, Singapore, which he cofounded in 2011 with two other engineers. Visit http://rivaledge.sg for more information. He can be followed on twitter @aungsithu and his LinkedIn profile http://www. linkedin.com/in/aungsithu.
Irrlicht 1.7 Realtime 3D Engine Beginner’s Guide Irrlicht 1.7 Realtime 3D Engine Beginner's Guide will teach you to master all that is required to create 2D and 3D applications using Irrlicht, beginning right from installation, and proceeding step-by-step to deployment. Beginning with installation, this book guides you through creating a basic template application, followed by using meshes, creating overlays, and UI. You will then scan through data types, nodes, scenes, camera, lights, and particle systems. Finally, you will learn about some advanced concepts such as handling data, files, and shaders, followed by the last stage—deployment—in an appendix. This book is a step-by-step guide to Irrlicht that starts at an easy level for beginners and then gradually works to more advanced topics through clear code examples and a number of demos, which illustrate theoretical concepts.
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
What This Book Covers Chapter 1, Installing Irrlicht, shows how to get and set up Irrlicht across different platforms such as Windows, Max, and Linux. Chapter 2, Creating a Basic Template Application, explains how to set up and create a basic Irrlicht application using different Integrated Development Environments (IDEs) such as Microsoft Visual Studio, XCode, CodeBlocks, and so on. Chapter 3, Loading Meshes, shows how to add 3D meshes to our application, apply materials, and animate them. Chapter 4, Overlays and User Interface, explores the 2D capabilities of Irrlicht, creating graphical user interface (GUI), loading, and rendering 2D images. Chapter 5, Understanding Data Types, provides a primer on C++ templates as well as some mathematical concepts such as vectors. Chapter 6, Managing Scenes, introduces using CopperCube, which is a visual 3D scene editor for Irrlicht available for both free and commercial use. Chapter 7, Using Nodes—The Basic Objects of the Irrlicht 3D engine, explains what the nodes in Irrlicht are and how to use animators to do some basic animations. Chapter 8, Managing Cameras, shows how to use camera scene nodes and touches a little bit on creating a simple terrain to test the walkthrough camera. Chapter 9, Lightening the Scene, introduces lighting concepts, materials, shadows, and using CopperCube/IrrEdit. Chapter 10, Creating Eye Candy Effects with Particle Systems, shows how to implement particle systems both in the visual editor and from the code. Chapter 11, Handling Data and Files, explains how to save and load game-related data files such as player save files. Chapter 12, Using Shaders in Irrlicht, introduces how the graphics rendering pipeline works and practical usage of shaders in Irrlicht. The Appendix, Deploying an Irrlicht Application, explains the procedures and essential dependencies to be included while packaging an Irrlicht application for different platforms such as Windows, Mac, or Linux.
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
3
Loading Meshes Now that we know how to set up our Irrlicht development environment, it is me to get started with programming our first applica on with Irrlicht.
In this chapter, we shall:
Learn what a polygon mesh is
Load a mesh from Irrlicht's media folder
Apply a texture to our mesh
Load and access our mesh in our applica on
Animate our mesh
Model a mesh using Blender
So, let's take a look at it...
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
What is a mesh? A polygon mesh is basically a construct of ver ces, faces, and edges that defines the shape of an object which are then rendered on the screen by one of the chosen renderers that Irrlicht provides:
You might already know that there are different file formats for storing mesh data, for example, OBJ, MD2, MD3, and so on. Irrlicht supports 15 different file formats out of the box, so you should not run into problems when expor ng your own model. These formats are 3DS, B3D, BSP, IRRMESH, LMTS, LWO, OBJ, MD2, MD3, MS3D, MY3D, OGRE, PLY, STL, and the X file format. Frequently used file formats are explained later in this chapter. If you want to import a format that Irrlicht doesn't support, you can always write your own mesh file loader by extending the IMeshLoader interface that Irrlicht has provided. And hopefully, you can contribute back to the community again by distribu ng your newlycreated loader class for Irrlicht. It's always a good idea to involve yourself in forums if you need that kind of support. Somebody probably must have already wri en what you need and posted it on the forum.
[ 50 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Chapter 3
Time for action – loading a mesh Now, we are going to load and display a mesh in our applica on. Let's take the template applica on, we made in Chapter 2, Crea ng a Basic Template Applica on and take it a bit further:
1.
Open the media folder of your extracted Irrlicht package.
2.
Copy sydney.md2 and sydney.bmp to where the executable of your new applica on is: Note for Xcode users: Right-click on the Xcode project and Add | Exis ng Files... and select sydney.md2 and sydney.bmp. Check the op on Copy items into des na on group's folder (if needed). Make sure those two files are listed in Copy Bundle Resources in your build target.
3.
Add the line ISceneManager* smgr = device->getSceneManager(); directly a er where we create our instance to the video driver.
4.
To actually load a mesh, insert IMesh* mesh = smgr->getMesh('sydney.md2').
5.
Now that our mesh is loaded, we s ll can't see our mesh. We need a node to display the mesh in our scene. Let's create one by adding this code: IMeshSceneNode* node = smgr->addMeshSceneNode(mesh).
6.
Our node is added to the scene, but we need to adjust the camera to be able to actually see the mesh. Add this line: smgr->addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0)).
7.
Add the line smgr->drawAll(); into our 'game loop' between the beginScene() and endScene() func on.
[ 51 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
Your complete source code should look like this: #include using using using using
namespace namespace namespace namespace
irr; core; video; scene;
#if defined(_MSC_VER) #pragma comment(lib, 'Irrlicht.lib') #endif int main() { IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2d(640, 480), 16, false, false, false, 0); if (!device) return 1; IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IMesh* mesh = smgr->getMesh('sydney.md2'); IMeshSceneNode* node = smgr->addMeshSceneNode(mesh); smgr->addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0)); while (device->run()) { driver->beginScene(true, true, SColor(255, 255, 255, 255)); smgr->drawAll(); driver->endScene(); } device->drop(); return 0; }
[ 52 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Chapter 3
What just happened? We just copied a ready-to-go mesh from the Irrlicht media folder to demonstrate some of the possibili es of what we can do with meshes. In step 3, we retrieved the instance of a scene manager from our Irrlicht device object. A scene manager is responsible for everything inside our scene, including the meshes we want to render. It holds several child nodes, some of which are drawable objects while others are used just for references such as camera nodes. A er that we load our mesh file and store it in an IMesh object. But, to actually draw this mesh on screen, we need to add this mesh as a child node under our scene manager so that it knows about the existence of our mesh node. Step 5 shows how to add that node to the scene manager with the ini al posi on and rota on vectors. In step 6, we set up a camera to be able to see the mesh. The first parameter is the parent scene node, which we leave at 0 at the moment, because this camera does not have a parent node. We could a ach our mesh scene node as the parent to make our camera follow wherever our mesh goes. The second parameter is the posi on of the camera in the scene, while the third parameter is the point where the camera looks at. The drawAll() func on is the most important line in this code snippet, it makes sure that everything we have added to the scene manager will be drawn.
[ 53 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
Differences between mesh formats In this example, we added a mesh with the extension MD2. Let's take a quick look at the most popular mesh formats and their advantages and disadvantages:
OBJ The OBJ file format has been developed by Wavefront Technologies. It is one of the simplest mesh formats and stores a list of ver ces, normals, and texture coordinates. There can be a reference to a material file, but the material or any other data, besides the object geometry itself, is not stored in this file format. An OBJ file can be viewed and edited in any text editor.
MD2/MD3 These file formats were created by ID So ware for Quake 2 and Quake 3 respec vely. They are very common in game development and can store addi onally to the geometry itself as an anima on data.
COLLADA COLLADA is a rela vely new mesh format that uses XML file specifica ons and can be easily edited using any text editor. COLLADA files can store not only a single mesh, but also the whole scene with mul ple meshes together with lights, cameras, and so on. Irrlicht loader for COLLADA files can set up a scene as described in the file.
X This is the mesh file format developed by Microso along with DirectX. It can be saved as text or binary and can also store anima ons and skeleton informa on as well, in addi on to the geometry data.
Using textures At the moment, we just have a blackish mesh on a white background. This is because MD2 and MD3 formats don't have material informa on on which textures to load. So let's try to change that by loading the required texture manually. If we use a mesh file format that contains material informa on such as ninja.b3d from media directory, it would load the texture automa cally with the mesh.
[ 54 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Chapter 3
Time for action – applying texture to a mesh Our mesh looks a bit dull at the moment, now we are going to try to add a texture. Think of textures as clothes for meshes.
1.
Go to the line where you added the mesh to the scene node.
2.
Add the following code directly a er that: if (node) { node->setMaterialFlag(EMF_LIGHTING, false); node->setMaterialTexture(0, driver->getTexture('sydney.bmp')); }
[ 55 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
And this is what it should look like: #include using using using using
namespace namespace namespace namespace
irr; core; video; scene;
#if defined(_MSC_VER) #pragma comment(lib, 'Irrlicht.lib') #endif int main() { IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2d(640, 480), 16, false, false, false, 0); if (!device) return 1; IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IMesh* mesh = smgr->getMesh('sydney.md2'); IMeshSceneNode* node = smgr->addMeshSceneNode(mesh); if (node) { node->setMaterialFlag(EMF_LIGHTING, false); node->setMaterialTexture(0, driver>getTexture('sydney.bmp')); } smgr->addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0)); while (device->run()) { driver->beginScene(true, true, SColor(255, 255, 255, 255)); smgr->drawAll(); driver->endScene(); } device->drop(); return 0; }
[ 56 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Chapter 3
What just happened? At first, we are going to check if the mesh node object is successfully loaded and if so con nue with the actual code. The first line sets a material flag. Because we don't have any lights set in the scene, we are going to disable ligh ng on materials. If this flag would s ll be set to true, the whole model would s ll be displayed in black. In the second line, we assign a texture as a material for this node. The first parameter stands for the layer of the texture and as we don't have any layered textures, we just set it to zero. The second parameter requires a texture interface pointer.
Time for action – manipulating our mesh Now that we have a textured mesh object on the screen, let's try to rotate, scale, and update the posi on of our mesh:
1.
Go to the line where we check if our node has been assigned.
2.
Add node->setRotation(vector3df(0.0f, -70.0f, 0.0f)); a er we set the material of the mesh.
3.
Add node->setPosition(vector3df(10.0f, -10.0f, 0.0f));.
4.
Add the line node->setScale(vector3df(0.5f, 1.4f, 1.0f));:
[ 57 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
If you have had any problem, take a look at the full source code of this example: #include using using using using
namespace namespace namespace namespace
irr; core; video; scene;
#if defined(_MSC_VER) #pragma comment(lib, 'Irrlicht.lib') #endif int main() { IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2d(640, 480), 16, false, false, false, 0); if (!device) return 1; IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IMesh* mesh = smgr->getMesh('sydney.md2'); IMeshSceneNode* node = smgr->addMeshSceneNode(mesh); if (node) { node->setMaterialFlag(EMF_LIGHTING, false); node->setMaterialTexture(0, driver>getTexture('sydney.bmp')); node->setRotation(vector3df(0.0f, -70.0f, 0.0f)); node->setPosition(vector3df(10.0f, -10.0f, 0.0f)); node->setScale(vector3df(0.5f, 1.4f, 1.0f)); } smgr->addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0)); while (device->run()) { driver->beginScene(true, true, SColor(255, 255, 255, 255)); smgr->drawAll(); driver->endScene(); } device->drop(); return 0; } [ 58 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Chapter 3
What just happened? Now we have mesh that rotated -70 degrees around the Y-axis, hideously scaled, and moved 10 units on the X-axis and -10 units on the Y-axis. The most important thing here is that we don't actually manipulate the mesh itself, but the scene node with the mesh. Each of these func ons we have been introduced to require a 3D vector as a parameter. Create one using the vector3df() func on, whose parameters are the X, Y, and Z values.
Time for action – animating our mesh Let's take our code that we used before we accessed and manipulated our mesh. Fortunately, the exemplary sydney.md2 file already contains some anima ons for us to play with. We are going to use an animated mesh instead of our sta c mesh and add an anima on:
1.
Change all references from IMesh to IAnimatedMesh.
2.
Change all references from IMeshSceneNode to IAnimatedMeshSceneNode.
3.
Change all references from addMeshSceneNode to addAnimatedMeshSceneNode.
4.
Add node->setMD2Animation(scene::EMAT_ATTACK); a er the place where we check if the node has been assigned.
5.
If you think the anima on is running too fast, you can adjust it by using node>setAnimationSpeed(25); method and passing the speed you want to use.
[ 59 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
6.
Compile and run the applica on to enjoy our first animated mesh:
The complete source code should look something like this: #include using using using using
namespace namespace namespace namespace
irr; core; video; scene;
#if defined(_MSC_VER) #pragma comment(lib, 'Irrlicht.lib') #endif int main() { IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2d(640, 480), 16, false, false, false, 0); [ 60 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Chapter 3 if (!device) return 1; IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); IAnimatedMesh* mesh = smgr->getMesh('sydney.md2'); IAnimatedMeshSceneNode* node = smgr>addAnimatedMeshSceneNode(mesh); if (node) { node->setMD2Animation(EMAT_ATTACK); node->setAnimationSpeed(25); node->setMaterialFlag(EMF_LIGHTING, false); node->setMaterialTexture(0, driver>getTexture('sydney.bmp')); } smgr->addCameraSceneNode(0, vector3df(0, 30, -40), vector3df(0, 5, 0)); while (device->run()) { driver->beginScene(true, true, SColor(255, 255, 255, 255)); smgr->drawAll(); driver->endScene(); } device->drop(); return 0; }
What just happened? An animated mesh is a derived object from IMesh. To be able to use anima ons, we have to use an animated scene node. We need to set the anima on we want to play using setMD2Animation. The Quake 2 mesh file is able to store up to nearly a dozen different anima ons from standing and running to a acking and dying. If you use a different mesh format than MD2, you need to call the node->setFrameLoop(0, 15); method specifying the posi on of frames for the par cular anima on of that mesh file.
[ 61 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Loading Meshes
Have a go hero – switching animations In the above example, we play the a ack anima on with an infinite loop. Now try to play the stand anima on. Disable the infinite loop and switch to run anima on once the stand anima on has finished playing. Hint: you'll need to use setLoopMode() and setAnimationEndCallback() methods of the animated mesh scene node. Plus, you'll also need to implement your own AnimationEndCallback class extending from IAnimationEndCallback.
Pop quiz 1. You want to use a mesh file format that Irrlicht doesn't support currently. To import this format into the engine, you can implement your own reader class deriving from: a. IMeshLoader b. IAnimatedMesh c.
ISceneManager
2. You have an animated mesh loaded into the scene. Which of the following is responsible for manipula ng that animated mesh's a ributes such as posi on, rota on, scale, and so on? a. IMesh b. IMeshSceneNode c.
IAnimatedMesh
Summary In this chapter, we explored different types of mesh and how to work with them in Irrlicht. In par cular, we've covered:
Loading and manipula ng a mesh in Irrlicht
Dressing up meshes with textures
File formats
Anima ng a mesh
Now that we've learned how to load meshes, we should get deeper into 3D graphics programming. To do so, we need to understand more about vectors, points, and other data types available in Irrlicht. We'll look into that in the next chapter. [ 62 ]
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book
Where to buy this book You can buy Irrlicht 1.7 Realtime 3D Engine Beginner’s Guide from the Packt Publishing website: http://www.packtpub.com/irrlicht-171-realtime-3dengine-beginners-guide/book. Free shipping to the US, UK, Europe and selected Asian countries. For more information, please read our shipping policy.
Alternatively, you can buy the book from Amazon, BN.com, Computer Manuals and most internet book retailers.
www.PacktPub.com
For More Information: www.packtpub.com/irrlicht-171-realtime-3d-engine-beginners-guide/book