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-2006 Torus Knot Software Ltd |
---|
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 | Lighting.h |
---|
18 | \brief |
---|
19 | Shows lighting support in OGRE. Also demonstrates the use of billboards |
---|
20 | and controllers for automatic time-relative behaviour. |
---|
21 | */ |
---|
22 | |
---|
23 | |
---|
24 | #include "ExampleApplication.h" |
---|
25 | |
---|
26 | |
---|
27 | /** This class turns lights & billboards linked to them on & off */ |
---|
28 | class LightFlasher : public ControllerValue<Real> |
---|
29 | { |
---|
30 | protected: |
---|
31 | Light* mLight; |
---|
32 | Billboard* mBillboard; |
---|
33 | ColourValue mMaxColour; |
---|
34 | Real intensity; |
---|
35 | public: |
---|
36 | LightFlasher(Light* light, Billboard* billboard, ColourValue maxColour) |
---|
37 | { |
---|
38 | mLight = light; |
---|
39 | mBillboard = billboard; |
---|
40 | mMaxColour = maxColour; |
---|
41 | } |
---|
42 | |
---|
43 | virtual Real getValue (void) const |
---|
44 | { |
---|
45 | return intensity; |
---|
46 | } |
---|
47 | |
---|
48 | virtual void setValue (Real value) |
---|
49 | { |
---|
50 | intensity = value; |
---|
51 | |
---|
52 | ColourValue newColour; |
---|
53 | |
---|
54 | // Attenuate the brightness of the light |
---|
55 | newColour.r = mMaxColour.r * intensity; |
---|
56 | newColour.g = mMaxColour.g * intensity; |
---|
57 | newColour.b = mMaxColour.b * intensity; |
---|
58 | |
---|
59 | mLight->setDiffuseColour(newColour); |
---|
60 | mBillboard->setColour(newColour); |
---|
61 | } |
---|
62 | }; |
---|
63 | |
---|
64 | |
---|
65 | /** Controller function mapping waveform to light intensity */ |
---|
66 | class LightFlasherControllerFunction : public WaveformControllerFunction |
---|
67 | { |
---|
68 | public: |
---|
69 | LightFlasherControllerFunction(WaveformType wavetype, Real frequency, Real phase) : WaveformControllerFunction(wavetype, 0, frequency, phase, 1, true) |
---|
70 | { |
---|
71 | |
---|
72 | } |
---|
73 | }; |
---|
74 | |
---|
75 | |
---|
76 | // Some global node data |
---|
77 | SceneNode* mRedYellowLightsNode; |
---|
78 | SceneNode* mGreenBlueLightsNode; |
---|
79 | std::vector<AnimationState*> mAnimStateList; |
---|
80 | |
---|
81 | |
---|
82 | // Listener class for frame updates |
---|
83 | class LightingListener : public ExampleFrameListener |
---|
84 | { |
---|
85 | protected: |
---|
86 | public: |
---|
87 | LightingListener(RenderWindow* win, Camera* cam) |
---|
88 | : ExampleFrameListener(win, cam) |
---|
89 | { |
---|
90 | } |
---|
91 | |
---|
92 | bool frameStarted(const FrameEvent& evt) |
---|
93 | { |
---|
94 | if( ExampleFrameListener::frameStarted(evt) == false ) |
---|
95 | return false; |
---|
96 | |
---|
97 | std::vector<AnimationState*>::iterator animi; |
---|
98 | for (animi = mAnimStateList.begin(); animi != mAnimStateList.end(); ++animi) |
---|
99 | { |
---|
100 | (*animi)->addTime(evt.timeSinceLastFrame); |
---|
101 | } |
---|
102 | |
---|
103 | return true; |
---|
104 | } |
---|
105 | }; |
---|
106 | |
---|
107 | /** Application class */ |
---|
108 | class LightingApplication : public ExampleApplication |
---|
109 | { |
---|
110 | protected: |
---|
111 | |
---|
112 | // The set of all the billboards used for the lights |
---|
113 | // 2 sets because we'll rotate them differently |
---|
114 | BillboardSet* mRedYellowLights; |
---|
115 | BillboardSet* mGreenBlueLights; |
---|
116 | |
---|
117 | // Billboards |
---|
118 | Billboard* mRedLightBoard; |
---|
119 | Billboard* mBlueLightBoard; |
---|
120 | Billboard* mYellowLightBoard; |
---|
121 | Billboard* mGreenLightBoard; |
---|
122 | |
---|
123 | // Lights |
---|
124 | Light* mRedLight; |
---|
125 | Light* mBlueLight; |
---|
126 | Light* mYellowLight; |
---|
127 | Light* mGreenLight; |
---|
128 | |
---|
129 | |
---|
130 | // Light flashers |
---|
131 | ControllerValueRealPtr mRedLightFlasher; |
---|
132 | ControllerValueRealPtr mBlueLightFlasher; |
---|
133 | ControllerValueRealPtr mYellowLightFlasher; |
---|
134 | ControllerValueRealPtr mGreenLightFlasher; |
---|
135 | |
---|
136 | // Light controller functions |
---|
137 | ControllerFunctionRealPtr mRedLightControllerFunc; |
---|
138 | ControllerFunctionRealPtr mBlueLightControllerFunc; |
---|
139 | ControllerFunctionRealPtr mYellowLightControllerFunc; |
---|
140 | ControllerFunctionRealPtr mGreenLightControllerFunc; |
---|
141 | |
---|
142 | // Light controllers |
---|
143 | Controller<Real>* mRedLightController; |
---|
144 | Controller<Real>* mBlueLightController; |
---|
145 | Controller<Real>* mYellowLightController; |
---|
146 | Controller<Real>* mGreenLightController; |
---|
147 | |
---|
148 | void createScene(void) |
---|
149 | { |
---|
150 | // Set a very low level of ambient lighting |
---|
151 | mSceneMgr->setAmbientLight(ColourValue(0.1, 0.1, 0.1)); |
---|
152 | |
---|
153 | // Use the "Space" skybox |
---|
154 | mSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox"); |
---|
155 | |
---|
156 | // Load ogre head |
---|
157 | Entity* head = mSceneMgr->createEntity("head", "ogrehead.mesh"); |
---|
158 | |
---|
159 | // Attach the head to the scene |
---|
160 | mSceneMgr->getRootSceneNode()->attachObject(head); |
---|
161 | |
---|
162 | /* |
---|
163 | // Create nodes for the lights to be rotated with |
---|
164 | mRedYellowLightsNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); |
---|
165 | mGreenBlueLightsNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); |
---|
166 | |
---|
167 | |
---|
168 | // First create the BillboardSets. This will define the materials for the billboards |
---|
169 | // in its set to use |
---|
170 | mRedYellowLights = mSceneMgr->createBillboardSet("RedYellowLights"); |
---|
171 | mRedYellowLights->setMaterialName("Examples/Flare"); |
---|
172 | mRedYellowLightsNode->attachObject(mRedYellowLights); |
---|
173 | |
---|
174 | mGreenBlueLights = mSceneMgr->createBillboardSet("GreenBlueLights"); |
---|
175 | mGreenBlueLights->setMaterialName("Examples/Flare"); |
---|
176 | mGreenBlueLightsNode->attachObject(mGreenBlueLights); |
---|
177 | |
---|
178 | // Red light billboard, in "off" state |
---|
179 | Vector3 redLightPosition(78, -8, -70); |
---|
180 | mRedLightBoard = mRedYellowLights->createBillboard(redLightPosition); |
---|
181 | mRedLightBoard->setColour(ColourValue::Black); |
---|
182 | |
---|
183 | |
---|
184 | // Blue light billboard, in "off" state |
---|
185 | Vector3 blueLightPosition(-90, -8, -70); |
---|
186 | mBlueLightBoard = mGreenBlueLights->createBillboard(blueLightPosition); |
---|
187 | mBlueLightBoard->setColour(ColourValue::Black); |
---|
188 | |
---|
189 | |
---|
190 | // Yellow light billboard, in "off" state |
---|
191 | Vector3 yellowLightPosition(-4.5, 30, -80); |
---|
192 | mYellowLightBoard = mRedYellowLights->createBillboard(yellowLightPosition); |
---|
193 | mYellowLightBoard->setColour(ColourValue::Black); |
---|
194 | |
---|
195 | // Green light billboard, in "off" state |
---|
196 | Vector3 greenLightPosition(50, 70, 80); |
---|
197 | mGreenLightBoard = mGreenBlueLights->createBillboard(greenLightPosition); |
---|
198 | mGreenLightBoard->setColour(ColourValue::Black); |
---|
199 | |
---|
200 | // Red light, in "off" state |
---|
201 | mRedLight = mSceneMgr->createLight("RedFlyingLight"); |
---|
202 | mRedLight->setType(Light::LT_POINT); |
---|
203 | mRedLight->setPosition(redLightPosition); |
---|
204 | mRedLight->setDiffuseColour(ColourValue::Black); |
---|
205 | mRedYellowLightsNode->attachObject(mRedLight); |
---|
206 | |
---|
207 | // Blue light, in "off" state |
---|
208 | mBlueLight = mSceneMgr->createLight("BlueFlyingLight"); |
---|
209 | mBlueLight->setType(Light::LT_POINT); |
---|
210 | mBlueLight->setPosition(blueLightPosition); |
---|
211 | mBlueLight->setDiffuseColour(ColourValue::Black); |
---|
212 | mGreenBlueLightsNode->attachObject(mBlueLight); |
---|
213 | |
---|
214 | // Yellow light in "off" state |
---|
215 | mYellowLight = mSceneMgr->createLight("YellowFlyingLight"); |
---|
216 | mYellowLight->setType(Light::LT_POINT); |
---|
217 | mYellowLight->setPosition(yellowLightPosition); |
---|
218 | mYellowLight->setDiffuseColour(ColourValue::Black); |
---|
219 | mRedYellowLightsNode->attachObject(mYellowLight); |
---|
220 | |
---|
221 | // Yellow light in "off" state |
---|
222 | mGreenLight = mSceneMgr->createLight("GreenFlyingLight"); |
---|
223 | mGreenLight->setType(Light::LT_POINT); |
---|
224 | mGreenLight->setPosition(greenLightPosition); |
---|
225 | mGreenLight->setDiffuseColour(ColourValue::Black); |
---|
226 | mGreenBlueLightsNode->attachObject(mGreenLight); |
---|
227 | |
---|
228 | // Light flashers |
---|
229 | mRedLightFlasher = ControllerValueRealPtr( |
---|
230 | new LightFlasher(mRedLight, mRedLightBoard, ColourValue::Red)); |
---|
231 | mBlueLightFlasher = ControllerValueRealPtr( |
---|
232 | new LightFlasher(mBlueLight, mBlueLightBoard, ColourValue::Blue)); |
---|
233 | mYellowLightFlasher = ControllerValueRealPtr( |
---|
234 | new LightFlasher(mYellowLight, mYellowLightBoard, ColourValue(1.0, 1.0, 0.0))); |
---|
235 | mGreenLightFlasher = ControllerValueRealPtr( |
---|
236 | new LightFlasher(mGreenLight, mGreenLightBoard, ColourValue::Green)); |
---|
237 | |
---|
238 | // Light controller functions |
---|
239 | mRedLightControllerFunc = ControllerFunctionRealPtr( |
---|
240 | new LightFlasherControllerFunction(Ogre::WFT_SINE, 0.5, 0.0)); |
---|
241 | mBlueLightControllerFunc = ControllerFunctionRealPtr( |
---|
242 | new LightFlasherControllerFunction(Ogre::WFT_SINE, 0.75, 0.5)); |
---|
243 | mYellowLightControllerFunc = ControllerFunctionRealPtr( |
---|
244 | new LightFlasherControllerFunction(Ogre::WFT_TRIANGLE, 0.25, 0.0)); |
---|
245 | mGreenLightControllerFunc = ControllerFunctionRealPtr( |
---|
246 | new LightFlasherControllerFunction(Ogre::WFT_SINE, 0.25, 0.5)); |
---|
247 | |
---|
248 | // Light controllers |
---|
249 | ControllerManager* mControllerManager = &ControllerManager::getSingleton(); |
---|
250 | mRedLightController = mControllerManager->createController(mControllerManager->getFrameTimeSource(), mRedLightFlasher, mRedLightControllerFunc); |
---|
251 | mBlueLightController = mControllerManager->createController(mControllerManager->getFrameTimeSource(), mBlueLightFlasher, mBlueLightControllerFunc); |
---|
252 | mYellowLightController = mControllerManager->createController(mControllerManager->getFrameTimeSource(), mYellowLightFlasher, mYellowLightControllerFunc); |
---|
253 | mGreenLightController = mControllerManager->createController(mControllerManager->getFrameTimeSource(), mGreenLightFlasher, mGreenLightControllerFunc); |
---|
254 | |
---|
255 | */ |
---|
256 | |
---|
257 | setupTrailLights(); |
---|
258 | |
---|
259 | } |
---|
260 | |
---|
261 | void setupTrailLights(void) |
---|
262 | { |
---|
263 | mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5)); |
---|
264 | Vector3 dir(-1, -1, 0.5); |
---|
265 | dir.normalise(); |
---|
266 | Light* l = mSceneMgr->createLight("light1"); |
---|
267 | l->setType(Light::LT_DIRECTIONAL); |
---|
268 | l->setDirection(dir); |
---|
269 | |
---|
270 | NameValuePairList pairList; |
---|
271 | pairList["numberOfChains"] = "2"; |
---|
272 | pairList["maxElements"] = "80"; |
---|
273 | RibbonTrail* trail = static_cast<RibbonTrail*>( |
---|
274 | mSceneMgr->createMovableObject("1", "RibbonTrail", &pairList)); |
---|
275 | trail->setMaterialName("Examples/LightRibbonTrail"); |
---|
276 | trail->setTrailLength(400); |
---|
277 | |
---|
278 | |
---|
279 | mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(trail); |
---|
280 | |
---|
281 | // Create 3 nodes for trail to follow |
---|
282 | SceneNode* animNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); |
---|
283 | animNode->setPosition(50,30,0); |
---|
284 | Animation* anim = mSceneMgr->createAnimation("an1", 14); |
---|
285 | anim->setInterpolationMode(Animation::IM_SPLINE); |
---|
286 | NodeAnimationTrack* track = anim->createNodeTrack(1, animNode); |
---|
287 | TransformKeyFrame* kf = track->createNodeKeyFrame(0); |
---|
288 | kf->setTranslate(Vector3(50,30,0)); |
---|
289 | kf = track->createNodeKeyFrame(2); |
---|
290 | kf->setTranslate(Vector3(100, -30, 0)); |
---|
291 | kf = track->createNodeKeyFrame(4); |
---|
292 | kf->setTranslate(Vector3(120, -100, 150)); |
---|
293 | kf = track->createNodeKeyFrame(6); |
---|
294 | kf->setTranslate(Vector3(30, -100, 50)); |
---|
295 | kf = track->createNodeKeyFrame(8); |
---|
296 | kf->setTranslate(Vector3(-50, 30, -50)); |
---|
297 | kf = track->createNodeKeyFrame(10); |
---|
298 | kf->setTranslate(Vector3(-150, -20, -100)); |
---|
299 | kf = track->createNodeKeyFrame(12); |
---|
300 | kf->setTranslate(Vector3(-50, -30, 0)); |
---|
301 | kf = track->createNodeKeyFrame(14); |
---|
302 | kf->setTranslate(Vector3(50,30,0)); |
---|
303 | |
---|
304 | AnimationState* animState = mSceneMgr->createAnimationState("an1"); |
---|
305 | animState->setEnabled(true); |
---|
306 | mAnimStateList.push_back(animState); |
---|
307 | |
---|
308 | trail->setInitialColour(0, 1.0, 0.8, 0); |
---|
309 | trail->setColourChange(0, 0.5, 0.5, 0.5, 0.5); |
---|
310 | trail->setInitialWidth(0, 5); |
---|
311 | trail->addNode(animNode); |
---|
312 | |
---|
313 | // Add light |
---|
314 | Light* l2 = mSceneMgr->createLight("l2"); |
---|
315 | l2->setDiffuseColour(trail->getInitialColour(0)); |
---|
316 | animNode->attachObject(l2); |
---|
317 | |
---|
318 | // Add billboard |
---|
319 | BillboardSet* bbs = mSceneMgr->createBillboardSet("bb", 1); |
---|
320 | bbs->createBillboard(Vector3::ZERO, trail->getInitialColour(0)); |
---|
321 | bbs->setMaterialName("Examples/Flare"); |
---|
322 | animNode->attachObject(bbs); |
---|
323 | |
---|
324 | animNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); |
---|
325 | animNode->setPosition(-50,100,0); |
---|
326 | anim = mSceneMgr->createAnimation("an2", 10); |
---|
327 | anim->setInterpolationMode(Animation::IM_SPLINE); |
---|
328 | track = anim->createNodeTrack(1, animNode); |
---|
329 | kf = track->createNodeKeyFrame(0); |
---|
330 | kf->setTranslate(Vector3(-50,100,0)); |
---|
331 | kf = track->createNodeKeyFrame(2); |
---|
332 | kf->setTranslate(Vector3(-100, 150, -30)); |
---|
333 | kf = track->createNodeKeyFrame(4); |
---|
334 | kf->setTranslate(Vector3(-200, 0, 40)); |
---|
335 | kf = track->createNodeKeyFrame(6); |
---|
336 | kf->setTranslate(Vector3(0, -150, 70)); |
---|
337 | kf = track->createNodeKeyFrame(8); |
---|
338 | kf->setTranslate(Vector3(50, 0, 30)); |
---|
339 | kf = track->createNodeKeyFrame(10); |
---|
340 | kf->setTranslate(Vector3(-50,100,0)); |
---|
341 | |
---|
342 | animState = mSceneMgr->createAnimationState("an2"); |
---|
343 | animState->setEnabled(true); |
---|
344 | mAnimStateList.push_back(animState); |
---|
345 | |
---|
346 | trail->setInitialColour(1, 0.0, 1.0, 0.4); |
---|
347 | trail->setColourChange(1, 0.5, 0.5, 0.5, 0.5); |
---|
348 | trail->setInitialWidth(1, 5); |
---|
349 | trail->addNode(animNode); |
---|
350 | |
---|
351 | |
---|
352 | // Add light |
---|
353 | l2 = mSceneMgr->createLight("l3"); |
---|
354 | l2->setDiffuseColour(trail->getInitialColour(1)); |
---|
355 | animNode->attachObject(l2); |
---|
356 | |
---|
357 | // Add billboard |
---|
358 | bbs = mSceneMgr->createBillboardSet("bb2", 1); |
---|
359 | bbs->createBillboard(Vector3::ZERO, trail->getInitialColour(1)); |
---|
360 | bbs->setMaterialName("Examples/Flare"); |
---|
361 | animNode->attachObject(bbs); |
---|
362 | |
---|
363 | |
---|
364 | } |
---|
365 | |
---|
366 | void createFrameListener(void) |
---|
367 | { |
---|
368 | // This is where we instantiate our own frame listener |
---|
369 | mFrameListener= new LightingListener(mWindow, mCamera); |
---|
370 | mRoot->addFrameListener(mFrameListener); |
---|
371 | |
---|
372 | } |
---|
373 | |
---|
374 | }; |
---|