diff options
Diffstat (limited to 'libraries/irrlicht-1.8/examples/15.LoadIrrFile/main.cpp')
-rw-r--r-- | libraries/irrlicht-1.8/examples/15.LoadIrrFile/main.cpp | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/examples/15.LoadIrrFile/main.cpp b/libraries/irrlicht-1.8/examples/15.LoadIrrFile/main.cpp new file mode 100644 index 0000000..42327cd --- /dev/null +++ b/libraries/irrlicht-1.8/examples/15.LoadIrrFile/main.cpp | |||
@@ -0,0 +1,173 @@ | |||
1 | /** Example 015 Loading Scenes from .irr Files | ||
2 | |||
3 | Since version 1.1, Irrlicht is able to save and load | ||
4 | the full scene graph into an .irr file, an xml based | ||
5 | format. There is an editor available to edit | ||
6 | those files, named irrEdit (http://www.ambiera.com/irredit) | ||
7 | which can also be used as world and particle editor. | ||
8 | This tutorial shows how to use .irr files. | ||
9 | |||
10 | Lets start: Create an Irrlicht device and setup the window. | ||
11 | */ | ||
12 | |||
13 | #include <irrlicht.h> | ||
14 | #include "driverChoice.h" | ||
15 | |||
16 | using namespace irr; | ||
17 | |||
18 | #ifdef _MSC_VER | ||
19 | #pragma comment(lib, "Irrlicht.lib") | ||
20 | #endif | ||
21 | |||
22 | int main(int argc, char** argv) | ||
23 | { | ||
24 | // ask user for driver | ||
25 | video::E_DRIVER_TYPE driverType=driverChoiceConsole(); | ||
26 | if (driverType==video::EDT_COUNT) | ||
27 | return 1; | ||
28 | |||
29 | // create device and exit if creation failed | ||
30 | |||
31 | IrrlichtDevice* device = | ||
32 | createDevice(driverType, core::dimension2d<u32>(640, 480)); | ||
33 | |||
34 | if (device == 0) | ||
35 | return 1; // could not create selected driver. | ||
36 | |||
37 | device->setWindowCaption(L"Load .irr file example"); | ||
38 | |||
39 | video::IVideoDriver* driver = device->getVideoDriver(); | ||
40 | scene::ISceneManager* smgr = device->getSceneManager(); | ||
41 | |||
42 | /* | ||
43 | Now load our .irr file. | ||
44 | .irr files can store the whole scene graph including animators, | ||
45 | materials and particle systems. And there is also the possibility to | ||
46 | store arbitrary user data for every scene node in that file. To keep | ||
47 | this example simple, we are simply loading the scene here. See the | ||
48 | documentation at ISceneManager::loadScene and ISceneManager::saveScene | ||
49 | for more information. So to load and display a complicated huge scene, | ||
50 | we only need a single call to loadScene(). | ||
51 | */ | ||
52 | |||
53 | // load the scene | ||
54 | if (argc>1) | ||
55 | smgr->loadScene(argv[1]); | ||
56 | else | ||
57 | smgr->loadScene("../../media/example.irr"); | ||
58 | |||
59 | /* | ||
60 | Now we'll create a camera, and give it a collision response animator | ||
61 | that's built from the mesh nodes in the scene we just loaded. | ||
62 | */ | ||
63 | scene::ICameraSceneNode * camera = smgr->addCameraSceneNodeFPS(0, 50.f, 0.1f); | ||
64 | |||
65 | // Create a meta triangle selector to hold several triangle selectors. | ||
66 | scene::IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector(); | ||
67 | |||
68 | /* | ||
69 | Now we will find all the nodes in the scene and create triangle | ||
70 | selectors for all suitable nodes. Typically, you would want to make a | ||
71 | more informed decision about which nodes to performs collision checks | ||
72 | on; you could capture that information in the node name or Id. | ||
73 | */ | ||
74 | core::array<scene::ISceneNode *> nodes; | ||
75 | smgr->getSceneNodesFromType(scene::ESNT_ANY, nodes); // Find all nodes | ||
76 | |||
77 | for (u32 i=0; i < nodes.size(); ++i) | ||
78 | { | ||
79 | scene::ISceneNode * node = nodes[i]; | ||
80 | scene::ITriangleSelector * selector = 0; | ||
81 | |||
82 | switch(node->getType()) | ||
83 | { | ||
84 | case scene::ESNT_CUBE: | ||
85 | case scene::ESNT_ANIMATED_MESH: | ||
86 | // Because the selector won't animate with the mesh, | ||
87 | // and is only being used for camera collision, we'll just use an approximate | ||
88 | // bounding box instead of ((scene::IAnimatedMeshSceneNode*)node)->getMesh(0) | ||
89 | selector = smgr->createTriangleSelectorFromBoundingBox(node); | ||
90 | break; | ||
91 | |||
92 | case scene::ESNT_MESH: | ||
93 | case scene::ESNT_SPHERE: // Derived from IMeshSceneNode | ||
94 | selector = smgr->createTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node); | ||
95 | break; | ||
96 | |||
97 | case scene::ESNT_TERRAIN: | ||
98 | selector = smgr->createTerrainTriangleSelector((scene::ITerrainSceneNode*)node); | ||
99 | break; | ||
100 | |||
101 | case scene::ESNT_OCTREE: | ||
102 | selector = smgr->createOctreeTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node); | ||
103 | break; | ||
104 | |||
105 | default: | ||
106 | // Don't create a selector for this node type | ||
107 | break; | ||
108 | } | ||
109 | |||
110 | if(selector) | ||
111 | { | ||
112 | // Add it to the meta selector, which will take a reference to it | ||
113 | meta->addTriangleSelector(selector); | ||
114 | // And drop my reference to it, so that the meta selector owns it. | ||
115 | selector->drop(); | ||
116 | } | ||
117 | } | ||
118 | |||
119 | /* | ||
120 | Now that the mesh scene nodes have had triangle selectors created and added | ||
121 | to the meta selector, create a collision response animator from that meta selector. | ||
122 | */ | ||
123 | scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator( | ||
124 | meta, camera, core::vector3df(5,5,5), | ||
125 | core::vector3df(0,0,0)); | ||
126 | meta->drop(); // I'm done with the meta selector now | ||
127 | |||
128 | camera->addAnimator(anim); | ||
129 | anim->drop(); // I'm done with the animator now | ||
130 | |||
131 | // And set the camera position so that it doesn't start off stuck in the geometry | ||
132 | camera->setPosition(core::vector3df(0.f, 20.f, 0.f)); | ||
133 | |||
134 | // Point the camera at the cube node, by finding the first node of type ESNT_CUBE | ||
135 | scene::ISceneNode * cube = smgr->getSceneNodeFromType(scene::ESNT_CUBE); | ||
136 | if(cube) | ||
137 | camera->setTarget(cube->getAbsolutePosition()); | ||
138 | |||
139 | /* | ||
140 | That's it. Draw everything and finish as usual. | ||
141 | */ | ||
142 | |||
143 | int lastFPS = -1; | ||
144 | |||
145 | while(device->run()) | ||
146 | if (device->isWindowActive()) | ||
147 | { | ||
148 | driver->beginScene(true, true, video::SColor(0,200,200,200)); | ||
149 | smgr->drawAll(); | ||
150 | driver->endScene(); | ||
151 | |||
152 | int fps = driver->getFPS(); | ||
153 | |||
154 | if (lastFPS != fps) | ||
155 | { | ||
156 | core::stringw str = L"Load Irrlicht File example - Irrlicht Engine ["; | ||
157 | str += driver->getName(); | ||
158 | str += "] FPS:"; | ||
159 | str += fps; | ||
160 | |||
161 | device->setWindowCaption(str.c_str()); | ||
162 | lastFPS = fps; | ||
163 | } | ||
164 | |||
165 | } | ||
166 | |||
167 | device->drop(); | ||
168 | |||
169 | return 0; | ||
170 | } | ||
171 | |||
172 | /* | ||
173 | **/ | ||