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