source: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp @ 139

Revision 139, 8.8 KB checked in by mattausch, 19 years ago (diff)

fixed bug with tight octree boxes
added more flexible renderqueue (can delete per flag)
reordered functions in visibility terrain scene manager

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