1 | /* ==========================================================================
|
---|
2 | * (C) 2006 Universitat Jaume I
|
---|
3 | * ==========================================================================
|
---|
4 | * PROYECT: GAME TOOLS
|
---|
5 | * ==========================================================================*/
|
---|
6 | /** CONTENT:
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * @file main.cpp
|
---|
10 | /** COMMENTS:
|
---|
11 | * Model must be in "media/models" folder.
|
---|
12 | * Lod file must be in "media/GT" folder.
|
---|
13 | /*===========================================================================*/
|
---|
14 | #include "ExampleApplication.h"
|
---|
15 | #include "GeoLodTreeLibrary.h"
|
---|
16 | #include "GeoMeshLoader.h"
|
---|
17 |
|
---|
18 |
|
---|
19 | // Distance values
|
---|
20 | #define dist_min 300
|
---|
21 | #define dist_max 700
|
---|
22 |
|
---|
23 | // Model name
|
---|
24 | #define model_name "arbol"
|
---|
25 |
|
---|
26 |
|
---|
27 | //Global variables
|
---|
28 | Entity* entity;
|
---|
29 | Geometry::LodTreeLibrary* myTrees;
|
---|
30 | Ogre::Mesh *ogreMesh=NULL;
|
---|
31 | Geometry::GeoMeshLoader *meshloader=NULL;
|
---|
32 | SceneNode* node;
|
---|
33 |
|
---|
34 | MaterialPtr *mat;
|
---|
35 |
|
---|
36 | ColourValue color=ColourValue::Red;
|
---|
37 |
|
---|
38 | Camera* theCam;
|
---|
39 | Entity* pPlaneEnt;
|
---|
40 |
|
---|
41 | OverlayElement* mInfo;
|
---|
42 | OverlayElement* mInfo2;
|
---|
43 |
|
---|
44 |
|
---|
45 | void DumpDataToOgreBuffers(Ogre::Mesh *original_mesh, Geometry::LodTreeLibrary *lodTreesLib)
|
---|
46 | {
|
---|
47 | // Copy to Ogre buffers including the degenerated triangles
|
---|
48 |
|
---|
49 | Ogre::HardwareIndexBufferSharedPtr ibuf;
|
---|
50 | Ogre::IndexData *indexes;
|
---|
51 | Ogre::RenderOperation mRenderOp;
|
---|
52 |
|
---|
53 | for (int submesh=0; submesh < original_mesh->getNumSubMeshes(); submesh++)
|
---|
54 | {
|
---|
55 | bool istrunk = lodTreesLib->GetLeavesSubMesh()!=submesh;
|
---|
56 | original_mesh->getSubMesh(submesh)->_getRenderOperation(mRenderOp,0);
|
---|
57 |
|
---|
58 | // we will suppose this submesh is the foliage
|
---|
59 | int indices_to_render = lodTreesLib->CurrentLOD_Foliage_IndexCount();
|
---|
60 | int offset = 0;
|
---|
61 |
|
---|
62 | if (istrunk)
|
---|
63 | {
|
---|
64 | // this submesh is the trunk
|
---|
65 | offset = lodTreesLib->GetValidTrunkOffset(submesh);
|
---|
66 | indices_to_render = lodTreesLib->GetValidTrunkIndexCount(submesh);
|
---|
67 | }
|
---|
68 |
|
---|
69 | ibuf = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer(
|
---|
70 | Ogre::HardwareIndexBuffer::IT_32BIT, // type of index
|
---|
71 | indices_to_render, // number of indexes
|
---|
72 | Ogre::HardwareBuffer::HBU_DISCARDABLE , // usage
|
---|
73 | false); // no shadow buffer
|
---|
74 |
|
---|
75 | mRenderOp.indexData->indexBuffer = ibuf;
|
---|
76 | mRenderOp.indexData->indexStart = 0;
|
---|
77 | mRenderOp.indexData->indexCount = indices_to_render;
|
---|
78 |
|
---|
79 | unsigned long* pIdx = static_cast<unsigned long*>(ibuf->lock(Ogre::HardwareBuffer::HBL_NORMAL));
|
---|
80 |
|
---|
81 | for (int k=0; k<indices_to_render; k++)
|
---|
82 | if (istrunk)
|
---|
83 | pIdx[k] = lodTreesLib->CurrentLOD_Trunk_Indices()->GetIndex(k+offset);
|
---|
84 | else
|
---|
85 | pIdx[k] = lodTreesLib->CurrentLOD_Foliage_Indices()->GetIndex(k);
|
---|
86 |
|
---|
87 |
|
---|
88 | ibuf->unlock();
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | class FresnelFrameListener : public ExampleFrameListener
|
---|
93 | {
|
---|
94 | int manage;
|
---|
95 |
|
---|
96 | public:
|
---|
97 |
|
---|
98 | FresnelFrameListener(RenderWindow* win, Camera* cam)
|
---|
99 | : ExampleFrameListener(win, cam, false, false)
|
---|
100 | {
|
---|
101 | manage=1;
|
---|
102 | }
|
---|
103 |
|
---|
104 | bool frameStarted(const FrameEvent& evt)
|
---|
105 | {
|
---|
106 | Vector3 dist;
|
---|
107 | int distance=0,inc2=0,d;
|
---|
108 | unsigned int nlod,diflods;
|
---|
109 |
|
---|
110 | // Move upto 80 units/second
|
---|
111 | Real MoveFactor = 180.0 * evt.timeSinceLastFrame;
|
---|
112 |
|
---|
113 | // Copy the current state of the input devices
|
---|
114 | mInputDevice->capture();
|
---|
115 |
|
---|
116 | // If this is the first frame, pick a speed
|
---|
117 | if (evt.timeSinceLastFrame == 0)
|
---|
118 | {
|
---|
119 | mMoveScale = 1;
|
---|
120 | mRotScale = 0.1;
|
---|
121 | }
|
---|
122 | // Otherwise scale movement units by time passed since last frame
|
---|
123 | else
|
---|
124 | {
|
---|
125 | // Move about 100 units per second,
|
---|
126 | mMoveScale = mMoveSpeed * evt.timeSinceLastFrame;
|
---|
127 | // Take about 10 seconds for full rotation
|
---|
128 | mRotScale = mRotateSpeed * evt.timeSinceLastFrame;
|
---|
129 | }
|
---|
130 |
|
---|
131 | mRotX = 0;
|
---|
132 | mRotY = 0;
|
---|
133 | mTranslateVector = Vector3::ZERO;
|
---|
134 |
|
---|
135 | //LOD selection
|
---|
136 | int difdist = dist_max - dist_min;
|
---|
137 |
|
---|
138 | int i=0;
|
---|
139 |
|
---|
140 | dist = node->getPosition() - mCamera->getPosition();
|
---|
141 | distance =dist.length();
|
---|
142 |
|
---|
143 | DumpDataToOgreBuffers(ogreMesh,myTrees);
|
---|
144 |
|
---|
145 | float lodfactor = (float)(distance - dist_min) / (float)(dist_max - dist_min);
|
---|
146 | lodfactor = 1.0f - lodfactor;
|
---|
147 |
|
---|
148 | if (lodfactor >= 0.0f && lodfactor <= 1.0f)
|
---|
149 | {
|
---|
150 | myTrees->GoToLod(lodfactor);
|
---|
151 |
|
---|
152 | if (lodfactor < 0.333f)
|
---|
153 | {
|
---|
154 | color.r=0.0;
|
---|
155 | color.g=(lodfactor)*3.0f;//nlod*1.0/(diflods/3);
|
---|
156 | color.b=(1.0-lodfactor)*3.0f;//(nlod*1.0/(diflods/3));
|
---|
157 | }
|
---|
158 | else
|
---|
159 | {
|
---|
160 | if (lodfactor<0.666f)
|
---|
161 | {
|
---|
162 | color.r=(lodfactor-0.333f)*3.0f;//(nlod-diflods/3)*1.0/(diflods/3);
|
---|
163 | color.g=1.0;
|
---|
164 | color.b=0.0;
|
---|
165 | }
|
---|
166 | else
|
---|
167 | {
|
---|
168 | color.r=1.0;
|
---|
169 | color.g=(1.0-lodfactor)*3.0f;//(nlod-(2*diflods/3))*1.0/(diflods/3);
|
---|
170 | color.b=0.0;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | mat[0]->setDiffuse(color);
|
---|
174 | }
|
---|
175 | /* else
|
---|
176 | {
|
---|
177 | if ((distance > dist_max))
|
---|
178 | {
|
---|
179 | // myStrips->GoToLod(1.0f);
|
---|
180 |
|
---|
181 | color.r=0.0;
|
---|
182 | color.g=0.0;
|
---|
183 | color.b=1.0;
|
---|
184 | mat[0]->setDiffuse(color);
|
---|
185 |
|
---|
186 | }
|
---|
187 | else
|
---|
188 | {
|
---|
189 | if ((distance < dist_min))
|
---|
190 | {
|
---|
191 | // myStrips->GoToLod(0.0f);
|
---|
192 |
|
---|
193 | color.r=1.0;
|
---|
194 | color.g=0.0;
|
---|
195 | color.b=0.0;
|
---|
196 | mat[0]->setDiffuse(color);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }*/
|
---|
200 |
|
---|
201 | // Move the node
|
---|
202 | if(mInputDevice->isKeyDown(Ogre::KC_UP))
|
---|
203 | mTranslateVector.z = -mMoveScale;
|
---|
204 |
|
---|
205 |
|
---|
206 | if(mInputDevice->isKeyDown(Ogre::KC_DOWN))
|
---|
207 | mTranslateVector.z = mMoveScale;
|
---|
208 |
|
---|
209 | // Instead of moving the ship left and right, rotate it using yaw()
|
---|
210 | if(mInputDevice->isKeyDown(Ogre::KC_LEFT))
|
---|
211 | mCamera->yaw(mRotScale);
|
---|
212 |
|
---|
213 | if(mInputDevice->isKeyDown(Ogre::KC_RIGHT))
|
---|
214 | mCamera->yaw(-mRotScale);
|
---|
215 |
|
---|
216 | // Move the node
|
---|
217 | if(mInputDevice->isKeyDown(Ogre::KC_W))
|
---|
218 | node->translate(0,mMoveScale,0);
|
---|
219 |
|
---|
220 | if(mInputDevice->isKeyDown(Ogre::KC_S))
|
---|
221 | node->translate(0,-mMoveScale,0);
|
---|
222 |
|
---|
223 | if(mInputDevice->isKeyDown(Ogre::KC_Z))
|
---|
224 | node->scale(1.01,1.01,1.01);
|
---|
225 |
|
---|
226 | if(mInputDevice->isKeyDown(Ogre::KC_X))
|
---|
227 | node->scale(0.99,0.99,0.99);
|
---|
228 |
|
---|
229 | // Rotate
|
---|
230 | if(mInputDevice->isKeyDown(Ogre::KC_A))
|
---|
231 | node->yaw(mRotScale);
|
---|
232 |
|
---|
233 | if(mInputDevice->isKeyDown(Ogre::KC_D))
|
---|
234 | node->yaw(-mRotScale);
|
---|
235 |
|
---|
236 | if(mInputDevice->isKeyDown(Ogre::KC_ESCAPE))
|
---|
237 | {
|
---|
238 | delete myTrees;
|
---|
239 |
|
---|
240 | delete [] mat;
|
---|
241 |
|
---|
242 | return false;
|
---|
243 | }
|
---|
244 |
|
---|
245 | if( mInputDevice->getMouseButton( 1 ) )
|
---|
246 | {
|
---|
247 | mTranslateVector.x += mInputDevice->getMouseRelativeX() * 0.13;
|
---|
248 | mTranslateVector.y -= mInputDevice->getMouseRelativeY() * 0.13;
|
---|
249 | }
|
---|
250 | else
|
---|
251 | {
|
---|
252 | mRotX = Degree(-mInputDevice->getMouseRelativeX() * 0.13);
|
---|
253 | mRotY = Degree(-mInputDevice->getMouseRelativeY() * 0.13);
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | char cadena[256];
|
---|
258 |
|
---|
259 |
|
---|
260 | sprintf(cadena,"Distance: %d",distance);
|
---|
261 |
|
---|
262 | mInfo->setCaption(cadena);
|
---|
263 |
|
---|
264 | if (lodfactor<0.0f)
|
---|
265 | lodfactor=0.0f;
|
---|
266 | if (lodfactor>1.0f)
|
---|
267 | lodfactor=1.0f;
|
---|
268 | sprintf(cadena,"LOD factor: %f",lodfactor);
|
---|
269 |
|
---|
270 | mInfo2->setCaption(cadena);
|
---|
271 |
|
---|
272 | mCamera->yaw(mRotX);
|
---|
273 | mCamera->pitch(mRotY);
|
---|
274 | mCamera->moveRelative(mTranslateVector);
|
---|
275 |
|
---|
276 | return true;
|
---|
277 | }
|
---|
278 | };
|
---|
279 |
|
---|
280 | class FresnelApplication : public ExampleApplication
|
---|
281 | {
|
---|
282 | protected:
|
---|
283 | public:
|
---|
284 | FresnelApplication()
|
---|
285 | {
|
---|
286 | }
|
---|
287 |
|
---|
288 | ~FresnelApplication()
|
---|
289 | {
|
---|
290 | }
|
---|
291 | protected:
|
---|
292 |
|
---|
293 |
|
---|
294 |
|
---|
295 | // Just override the mandatory create scene method
|
---|
296 | void createScene(void)
|
---|
297 | {
|
---|
298 | mat = new MaterialPtr[1];
|
---|
299 |
|
---|
300 | theCam = mCamera;
|
---|
301 | theCam->setPosition(0,20,dist_max+50);
|
---|
302 | // Set ambient light
|
---|
303 | mSceneMgr->setAmbientLight(ColourValue(0.3, 0.3, 0.3));
|
---|
304 |
|
---|
305 | // Create a point light
|
---|
306 | Light* l = mSceneMgr->createLight("MainLight");
|
---|
307 | l->setType(Light::LT_DIRECTIONAL);
|
---|
308 | l->setDirection(0.0,0.0,-1.0);
|
---|
309 |
|
---|
310 | // Define a floor plane mesh
|
---|
311 | Plane plane( Vector3::UNIT_Y, 0 );
|
---|
312 |
|
---|
313 | MeshManager::getSingleton().createPlane("ground",
|
---|
314 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
|
---|
315 | 1500,1500,20,20,true,1,5,5,Vector3::UNIT_Z);
|
---|
316 |
|
---|
317 | Entity* suelo = mSceneMgr->createEntity( "GroundEntity", "ground" );
|
---|
318 | mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(suelo);
|
---|
319 |
|
---|
320 |
|
---|
321 | suelo->setMaterialName("Examples/GrassFloor");
|
---|
322 | suelo->setCastShadows(false);
|
---|
323 |
|
---|
324 |
|
---|
325 | mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");
|
---|
326 |
|
---|
327 | // My node to which all objects will be attached
|
---|
328 | SceneNode* myRootNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
329 |
|
---|
330 | std::string model_file=model_name;
|
---|
331 | model_file.append(".mesh");
|
---|
332 |
|
---|
333 | //Models
|
---|
334 | entity = mSceneMgr->createEntity(model_name, "../../../OgreStuff/media/GT/betulalod.mesh");
|
---|
335 |
|
---|
336 | ogreMesh = entity->getMesh().getPointer();
|
---|
337 |
|
---|
338 | // load LOD info from the object
|
---|
339 | meshloader=new Geometry::GeoMeshLoader;
|
---|
340 | Geometry::Mesh *themesh = meshloader->load("../../../OgreStuff/media/GT/betulalod.mesh");
|
---|
341 |
|
---|
342 | node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
343 | node->attachObject( entity );
|
---|
344 | node->rotate(Ogre::Vector3(1,0,0),Ogre::Radian(-90.0f));
|
---|
345 |
|
---|
346 | // myStrips = new LodStripsLibrary(lod_file,entity->getMesh().getPointer());
|
---|
347 |
|
---|
348 | if (!meshloader->GetLodStripsData() || !meshloader->GetTreeSimpSeq())
|
---|
349 | exit(1);
|
---|
350 |
|
---|
351 | // myStrips = new Geometry::LodStripsLibrary(meshloader->GetLodStripsData(),themesh);
|
---|
352 | myTrees = new Geometry::LodTreeLibrary(meshloader->GetLodStripsData(),meshloader->GetTreeSimpSeq(),themesh);
|
---|
353 |
|
---|
354 | entity->setNormaliseNormals(true);
|
---|
355 |
|
---|
356 | /* // Colour-coded material
|
---|
357 | mat[0] = MaterialManager::getSingleton().create("test_mat",
|
---|
358 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
359 | mat[0]->setCullingMode(CULL_ANTICLOCKWISE);
|
---|
360 | mat[0]->setAmbient(ColourValue::Black);
|
---|
361 | mat[0]->setDiffuse(color);
|
---|
362 | mat[0]->setLightingEnabled(true);
|
---|
363 | entity->setMaterialName("test_mat");*/
|
---|
364 |
|
---|
365 |
|
---|
366 | // Colour-coded material
|
---|
367 | mat[0] = MaterialManager::getSingleton().create("test_mat",
|
---|
368 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
369 | mat[0]->setCullingMode(CULL_NONE);
|
---|
370 | mat[0]->setAmbient(ColourValue::Black);
|
---|
371 | mat[0]->setDiffuse(color);
|
---|
372 | mat[0]->setLightingEnabled(true);
|
---|
373 | mat[0]->createTechnique()->createPass()->createTextureUnitState("nm_rt.png");
|
---|
374 | //entity->setMaterialName("test_mat");
|
---|
375 | entity->setMaterialName("Examples/GrassFloor");
|
---|
376 |
|
---|
377 |
|
---|
378 | // show overlay
|
---|
379 | Overlay* pOver = OverlayManager::getSingleton().getByName("Demo_LodStrips/Overlay");
|
---|
380 | mInfo = OverlayManager::getSingleton().getOverlayElement("Demo_LodStrips/Info_1");
|
---|
381 | mInfo2 = OverlayManager::getSingleton().getOverlayElement("Demo_LodStrips/Info_2");
|
---|
382 | pOver->show();
|
---|
383 |
|
---|
384 | }
|
---|
385 |
|
---|
386 | void createFrameListener(void)
|
---|
387 | {
|
---|
388 | mFrameListener= new FresnelFrameListener(mWindow, mCamera);
|
---|
389 | mFrameListener->showDebugOverlay(true);
|
---|
390 | mRoot->addFrameListener(mFrameListener);
|
---|
391 | }
|
---|
392 |
|
---|
393 | };
|
---|
394 |
|
---|
395 |
|
---|
396 |
|
---|
397 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
398 | #define WIN32_LEAN_AND_MEAN
|
---|
399 | #include "windows.h"
|
---|
400 | #endif
|
---|
401 |
|
---|
402 | #ifdef __cplusplus
|
---|
403 | extern "C" {
|
---|
404 | #endif
|
---|
405 |
|
---|
406 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
407 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
|
---|
408 | #else
|
---|
409 | int main(int argc, char **argv)
|
---|
410 | #endif
|
---|
411 | {
|
---|
412 | // Create application object
|
---|
413 | FresnelApplication app;
|
---|
414 |
|
---|
415 | try {
|
---|
416 | app.go();
|
---|
417 | } catch( Exception& e ) {
|
---|
418 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
419 | MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
|
---|
420 | #else
|
---|
421 | std::cerr << "An exception has occured: " << e.getFullDescription();
|
---|
422 | #endif
|
---|
423 | }
|
---|
424 |
|
---|
425 |
|
---|
426 | return 0;
|
---|
427 | }
|
---|
428 |
|
---|
429 | #ifdef __cplusplus
|
---|
430 | }
|
---|
431 | #endif
|
---|