source: GTP/trunk/App/Demos/Geom/Demo_LodStrips/main.cpp @ 1542

Revision 1542, 9.6 KB checked in by gumbau, 18 years ago (diff)
Line 
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 2000
22
23// Model name
24#define model_name "dwarf2"
25
26
27//Global variables
28Entity* entity;
29Geometry::LodStripsLibrary* myStrips;
30Ogre::Mesh *ogreMesh=NULL;
31Geometry::GeoMeshLoader *meshloader=NULL;
32SceneNode* node;
33
34MaterialPtr *mat;
35
36ColourValue color=ColourValue::Red;
37
38Camera* theCam;
39Entity* pPlaneEnt;
40
41OverlayElement* mInfo;
42OverlayElement* mInfo2;
43
44class CustomIndexData : public Geometry::IndexData
45{
46private:
47        Ogre::Mesh *targetMesh;
48        Ogre::HardwareIndexBufferSharedPtr ibuf;
49        Ogre::RenderOperation mRenderOp;
50        unsigned long* pIdx;
51public:
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
73class FresnelFrameListener : public ExampleFrameListener
74{
75        int manage;
76
77public:
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
161                if(mInputDevice->isKeyDown(Ogre::KC_LEFT))
162                  mCamera->yaw(mRotScale);
163
164                if(mInputDevice->isKeyDown(Ogre::KC_RIGHT))
165                  mCamera->yaw(-mRotScale);
166
167                // Move the cam
168/*              if(mInputDevice->isKeyDown(Ogre::KC_W))
169                  node->translate(0,mMoveScale,0);
170
171                if(mInputDevice->isKeyDown(Ogre::KC_S))
172                  node->translate(0,-mMoveScale,0);
173
174                if(mInputDevice->isKeyDown(Ogre::KC_Z))
175                  node->scale(1.01,1.01,1.01);
176
177                if(mInputDevice->isKeyDown(Ogre::KC_X))
178                  node->scale(0.99,0.99,0.99);
179
180                // Rotate
181                if(mInputDevice->isKeyDown(Ogre::KC_A))
182                  node->yaw(mRotScale);
183
184                if(mInputDevice->isKeyDown(Ogre::KC_D))
185                  node->yaw(-mRotScale);*/
186
187                if(mInputDevice->isKeyDown(Ogre::KC_ESCAPE))
188                {
189                        delete myStrips;
190
191                        delete [] mat;
192
193                    return false;
194                }
195
196        if( mInputDevice->getMouseButton( 1 ) )
197        {
198            mTranslateVector.x += mInputDevice->getMouseRelativeX() * 0.13;
199            mTranslateVector.y -= mInputDevice->getMouseRelativeY() * 0.13;
200        }
201        else
202        {
203            mRotX = Degree(-mInputDevice->getMouseRelativeX() * 0.13);
204            mRotY = Degree(-mInputDevice->getMouseRelativeY() * 0.13);
205        }
206
207
208                char cadena[256];
209
210                               
211                sprintf(cadena,"Distance: %d",distance);
212               
213                mInfo->setCaption(cadena);
214
215                sprintf(cadena,"LOD factor: %f",lodfactor);
216
217                mInfo2->setCaption(cadena);
218
219                mCamera->yaw(mRotX);
220        mCamera->pitch(mRotY);
221        mCamera->moveRelative(mTranslateVector);
222
223                return true;
224    }
225};
226
227class LodStripsApplication : public ExampleApplication
228{
229protected:
230public:
231    LodStripsApplication() {}
232    ~LodStripsApplication() {}
233
234protected:
235
236    // Just override the mandatory create scene method
237    void createScene(void)
238    {
239                Entity* pEnt;
240
241                mat = new MaterialPtr[1];
242
243        theCam = mCamera;
244        theCam->setPosition(0,-100,dist_min-600.0f);
245        // Set ambient light
246        mSceneMgr->setAmbientLight(ColourValue(0.3, 0.3, 0.3));
247
248        // Create a directional light
249        Light* l = mSceneMgr->createLight("MainLight");
250        l->setType(Light::LT_DIRECTIONAL);
251        l->setDirection(0.0,0.0,-1.0);
252       
253       
254        mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");
255
256        // My node to which all objects will be attached
257        SceneNode* myRootNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
258
259
260        pEnt = mSceneMgr->createEntity( "PoolFloor", "PoolFloor.mesh" );
261        myRootNode->attachObject(pEnt);
262                myRootNode->scale(6.0,6.0,6.0);
263
264                std::string model_file=model_name;
265                model_file.append(".mesh");
266
267                //Models
268        entity = mSceneMgr->createEntity(model_name, "../../../OgreStuff/media/GT/ogrolod.mesh");
269
270                ogreMesh = entity->getMesh().getPointer();
271
272                // load LOD info from the object
273                meshloader=new Geometry::GeoMeshLoader;
274                Geometry::Mesh *themesh = meshloader->load("../../../OgreStuff/media/GT/ogrolod.mesh");
275
276        node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
277        node->attachObject( entity );
278
279                std::string lod_file="../../media/GT/";
280                lod_file.append(model_name);
281                lod_file.append(".lod");
282
283                if (!meshloader->GetLodStripsData())
284            OGRE_EXCEPT(1, "The loaded mesh does not contain any LOD info","LOD Demo");
285
286                myStrips = new Geometry::LodStripsLibrary(      meshloader->GetLodStripsData(),
287                                                                                                        themesh,
288                                                                                                        new CustomIndexData(ogreMesh)   );
289               
290                entity->setNormaliseNormals(true);
291                entity->setMaterialName("LODStripsDemo/Ogro");
292
293                for (int i=-3; i<6; i++) // 7
294                        for (int j=0; j<10; j++) // 4
295                        {
296                                char newObjName[16]="";
297                                sprintf(newObjName,"arbol_%d_%d",i,j);
298                                Ogre::SceneNode * auxnode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
299                                Ogre::Entity *auxen = entity->clone(newObjName);
300                                auxnode->attachObject(auxen);
301                                auxnode->rotate(Ogre::Vector3(0,1,0),Ogre::Degree(180.0f),Ogre::Node::TS_WORLD);
302                                auxnode->scale(30.0f,30.0f,30.0f);
303                                float randomsepx = (float)((rand()%18)-9);
304                                float randomsepy = (float)((rand()%12)-6);
305                                auxnode->translate(i*70.0f+randomsepx,-150.0f,-j*70.0f-randomsepx);
306                                auxen->setNormaliseNormals(true);
307                        }
308
309                node->translate(0,0,-500.0f);
310
311        // show overlay
312        Overlay* pOver = OverlayManager::getSingleton().getByName("Demo_LodStrips/Overlay");   
313        mInfo = OverlayManager::getSingleton().getOverlayElement("Demo_LodStrips/Info_1");
314                mInfo2 = OverlayManager::getSingleton().getOverlayElement("Demo_LodStrips/Info_2");
315            pOver->show();
316
317    }
318
319    void createFrameListener(void)
320    {
321        mFrameListener= new FresnelFrameListener(mWindow, mCamera);
322        mFrameListener->showDebugOverlay(true);
323        mRoot->addFrameListener(mFrameListener);
324    }
325
326};
327
328
329
330#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
331#define WIN32_LEAN_AND_MEAN
332#include "windows.h"
333#endif
334
335#ifdef __cplusplus
336extern "C" {
337#endif
338
339#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
340INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
341#else
342int main(int argc, char **argv)
343#endif
344{
345    // Create application object
346    LodStripsApplication app;
347
348    try {
349        app.go();
350    } catch( Exception& e ) {
351#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
352        MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
353#else
354        std::cerr << "An exception has occured: " << e.getFullDescription();
355#endif
356    }
357
358
359    return 0;
360}
361
362#ifdef __cplusplus
363}
364#endif
Note: See TracBrowser for help on using the repository browser.