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

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