1 |
|
---|
2 |
|
---|
3 |
|
---|
4 | /*
|
---|
5 | -----------------------------------------------------------------------------
|
---|
6 | This source file is part of OGRE
|
---|
7 | (Object-oriented Graphics Rendering Engine)
|
---|
8 | For the latest info, see http://www.ogre3d.org/
|
---|
9 |
|
---|
10 | Copyright (c) 2000-2005 The OGRE Team
|
---|
11 | Also see acknowledgements in Readme.html
|
---|
12 |
|
---|
13 | You may use this sample code for anything you like, it is not covered by the
|
---|
14 | LGPL like the rest of the engine.
|
---|
15 | -----------------------------------------------------------------------------
|
---|
16 | */
|
---|
17 |
|
---|
18 | /**
|
---|
19 | \file
|
---|
20 | Lighting.h
|
---|
21 | \brief
|
---|
22 | Shows lighting support in OGRE. Also demonstrates the use of billboards
|
---|
23 | and controllers for automatic time-relative behaviour.
|
---|
24 | */
|
---|
25 |
|
---|
26 |
|
---|
27 | #include "ExampleApplication.h"
|
---|
28 | #include "OgreIlluminationManager.h"
|
---|
29 | #include "SpriteParticleRenderer.h"
|
---|
30 |
|
---|
31 |
|
---|
32 | // Listener class for frame updates
|
---|
33 | class RaytraceDemoListener : public ExampleFrameListener
|
---|
34 | {
|
---|
35 | protected:
|
---|
36 | unsigned long framecount;
|
---|
37 | SceneNode* objectNode;
|
---|
38 |
|
---|
39 | public:
|
---|
40 | RaytraceDemoListener(RenderWindow* window, Camera* maincam, SceneNode* objectNode)
|
---|
41 | :ExampleFrameListener(window, maincam)
|
---|
42 |
|
---|
43 | {
|
---|
44 | this->mMoveSpeed = 0.5;
|
---|
45 | framecount = 0;
|
---|
46 | this->objectNode = objectNode;
|
---|
47 | }
|
---|
48 | bool processUnbufferedKeyInput(const FrameEvent& evt)
|
---|
49 | {
|
---|
50 | static float objmove = 0.01;
|
---|
51 | if (mInputDevice->isKeyDown(KC_UP))
|
---|
52 | {
|
---|
53 | objectNode->translate(0,0,objmove);
|
---|
54 | }
|
---|
55 | if (mInputDevice->isKeyDown(KC_MULTIPLY))
|
---|
56 | {
|
---|
57 | //objectNode->scale(1.1,1.1,1.1);
|
---|
58 | objectNode->rotate(Vector3(0,0,1), Degree(10));
|
---|
59 | mTimeUntilNextToggle = 1;
|
---|
60 | }
|
---|
61 | if (mInputDevice->isKeyDown(KC_DIVIDE))
|
---|
62 | {
|
---|
63 | //objectNode->scale(1.0/1.1,1.0/1.1,1.0/1.1);
|
---|
64 | objectNode->rotate(Vector3(1,0,0), Degree(10));
|
---|
65 | mTimeUntilNextToggle = 1;
|
---|
66 | }
|
---|
67 | if (mInputDevice->isKeyDown(KC_ADD))
|
---|
68 | {
|
---|
69 | objectNode->rotate(Vector3(0,1,0), Degree(10));
|
---|
70 | }
|
---|
71 | if (mInputDevice->isKeyDown(KC_HOME))
|
---|
72 | {
|
---|
73 | objectNode->translate(0,objmove,0);
|
---|
74 | }
|
---|
75 | if (mInputDevice->isKeyDown(KC_END))
|
---|
76 | {
|
---|
77 | objectNode->translate(0,-objmove,0);
|
---|
78 | }
|
---|
79 | if (mInputDevice->isKeyDown(KC_DOWN))
|
---|
80 | {
|
---|
81 | objectNode->translate(0,0,-objmove);
|
---|
82 | }
|
---|
83 | if (mInputDevice->isKeyDown(KC_RIGHT))
|
---|
84 | {
|
---|
85 | objectNode->translate(-objmove,0,0);
|
---|
86 | }
|
---|
87 | if (mInputDevice->isKeyDown(KC_LEFT))
|
---|
88 | {
|
---|
89 | objectNode->translate(objmove,0,0);
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (mInputDevice->isKeyDown(KC_A))
|
---|
93 | {
|
---|
94 | // Move camera left
|
---|
95 | mTranslateVector.x = -mMoveScale;
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (mInputDevice->isKeyDown(KC_D))
|
---|
99 | {
|
---|
100 | // Move camera RIGHT
|
---|
101 | mTranslateVector.x = mMoveScale;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /* Move camera forward by keypress. */
|
---|
105 | if ( mInputDevice->isKeyDown(KC_W) )
|
---|
106 | {
|
---|
107 | mTranslateVector.z = -mMoveScale;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /* Move camera backward by keypress. */
|
---|
111 | if (mInputDevice->isKeyDown(KC_S) )
|
---|
112 | {
|
---|
113 | mTranslateVector.z = mMoveScale;
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (mInputDevice->isKeyDown(KC_PGUP))
|
---|
117 | {
|
---|
118 | // Move camera up
|
---|
119 | mTranslateVector.y = mMoveScale;
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (mInputDevice->isKeyDown(KC_PGDOWN))
|
---|
123 | {
|
---|
124 | // Move camera down
|
---|
125 | mTranslateVector.y = -mMoveScale;
|
---|
126 | }
|
---|
127 |
|
---|
128 | if( mInputDevice->isKeyDown( KC_ESCAPE) )
|
---|
129 | {
|
---|
130 | return false;
|
---|
131 | }
|
---|
132 |
|
---|
133 | // see if switching is on, and you want to toggle
|
---|
134 | if (mInputTypeSwitchingOn && mInputDevice->isKeyDown(KC_M) && mTimeUntilNextToggle <= 0)
|
---|
135 | {
|
---|
136 | switchMouseMode();
|
---|
137 | mTimeUntilNextToggle = 1;
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (mInputTypeSwitchingOn && mInputDevice->isKeyDown(KC_K) && mTimeUntilNextToggle <= 0)
|
---|
141 | {
|
---|
142 | // must be going from immediate keyboard to buffered keyboard
|
---|
143 | switchKeyMode();
|
---|
144 | mTimeUntilNextToggle = 1;
|
---|
145 | }
|
---|
146 | if (mInputDevice->isKeyDown(KC_F) && mTimeUntilNextToggle <= 0)
|
---|
147 | {
|
---|
148 | mStatsOn = !mStatsOn;
|
---|
149 | showDebugOverlay(mStatsOn);
|
---|
150 |
|
---|
151 | mTimeUntilNextToggle = 1;
|
---|
152 | }
|
---|
153 |
|
---|
154 | if (mInputDevice->isKeyDown(KC_SYSRQ) && mTimeUntilNextToggle <= 0)
|
---|
155 | {
|
---|
156 | char tmp[20];
|
---|
157 | sprintf(tmp, "screenshot_%d.png", ++mNumScreenShots);
|
---|
158 | mWindow->writeContentsToFile(tmp);
|
---|
159 | mTimeUntilNextToggle = 0.5;
|
---|
160 | mWindow->setDebugText(String("Wrote ") + tmp);
|
---|
161 | }
|
---|
162 |
|
---|
163 | static bool displayCameraDetails = false;
|
---|
164 | if (mInputDevice->isKeyDown(KC_P) && mTimeUntilNextToggle <= 0)
|
---|
165 | {
|
---|
166 | displayCameraDetails = !displayCameraDetails;
|
---|
167 | mTimeUntilNextToggle = 0.5;
|
---|
168 | if (!displayCameraDetails)
|
---|
169 | mWindow->setDebugText("");
|
---|
170 | }
|
---|
171 | if (displayCameraDetails)
|
---|
172 | {
|
---|
173 | // Print camera details
|
---|
174 | mWindow->setDebugText("P: " + StringConverter::toString(mCamera->getDerivedPosition()) + " " +
|
---|
175 | "O: " + StringConverter::toString(mCamera->getDerivedOrientation()));
|
---|
176 | }
|
---|
177 |
|
---|
178 | // Return true to continue rendering
|
---|
179 | return true;
|
---|
180 | }
|
---|
181 | bool frameStarted(const FrameEvent& evt)
|
---|
182 | {
|
---|
183 | bool result = ExampleFrameListener::frameStarted(evt);
|
---|
184 | framecount++;
|
---|
185 | return result;
|
---|
186 | }
|
---|
187 | };
|
---|
188 |
|
---|
189 | /** Application class */
|
---|
190 | class RaytraceDemoApplication : public ExampleApplication
|
---|
191 | {
|
---|
192 |
|
---|
193 | public:
|
---|
194 |
|
---|
195 |
|
---|
196 | protected:
|
---|
197 |
|
---|
198 | SceneNode* objectNode;
|
---|
199 |
|
---|
200 | void createScene(void)
|
---|
201 | {
|
---|
202 |
|
---|
203 | OgreIlluminationManager::getSingleton().setMainCamera(mCamera);
|
---|
204 | OgreIlluminationManager::getSingleton().setMainViewport(mWindow->getViewport(0));
|
---|
205 |
|
---|
206 | OgreIlluminationManager::getSingleton().setMaxJoinRadius(420);
|
---|
207 |
|
---|
208 | Root::getSingleton()._setCurrentSceneManager(mSceneMgr);
|
---|
209 |
|
---|
210 | mCamera->setPosition(0,1,0);
|
---|
211 | mCamera->setFOVy(Radian(Degree(80)));
|
---|
212 | mCamera->setFarClipDistance(200);
|
---|
213 | mCamera->setNearClipDistance(0.1);
|
---|
214 |
|
---|
215 | OgreIlluminationManager::getSingleton().setFocusingSM(false);
|
---|
216 | OgreIlluminationManager::getSingleton().setShadowMapSize(512);
|
---|
217 | OgreIlluminationManager::getSingleton().setBlurShadowMap(true);
|
---|
218 | OgreIlluminationManager::getSingleton().setShadowMapMaterialName("GameTools/ShadowMapDistance");
|
---|
219 | mSceneMgr->setAmbientLight(ColourValue(0.505 * 0.2, 0.897 * 0.2, 0.914 * 0.2));
|
---|
220 |
|
---|
221 | SceneNode* rootNode = mSceneMgr->getRootSceneNode();
|
---|
222 |
|
---|
223 | Entity* object = mSceneMgr->createEntity("object", "teapot.mesh");
|
---|
224 | object->setMaterialName("MetalTeapotMultipleBounce");
|
---|
225 | objectNode = rootNode->createChildSceneNode();
|
---|
226 | objectNode->attachObject(object);
|
---|
227 | objectNode->scale(0.5,0.5,0.5);
|
---|
228 | //objectNode->scale(0.005,0.005,0.005);
|
---|
229 | objectNode->setPosition(0,1.2,0);
|
---|
230 | objectNode->_updateBounds();
|
---|
231 |
|
---|
232 | Entity* room = mSceneMgr->createEntity("scene", "difflab.mesh");
|
---|
233 | //room->setMaterialName("GameTools/Phong");
|
---|
234 | SceneNode* roomNode = rootNode->createChildSceneNode();
|
---|
235 | roomNode->attachObject(room);
|
---|
236 | roomNode->_updateBounds();
|
---|
237 |
|
---|
238 | mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE);
|
---|
239 | Light* redLight1 = mSceneMgr->createLight("RedLight1");
|
---|
240 | redLight1->setType(Light::LT_POINT);
|
---|
241 | redLight1->setCastShadows(false);
|
---|
242 | redLight1->setDiffuseColour(ColourValue(1,0.055, 0.0));
|
---|
243 | redLight1->setPowerScale(1.6);
|
---|
244 | SceneNode* redLightNode1 = rootNode->createChildSceneNode();
|
---|
245 | redLightNode1->setPosition(0.05,2.361,-1.07);
|
---|
246 | redLightNode1->attachObject(redLight1);
|
---|
247 |
|
---|
248 | Light* greenLight1 = mSceneMgr->createLight("GreenLight1");
|
---|
249 | greenLight1->setType(Light::LT_POINT);
|
---|
250 | greenLight1->setCastShadows(false);
|
---|
251 | greenLight1->setDiffuseColour(ColourValue(0.362,0.783, 0.685));
|
---|
252 | greenLight1->setPowerScale(2.0);
|
---|
253 | SceneNode* greenLightNode1 = rootNode->createChildSceneNode();
|
---|
254 | greenLightNode1->setPosition(1.312,1.313,0);
|
---|
255 | greenLightNode1->attachObject(greenLight1);
|
---|
256 |
|
---|
257 | Light* mainBlueLight = mSceneMgr->createLight("MainBlueLight");
|
---|
258 | mainBlueLight->setType(Light::LT_SPOTLIGHT);
|
---|
259 | mainBlueLight->setCastShadows(true);
|
---|
260 | mainBlueLight->setDiffuseColour(ColourValue(0.395,0.766, 1.0));
|
---|
261 | mainBlueLight->setPowerScale(5.0);
|
---|
262 | mainBlueLight->setSpotlightRange(Degree(0),Degree(160));
|
---|
263 | mainBlueLight->setAttenuation(2.5, 0, 1, 0);
|
---|
264 | SceneNode* mainBlueLightNode = rootNode->createChildSceneNode();
|
---|
265 | mainBlueLightNode->setPosition(-0.546,2.095,1.035);
|
---|
266 | //mainBlueLightNode->rotate(Vector3::UNIT_X, Degree(161.396));
|
---|
267 | //mainBlueLightNode->rotate(Vector3::UNIT_Y, Degree(53.955));
|
---|
268 | //mainBlueLightNode->rotate(Vector3::UNIT_Z, Degree(-145.065));
|
---|
269 | mainBlueLight->setDirection(-1.5,-2.0,1.0);
|
---|
270 | mainBlueLightNode->attachObject(mainBlueLight);
|
---|
271 |
|
---|
272 | OgreIlluminationManager::getSingleton().initTechniques();
|
---|
273 |
|
---|
274 | }
|
---|
275 |
|
---|
276 | void createFrameListener(void)
|
---|
277 | {
|
---|
278 | // This is where we instantiate our own frame listener
|
---|
279 | mFrameListener= new RaytraceDemoListener(mWindow, mCamera, objectNode);
|
---|
280 | mFrameListener->setPriority(1);
|
---|
281 | OgreIlluminationManager::getSingleton().setPriority(2);
|
---|
282 | mRoot->addFrameListener(mFrameListener);
|
---|
283 | mRoot->addFrameListener(&OgreIlluminationManager::getSingleton());
|
---|
284 |
|
---|
285 | }
|
---|
286 |
|
---|
287 | };
|
---|
288 |
|
---|