[61] | 1 | #include <OgreNoMemoryMacros.h>
|
---|
| 2 | #include <CEGUI/CEGUI.h>
|
---|
| 3 | #include <../CEGUIRenderer/include/OgreCEGUIRenderer.h>
|
---|
| 4 | #include <../CEGUIRenderer/include/OgreCEGUIResourceProvider.h>
|
---|
| 5 | #include <../CEGUIRenderer/include/OgreCEGUITexture.h>
|
---|
| 6 | #include <OgreMemoryMacros.h>
|
---|
| 7 |
|
---|
[100] | 8 | #include <Ogre.h>
|
---|
[115] | 9 | //#include "OgreReferenceAppLayer.h"
|
---|
[100] | 10 | //#include "OgreRefAppWorld.h"
|
---|
[61] | 11 | #include "TestCullingTerrainApplication.h"
|
---|
| 12 |
|
---|
[115] | 13 |
|
---|
[61] | 14 | #define WIN32_LEAN_AND_MEAN
|
---|
[94] | 15 | #include <windows.h>
|
---|
[61] | 16 |
|
---|
[80] | 17 | /*******************************************************/
|
---|
| 18 | /* TestCullingTerrainApplication implementation */
|
---|
| 19 | /*******************************************************/
|
---|
[145] | 20 | TestCullingTerrainApplication::TestCullingTerrainApplication():
|
---|
| 21 | mTerrainContentGenerator(NULL)
|
---|
| 22 | {
|
---|
| 23 | }
|
---|
[100] | 24 | //-----------------------------------------------------------------------
|
---|
[61] | 25 | TestCullingTerrainApplication::~TestCullingTerrainApplication()
|
---|
| 26 | {
|
---|
[122] | 27 | if (mTerrainContentGenerator)
|
---|
| 28 | {
|
---|
[93] | 29 | delete mTerrainContentGenerator;
|
---|
[122] | 30 | mTerrainContentGenerator = NULL;
|
---|
| 31 | }
|
---|
[99] | 32 | //if(mRenderTargetListener) delete mRenderTargetListener;
|
---|
[61] | 33 | }
|
---|
| 34 | //-----------------------------------------------------------------------
|
---|
[93] | 35 | void TestCullingTerrainApplication::createCamera()
|
---|
[61] | 36 | {
|
---|
[99] | 37 | // create the camera
|
---|
[103] | 38 | mCamera = mSceneMgr->createCamera("PlayerCam");
|
---|
[93] | 39 |
|
---|
[100] | 40 | /** set a nice viewpoint
|
---|
| 41 | * we use a camera node here and apply all transformations on it instead
|
---|
| 42 | * of applying all transformations directly to the camera
|
---|
| 43 | * because then the camera is displayed correctly in the visualization
|
---|
| 44 | */
|
---|
[137] | 45 | mCamNode = mSceneMgr->getRootSceneNode()->
|
---|
| 46 | createChildSceneNode("CamNode1", Vector3(707, 5000, 528));
|
---|
[100] | 47 | //mCamera->setPosition(707, 5000, 528);
|
---|
| 48 | mCamNode->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
|
---|
| 49 | mCamNode->attachObject(mCamera);
|
---|
[61] | 50 |
|
---|
[100] | 51 | //-- create visualization camera
|
---|
| 52 | mVizCamera = mSceneMgr->createCamera("VizCam");
|
---|
| 53 | mVizCamera->setPosition(mCamNode->getPosition());
|
---|
| 54 |
|
---|
| 55 | mVizCamera->setNearClipDistance(1);
|
---|
[93] | 56 | mCamera->setNearClipDistance(1);
|
---|
[61] | 57 |
|
---|
[99] | 58 | // infinite far plane?
|
---|
[93] | 59 | if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_INFINITE_FAR_PLANE))
|
---|
| 60 | {
|
---|
[100] | 61 | mVizCamera->setFarClipDistance(0);
|
---|
[93] | 62 | mCamera->setFarClipDistance(0);
|
---|
| 63 | }
|
---|
| 64 | else
|
---|
| 65 | {
|
---|
[104] | 66 | mVizCamera->setFarClipDistance(20000);
|
---|
| 67 | mCamera->setFarClipDistance(20000);
|
---|
[99] | 68 | }
|
---|
[61] | 69 | }
|
---|
[99] | 70 |
|
---|
[61] | 71 | //-----------------------------------------------------------------------
|
---|
[99] | 72 | bool TestCullingTerrainApplication::setup()
|
---|
| 73 | {
|
---|
[145] | 74 | bool carryOn = ExampleApplication::setup();
|
---|
[99] | 75 |
|
---|
[145] | 76 | if (carryOn)
|
---|
[144] | 77 | createRenderTargetListener();
|
---|
[99] | 78 |
|
---|
[145] | 79 | return carryOn;
|
---|
[99] | 80 | }
|
---|
| 81 | //-----------------------------------------------------------------------
|
---|
| 82 | void TestCullingTerrainApplication::createRenderTargetListener()
|
---|
| 83 | {
|
---|
| 84 | mWindow->addListener(new VisualizationRenderTargetListener(mSceneMgr));
|
---|
| 85 | }
|
---|
| 86 | //-----------------------------------------------------------------------
|
---|
[75] | 87 | void TestCullingTerrainApplication::createScene()
|
---|
[61] | 88 | {
|
---|
| 89 | // Set ambient light
|
---|
[139] | 90 | mAmbientLight = ColourValue(0.5, 0.5, 0.5);
|
---|
| 91 | mSceneMgr->setAmbientLight(mAmbientLight);
|
---|
[113] | 92 |
|
---|
[110] | 93 | //-- create light
|
---|
[111] | 94 | mSunLight = mSceneMgr->createLight("SunLight");
|
---|
| 95 | mSunLight->setType(Light::LT_DIRECTIONAL);
|
---|
| 96 | //mSunLight->setType(Light::LT_SPOTLIGHT);
|
---|
[112] | 97 | //mSunLight->setSpotlightRange(Degree(30), Degree(50));
|
---|
| 98 |
|
---|
[111] | 99 | mSunLight->setPosition(707, 2000, 500);
|
---|
| 100 | mSunLight->setCastShadows(true);
|
---|
| 101 |
|
---|
[137] | 102 | // set light angle not too sharp, otherwise shadows textures will be broken
|
---|
[113] | 103 | Vector3 dir(0.5, 1, 0.5);
|
---|
[110] | 104 | dir.normalise();
|
---|
[112] | 105 | mSunLight->setDirection(dir);
|
---|
| 106 | //mSunLight->setDirection(Vector3::NEGATIVE_UNIT_Y);
|
---|
[111] | 107 | mSunLight->setDiffuseColour(1, 1, 1);
|
---|
| 108 | mSunLight->setSpecularColour(1, 1, 1);
|
---|
[110] | 109 |
|
---|
[137] | 110 | // -- Fog
|
---|
[61] | 111 | // NB it's VERY important to set this before calling setWorldGeometry
|
---|
| 112 | // because the vertex program picked will be different
|
---|
| 113 | ColourValue fadeColour(0.93, 0.86, 0.76);
|
---|
| 114 | mWindow->getViewport(0)->setBackgroundColour(fadeColour);
|
---|
| 115 | //mSceneMgr->setFog( FOG_LINEAR, fadeColour, .001, 500, 1000);
|
---|
[94] | 116 |
|
---|
[61] | 117 | // Create a skybox
|
---|
[103] | 118 | mSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox", 5000, false);
|
---|
| 119 |
|
---|
[61] | 120 | std::string terrain_cfg("terrain.cfg");
|
---|
| 121 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
|
---|
| 122 | terrain_cfg = mResourcePath + terrain_cfg;
|
---|
| 123 | #endif
|
---|
[74] | 124 | mSceneMgr->setWorldGeometry(terrain_cfg);
|
---|
[75] | 125 |
|
---|
[100] | 126 | //-- CEGUI setup
|
---|
[61] | 127 | setupGui();
|
---|
[100] | 128 |
|
---|
[111] | 129 | // Floor plane
|
---|
[121] | 130 | /*
|
---|
| 131 | Plane plane;
|
---|
[111] | 132 | plane.normal = Vector3::UNIT_Y;
|
---|
| 133 | plane.d = -60;
|
---|
| 134 | MeshManager::getSingleton().createPlane("Myplane",
|
---|
| 135 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
|
---|
| 136 | 5000,5000,100,100,true,1,5,5,Vector3::UNIT_Z);
|
---|
| 137 | Entity* pPlaneEnt = mSceneMgr->createEntity( "plane", "Myplane" );
|
---|
| 138 | pPlaneEnt->setMaterialName("Examples/Rockwall");
|
---|
| 139 | pPlaneEnt->setCastShadows(true);
|
---|
[121] | 140 | mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(pPlaneEnt);
|
---|
| 141 | */
|
---|
[137] | 142 | mSceneMgr->setShadowTextureSettings(1024, 2);
|
---|
[112] | 143 | mSceneMgr->setShadowColour(ColourValue(0.5, 0.5, 0.5));
|
---|
[139] | 144 | //mSceneMgr->setShowDebugShadows(true);
|
---|
[111] | 145 |
|
---|
| 146 |
|
---|
[100] | 147 | //-- terrain content setup
|
---|
| 148 |
|
---|
[80] | 149 | // HACK: necessary to call once before the content creation for
|
---|
| 150 | // terrain initialisation
|
---|
| 151 | mSceneMgr->_renderScene(mCamera, mWindow->getViewport(0), true);
|
---|
[74] | 152 |
|
---|
[82] | 153 | mTerrainContentGenerator = new TerrainContentGenerator(mSceneMgr);
|
---|
[110] | 154 |
|
---|
[107] | 155 | // if no objects file generate yourself
|
---|
[110] | 156 | if (!mTerrainContentGenerator->LoadObjects("objects.out"))
|
---|
| 157 | {
|
---|
[135] | 158 | // to provide much occlusion,
|
---|
| 159 | // height is restricted to 50, so no objects appear on peaks
|
---|
[107] | 160 | mTerrainContentGenerator->SetMaxPos(Vector3(3000.0f, 50.0f, 3000.0f));
|
---|
[111] | 161 | mTerrainContentGenerator->SetOffset(0);
|
---|
[110] | 162 |
|
---|
| 163 | // the objects are generated on the whole terrain
|
---|
[155] | 164 | mTerrainContentGenerator->GenerateScene(1500, "robot");
|
---|
| 165 | //mTerrainContentGenerator->GenerateScene(1500, "athene");
|
---|
[120] | 166 | //mTerrainContentGenerator->GenerateScene(500, "ninja");
|
---|
[110] | 167 | }
|
---|
[86] | 168 |
|
---|
[84] | 169 | // no limitations needed anymore: the user can set
|
---|
| 170 | // objects also on peaks of terrain
|
---|
[107] | 171 | mTerrainContentGenerator->SetMaxPos(Vector3(3000.0f, 5000.0f, 3000.0f));
|
---|
[61] | 172 | }
|
---|
| 173 | //-----------------------------------------------------------------------
|
---|
[80] | 174 | void TestCullingTerrainApplication::setupGui()
|
---|
[61] | 175 | {
|
---|
[75] | 176 | mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
|
---|
| 177 | false, 3000, ST_EXTERIOR_CLOSE);
|
---|
[61] | 178 | mGUISystem = new CEGUI::System(mGUIRenderer);
|
---|
| 179 |
|
---|
| 180 | // Mouse
|
---|
| 181 | CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLook.scheme");
|
---|
| 182 | CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
|
---|
[133] | 183 | mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook",
|
---|
| 184 | (CEGUI::utf8*)"MouseArrow");
|
---|
[61] | 185 |
|
---|
[75] | 186 | CEGUI::MouseCursor::getSingleton().show();
|
---|
[61] | 187 | }
|
---|
| 188 | //-----------------------------------------------------------------------
|
---|
[80] | 189 | void TestCullingTerrainApplication::createFrameListener()
|
---|
[61] | 190 | {
|
---|
[133] | 191 | mTerrainFrameListener = new TerrainFrameListener(mWindow, mCamera, mSceneMgr,
|
---|
[121] | 192 | mGUIRenderer, mTerrainContentGenerator, mVizCamera, mCamNode, mSunLight);
|
---|
[120] | 193 |
|
---|
[115] | 194 | mRoot->addFrameListener(mTerrainFrameListener);
|
---|
[61] | 195 | }
|
---|
| 196 | //-----------------------------------------------------------------------
|
---|
[80] | 197 | void TestCullingTerrainApplication::chooseSceneManager()
|
---|
[61] | 198 | {
|
---|
| 199 | mSceneMgr = mRoot->getSceneManager(ST_EXTERIOR_CLOSE);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|
[99] | 203 | /**************************************************************/
|
---|
| 204 | /* VisualizationRenderTargetListener implementation */
|
---|
| 205 | /**************************************************************/
|
---|
[107] | 206 | //-----------------------------------------------------------------------
|
---|
[99] | 207 | VisualizationRenderTargetListener::VisualizationRenderTargetListener(SceneManager *sceneMgr)
|
---|
| 208 | :RenderTargetListener(), mSceneMgr(sceneMgr)
|
---|
| 209 | {
|
---|
| 210 | }
|
---|
[61] | 211 | //-----------------------------------------------------------------------
|
---|
[99] | 212 | void VisualizationRenderTargetListener::preViewportUpdate(const RenderTargetViewportEvent &evt)
|
---|
| 213 | {
|
---|
[133] | 214 | // visualization viewport
|
---|
| 215 | const bool showViz = evt.source->getZOrder() == VIZ_VIEWPORT_Z_ORDER;
|
---|
[100] | 216 | const bool nShowViz = !showViz;
|
---|
| 217 |
|
---|
[139] | 218 | mSavedShadowTechnique = mSceneMgr->getShadowTechnique();
|
---|
| 219 | mSavedAmbientLight = mSceneMgr->getAmbientLight();
|
---|
| 220 |
|
---|
| 221 | // -- ambient light must be full for visualization, shadows disabled
|
---|
[113] | 222 | if (showViz)
|
---|
| 223 | {
|
---|
| 224 | mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
|
---|
[139] | 225 | mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE);
|
---|
[113] | 226 | }
|
---|
[139] | 227 |
|
---|
| 228 | mSceneMgr->setOption("PrepareVisualization", &showViz);
|
---|
[100] | 229 | mSceneMgr->setOption("SkyBoxEnabled", &nShowViz);
|
---|
| 230 | //mSceneMgr->setOption("SkyPlaneEnabled", &showViz);
|
---|
[99] | 231 |
|
---|
| 232 | RenderTargetListener::preViewportUpdate(evt);
|
---|
| 233 | }
|
---|
| 234 | //-----------------------------------------------------------------------
|
---|
| 235 | void VisualizationRenderTargetListener::postRenderTargetUpdate(const RenderTargetEvent &evt)
|
---|
| 236 | {
|
---|
[139] | 237 | // reset values
|
---|
| 238 | mSceneMgr->setShadowTechnique(mSavedShadowTechnique);
|
---|
| 239 | mSceneMgr->setAmbientLight(mSavedAmbientLight);
|
---|
| 240 |
|
---|
[99] | 241 | RenderTargetListener::postRenderTargetUpdate(evt);
|
---|
| 242 | }
|
---|
| 243 | //-----------------------------------------------------------------------
|
---|
[61] | 244 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
|
---|
| 245 | {
|
---|
| 246 | // Create application object
|
---|
| 247 | TestCullingTerrainApplication app;
|
---|
| 248 |
|
---|
| 249 | try
|
---|
| 250 | {
|
---|
| 251 | app.go();
|
---|
| 252 | }
|
---|
| 253 | catch( Ogre::Exception& e )
|
---|
| 254 | {
|
---|
[75] | 255 | MessageBox( NULL, e.getFullDescription().c_str(),
|
---|
| 256 | "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
|
---|
[61] | 257 | }
|
---|
| 258 |
|
---|
| 259 | return 0;
|
---|
| 260 | } |
---|