[777] | 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 | #include "OgreIlluminationManager.h"
|
---|
| 26 | #include "SpriteParticleRenderer.h"
|
---|
| 27 |
|
---|
| 28 | bool HighQuality = false;
|
---|
[875] | 29 | float causize = 7;
|
---|
| 30 | float cauintens = 40;
|
---|
[777] | 31 |
|
---|
| 32 | // Listener class for frame updates
|
---|
| 33 | class RaytraceDemoListener : public ExampleFrameListener
|
---|
| 34 | {
|
---|
| 35 | protected:
|
---|
| 36 | SceneNode* goldSphereNode;
|
---|
| 37 | SceneNode* silverSphereNode;
|
---|
| 38 | SceneNode* copperSphereNode;
|
---|
| 39 | SceneNode* aluSphereNode;
|
---|
| 40 | SceneNode* lightNode;
|
---|
| 41 | unsigned long framecount;
|
---|
| 42 |
|
---|
| 43 | public:
|
---|
| 44 | RaytraceDemoListener(RenderWindow* window, Camera* maincam,SceneNode* lightNode,
|
---|
| 45 | SceneNode* goldSphereNode,
|
---|
| 46 | SceneNode* silverSphereNode,
|
---|
| 47 | SceneNode* copperSphereNode,
|
---|
| 48 | SceneNode* aluSphereNode)
|
---|
| 49 | :ExampleFrameListener(window, maincam)
|
---|
| 50 |
|
---|
| 51 | {
|
---|
| 52 | framecount = 0;
|
---|
| 53 | this->goldSphereNode = goldSphereNode;
|
---|
| 54 | this->silverSphereNode = silverSphereNode;
|
---|
| 55 | this->copperSphereNode = copperSphereNode;
|
---|
| 56 | this->aluSphereNode = aluSphereNode;
|
---|
| 57 | this->lightNode = lightNode;
|
---|
| 58 | }
|
---|
| 59 | bool processUnbufferedKeyInput(const FrameEvent& evt)
|
---|
| 60 | {
|
---|
| 61 | MaterialPtr mat = MaterialManager::getSingleton().getByName("GameTools/Cau");
|
---|
| 62 | GpuProgramParameters* Vparams = mat->getTechnique(0)->getPass(0)->getVertexProgramParameters().getPointer();
|
---|
| 63 | GpuProgramParameters* Fparams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters().getPointer();
|
---|
| 64 |
|
---|
| 65 | if (mInputDevice->isKeyDown(KC_SUBTRACT))
|
---|
| 66 | {
|
---|
[807] | 67 | cauintens -= 0.5;
|
---|
[777] | 68 | cauintens = std::max(cauintens, 1.0f);
|
---|
| 69 | float intensity = pow( cauintens / 100.0f, 2 );
|
---|
| 70 | Fparams->setNamedConstant("CauSpriteIntens", intensity);
|
---|
| 71 | }
|
---|
| 72 | if (mInputDevice->isKeyDown(KC_ADD))
|
---|
| 73 | {
|
---|
[807] | 74 | cauintens += 0.5;
|
---|
[777] | 75 | cauintens = std::min(cauintens, 100.0f);
|
---|
| 76 | float intensity = pow( cauintens / 100.0f, 2 );
|
---|
| 77 | Fparams->setNamedConstant("CauSpriteIntens", intensity);
|
---|
| 78 | }
|
---|
| 79 | if (mInputDevice->isKeyDown(KC_MULTIPLY))
|
---|
| 80 | {
|
---|
[807] | 81 | causize = std::max(causize - 0.1, 0.0);
|
---|
[777] | 82 | Vparams->setNamedConstant("CauSpriteSize", causize);
|
---|
| 83 | }
|
---|
| 84 | if (mInputDevice->isKeyDown(KC_DIVIDE))
|
---|
| 85 | {
|
---|
[807] | 86 | causize = causize + 0.1;
|
---|
[777] | 87 | Vparams->setNamedConstant("CauSpriteSize", causize);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | return ExampleFrameListener::processUnbufferedKeyInput(evt);
|
---|
| 91 | }
|
---|
| 92 | bool frameStarted(const FrameEvent& evt)
|
---|
| 93 | {
|
---|
| 94 | static bool first = true;
|
---|
| 95 | if(first)
|
---|
| 96 | {
|
---|
| 97 | MaterialPtr mat = MaterialManager::getSingleton().getByName("GameTools/Cau");
|
---|
| 98 | GpuProgramParameters* Vparams = mat->getTechnique(0)->getPass(0)->getVertexProgramParameters().getPointer();
|
---|
| 99 | GpuProgramParameters* Fparams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters().getPointer();
|
---|
| 100 | float intensity = pow( cauintens / 100.0f, 2 );
|
---|
| 101 | Fparams->setNamedConstant("CauSpriteIntens", intensity);
|
---|
| 102 | Vparams->setNamedConstant("CauSpriteSize", causize);
|
---|
| 103 | first = false;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | lightNode->setPosition( 100.0 * Vector3(cos(-(float)framecount/500.0),2.0,sin(-(float)framecount/500.0)));
|
---|
| 107 | //lightNode->setDirection(-lightNode->getPosition());
|
---|
| 108 |
|
---|
| 109 | goldSphereNode->setPosition( 100.0 *
|
---|
| 110 | Vector3(
|
---|
| 111 | cos((float)framecount/700.0),
|
---|
| 112 | 1.0,
|
---|
| 113 | sin((float)framecount/700.0)));
|
---|
| 114 | silverSphereNode->setPosition( 100.0 *
|
---|
| 115 | Vector3(
|
---|
| 116 | cos((float)framecount/700.0 + Math::PI / 2.0),
|
---|
| 117 | 1.0,
|
---|
| 118 | sin((float)framecount/700.0 + Math::PI / 2.0)));
|
---|
| 119 | copperSphereNode->setPosition( 100.0 *
|
---|
| 120 | Vector3(
|
---|
| 121 | cos((float)framecount/700.0 + Math::PI ),
|
---|
| 122 | 1.0,
|
---|
| 123 | sin((float)framecount/700.0 + Math::PI)));
|
---|
| 124 | aluSphereNode->setPosition( 100.0 *
|
---|
| 125 | Vector3(
|
---|
| 126 | cos((float)framecount/700.0 + 3 * Math::PI / 2.0),
|
---|
| 127 | 1.0,
|
---|
| 128 | sin((float)framecount/700.0+ 3 * Math::PI / 2.0)));
|
---|
| 129 |
|
---|
| 130 | bool result = ExampleFrameListener::frameStarted(evt);
|
---|
| 131 | framecount++;
|
---|
| 132 | return result;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | };
|
---|
| 138 |
|
---|
| 139 | /** Application class */
|
---|
| 140 | class RaytraceDemoApplication : public ExampleApplication
|
---|
| 141 | {
|
---|
| 142 |
|
---|
| 143 | public:
|
---|
| 144 | virtual void go(void)
|
---|
| 145 | {
|
---|
| 146 | if (!setup())
|
---|
| 147 | return;
|
---|
| 148 |
|
---|
| 149 | renderScene();
|
---|
| 150 |
|
---|
| 151 | // clean up
|
---|
| 152 | destroyScene();
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[875] | 155 |
|
---|
[777] | 156 | protected:
|
---|
| 157 | SceneNode* kupolaNode;
|
---|
| 158 | SceneNode* mainLightNode;
|
---|
| 159 | SceneNode* goldSphereNode;
|
---|
| 160 | SceneNode* silverSphereNode;
|
---|
| 161 | SceneNode* copperSphereNode;
|
---|
| 162 | SceneNode* aluSphereNode;
|
---|
| 163 |
|
---|
[875] | 164 | void renderScene(){
|
---|
[777] | 165 |
|
---|
| 166 | mWindow->resetStatistics();
|
---|
| 167 | while(true){
|
---|
| 168 |
|
---|
| 169 |
|
---|
| 170 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
| 171 | // Pump events on Win32
|
---|
| 172 | MSG msg;
|
---|
| 173 | while( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
|
---|
| 174 | {
|
---|
| 175 | TranslateMessage( &msg );
|
---|
| 176 | DispatchMessage( &msg );
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | #endif
|
---|
| 180 |
|
---|
| 181 | if(!mRoot->_fireFrameStarted()){
|
---|
| 182 | break;
|
---|
| 183 | }
|
---|
| 184 | unsigned long framenum = mRoot->getCurrentFrameNumber();
|
---|
| 185 |
|
---|
| 186 | OgreIlluminationManager::getSingleton().update(framenum, mWindow);
|
---|
| 187 |
|
---|
[875] | 188 | //mRoot->renderOneFrame();
|
---|
| 189 | mRoot->_updateAllRenderTargets();
|
---|
| 190 |
|
---|
[777] | 191 | mRoot->_fireFrameEnded();
|
---|
| 192 |
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | void createPostproc()
|
---|
| 197 | {
|
---|
[788] | 198 | CompositorManager::getSingleton().addCompositor(mWindow->getViewport(0),
|
---|
[777] | 199 | "GameTools/Glow");
|
---|
| 200 | CompositorManager::getSingleton().setCompositorEnabled(mWindow->getViewport(0),
|
---|
[788] | 201 | "GameTools/Glow", true);
|
---|
[777] | 202 | /*
|
---|
| 203 | CompositorManager::getSingleton().addCompositor(mWindow->getViewport(0),
|
---|
| 204 | "GameTools/ToneMap");
|
---|
| 205 | CompositorManager::getSingleton().setCompositorEnabled(mWindow->getViewport(0),
|
---|
| 206 | "GameTools/ToneMap", true);*/
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 |
|
---|
| 210 | void createScene(void)
|
---|
| 211 | {
|
---|
| 212 |
|
---|
| 213 | createPostproc();
|
---|
| 214 |
|
---|
| 215 | OgreIlluminationManager::getSingleton().setMainCamera(mCamera);
|
---|
| 216 | OgreIlluminationManager::getSingleton().setMainViewport(mWindow->getViewport(0));
|
---|
| 217 |
|
---|
| 218 | if(! HighQuality)
|
---|
| 219 | OgreIlluminationManager::getSingleton().setMaxJoinRadius(420);
|
---|
| 220 | else
|
---|
| 221 | OgreIlluminationManager::getSingleton().setMaxJoinRadius(0.1);
|
---|
| 222 |
|
---|
| 223 |
|
---|
| 224 | Root::getSingleton()._setCurrentSceneManager(mSceneMgr);
|
---|
| 225 | mCamera->setPosition(0,100,240);
|
---|
| 226 | mCamera->setFOVy(Radian(Degree(80)));
|
---|
| 227 | //mCamera->setFarClipDistance(1000);
|
---|
| 228 | // Set ambient light
|
---|
| 229 | mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
|
---|
| 230 |
|
---|
| 231 | // Skybox
|
---|
| 232 | mSceneMgr->setSkyBox(true, "Examples/MorningSkyBox");
|
---|
| 233 |
|
---|
| 234 | SceneNode* rootNode = mSceneMgr->getRootSceneNode();
|
---|
| 235 |
|
---|
| 236 |
|
---|
| 237 | //init objects
|
---|
| 238 | Entity* kupola = mSceneMgr->createEntity("kupola", "kupola.mesh");
|
---|
| 239 | Entity* athene = mSceneMgr->createEntity("athene", "head.mesh");
|
---|
| 240 | Entity* goldSphere = mSceneMgr->createEntity("goldSphere", "head.mesh");
|
---|
| 241 | Entity* silverSphere = mSceneMgr->createEntity("silverSphere", "head.mesh");
|
---|
| 242 | Entity* copperSphere = mSceneMgr->createEntity("copperSphere", "head.mesh");
|
---|
| 243 | Entity* aluSphere = mSceneMgr->createEntity("aluSphere", "head.mesh");
|
---|
| 244 |
|
---|
[807] | 245 | kupola->setCastShadows(false);
|
---|
[777] | 246 | //lod
|
---|
| 247 | //MeshPtr amesh = athene->getMesh();
|
---|
| 248 | //Mesh::LodDistanceList distances;
|
---|
| 249 | //distances.push_back(1);
|
---|
| 250 | //distances.push_back(2);
|
---|
[807] | 251 | //distances.push_back(3);
|
---|
| 252 | //distances.push_back(4);
|
---|
[777] | 253 | ////distances.push_back(5);
|
---|
| 254 | //amesh->generateLodLevels(distances,ProgressiveMesh::VertexReductionQuota::VRQ_PROPORTIONAL,0.6);
|
---|
| 255 | //athene->setMeshLodBias(50000.0,0);
|
---|
| 256 | //tangents
|
---|
| 257 | //amesh->buildTangentVectors(0,1);
|
---|
| 258 | MeshPtr kmesh = kupola->getMesh();
|
---|
| 259 | kmesh->buildTangentVectors(0,1);
|
---|
| 260 |
|
---|
| 261 | //init nodes
|
---|
| 262 | kupolaNode = rootNode->createChildSceneNode( "kupola" );
|
---|
| 263 | kupolaNode->rotate(Vector3(0,1,0),Radian(Degree(90)));
|
---|
| 264 | kupolaNode->attachObject( kupola );
|
---|
| 265 | kupolaNode->scale(25,21,25);
|
---|
[807] | 266 | //kupolaNode->scale(2,2,2);
|
---|
| 267 | //kupolaNode->setPosition(0,100,0);
|
---|
[777] | 268 | SceneNode* atheneNode = rootNode->createChildSceneNode("athene");
|
---|
| 269 | atheneNode->attachObject(athene);
|
---|
| 270 | //atheneNode->setScale(3.5,5.5,5.5);
|
---|
| 271 | atheneNode->setScale(4,4,4);
|
---|
| 272 | //atheneNode->setScale(0.5,0.5,0.5);
|
---|
| 273 | AxisAlignedBox bbox = athene->getBoundingBox();
|
---|
| 274 | atheneNode->setPosition(0, 100,0);
|
---|
| 275 |
|
---|
| 276 |
|
---|
| 277 | goldSphereNode = rootNode->createChildSceneNode( "goldSphere" );
|
---|
| 278 | //goldSphereNode->scale(0.3,0.3,0.3);
|
---|
| 279 | goldSphereNode->scale(2,2,2);
|
---|
| 280 | silverSphereNode = rootNode->createChildSceneNode( "silverSphere" );
|
---|
| 281 | //silverSphereNode->scale(0.3,0.3,0.3);
|
---|
| 282 | silverSphereNode->scale(2,2,2);
|
---|
| 283 | copperSphereNode = rootNode->createChildSceneNode( "copperSphere" );
|
---|
| 284 | //copperSphereNode->scale(0.3,0.3,0.3);
|
---|
| 285 | copperSphereNode->scale(2,2,2);
|
---|
| 286 | aluSphereNode = rootNode->createChildSceneNode( "aluSphere" );
|
---|
| 287 | //aluSphereNode->scale(0.3,0.3,0.3);
|
---|
| 288 | aluSphereNode->scale(2,2,2);
|
---|
| 289 |
|
---|
| 290 | goldSphereNode->attachObject(goldSphere);
|
---|
| 291 | silverSphereNode->attachObject(silverSphere);
|
---|
| 292 | aluSphereNode->attachObject(aluSphere);
|
---|
| 293 | copperSphereNode->attachObject(copperSphere);
|
---|
| 294 |
|
---|
| 295 | goldSphereNode->_updateBounds();
|
---|
| 296 | silverSphereNode->_updateBounds();
|
---|
| 297 | aluSphereNode->_updateBounds();
|
---|
| 298 | copperSphereNode->_updateBounds();
|
---|
| 299 |
|
---|
| 300 | kupolaNode->_updateBounds();
|
---|
| 301 | //atheneNode->_updateBounds();
|
---|
| 302 |
|
---|
| 303 | //Init lights
|
---|
| 304 | Light* mainLight = mSceneMgr->createLight("MainLight");
|
---|
| 305 | mainLight->setType(Light::LT_POINT);
|
---|
| 306 | mainLight->setDiffuseColour(ColourValue::White);
|
---|
| 307 | mainLight->setSpecularColour(ColourValue::White);
|
---|
| 308 | mainLightNode = rootNode->createChildSceneNode();
|
---|
| 309 | mainLightNode->setPosition(0,0,0);
|
---|
| 310 | mainLight->setPosition(mainLightNode->getWorldPosition());
|
---|
[835] | 311 | mainLightNode->attachObject(mainLight);
|
---|
[777] | 312 |
|
---|
[835] | 313 | athene->setMaterialName("GameTools/CubeMap/Localized");
|
---|
| 314 | aluSphere->setMaterialName("EnvMetals/Alu");
|
---|
| 315 | copperSphere->setMaterialName("EnvMetals/Copper");
|
---|
| 316 | goldSphere->setMaterialName("EnvMetals/Gold");
|
---|
| 317 | silverSphere->setMaterialName("EnvMetals/Silver");
|
---|
[777] | 318 |
|
---|
[835] | 319 | OgreIlluminationManager::getSingleton().initTechniques();
|
---|
[777] | 320 |
|
---|
| 321 |
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | void createFrameListener(void)
|
---|
| 325 | {
|
---|
| 326 | // This is where we instantiate our own frame listener
|
---|
| 327 | mFrameListener= new RaytraceDemoListener(mWindow,
|
---|
| 328 | mCamera,
|
---|
| 329 | mainLightNode,
|
---|
| 330 | goldSphereNode,
|
---|
| 331 | silverSphereNode,
|
---|
| 332 | copperSphereNode,
|
---|
| 333 | aluSphereNode);
|
---|
| 334 | mRoot->addFrameListener(mFrameListener);
|
---|
[875] | 335 | //mRoot->addFrameListener(&OgreIlluminationManager::getSingleton());
|
---|
[777] | 336 |
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | };
|
---|
| 340 |
|
---|