1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | You may use this sample code for anything you like, it is not covered by the
|
---|
11 | LGPL like the rest of the engine.
|
---|
12 | -----------------------------------------------------------------------------
|
---|
13 | */
|
---|
14 |
|
---|
15 | /**
|
---|
16 | \file
|
---|
17 | Terrain.h
|
---|
18 | \brief
|
---|
19 | Specialisation of OGRE's framework application to show the
|
---|
20 | terrain rendering plugin
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "ExampleApplication.h"
|
---|
24 |
|
---|
25 | RaySceneQuery* raySceneQuery = 0;
|
---|
26 |
|
---|
27 | // Event handler to add ability to alter curvature
|
---|
28 | class TerrainFrameListener : public ExampleFrameListener
|
---|
29 | {
|
---|
30 | public:
|
---|
31 | TerrainFrameListener(RenderWindow* win, Camera* cam)
|
---|
32 | : ExampleFrameListener(win, cam)
|
---|
33 | {
|
---|
34 | // Reduce move speed
|
---|
35 | mMoveSpeed = 50;
|
---|
36 |
|
---|
37 | }
|
---|
38 |
|
---|
39 | bool frameStarted(const FrameEvent& evt)
|
---|
40 | {
|
---|
41 | // clamp to terrain
|
---|
42 | bool ret = ExampleFrameListener::frameStarted(evt);
|
---|
43 | static Ray updateRay;
|
---|
44 | updateRay.setOrigin(mCamera->getPosition());
|
---|
45 | updateRay.setDirection(Vector3::NEGATIVE_UNIT_Y);
|
---|
46 | raySceneQuery->setRay(updateRay);
|
---|
47 | RaySceneQueryResult& qryResult = raySceneQuery->execute();
|
---|
48 | RaySceneQueryResult::iterator i = qryResult.begin();
|
---|
49 | if (i != qryResult.end() && i->worldFragment)
|
---|
50 | {
|
---|
51 | SceneQuery::WorldFragment* wf = i->worldFragment;
|
---|
52 | mCamera->setPosition(mCamera->getPosition().x,
|
---|
53 | i->worldFragment->singleIntersection.y + 10,
|
---|
54 | mCamera->getPosition().z);
|
---|
55 | }
|
---|
56 |
|
---|
57 | return ret;
|
---|
58 |
|
---|
59 | }
|
---|
60 |
|
---|
61 | };
|
---|
62 |
|
---|
63 |
|
---|
64 |
|
---|
65 | class TerrainApplication : public ExampleApplication
|
---|
66 | {
|
---|
67 | public:
|
---|
68 | TerrainApplication() {}
|
---|
69 |
|
---|
70 | ~TerrainApplication()
|
---|
71 | {
|
---|
72 | delete raySceneQuery;
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected:
|
---|
76 |
|
---|
77 |
|
---|
78 | virtual void chooseSceneManager(void)
|
---|
79 | {
|
---|
80 | // Get the SceneManager, in this case a generic one
|
---|
81 | mSceneMgr = mRoot->createSceneManager("TerrainSceneManager");
|
---|
82 | }
|
---|
83 |
|
---|
84 | virtual void createCamera(void)
|
---|
85 | {
|
---|
86 | // Create the camera
|
---|
87 | mCamera = mSceneMgr->createCamera("PlayerCam");
|
---|
88 |
|
---|
89 | // Position it at 500 in Z direction
|
---|
90 | mCamera->setPosition(Vector3(128,25,128));
|
---|
91 | // Look back along -Z
|
---|
92 | mCamera->lookAt(Vector3(0,0,-300));
|
---|
93 | mCamera->setNearClipDistance( 1 );
|
---|
94 | mCamera->setFarClipDistance( 1000 );
|
---|
95 |
|
---|
96 | }
|
---|
97 |
|
---|
98 | // Just override the mandatory create scene method
|
---|
99 | void createScene(void)
|
---|
100 | {
|
---|
101 | Plane waterPlane;
|
---|
102 |
|
---|
103 | // Set ambient light
|
---|
104 | mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
|
---|
105 |
|
---|
106 | // Create a light
|
---|
107 | Light* l = mSceneMgr->createLight("MainLight");
|
---|
108 | // Accept default settings: point light, white diffuse, just set position
|
---|
109 | // NB I could attach the light to a SceneNode if I wanted it to move automatically with
|
---|
110 | // other objects, but I don't
|
---|
111 | l->setPosition(20,80,50);
|
---|
112 |
|
---|
113 | // Fog
|
---|
114 | // NB it's VERY important to set this before calling setWorldGeometry
|
---|
115 | // because the vertex program picked will be different
|
---|
116 | ColourValue fadeColour(0.93, 0.86, 0.76);
|
---|
117 | mSceneMgr->setFog( FOG_LINEAR, fadeColour, .001, 500, 1000);
|
---|
118 | mWindow->getViewport(0)->setBackgroundColour(fadeColour);
|
---|
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 | // Infinite far plane?
|
---|
126 | if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_INFINITE_FAR_PLANE))
|
---|
127 | {
|
---|
128 | mCamera->setFarClipDistance(0);
|
---|
129 | }
|
---|
130 |
|
---|
131 | // Define the required skyplane
|
---|
132 | Plane plane;
|
---|
133 | // 5000 world units from the camera
|
---|
134 | plane.d = 5000;
|
---|
135 | // Above the camera, facing down
|
---|
136 | plane.normal = -Vector3::UNIT_Y;
|
---|
137 |
|
---|
138 | // Set a nice viewpoint
|
---|
139 | mCamera->setPosition(707,2500,528);
|
---|
140 | mCamera->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
|
---|
141 | //mRoot -> showDebugOverlay( true );
|
---|
142 |
|
---|
143 | raySceneQuery = mSceneMgr->createRayQuery(
|
---|
144 | Ray(mCamera->getPosition(), Vector3::NEGATIVE_UNIT_Y));
|
---|
145 |
|
---|
146 |
|
---|
147 | }
|
---|
148 | // Create new frame listener
|
---|
149 | void createFrameListener(void)
|
---|
150 | {
|
---|
151 | mFrameListener= new TerrainFrameListener(mWindow, mCamera);
|
---|
152 | mRoot->addFrameListener(mFrameListener);
|
---|
153 | }
|
---|
154 |
|
---|
155 | };
|
---|