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

Revision 145, 8.9 KB checked in by mattausch, 19 years ago (diff)

fixed bug when canceling login
fixed visibility queries using occlusion queries

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/*******************************************************/
20TestCullingTerrainApplication::TestCullingTerrainApplication():
21mTerrainContentGenerator(NULL)
22{
23}
24//-----------------------------------------------------------------------
25TestCullingTerrainApplication::~TestCullingTerrainApplication()
26{
27        if (mTerrainContentGenerator)
28        {
29                delete mTerrainContentGenerator;
30                mTerrainContentGenerator = NULL;
31        }
32        //if(mRenderTargetListener)     delete mRenderTargetListener;
33}
34//-----------------------------------------------------------------------
35void TestCullingTerrainApplication::createCamera()
36{
37        // create the camera
38        mCamera = mSceneMgr->createCamera("PlayerCam");
39       
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        */
45        mCamNode = mSceneMgr->getRootSceneNode()->
46                createChildSceneNode("CamNode1", Vector3(707, 5000, 528));
47        //mCamera->setPosition(707, 5000, 528);
48        mCamNode->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
49        mCamNode->attachObject(mCamera);
50
51        //-- create visualization camera
52        mVizCamera = mSceneMgr->createCamera("VizCam");
53        mVizCamera->setPosition(mCamNode->getPosition());
54
55        mVizCamera->setNearClipDistance(1);
56        mCamera->setNearClipDistance(1);
57
58        // infinite far plane?
59        if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_INFINITE_FAR_PLANE))
60        {
61                mVizCamera->setFarClipDistance(0);
62                mCamera->setFarClipDistance(0);
63        }
64        else
65        {
66                 mVizCamera->setFarClipDistance(20000);
67                 mCamera->setFarClipDistance(20000);
68        }       
69}
70
71//-----------------------------------------------------------------------
72bool TestCullingTerrainApplication::setup()
73{
74        bool carryOn = ExampleApplication::setup();
75
76        if (carryOn)
77                createRenderTargetListener();
78
79        return carryOn;
80}
81//-----------------------------------------------------------------------
82void TestCullingTerrainApplication::createRenderTargetListener()
83{
84        mWindow->addListener(new VisualizationRenderTargetListener(mSceneMgr));
85}
86//-----------------------------------------------------------------------
87void TestCullingTerrainApplication::createScene()
88{
89        // Set ambient light
90        mAmbientLight = ColourValue(0.5, 0.5, 0.5);
91        mSceneMgr->setAmbientLight(mAmbientLight);
92       
93        //-- create light
94        mSunLight = mSceneMgr->createLight("SunLight");
95        mSunLight->setType(Light::LT_DIRECTIONAL);
96        //mSunLight->setType(Light::LT_SPOTLIGHT);
97        //mSunLight->setSpotlightRange(Degree(30), Degree(50));
98
99    mSunLight->setPosition(707, 2000, 500);
100        mSunLight->setCastShadows(true);
101
102        // set light angle not too sharp, otherwise shadows textures will be broken
103        Vector3 dir(0.5, 1, 0.5);
104        dir.normalise();
105        mSunLight->setDirection(dir);
106        //mSunLight->setDirection(Vector3::NEGATIVE_UNIT_Y);
107        mSunLight->setDiffuseColour(1, 1, 1);
108        mSunLight->setSpecularColour(1, 1, 1);
109
110        // -- Fog
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);
116       
117        // Create a skybox
118        mSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox", 5000, false);
119       
120        std::string terrain_cfg("terrain.cfg");
121#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
122        terrain_cfg = mResourcePath + terrain_cfg;
123#endif
124        mSceneMgr->setWorldGeometry(terrain_cfg);
125       
126        //-- CEGUI setup
127        setupGui();
128
129        // Floor plane
130        /*
131        Plane plane;
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);
140        mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(pPlaneEnt);
141        */
142        mSceneMgr->setShadowTextureSettings(1024, 2);
143        mSceneMgr->setShadowColour(ColourValue(0.5, 0.5, 0.5));
144    //mSceneMgr->setShowDebugShadows(true);
145
146
147        //-- terrain content setup
148
149        // HACK: necessary to call once before the content creation for
150        // terrain initialisation
151        mSceneMgr->_renderScene(mCamera, mWindow->getViewport(0), true);
152
153        mTerrainContentGenerator = new TerrainContentGenerator(mSceneMgr);
154       
155        // if no objects file generate yourself
156        if (!mTerrainContentGenerator->LoadObjects("objects.out"))
157        {
158                // to provide much occlusion,
159                // height is restricted to 50, so no objects appear on peaks
160                mTerrainContentGenerator->SetMaxPos(Vector3(3000.0f, 50.0f, 3000.0f));
161                mTerrainContentGenerator->SetOffset(0);
162
163                // the objects are generated on the whole terrain
164                mTerrainContentGenerator->GenerateScene(1500, "robot");
165                //mTerrainContentGenerator->GenerateScene(1500, "athene");
166                //mTerrainContentGenerator->GenerateScene(500, "ninja");
167        }
168
169        // no limitations needed anymore: the user can set
170        // objects also on peaks of terrain
171        mTerrainContentGenerator->SetMaxPos(Vector3(3000.0f, 5000.0f, 3000.0f));
172}
173//-----------------------------------------------------------------------
174void TestCullingTerrainApplication::setupGui()
175{
176         mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
177                 false, 3000, ST_EXTERIOR_CLOSE);
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");
183         mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook",
184                                                                           (CEGUI::utf8*)"MouseArrow");
185
186         CEGUI::MouseCursor::getSingleton().show();
187}
188//-----------------------------------------------------------------------
189void TestCullingTerrainApplication::createFrameListener()
190{
191        mTerrainFrameListener = new TerrainFrameListener(mWindow, mCamera, mSceneMgr,
192                mGUIRenderer, mTerrainContentGenerator, mVizCamera, mCamNode, mSunLight);
193       
194        mRoot->addFrameListener(mTerrainFrameListener);
195}
196//-----------------------------------------------------------------------
197void TestCullingTerrainApplication::chooseSceneManager()
198{
199        mSceneMgr = mRoot->getSceneManager(ST_EXTERIOR_CLOSE);
200}
201
202
203/**************************************************************/
204/*      VisualizationRenderTargetListener implementation      */
205/**************************************************************/
206//-----------------------------------------------------------------------
207VisualizationRenderTargetListener::VisualizationRenderTargetListener(SceneManager *sceneMgr)
208:RenderTargetListener(), mSceneMgr(sceneMgr)
209{
210}
211//-----------------------------------------------------------------------
212void VisualizationRenderTargetListener::preViewportUpdate(const RenderTargetViewportEvent &evt)
213{
214        // visualization viewport
215        const bool showViz = evt.source->getZOrder() == VIZ_VIEWPORT_Z_ORDER;
216        const bool nShowViz = !showViz;
217
218        mSavedShadowTechnique = mSceneMgr->getShadowTechnique();
219        mSavedAmbientLight = mSceneMgr->getAmbientLight();
220
221        // -- ambient light must be full for visualization, shadows disabled
222    if (showViz)
223        {
224                mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
225                mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE);
226        }
227       
228    mSceneMgr->setOption("PrepareVisualization", &showViz);
229        mSceneMgr->setOption("SkyBoxEnabled", &nShowViz);
230        //mSceneMgr->setOption("SkyPlaneEnabled", &showViz);
231       
232        RenderTargetListener::preViewportUpdate(evt);
233}
234//-----------------------------------------------------------------------
235void VisualizationRenderTargetListener::postRenderTargetUpdate(const RenderTargetEvent &evt)
236{
237        // reset values
238        mSceneMgr->setShadowTechnique(mSavedShadowTechnique);
239        mSceneMgr->setAmbientLight(mSavedAmbientLight);
240       
241        RenderTargetListener::postRenderTargetUpdate(evt);
242}
243//-----------------------------------------------------------------------
244INT 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        {
255        MessageBox( NULL, e.getFullDescription().c_str(),
256                        "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
257    }   
258
259    return 0;
260}
Note: See TracBrowser for help on using the repository browser.