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 | 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 |
|
---|
80 | // Listener class for frame updates
|
---|
81 | class LightingListener : public ExampleFrameListener
|
---|
82 | {
|
---|
83 | protected:
|
---|
84 | public:
|
---|
85 | LightingListener(RenderWindow* win, Camera* cam)
|
---|
86 | : ExampleFrameListener(win, cam)
|
---|
87 | {
|
---|
88 | }
|
---|
89 |
|
---|
90 | bool frameStarted(const FrameEvent& evt)
|
---|
91 | {
|
---|
92 | mRedYellowLightsNode->yaw(Degree(evt.timeSinceLastFrame * 10));
|
---|
93 | mGreenBlueLightsNode->pitch(Degree(evt.timeSinceLastFrame * 20));
|
---|
94 | // Call superclass
|
---|
95 | return ExampleFrameListener::frameStarted(evt);
|
---|
96 | }
|
---|
97 | };
|
---|
98 |
|
---|
99 | /** Application class */
|
---|
100 | class LightingApplication : public ExampleApplication
|
---|
101 | {
|
---|
102 | protected:
|
---|
103 |
|
---|
104 | // The set of all the billboards used for the lights
|
---|
105 | // 2 sets because we'll rotate them differently
|
---|
106 | BillboardSet* mRedYellowLights;
|
---|
107 | BillboardSet* mGreenBlueLights;
|
---|
108 |
|
---|
109 | // Billboards
|
---|
110 | Billboard* mRedLightBoard;
|
---|
111 | Billboard* mBlueLightBoard;
|
---|
112 | Billboard* mYellowLightBoard;
|
---|
113 | Billboard* mGreenLightBoard;
|
---|
114 |
|
---|
115 | // Lights
|
---|
116 | Light* mRedLight;
|
---|
117 | Light* mBlueLight;
|
---|
118 | Light* mYellowLight;
|
---|
119 | Light* mGreenLight;
|
---|
120 |
|
---|
121 |
|
---|
122 | // Light flashers
|
---|
123 | ControllerValueRealPtr mRedLightFlasher;
|
---|
124 | ControllerValueRealPtr mBlueLightFlasher;
|
---|
125 | ControllerValueRealPtr mYellowLightFlasher;
|
---|
126 | ControllerValueRealPtr mGreenLightFlasher;
|
---|
127 |
|
---|
128 | // Light controller functions
|
---|
129 | ControllerFunctionRealPtr mRedLightControllerFunc;
|
---|
130 | ControllerFunctionRealPtr mBlueLightControllerFunc;
|
---|
131 | ControllerFunctionRealPtr mYellowLightControllerFunc;
|
---|
132 | ControllerFunctionRealPtr mGreenLightControllerFunc;
|
---|
133 |
|
---|
134 | // Light controllers
|
---|
135 | Controller<Real>* mRedLightController;
|
---|
136 | Controller<Real>* mBlueLightController;
|
---|
137 | Controller<Real>* mYellowLightController;
|
---|
138 | Controller<Real>* mGreenLightController;
|
---|
139 |
|
---|
140 | void createScene(void)
|
---|
141 | {
|
---|
142 | // Set a very low level of ambient lighting
|
---|
143 | mSceneMgr->setAmbientLight(ColourValue(0.1, 0.1, 0.1));
|
---|
144 |
|
---|
145 | // Use the "Space" skybox
|
---|
146 | mSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox");
|
---|
147 |
|
---|
148 | // Load ogre head
|
---|
149 | Entity* head = mSceneMgr->createEntity("head", "ogrehead.mesh");
|
---|
150 |
|
---|
151 | // Attach the head to the scene
|
---|
152 | mSceneMgr->getRootSceneNode()->attachObject(head);
|
---|
153 |
|
---|
154 | // Create nodes for the lights to be rotated with
|
---|
155 | mRedYellowLightsNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
156 | mGreenBlueLightsNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
157 |
|
---|
158 |
|
---|
159 | // First create the BillboardSets. This will define the materials for the billboards
|
---|
160 | // in its set to use
|
---|
161 | mRedYellowLights = mSceneMgr->createBillboardSet("RedYellowLights");
|
---|
162 | mRedYellowLights->setMaterialName("Examples/Flare");
|
---|
163 | mRedYellowLightsNode->attachObject(mRedYellowLights);
|
---|
164 |
|
---|
165 | mGreenBlueLights = mSceneMgr->createBillboardSet("GreenBlueLights");
|
---|
166 | mGreenBlueLights->setMaterialName("Examples/Flare");
|
---|
167 | mGreenBlueLightsNode->attachObject(mGreenBlueLights);
|
---|
168 |
|
---|
169 | // Red light billboard, in "off" state
|
---|
170 | Vector3 redLightPosition(78, -8, -70);
|
---|
171 | mRedLightBoard = mRedYellowLights->createBillboard(redLightPosition);
|
---|
172 | mRedLightBoard->setColour(ColourValue::Black);
|
---|
173 |
|
---|
174 |
|
---|
175 | // Blue light billboard, in "off" state
|
---|
176 | Vector3 blueLightPosition(-90, -8, -70);
|
---|
177 | mBlueLightBoard = mGreenBlueLights->createBillboard(blueLightPosition);
|
---|
178 | mBlueLightBoard->setColour(ColourValue::Black);
|
---|
179 |
|
---|
180 |
|
---|
181 | // Yellow light billboard, in "off" state
|
---|
182 | Vector3 yellowLightPosition(-4.5, 30, -80);
|
---|
183 | mYellowLightBoard = mRedYellowLights->createBillboard(yellowLightPosition);
|
---|
184 | mYellowLightBoard->setColour(ColourValue::Black);
|
---|
185 |
|
---|
186 | // Green light billboard, in "off" state
|
---|
187 | Vector3 greenLightPosition(50, 70, 80);
|
---|
188 | mGreenLightBoard = mGreenBlueLights->createBillboard(greenLightPosition);
|
---|
189 | mGreenLightBoard->setColour(ColourValue::Black);
|
---|
190 |
|
---|
191 | // Red light, in "off" state
|
---|
192 | mRedLight = mSceneMgr->createLight("RedFlyingLight");
|
---|
193 | mRedLight->setType(Light::LT_POINT);
|
---|
194 | mRedLight->setPosition(redLightPosition);
|
---|
195 | mRedLight->setDiffuseColour(ColourValue::Black);
|
---|
196 | mRedYellowLightsNode->attachObject(mRedLight);
|
---|
197 |
|
---|
198 | // Blue light, in "off" state
|
---|
199 | mBlueLight = mSceneMgr->createLight("BlueFlyingLight");
|
---|
200 | mBlueLight->setType(Light::LT_POINT);
|
---|
201 | mBlueLight->setPosition(blueLightPosition);
|
---|
202 | mBlueLight->setDiffuseColour(ColourValue::Black);
|
---|
203 | mGreenBlueLightsNode->attachObject(mBlueLight);
|
---|
204 |
|
---|
205 | // Yellow light in "off" state
|
---|
206 | mYellowLight = mSceneMgr->createLight("YellowFlyingLight");
|
---|
207 | mYellowLight->setType(Light::LT_POINT);
|
---|
208 | mYellowLight->setPosition(yellowLightPosition);
|
---|
209 | mYellowLight->setDiffuseColour(ColourValue::Black);
|
---|
210 | mRedYellowLightsNode->attachObject(mYellowLight);
|
---|
211 |
|
---|
212 | // Yellow light in "off" state
|
---|
213 | mGreenLight = mSceneMgr->createLight("GreenFlyingLight");
|
---|
214 | mGreenLight->setType(Light::LT_POINT);
|
---|
215 | mGreenLight->setPosition(greenLightPosition);
|
---|
216 | mGreenLight->setDiffuseColour(ColourValue::Black);
|
---|
217 | mGreenBlueLightsNode->attachObject(mGreenLight);
|
---|
218 |
|
---|
219 | // Light flashers
|
---|
220 | mRedLightFlasher = ControllerValueRealPtr(
|
---|
221 | new LightFlasher(mRedLight, mRedLightBoard, ColourValue::Red));
|
---|
222 | mBlueLightFlasher = ControllerValueRealPtr(
|
---|
223 | new LightFlasher(mBlueLight, mBlueLightBoard, ColourValue::Blue));
|
---|
224 | mYellowLightFlasher = ControllerValueRealPtr(
|
---|
225 | new LightFlasher(mYellowLight, mYellowLightBoard, ColourValue(1.0, 1.0, 0.0)));
|
---|
226 | mGreenLightFlasher = ControllerValueRealPtr(
|
---|
227 | new LightFlasher(mGreenLight, mGreenLightBoard, ColourValue::Green));
|
---|
228 |
|
---|
229 | // Light controller functions
|
---|
230 | mRedLightControllerFunc = ControllerFunctionRealPtr(
|
---|
231 | new LightFlasherControllerFunction(Ogre::WFT_SINE, 0.5, 0.0));
|
---|
232 | mBlueLightControllerFunc = ControllerFunctionRealPtr(
|
---|
233 | new LightFlasherControllerFunction(Ogre::WFT_SINE, 0.75, 0.5));
|
---|
234 | mYellowLightControllerFunc = ControllerFunctionRealPtr(
|
---|
235 | new LightFlasherControllerFunction(Ogre::WFT_TRIANGLE, 0.25, 0.0));
|
---|
236 | mGreenLightControllerFunc = ControllerFunctionRealPtr(
|
---|
237 | new LightFlasherControllerFunction(Ogre::WFT_SINE, 0.25, 0.5));
|
---|
238 |
|
---|
239 | // Light controllers
|
---|
240 | ControllerManager* mControllerManager = &ControllerManager::getSingleton();
|
---|
241 | mRedLightController = mControllerManager->createController(mControllerManager->getFrameTimeSource(), mRedLightFlasher, mRedLightControllerFunc);
|
---|
242 | mBlueLightController = mControllerManager->createController(mControllerManager->getFrameTimeSource(), mBlueLightFlasher, mBlueLightControllerFunc);
|
---|
243 | mYellowLightController = mControllerManager->createController(mControllerManager->getFrameTimeSource(), mYellowLightFlasher, mYellowLightControllerFunc);
|
---|
244 | mGreenLightController = mControllerManager->createController(mControllerManager->getFrameTimeSource(), mGreenLightFlasher, mGreenLightControllerFunc);
|
---|
245 |
|
---|
246 | }
|
---|
247 |
|
---|
248 | void createFrameListener(void)
|
---|
249 | {
|
---|
250 | // This is where we instantiate our own frame listener
|
---|
251 | mFrameListener= new LightingListener(mWindow, mCamera);
|
---|
252 | mRoot->addFrameListener(mFrameListener);
|
---|
253 |
|
---|
254 | }
|
---|
255 |
|
---|
256 | };
|
---|