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

Revision 135, 8.8 KB checked in by mattausch, 19 years ago (diff)
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()->createChildSceneNode(
43                "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        mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
87       
88        //-- create light
89        mSunLight = mSceneMgr->createLight("SunLight");
90        mSunLight->setType(Light::LT_DIRECTIONAL);
91        //mSunLight->setType(Light::LT_SPOTLIGHT);
92        //mSunLight->setSpotlightRange(Degree(30), Degree(50));
93
94    mSunLight->setPosition(707, 2000, 500);
95        mSunLight->setCastShadows(true);
96
97        Vector3 dir(0.5, 1, 0.5);
98        dir.normalise();
99        mSunLight->setDirection(dir);
100        //mSunLight->setDirection(Vector3::NEGATIVE_UNIT_Y);
101
102        mSunLight->setDiffuseColour(1, 1, 1);
103        mSunLight->setSpecularColour(1, 1, 1);
104
105        // --Fog
106        // NB it's VERY important to set this before calling setWorldGeometry
107        // because the vertex program picked will be different
108        ColourValue fadeColour(0.93, 0.86, 0.76);
109        mWindow->getViewport(0)->setBackgroundColour(fadeColour);
110        //mSceneMgr->setFog( FOG_LINEAR, fadeColour, .001, 500, 1000);
111       
112        // Create a skybox
113        mSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox", 5000, false);
114       
115        std::string terrain_cfg("terrain.cfg");
116#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
117        terrain_cfg = mResourcePath + terrain_cfg;
118#endif
119        mSceneMgr->setWorldGeometry(terrain_cfg);
120       
121        //-- CEGUI setup
122        setupGui();
123
124        // Floor plane
125        /*
126        Plane plane;
127        plane.normal = Vector3::UNIT_Y;
128        plane.d = -60;
129        MeshManager::getSingleton().createPlane("Myplane",
130                ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
131                5000,5000,100,100,true,1,5,5,Vector3::UNIT_Z);
132        Entity* pPlaneEnt = mSceneMgr->createEntity( "plane", "Myplane" );
133        pPlaneEnt->setMaterialName("Examples/Rockwall");
134        pPlaneEnt->setCastShadows(true);
135        mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(pPlaneEnt);
136        */
137
138        if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_HWRENDER_TO_TEXTURE))
139    {
140                // In D3D, use a 1024x1024 shadow texture
141                mSceneMgr->setShadowTextureSettings(1024, 2);
142        }
143        else
144        {
145                // Use 512x512 texture in GL since we can't go higher than the window res
146                mSceneMgr->setShadowTextureSettings(512, 2);
147        }
148        //mSceneMgr->setShadowColour(ColourValue(0, 0, 0));
149        mSceneMgr->setShadowColour(ColourValue(0.5, 0.5, 0.5));
150    // mSceneMgr->setShowDebugShadows(true);
151
152
153        //-- terrain content setup
154
155        // HACK: necessary to call once before the content creation for
156        // terrain initialisation
157        mSceneMgr->_renderScene(mCamera, mWindow->getViewport(0), true);
158
159        mTerrainContentGenerator = new TerrainContentGenerator(mSceneMgr);
160       
161        // if no objects file generate yourself
162        if (!mTerrainContentGenerator->LoadObjects("objects.out"))
163        {
164                // to provide much occlusion,
165                // height is restricted to 50, so no objects appear on peaks
166                mTerrainContentGenerator->SetMaxPos(Vector3(3000.0f, 50.0f, 3000.0f));
167                mTerrainContentGenerator->SetOffset(0);
168
169                // the objects are generated on the whole terrain
170                mTerrainContentGenerator->GenerateScene(1500, "robot");
171                //mTerrainContentGenerator->GenerateScene(1500, "athene");
172                //mTerrainContentGenerator->GenerateScene(500, "ninja");
173        }
174
175        // no limitations needed anymore: the user can set
176        // objects also on peaks of terrain
177        mTerrainContentGenerator->SetMaxPos(Vector3(3000.0f, 5000.0f, 3000.0f));
178}
179//-----------------------------------------------------------------------
180void TestCullingTerrainApplication::setupGui()
181{
182         mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY,
183                 false, 3000, ST_EXTERIOR_CLOSE);
184     mGUISystem = new CEGUI::System(mGUIRenderer);
185
186         // Mouse
187     CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLook.scheme");
188     CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
189         mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook",
190                                                                           (CEGUI::utf8*)"MouseArrow");
191
192         CEGUI::MouseCursor::getSingleton().show();
193}
194//-----------------------------------------------------------------------
195void TestCullingTerrainApplication::createFrameListener()
196{
197        mTerrainFrameListener = new TerrainFrameListener(mWindow, mCamera, mSceneMgr,
198                mGUIRenderer, mTerrainContentGenerator, mVizCamera, mCamNode, mSunLight);
199       
200        mRoot->addFrameListener(mTerrainFrameListener);
201}
202//-----------------------------------------------------------------------
203void TestCullingTerrainApplication::chooseSceneManager()
204{
205        mSceneMgr = mRoot->getSceneManager(ST_EXTERIOR_CLOSE);
206}
207
208
209/**************************************************************/
210/*      VisualizationRenderTargetListener implementation      */
211/**************************************************************/
212//-----------------------------------------------------------------------
213VisualizationRenderTargetListener::VisualizationRenderTargetListener(SceneManager *sceneMgr)
214:RenderTargetListener(), mSceneMgr(sceneMgr)
215{
216}
217//-----------------------------------------------------------------------
218void VisualizationRenderTargetListener::preViewportUpdate(const RenderTargetViewportEvent &evt)
219{
220        // visualization viewport
221        const bool showViz = evt.source->getZOrder() == VIZ_VIEWPORT_Z_ORDER;
222        const bool nShowViz = !showViz;
223
224        // ambient light must be full for visualization
225    if (showViz)
226        {
227                mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
228        }
229        else
230        {
231                mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
232        }
233
234    mSceneMgr->setOption("ShowVisualization", &showViz);
235        mSceneMgr->setOption("SkyBoxEnabled", &nShowViz);
236        //mSceneMgr->setOption("SkyPlaneEnabled", &showViz);
237       
238        RenderTargetListener::preViewportUpdate(evt);
239}
240//-----------------------------------------------------------------------
241void VisualizationRenderTargetListener::postRenderTargetUpdate(const RenderTargetEvent &evt)
242{
243        RenderTargetListener::postRenderTargetUpdate(evt);
244}
245//-----------------------------------------------------------------------
246INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
247{
248    // Create application object
249    TestCullingTerrainApplication app;
250
251        try
252        {
253        app.go();
254    }
255        catch( Ogre::Exception& e )
256        {
257        MessageBox( NULL, e.getFullDescription().c_str(),
258                        "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
259    }   
260
261    return 0;
262}
Note: See TracBrowser for help on using the repository browser.