[1092] | 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
|
---|
[1783] | 20 | #define dist_min 40
|
---|
| 21 | #define dist_max 800
|
---|
[1092] | 22 |
|
---|
[1840] | 23 | #define ENABLE_TERRAIN_SHADOWS
|
---|
| 24 | #define TERRAIN_SHADOW_SIZE 2048
|
---|
| 25 |
|
---|
[1092] | 26 | //Global variables
|
---|
| 27 | Geometry::GeoMeshLoader *meshloader=NULL;
|
---|
[1104] | 28 | bool force_maxLODfactor = false;
|
---|
[1783] | 29 | const int num_tree_types=10;
|
---|
| 30 | Vector3 *centers=NULL;
|
---|
[1092] | 31 |
|
---|
| 32 | ColourValue color=ColourValue::Red;
|
---|
| 33 |
|
---|
| 34 | Camera* theCam;
|
---|
| 35 | Entity* pPlaneEnt;
|
---|
| 36 |
|
---|
| 37 | OverlayElement* mInfo;
|
---|
| 38 | OverlayElement* mInfo2;
|
---|
[1561] | 39 | OverlayElement* mHelp;
|
---|
[1840] | 40 | Ogre::Light *theLight = NULL;
|
---|
| 41 | Ogre::Viewport *shadowViewport = NULL;
|
---|
| 42 | RenderTarget *render_target = NULL;
|
---|
[1092] | 43 |
|
---|
[1813] | 44 | char HelpString[]="(F1) Quit Help\n"
|
---|
[1783] | 45 | "This demo shows an example of the LodTree model in action. The scene is composed of\n"
|
---|
| 46 | "several tree groups which are associated to a LodTree object that manage the level\n"
|
---|
| 47 | "of detail of both their trunk and leaves. The level of detail of the objects depends\n"
|
---|
| 48 | "on the distance to the camera. When the camera goes away from them, the level of detail\n"
|
---|
| 49 | "decreases, and when the camera gets closer to them increases to restore the original\n"
|
---|
| 50 | "geometry of the model.\n"
|
---|
| 51 | "The level of detail begins to decrease at a certain distance of each group, and stops\n"
|
---|
[1561] | 52 | "decreasing when the objects reach the their minimum LOD. This 'lodding' distance is\n"
|
---|
[1783] | 53 | "customizable from the source code of the demo.\n"
|
---|
| 54 | "The user can compare the differences in performance disabling the LOD management while\n"
|
---|
| 55 | "maintaining pressed F2.";
|
---|
[1092] | 56 |
|
---|
[1813] | 57 | char NoHelpString[]="\n\n\n\n\n\n\n\n\n\n\n\n\n(F1) Help\n";
|
---|
[1092] | 58 |
|
---|
[1561] | 59 |
|
---|
| 60 |
|
---|
[1813] | 61 |
|
---|
[1540] | 62 | class LodTreeFrameListener : public ExampleFrameListener
|
---|
[1092] | 63 | {
|
---|
| 64 | int manage;
|
---|
[1783] | 65 | Ogre::RaySceneQuery * raySceneQuery;
|
---|
| 66 | float *lodfactorBefore; // one lodfactor per LOD object type
|
---|
| 67 | Geometry::LodTreeLibrary **lod_tree_types;
|
---|
[1092] | 68 |
|
---|
[1812] | 69 | Ogre::TexturePtr shadow_map;
|
---|
| 70 |
|
---|
[1092] | 71 | public:
|
---|
| 72 |
|
---|
[1783] | 73 | LodTreeFrameListener(RenderWindow* win, Camera* cam,RaySceneQuery *rsq,Geometry::LodTreeLibrary **lod_trees)
|
---|
[1092] | 74 | : ExampleFrameListener(win, cam, false, false)
|
---|
| 75 | {
|
---|
| 76 | manage=1;
|
---|
[1783] | 77 | raySceneQuery=rsq;
|
---|
| 78 | lodfactorBefore=new float[num_tree_types];
|
---|
| 79 | for (int i=0; i<num_tree_types; i++)
|
---|
| 80 | lodfactorBefore[i]=-1;
|
---|
[1812] | 81 | lod_tree_types=lod_trees;
|
---|
[1092] | 82 | }
|
---|
| 83 |
|
---|
| 84 | bool frameStarted(const FrameEvent& evt)
|
---|
| 85 | {
|
---|
| 86 | // Move upto 80 units/second
|
---|
[1783] | 87 | Real MoveFactor = 120.0 * evt.timeSinceLastFrame;
|
---|
[1092] | 88 |
|
---|
| 89 | // Copy the current state of the input devices
|
---|
| 90 | mInputDevice->capture();
|
---|
| 91 |
|
---|
| 92 | // If this is the first frame, pick a speed
|
---|
| 93 | if (evt.timeSinceLastFrame == 0)
|
---|
| 94 | {
|
---|
| 95 | mMoveScale = 1;
|
---|
| 96 | mRotScale = 0.1;
|
---|
| 97 | }
|
---|
| 98 | // Otherwise scale movement units by time passed since last frame
|
---|
| 99 | else
|
---|
| 100 | {
|
---|
| 101 | // Move about 100 units per second,
|
---|
| 102 | mMoveScale = mMoveSpeed * evt.timeSinceLastFrame;
|
---|
| 103 | // Take about 10 seconds for full rotation
|
---|
| 104 | mRotScale = mRotateSpeed * evt.timeSinceLastFrame;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | mRotX = 0;
|
---|
| 108 | mRotY = 0;
|
---|
| 109 | mTranslateVector = Vector3::ZERO;
|
---|
| 110 |
|
---|
| 111 | //LOD selection
|
---|
[1783] | 112 | for (int i=0; i<num_tree_types; i++)
|
---|
| 113 | {
|
---|
[1813] | 114 | Vector3 dist = centers[i] - mCamera->getPosition();
|
---|
[1783] | 115 | int distance = dist.length();
|
---|
[1092] | 116 |
|
---|
[1783] | 117 | float lodfactor = 0.0f;
|
---|
| 118 | if (distance < dist_min)
|
---|
| 119 | lodfactor = 1.0f;
|
---|
| 120 | else if (distance > dist_max)
|
---|
| 121 | lodfactor = 0.0f;
|
---|
| 122 | else
|
---|
| 123 | {
|
---|
| 124 | lodfactor = (float)(distance - dist_min) / (float)(dist_max - dist_min);
|
---|
| 125 | lodfactor = 1.0f - lodfactor;
|
---|
| 126 | }
|
---|
[1092] | 127 |
|
---|
[1783] | 128 | assert(lodfactor>=0.0f);
|
---|
| 129 | assert(lodfactor<=1.0f);
|
---|
[1092] | 130 |
|
---|
[1783] | 131 | if (force_maxLODfactor)
|
---|
| 132 | lodfactor=1.0f;
|
---|
[1104] | 133 |
|
---|
[1840] | 134 | if (fabsf(lodfactorBefore[i]-lodfactor)>0.05f)
|
---|
[1783] | 135 | {
|
---|
| 136 | lod_tree_types[i]->GoToLod(lodfactor);
|
---|
| 137 | lodfactorBefore[i]=lodfactor;
|
---|
| 138 | }
|
---|
[1092] | 139 | }
|
---|
| 140 |
|
---|
[1540] | 141 | // Move the cam
|
---|
| 142 | if(mInputDevice->isKeyDown(Ogre::KC_UP) ||
|
---|
| 143 | mInputDevice->isKeyDown(Ogre::KC_W) ||
|
---|
| 144 | mInputDevice->isKeyDown(Ogre::KC_NUMPAD5))
|
---|
[1092] | 145 | mTranslateVector.z = -mMoveScale;
|
---|
| 146 |
|
---|
[1540] | 147 | if(mInputDevice->isKeyDown(Ogre::KC_DOWN) ||
|
---|
| 148 | mInputDevice->isKeyDown(Ogre::KC_S) ||
|
---|
| 149 | mInputDevice->isKeyDown(Ogre::KC_NUMPAD2))
|
---|
[1092] | 150 | mTranslateVector.z = mMoveScale;
|
---|
| 151 |
|
---|
[1540] | 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 |
|
---|
[1092] | 160 | if(mInputDevice->isKeyDown(Ogre::KC_LEFT))
|
---|
| 161 | mCamera->yaw(mRotScale);
|
---|
| 162 |
|
---|
[1104] | 163 | force_maxLODfactor=mInputDevice->isKeyDown(Ogre::KC_F2);
|
---|
| 164 |
|
---|
[1092] | 165 | if(mInputDevice->isKeyDown(Ogre::KC_RIGHT))
|
---|
| 166 | mCamera->yaw(-mRotScale);
|
---|
| 167 |
|
---|
[1561] | 168 | static bool newpush = true;
|
---|
| 169 | if (!mInputDevice->isKeyDown(Ogre::KC_F1))
|
---|
| 170 | newpush = true;
|
---|
| 171 |
|
---|
| 172 | static bool showing_help = false;
|
---|
| 173 | if (mInputDevice->isKeyDown(Ogre::KC_F1) && newpush)
|
---|
| 174 | {
|
---|
| 175 | newpush = false;
|
---|
| 176 | if (showing_help = !showing_help)
|
---|
| 177 | mHelp->setCaption(HelpString);
|
---|
| 178 | else
|
---|
| 179 | mHelp->setCaption(NoHelpString);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 |
|
---|
[1092] | 183 | if(mInputDevice->isKeyDown(Ogre::KC_ESCAPE))
|
---|
| 184 | return false;
|
---|
| 185 |
|
---|
| 186 | if( mInputDevice->getMouseButton( 1 ) )
|
---|
| 187 | {
|
---|
| 188 | mTranslateVector.x += mInputDevice->getMouseRelativeX() * 0.13;
|
---|
| 189 | mTranslateVector.y -= mInputDevice->getMouseRelativeY() * 0.13;
|
---|
| 190 | }
|
---|
| 191 | else
|
---|
| 192 | {
|
---|
| 193 | mRotX = Degree(-mInputDevice->getMouseRelativeX() * 0.13);
|
---|
| 194 | mRotY = Degree(-mInputDevice->getMouseRelativeY() * 0.13);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | mCamera->yaw(mRotX);
|
---|
| 198 | mCamera->pitch(mRotY);
|
---|
| 199 | mCamera->moveRelative(mTranslateVector);
|
---|
| 200 |
|
---|
[1783] | 201 | // make the camera walk onto the ground
|
---|
| 202 | Ogre::Ray ray(mCamera->getPosition()+Ogre::Vector3(0.0f,200.0f,0.0f), Ogre::Vector3::NEGATIVE_UNIT_Y);
|
---|
| 203 | raySceneQuery->setRay(ray);
|
---|
| 204 | RaySceneQueryResult &rayRes = raySceneQuery->execute();
|
---|
| 205 | RaySceneQueryResult::iterator itr = rayRes.begin();
|
---|
| 206 | if (itr != rayRes.end() && itr->worldFragment)
|
---|
| 207 | mCamera->setPosition(itr->worldFragment->singleIntersection+Ogre::Vector3(0.0f,15.0f,0.0f));
|
---|
| 208 |
|
---|
[1813] | 209 |
|
---|
| 210 | char cadena[256];
|
---|
| 211 | const RenderTarget::FrameStats& stats = mWindow->getStatistics();
|
---|
| 212 | sprintf(cadena,"Current FPS: %f\n",stats.lastFPS);
|
---|
| 213 | mInfo2->setCaption(cadena);
|
---|
| 214 |
|
---|
[1092] | 215 | return true;
|
---|
| 216 | }
|
---|
| 217 | };
|
---|
| 218 |
|
---|
[1529] | 219 | class CustomIndexData : public Geometry::IndexData
|
---|
| 220 | {
|
---|
| 221 | private:
|
---|
| 222 | Ogre::Mesh *targetMesh;
|
---|
| 223 | Ogre::HardwareIndexBufferSharedPtr ibuf;
|
---|
| 224 | Ogre::RenderOperation mRenderOp;
|
---|
| 225 | unsigned long* pIdx;
|
---|
| 226 | public:
|
---|
| 227 | CustomIndexData(Ogre::Mesh *ogremesh):Geometry::IndexData(){
|
---|
| 228 | targetMesh=ogremesh;
|
---|
| 229 | pIdx=NULL;
|
---|
| 230 | }
|
---|
| 231 | virtual ~CustomIndexData(void){}
|
---|
| 232 |
|
---|
| 233 | virtual void Begin(unsigned int submeshid, unsigned int indexcount){
|
---|
| 234 | targetMesh->getSubMesh(submeshid)->_getRenderOperation(mRenderOp,0);
|
---|
| 235 | ibuf = mRenderOp.indexData->indexBuffer;
|
---|
| 236 | mRenderOp.indexData->indexCount = indexcount;
|
---|
| 237 | pIdx = static_cast<unsigned long*>(ibuf->lock(Ogre::HardwareBuffer::HBL_NORMAL));
|
---|
| 238 | }
|
---|
| 239 | virtual void SetIndex(unsigned int i, unsigned int index){
|
---|
[1783] | 240 | pIdx[i] = index;
|
---|
[1529] | 241 | }
|
---|
| 242 | virtual void End(){
|
---|
| 243 | ibuf->unlock();
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | virtual void BorrowIndexData(const Geometry::IndexData *){}
|
---|
| 247 | };
|
---|
| 248 |
|
---|
| 249 |
|
---|
[1840] | 250 | #include "OgreTerrainSceneManager.h"
|
---|
| 251 |
|
---|
| 252 | class LodTreeApplication : public ExampleApplication, public RenderTargetListener
|
---|
[1092] | 253 | {
|
---|
| 254 | protected:
|
---|
| 255 | public:
|
---|
[1783] | 256 | LodTreeApplication(){
|
---|
| 257 | raySceneQuery=NULL;
|
---|
| 258 | centers=new Vector3[num_tree_types]; // one center per object
|
---|
| 259 | }
|
---|
[1540] | 260 | ~LodTreeApplication(){}
|
---|
[1092] | 261 |
|
---|
[1783] | 262 | Ogre::RaySceneQuery * raySceneQuery;
|
---|
| 263 | Geometry::LodTreeLibrary **lod_tree_types;
|
---|
| 264 |
|
---|
[1092] | 265 | protected:
|
---|
| 266 |
|
---|
[1783] | 267 | void chooseSceneManager(void)
|
---|
| 268 | {
|
---|
[1812] | 269 | mSceneMgr = mRoot->createSceneManager("TerrainSceneManager");
|
---|
[1783] | 270 | }
|
---|
[1092] | 271 |
|
---|
[1840] | 272 | // render target events
|
---|
| 273 | void preRenderTargetUpdate(const RenderTargetEvent& evt)
|
---|
| 274 | {
|
---|
| 275 | if (render_target)
|
---|
| 276 | {
|
---|
| 277 | (static_cast<TerrainSceneManager*>(mSceneMgr))->getTerrainRootNode()->setVisible(false);
|
---|
| 278 | mSceneMgr->setSkyBox(false, "Examples/CloudyNoonSkyBox");
|
---|
| 279 | mSceneMgr->setFog(FOG_EXP, Ogre::ColourValue(0.5f,0.5f,0.5f), 1000 );
|
---|
| 280 | mInfo2->hide();
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | void postRenderTargetUpdate(const RenderTargetEvent& evt)
|
---|
| 284 | {
|
---|
| 285 | if (render_target)
|
---|
| 286 | {
|
---|
| 287 | (static_cast<TerrainSceneManager*>(mSceneMgr))->getTerrainRootNode()->setVisible(true);
|
---|
| 288 | mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");
|
---|
| 289 | mSceneMgr->setFog( FOG_EXP, Ogre::ColourValue(0.4f,0.5f,0.6f), 0.001 );
|
---|
| 290 | mInfo2->show();
|
---|
| 291 | // render_target->removeAllListeners();
|
---|
| 292 | // render_target->removeAllViewports();
|
---|
| 293 | render_target->setAutoUpdated(false);
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
[1092] | 296 |
|
---|
[1840] | 297 |
|
---|
[1092] | 298 | // Just override the mandatory create scene method
|
---|
| 299 | void createScene(void)
|
---|
| 300 | {
|
---|
| 301 | theCam = mCamera;
|
---|
[1783] | 302 | theCam->setPosition(500,100,dist_min+600);
|
---|
[1092] | 303 | // Set ambient light
|
---|
[1124] | 304 | mSceneMgr->setAmbientLight(ColourValue(0.4, 0.4, 0.4));
|
---|
[1561] | 305 | theCam->setNearClipDistance(0.1f);
|
---|
[1092] | 306 |
|
---|
[1840] | 307 | // mSceneMgr->setFog( FOG_EXP, Ogre::ColourValue(0.4f,0.5f,0.6f), 0.001 );
|
---|
[1783] | 308 |
|
---|
[1104] | 309 | // Create a directional light
|
---|
[1840] | 310 | theLight = mSceneMgr->createLight("MainLight");
|
---|
| 311 | theLight->setType(Light::LT_DIRECTIONAL);
|
---|
| 312 | theLight->setDirection(0.1,-1.0,-0.2);
|
---|
[1092] | 313 |
|
---|
[1783] | 314 | // terrain
|
---|
| 315 | mSceneMgr->setWorldGeometry( "terrain.cfg" );
|
---|
[1840] | 316 |
|
---|
[1092] | 317 | mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");
|
---|
| 318 |
|
---|
| 319 | // My node to which all objects will be attached
|
---|
| 320 | SceneNode* myRootNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
| 321 |
|
---|
[1783] | 322 | // initialize the rayscene queryer
|
---|
| 323 | raySceneQuery = mSceneMgr->createRayQuery(Ray());
|
---|
[1092] | 324 |
|
---|
[1783] | 325 | // the blacksmith
|
---|
| 326 | Vector3 BS_center=Ogre::Vector3(780.0f,200.0f,650.0f);
|
---|
| 327 | Ogre::Ray ray(BS_center, Ogre::Vector3::NEGATIVE_UNIT_Y);
|
---|
| 328 | raySceneQuery->setRay(ray);
|
---|
| 329 | RaySceneQueryResult &rayRes = raySceneQuery->execute();
|
---|
| 330 | RaySceneQueryResult::iterator itr = rayRes.begin();
|
---|
| 331 | if (itr != rayRes.end() && itr->worldFragment)
|
---|
| 332 | BS_center=itr->worldFragment->singleIntersection-Ogre::Vector3(0.0f,1.0f,0.0f);
|
---|
[1092] | 333 |
|
---|
[1783] | 334 | Ogre::Entity *entBS = mSceneMgr->createEntity("blacksmith","../../../OgreStuff/media/models/blacksmith.mesh");
|
---|
| 335 | Ogre::SceneNode * nodeBS = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
| 336 | nodeBS->attachObject(entBS);
|
---|
| 337 | nodeBS->translate(BS_center);
|
---|
| 338 | nodeBS->scale(0.1f,0.1f,0.1f);
|
---|
[1092] | 339 |
|
---|
[1783] | 340 | lod_tree_types = new Geometry::LodTreeLibrary*[num_tree_types];
|
---|
| 341 |
|
---|
| 342 | // trees
|
---|
| 343 | lod_tree_types[0] = CreateLODTrees("../../../OgreStuff/media/GT/ML06a_01.mesh",5,780,630,200,200,centers[0], 4.0f);
|
---|
| 344 | lod_tree_types[1] = CreateLODTrees("../../../OgreStuff/media/GT/ML13a_02.mesh",4,150,150,200,200,centers[1], 2.0f);
|
---|
| 345 | lod_tree_types[2] = CreateLODTrees("../../../OgreStuff/media/GT/ML15a_03.mesh",10,300,400,200,200,centers[2], 6.0f);
|
---|
| 346 | lod_tree_types[3] = CreateLODTrees("../../../OgreStuff/media/GT/ML06a_01.mesh",5,330,900,500,500,centers[3], 3.0f);
|
---|
| 347 | lod_tree_types[4] = CreateLODTrees("../../../OgreStuff/media/GT/ML13a_02.mesh",5,1100,900,300,300,centers[4], 2.0f);
|
---|
| 348 | lod_tree_types[5] = CreateLODTrees("../../../OgreStuff/media/GT/ML15a_03.mesh",10,1100,300,200,200,centers[5], 6.0f);
|
---|
| 349 | lod_tree_types[6] = CreateLODTrees("../../../OgreStuff/media/GT/ML06a_01.mesh",7,900,500,300,300,centers[6], 2.0f);
|
---|
| 350 | lod_tree_types[7] = CreateLODTrees("../../../OgreStuff/media/GT/ML13a_02.mesh",5,500,950,200,200,centers[7], 3.0f);
|
---|
| 351 | lod_tree_types[8] = CreateLODTrees("../../../OgreStuff/media/GT/ML15a_03.mesh",9,600,400,200,200,centers[8], 6.0f);
|
---|
| 352 | lod_tree_types[9] = CreateLODTrees("../../../OgreStuff/media/GT/ML06a_01.mesh",7,950,1100,200,200,centers[9], 2.5f);
|
---|
| 353 |
|
---|
| 354 | // show overlay
|
---|
| 355 | Overlay* pOver = OverlayManager::getSingleton().getByName("Demo_LodStrips/Overlay");
|
---|
| 356 | mInfo = OverlayManager::getSingleton().getOverlayElement("Demo_LodStrips/Info_1");
|
---|
| 357 | mInfo2 = OverlayManager::getSingleton().getOverlayElement("Demo_LodStrips/Info_2");
|
---|
| 358 | mHelp = OverlayManager::getSingleton().getOverlayElement("Demo_LodStrips/Help");
|
---|
| 359 | mHelp->setCaption(NoHelpString);
|
---|
| 360 | pOver->show();
|
---|
| 361 |
|
---|
[1813] | 362 | mInfo->setCaption("\nMaintain F2 to disable LOD");
|
---|
[1783] | 363 |
|
---|
[1840] | 364 | #ifdef ENABLE_TERRAIN_SHADOWS
|
---|
| 365 |
|
---|
[1812] | 366 | // create the shadow texture
|
---|
| 367 | TexturePtr shadow_map = Ogre::TextureManager::getSingleton().createManual("TheShadowMap",
|
---|
| 368 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,TEX_TYPE_2D,
|
---|
[1840] | 369 | TERRAIN_SHADOW_SIZE,TERRAIN_SHADOW_SIZE,0,PF_R8G8B8,TU_RENDERTARGET);
|
---|
[1812] | 370 |
|
---|
| 371 | HardwarePixelBufferSharedPtr shadowBuffer = shadow_map->getBuffer();
|
---|
[1840] | 372 | render_target = shadowBuffer->getRenderTarget();
|
---|
[1812] | 373 |
|
---|
[1840] | 374 | Ogre::Camera *lightCam = mSceneMgr->createCamera("lightCam");
|
---|
| 375 | lightCam->setProjectionType(Ogre::PT_ORTHOGRAPHIC);
|
---|
| 376 | lightCam->setAspectRatio(1.0);
|
---|
| 377 | lightCam->setFOVy(Degree(164));
|
---|
| 378 | lightCam->rotate(Vector3(1,0,0),Degree(-90));
|
---|
| 379 | lightCam->move(BS_center+Ogre::Vector3(-50,400,78));
|
---|
[1812] | 380 |
|
---|
[1840] | 381 | shadowViewport = render_target->addViewport(lightCam);
|
---|
| 382 | shadowViewport->setClearEveryFrame(true);
|
---|
| 383 | shadowViewport->setBackgroundColour(ColourValue::White);
|
---|
[1812] | 384 |
|
---|
[1840] | 385 | render_target->addListener(this);
|
---|
[1812] | 386 |
|
---|
[1840] | 387 | MaterialPtr terrainMat = MaterialManager::getSingleton().getByName("Demos/TerrainMixed");
|
---|
| 388 | Ogre::Pass *thePass = terrainMat->getTechnique(0)->createPass();
|
---|
| 389 | thePass->setSceneBlending(Ogre::SBT_MODULATE);
|
---|
| 390 | Ogre::TextureUnitState *texState = thePass->createTextureUnitState();
|
---|
| 391 | texState->setTextureName("TheShadowMap");
|
---|
| 392 | thePass->_load();
|
---|
[1812] | 393 |
|
---|
[1840] | 394 | terrainMat->compile();
|
---|
| 395 | terrainMat->load();
|
---|
| 396 |
|
---|
| 397 | #endif
|
---|
[1783] | 398 | }
|
---|
| 399 |
|
---|
| 400 | void destroyScene(void)
|
---|
| 401 | {
|
---|
| 402 | if (raySceneQuery)
|
---|
| 403 | delete raySceneQuery;
|
---|
| 404 | delete[] lod_tree_types;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | void createFrameListener(void)
|
---|
| 408 | {
|
---|
| 409 | mFrameListener= new LodTreeFrameListener(mWindow, mCamera, raySceneQuery, lod_tree_types);
|
---|
[1812] | 410 | mFrameListener->showDebugOverlay(false);
|
---|
[1783] | 411 | mRoot->addFrameListener(mFrameListener);
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | Geometry::LodTreeLibrary *CreateLODTrees(const char *filename, int numObjs, float cx, float cz, float w, float h, Vector3 &outcenter, float scale)
|
---|
| 415 | {
|
---|
| 416 | static int iname = 0;
|
---|
| 417 | char newMeshName[16]="";
|
---|
| 418 | sprintf(newMeshName,"tree_%d",iname);
|
---|
| 419 |
|
---|
| 420 |
|
---|
[1092] | 421 | // load LOD info from the object
|
---|
| 422 | meshloader=new Geometry::GeoMeshLoader;
|
---|
[1783] | 423 | Geometry::Mesh *themesh = meshloader->load((char*)filename);
|
---|
[1092] | 424 |
|
---|
[1306] | 425 | if (!meshloader->GetLodStripsData())
|
---|
| 426 | OGRE_EXCEPT(1, "The loaded mesh does not contain LOD info for the trunk","LOD Demo");
|
---|
| 427 | if (!meshloader->GetTreeSimpSeq())
|
---|
| 428 | OGRE_EXCEPT(1, "The loaded mesh does not contain LOD info for the foliage","LOD Demo");
|
---|
| 429 |
|
---|
[1783] | 430 | Ogre::Entity *entity = mSceneMgr->createEntity(newMeshName,filename);
|
---|
[1812] | 431 | // if (!entity->getMesh().unique())
|
---|
| 432 | // {
|
---|
[1783] | 433 | static int iclone = 0;
|
---|
| 434 | char newCloneMesh[16]="";
|
---|
[1812] | 435 | sprintf(newCloneMesh,"clonemesh_%d",iclone);
|
---|
| 436 | char newCloneEntity[16]="";
|
---|
| 437 | sprintf(newCloneEntity,"cloneentity_%d",iclone);
|
---|
| 438 | iclone++;
|
---|
| 439 |
|
---|
| 440 | Ogre::MeshPtr newmesh = entity->getMesh()->clone(newCloneMesh);
|
---|
| 441 | entity = mSceneMgr->createEntity(newCloneEntity,newCloneMesh);
|
---|
| 442 | // }
|
---|
[1783] | 443 | Ogre::Mesh *ogreMesh = entity->getMesh().getPointer();
|
---|
| 444 | entity->setNormaliseNormals(true);
|
---|
| 445 |
|
---|
| 446 | Geometry::LodTreeLibrary *mytree = new Geometry::LodTreeLibrary(
|
---|
| 447 | meshloader->GetLodStripsData(),
|
---|
[1529] | 448 | meshloader->GetTreeSimpSeq(),
|
---|
| 449 | themesh,
|
---|
[1783] | 450 | new CustomIndexData(ogreMesh) );
|
---|
[1092] | 451 |
|
---|
[1783] | 452 | outcenter=Vector3::ZERO;
|
---|
[1092] | 453 |
|
---|
[1783] | 454 | for (int i=0; i<numObjs; i++)
|
---|
[1104] | 455 | {
|
---|
[1783] | 456 | char newTreeName[16]="";
|
---|
| 457 | sprintf(newTreeName,"arbol_%d_%d",iname,i);
|
---|
| 458 | Ogre::SceneNode * auxnode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
| 459 | Ogre::Entity *auxent = entity->clone(newTreeName);
|
---|
| 460 | auxnode->attachObject( auxent );
|
---|
[1092] | 461 |
|
---|
[1783] | 462 | float kx = (rand()%100)/100.0f;
|
---|
| 463 | float kz = (rand()%100)/100.0f;
|
---|
[1092] | 464 |
|
---|
[1783] | 465 | Ogre::Vector3 treepos(kx*(cx-w*0.5f) + (1.0f-kx)*(cx+w*0.5f), 200.0f,
|
---|
| 466 | kz*(cz-h*0.5f) + (1.0f-kz)*(cz+h*0.5f));
|
---|
[1092] | 467 |
|
---|
[1783] | 468 | Ogre::Ray ray(treepos,Ogre::Vector3::NEGATIVE_UNIT_Y);
|
---|
| 469 | raySceneQuery->setRay(ray);
|
---|
| 470 | RaySceneQueryResult &rayRes = raySceneQuery->execute();
|
---|
| 471 | RaySceneQueryResult::iterator itr = rayRes.begin();
|
---|
| 472 | if (itr != rayRes.end() && itr->worldFragment)
|
---|
| 473 | treepos = itr->worldFragment->singleIntersection;
|
---|
[1113] | 474 |
|
---|
[1783] | 475 | auxnode->translate(treepos);
|
---|
| 476 | auxnode->scale(scale,scale,scale);
|
---|
| 477 | auxnode->rotate(Ogre::Vector3(0,1,0),Ogre::Degree(rand()%360));
|
---|
| 478 | auxent->setNormaliseNormals(true);
|
---|
| 479 | outcenter+=treepos;
|
---|
| 480 | }
|
---|
| 481 | outcenter/=numObjs;
|
---|
| 482 | iname++;
|
---|
[1092] | 483 |
|
---|
[1783] | 484 | return mytree;
|
---|
| 485 | }
|
---|
[1092] | 486 |
|
---|
[1783] | 487 | float Rand(float minN, float maxN)
|
---|
| 488 | {
|
---|
| 489 | return (((rand()%1000)/1000.0f)*(maxN-minN))+minN;
|
---|
| 490 | }
|
---|
[1092] | 491 |
|
---|
| 492 | };
|
---|
| 493 |
|
---|
| 494 |
|
---|
| 495 |
|
---|
| 496 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
| 497 | #define WIN32_LEAN_AND_MEAN
|
---|
| 498 | #include "windows.h"
|
---|
| 499 | #endif
|
---|
| 500 |
|
---|
| 501 | #ifdef __cplusplus
|
---|
| 502 | extern "C" {
|
---|
| 503 | #endif
|
---|
| 504 |
|
---|
| 505 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
| 506 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
|
---|
| 507 | #else
|
---|
| 508 | int main(int argc, char **argv)
|
---|
| 509 | #endif
|
---|
| 510 | {
|
---|
| 511 | // Create application object
|
---|
[1540] | 512 | LodTreeApplication app;
|
---|
[1092] | 513 |
|
---|
| 514 | try {
|
---|
| 515 | app.go();
|
---|
| 516 | } catch( Exception& e ) {
|
---|
| 517 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
| 518 | MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
|
---|
| 519 | #else
|
---|
| 520 | std::cerr << "An exception has occured: " << e.getFullDescription();
|
---|
| 521 | #endif
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 |
|
---|
| 525 | return 0;
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | #ifdef __cplusplus
|
---|
| 529 | }
|
---|
| 530 | #endif
|
---|