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 "GeoLodStripsLibrary.h"
|
---|
16 | #include "GeoMeshLoader.h"
|
---|
17 |
|
---|
18 |
|
---|
19 | // Distance values
|
---|
20 | #define dist_min 1100
|
---|
21 | #define dist_max 2500
|
---|
22 |
|
---|
23 | // Model name
|
---|
24 | #define model_name "dwarf2"
|
---|
25 |
|
---|
26 |
|
---|
27 | //Global variables
|
---|
28 | Entity* entity;
|
---|
29 | Geometry::LodStripsLibrary* myStrips;
|
---|
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 | class CustomIndexData : public Geometry::IndexData
|
---|
45 | {
|
---|
46 | private:
|
---|
47 | Ogre::Mesh *targetMesh;
|
---|
48 | Ogre::HardwareIndexBufferSharedPtr ibuf;
|
---|
49 | Ogre::RenderOperation mRenderOp;
|
---|
50 | unsigned long* pIdx;
|
---|
51 | public:
|
---|
52 | CustomIndexData(Ogre::Mesh *ogremesh):Geometry::IndexData(){
|
---|
53 | targetMesh=ogremesh;
|
---|
54 | pIdx=NULL;
|
---|
55 | }
|
---|
56 | virtual ~CustomIndexData(void){}
|
---|
57 |
|
---|
58 | virtual void Begin(unsigned int submeshid, unsigned int indexcount){
|
---|
59 | targetMesh->getSubMesh(submeshid)->_getRenderOperation(mRenderOp,0);
|
---|
60 | ibuf = mRenderOp.indexData->indexBuffer;
|
---|
61 | mRenderOp.indexData->indexCount = indexcount;
|
---|
62 | pIdx = static_cast<unsigned long*>(ibuf->lock(Ogre::HardwareBuffer::HBL_NORMAL));
|
---|
63 | }
|
---|
64 | virtual void SetIndex(unsigned int i, unsigned int index){
|
---|
65 | pIdx[i] = index; //lodStripsLib->dataRetrievalInterface->GetIndex(k+offset);
|
---|
66 | }
|
---|
67 | virtual void End(){
|
---|
68 | ibuf->unlock();
|
---|
69 | }
|
---|
70 | virtual void BorrowIndexData(const Geometry::IndexData *){}
|
---|
71 | };
|
---|
72 |
|
---|
73 | class FresnelFrameListener : public ExampleFrameListener
|
---|
74 | {
|
---|
75 | int manage;
|
---|
76 |
|
---|
77 | public:
|
---|
78 |
|
---|
79 | FresnelFrameListener(RenderWindow* win, Camera* cam)
|
---|
80 | : ExampleFrameListener(win, cam, false, false)
|
---|
81 | {
|
---|
82 | manage=1;
|
---|
83 | }
|
---|
84 |
|
---|
85 | bool frameStarted(const FrameEvent& evt)
|
---|
86 | {
|
---|
87 | Vector3 dist;
|
---|
88 | int distance=0,inc2=0,d;
|
---|
89 | unsigned int nlod,diflods;
|
---|
90 |
|
---|
91 | // Move upto 80 units/second
|
---|
92 | Real MoveFactor = 180.0 * evt.timeSinceLastFrame;
|
---|
93 |
|
---|
94 | // Copy the current state of the input devices
|
---|
95 | mInputDevice->capture();
|
---|
96 |
|
---|
97 | // If this is the first frame, pick a speed
|
---|
98 | if (evt.timeSinceLastFrame == 0)
|
---|
99 | {
|
---|
100 | mMoveScale = 1;
|
---|
101 | mRotScale = 0.1;
|
---|
102 | }
|
---|
103 | // Otherwise scale movement units by time passed since last frame
|
---|
104 | else
|
---|
105 | {
|
---|
106 | // Move about 100 units per second,
|
---|
107 | mMoveScale = mMoveSpeed * evt.timeSinceLastFrame;
|
---|
108 | // Take about 10 seconds for full rotation
|
---|
109 | mRotScale = mRotateSpeed * evt.timeSinceLastFrame;
|
---|
110 | }
|
---|
111 |
|
---|
112 | mRotX = 0;
|
---|
113 | mRotY = 0;
|
---|
114 | mTranslateVector = Vector3::ZERO;
|
---|
115 |
|
---|
116 | //LOD selection
|
---|
117 | int difdist = dist_max - dist_min;
|
---|
118 |
|
---|
119 | int i=0;
|
---|
120 |
|
---|
121 | dist = node->getPosition() - mCamera->getPosition();
|
---|
122 | distance =dist.length();
|
---|
123 |
|
---|
124 | float lodfactor = (float)(distance - dist_min) / (float)(dist_max - dist_min);
|
---|
125 | lodfactor = 1.0f - lodfactor;
|
---|
126 |
|
---|
127 | if (lodfactor<0.0f)
|
---|
128 | lodfactor=0.0f;
|
---|
129 | if (lodfactor>1.0f)
|
---|
130 | lodfactor=1.0f;
|
---|
131 |
|
---|
132 | static float lodfactorBefore = -1.0f;
|
---|
133 | if (fabsf(lodfactorBefore-lodfactor)>0.03f ||
|
---|
134 | (lodfactorBefore>0.0f && lodfactor==0.0f) ||
|
---|
135 | (lodfactorBefore<1.0f && lodfactor==1.0f))
|
---|
136 | {
|
---|
137 | myStrips->GoToLod(lodfactor);
|
---|
138 | lodfactorBefore=lodfactor;
|
---|
139 | }
|
---|
140 |
|
---|
141 | // Move the node
|
---|
142 | if(mInputDevice->isKeyDown(Ogre::KC_UP) ||
|
---|
143 | mInputDevice->isKeyDown(Ogre::KC_W) ||
|
---|
144 | mInputDevice->isKeyDown(Ogre::KC_NUMPAD5))
|
---|
145 | mTranslateVector.z = -mMoveScale;
|
---|
146 |
|
---|
147 | if(mInputDevice->isKeyDown(Ogre::KC_DOWN) ||
|
---|
148 | mInputDevice->isKeyDown(Ogre::KC_S) ||
|
---|
149 | mInputDevice->isKeyDown(Ogre::KC_NUMPAD2))
|
---|
150 | mTranslateVector.z = mMoveScale;
|
---|
151 |
|
---|
152 | if (mInputDevice->isKeyDown(Ogre::KC_A) ||
|
---|
153 | mInputDevice->isKeyDown(Ogre::KC_NUMPAD1))
|
---|
154 | mTranslateVector.x = -mMoveScale;
|
---|
155 |
|
---|
156 | if (mInputDevice->isKeyDown(Ogre::KC_D) ||
|
---|
157 | mInputDevice->isKeyDown(Ogre::KC_NUMPAD3))
|
---|
158 | mTranslateVector.x = mMoveScale;
|
---|
159 |
|
---|
160 | /* if (mInputDevice->isKeyDown(Ogre::KC_K))
|
---|
161 | mCamera->setDetailLevel(SDL_WIREFRAME);*/
|
---|
162 |
|
---|
163 |
|
---|
164 | if(mInputDevice->isKeyDown(Ogre::KC_LEFT))
|
---|
165 | mCamera->yaw(mRotScale);
|
---|
166 |
|
---|
167 | if(mInputDevice->isKeyDown(Ogre::KC_RIGHT))
|
---|
168 | mCamera->yaw(-mRotScale);
|
---|
169 |
|
---|
170 | if(mInputDevice->isKeyDown(Ogre::KC_ESCAPE))
|
---|
171 | {
|
---|
172 | delete myStrips;
|
---|
173 | delete [] mat;
|
---|
174 | return false;
|
---|
175 | }
|
---|
176 |
|
---|
177 | if( mInputDevice->getMouseButton( 1 ) )
|
---|
178 | {
|
---|
179 | mTranslateVector.x += mInputDevice->getMouseRelativeX() * 0.13;
|
---|
180 | mTranslateVector.y -= mInputDevice->getMouseRelativeY() * 0.13;
|
---|
181 | }
|
---|
182 | else
|
---|
183 | {
|
---|
184 | mRotX = Degree(-mInputDevice->getMouseRelativeX() * 0.13);
|
---|
185 | mRotY = Degree(-mInputDevice->getMouseRelativeY() * 0.13);
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | char cadena[256];
|
---|
190 |
|
---|
191 |
|
---|
192 | sprintf(cadena,"Distance: %d",distance);
|
---|
193 |
|
---|
194 | mInfo->setCaption(cadena);
|
---|
195 |
|
---|
196 | sprintf(cadena,"LOD factor: %f",lodfactor);
|
---|
197 |
|
---|
198 | mInfo2->setCaption(cadena);
|
---|
199 |
|
---|
200 | mCamera->yaw(mRotX);
|
---|
201 | mCamera->pitch(mRotY);
|
---|
202 | mCamera->moveRelative(mTranslateVector);
|
---|
203 |
|
---|
204 | return true;
|
---|
205 | }
|
---|
206 | };
|
---|
207 |
|
---|
208 | class LodStripsApplication : public ExampleApplication
|
---|
209 | {
|
---|
210 | protected:
|
---|
211 | public:
|
---|
212 | LodStripsApplication() {}
|
---|
213 | ~LodStripsApplication() {}
|
---|
214 |
|
---|
215 | protected:
|
---|
216 |
|
---|
217 | // Just override the mandatory create scene method
|
---|
218 | void createScene(void)
|
---|
219 | {
|
---|
220 | Entity* pEnt;
|
---|
221 |
|
---|
222 | mat = new MaterialPtr[1];
|
---|
223 |
|
---|
224 | theCam = mCamera;
|
---|
225 | theCam->setPosition(0,50,dist_min-600.0f);
|
---|
226 | // Set ambient light
|
---|
227 | mSceneMgr->setAmbientLight(ColourValue(0.3, 0.3, 0.3));
|
---|
228 |
|
---|
229 | // Create a directional light
|
---|
230 | Light* l = mSceneMgr->createLight("MainLight");
|
---|
231 | l->setType(Light::LT_DIRECTIONAL);
|
---|
232 | l->setDirection(0.0,0.0,-1.0);
|
---|
233 |
|
---|
234 |
|
---|
235 | mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");
|
---|
236 |
|
---|
237 | // My node to which all objects will be attached
|
---|
238 | SceneNode* myRootNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
239 |
|
---|
240 | // Define a floor plane mesh
|
---|
241 | Plane plane( Vector3::UNIT_Y, 0 );
|
---|
242 |
|
---|
243 | MeshManager::getSingleton().createPlane("ground",
|
---|
244 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
|
---|
245 | 4000,4000,6,6,true,1,10,10,Vector3::UNIT_Z);
|
---|
246 |
|
---|
247 | Entity* groundEntity = mSceneMgr->createEntity( "GroundEntity", "ground" );
|
---|
248 | mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(groundEntity);
|
---|
249 |
|
---|
250 | groundEntity->setMaterialName("RockTerrain");
|
---|
251 | groundEntity->setCastShadows(false);
|
---|
252 |
|
---|
253 |
|
---|
254 | /* pEnt = mSceneMgr->createEntity( "PoolFloor", "PoolFloor.mesh" );
|
---|
255 | myRootNode->attachObject(pEnt);
|
---|
256 | myRootNode->scale(6.0,6.0,6.0);*/
|
---|
257 |
|
---|
258 | std::string model_file=model_name;
|
---|
259 | model_file.append(".mesh");
|
---|
260 |
|
---|
261 | //Models
|
---|
262 | entity = mSceneMgr->createEntity(model_name, "../../../OgreStuff/media/GT/ogrolod.mesh");
|
---|
263 | ogreMesh = entity->getMesh().getPointer();
|
---|
264 |
|
---|
265 | // load LOD info from the object
|
---|
266 | meshloader=new Geometry::GeoMeshLoader;
|
---|
267 | Geometry::Mesh *themesh = meshloader->load("../../../OgreStuff/media/GT/ogrolod.mesh");
|
---|
268 |
|
---|
269 | node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
270 | node->attachObject( entity );
|
---|
271 |
|
---|
272 | std::string lod_file="../../media/GT/";
|
---|
273 | lod_file.append(model_name);
|
---|
274 | lod_file.append(".lod");
|
---|
275 |
|
---|
276 | if (!meshloader->GetLodStripsData())
|
---|
277 | OGRE_EXCEPT(1, "The loaded mesh does not contain any LOD info","LOD Demo");
|
---|
278 |
|
---|
279 | myStrips = new Geometry::LodStripsLibrary( meshloader->GetLodStripsData(),
|
---|
280 | themesh,
|
---|
281 | new CustomIndexData(ogreMesh) );
|
---|
282 |
|
---|
283 | entity->setNormaliseNormals(true);
|
---|
284 | entity->setMaterialName("LODStripsDemo/Ogro");
|
---|
285 |
|
---|
286 | for (int i=-3; i<6; i++) // 7
|
---|
287 | for (int j=0; j<10; j++) // 4
|
---|
288 | {
|
---|
289 | char newObjName[16]="";
|
---|
290 | sprintf(newObjName,"arbol_%d_%d",i,j);
|
---|
291 | Ogre::SceneNode * auxnode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
292 | Ogre::Entity *auxen = entity->clone(newObjName);
|
---|
293 | auxnode->attachObject(auxen);
|
---|
294 | auxnode->rotate(Ogre::Vector3(0,1,0),Ogre::Degree(180.0f),Ogre::Node::TS_WORLD);
|
---|
295 | auxnode->scale(30.0f,30.0f,30.0f);
|
---|
296 | float randomsepx = (float)((rand()%18)-9);
|
---|
297 | float randomsepy = (float)((rand()%12)-6);
|
---|
298 | auxnode->translate(i*70.0f+randomsepx,0.0f,-j*70.0f-randomsepx);
|
---|
299 | auxen->setNormaliseNormals(true);
|
---|
300 | }
|
---|
301 |
|
---|
302 | node->translate(0,0,-500.0f);
|
---|
303 |
|
---|
304 | // show overlay
|
---|
305 | Overlay* pOver = OverlayManager::getSingleton().getByName("Demo_LodStrips/Overlay");
|
---|
306 | mInfo = OverlayManager::getSingleton().getOverlayElement("Demo_LodStrips/Info_1");
|
---|
307 | mInfo2 = OverlayManager::getSingleton().getOverlayElement("Demo_LodStrips/Info_2");
|
---|
308 | pOver->show();
|
---|
309 |
|
---|
310 | }
|
---|
311 |
|
---|
312 | void createFrameListener(void)
|
---|
313 | {
|
---|
314 | mFrameListener= new FresnelFrameListener(mWindow, mCamera);
|
---|
315 | mFrameListener->showDebugOverlay(true);
|
---|
316 | mRoot->addFrameListener(mFrameListener);
|
---|
317 | }
|
---|
318 |
|
---|
319 | };
|
---|
320 |
|
---|
321 |
|
---|
322 |
|
---|
323 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
324 | #define WIN32_LEAN_AND_MEAN
|
---|
325 | #include "windows.h"
|
---|
326 | #endif
|
---|
327 |
|
---|
328 | #ifdef __cplusplus
|
---|
329 | extern "C" {
|
---|
330 | #endif
|
---|
331 |
|
---|
332 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
333 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
|
---|
334 | #else
|
---|
335 | int main(int argc, char **argv)
|
---|
336 | #endif
|
---|
337 | {
|
---|
338 | // Create application object
|
---|
339 | LodStripsApplication app;
|
---|
340 |
|
---|
341 | try {
|
---|
342 | app.go();
|
---|
343 | } catch( Exception& e ) {
|
---|
344 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
345 | MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
|
---|
346 | #else
|
---|
347 | std::cerr << "An exception has occured: " << e.getFullDescription();
|
---|
348 | #endif
|
---|
349 | }
|
---|
350 |
|
---|
351 |
|
---|
352 | return 0;
|
---|
353 | }
|
---|
354 |
|
---|
355 | #ifdef __cplusplus
|
---|
356 | }
|
---|
357 | #endif
|
---|