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;
|
---|
29 | float causize = 4;
|
---|
30 | float cauintens = 25;
|
---|
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 | {
|
---|
67 | cauintens -= 0.5;
|
---|
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 | {
|
---|
74 | cauintens += 0.5;
|
---|
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 | {
|
---|
81 | causize = std::max(causize - 0.1, 0.0);
|
---|
82 | Vparams->setNamedConstant("CauSpriteSize", causize);
|
---|
83 | }
|
---|
84 | if (mInputDevice->isKeyDown(KC_DIVIDE))
|
---|
85 | {
|
---|
86 | causize = causize + 0.1;
|
---|
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 |
|
---|
155 | protected:
|
---|
156 | SceneNode* kupolaNode;
|
---|
157 | SceneNode* mainLightNode;
|
---|
158 | SceneNode* goldSphereNode;
|
---|
159 | SceneNode* silverSphereNode;
|
---|
160 | SceneNode* copperSphereNode;
|
---|
161 | SceneNode* aluSphereNode;
|
---|
162 |
|
---|
163 |
|
---|
164 | void renderScene(){
|
---|
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 |
|
---|
188 | //Root::getSingleton().renderOneFrame();
|
---|
189 |
|
---|
190 |
|
---|
191 |
|
---|
192 |
|
---|
193 | mRoot->_fireFrameEnded();
|
---|
194 |
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | void createPostproc()
|
---|
199 | {
|
---|
200 | CompositorManager::getSingleton().addCompositor(mWindow->getViewport(0),
|
---|
201 | "GameTools/Glow");
|
---|
202 | CompositorManager::getSingleton().setCompositorEnabled(mWindow->getViewport(0),
|
---|
203 | "GameTools/Glow", true);
|
---|
204 | /*
|
---|
205 | CompositorManager::getSingleton().addCompositor(mWindow->getViewport(0),
|
---|
206 | "GameTools/ToneMap");
|
---|
207 | CompositorManager::getSingleton().setCompositorEnabled(mWindow->getViewport(0),
|
---|
208 | "GameTools/ToneMap", true);*/
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | void createScene(void)
|
---|
213 | {
|
---|
214 |
|
---|
215 | createPostproc();
|
---|
216 |
|
---|
217 | OgreIlluminationManager::getSingleton().setMainCamera(mCamera);
|
---|
218 | OgreIlluminationManager::getSingleton().setMainViewport(mWindow->getViewport(0));
|
---|
219 |
|
---|
220 | if(! HighQuality)
|
---|
221 | OgreIlluminationManager::getSingleton().setMaxJoinRadius(420);
|
---|
222 | else
|
---|
223 | OgreIlluminationManager::getSingleton().setMaxJoinRadius(0.1);
|
---|
224 |
|
---|
225 |
|
---|
226 | Root::getSingleton()._setCurrentSceneManager(mSceneMgr);
|
---|
227 | mCamera->setPosition(0,100,240);
|
---|
228 | mCamera->setFOVy(Radian(Degree(80)));
|
---|
229 | //mCamera->setFarClipDistance(1000);
|
---|
230 | // Set ambient light
|
---|
231 | mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
|
---|
232 |
|
---|
233 | // Skybox
|
---|
234 | mSceneMgr->setSkyBox(true, "Examples/MorningSkyBox");
|
---|
235 |
|
---|
236 | SceneNode* rootNode = mSceneMgr->getRootSceneNode();
|
---|
237 |
|
---|
238 |
|
---|
239 | //init objects
|
---|
240 | Entity* kupola = mSceneMgr->createEntity("kupola", "kupola.mesh");
|
---|
241 | Entity* athene = mSceneMgr->createEntity("athene", "head.mesh");
|
---|
242 | Entity* goldSphere = mSceneMgr->createEntity("goldSphere", "head.mesh");
|
---|
243 | Entity* silverSphere = mSceneMgr->createEntity("silverSphere", "head.mesh");
|
---|
244 | Entity* copperSphere = mSceneMgr->createEntity("copperSphere", "head.mesh");
|
---|
245 | Entity* aluSphere = mSceneMgr->createEntity("aluSphere", "head.mesh");
|
---|
246 |
|
---|
247 | kupola->setCastShadows(false);
|
---|
248 | //lod
|
---|
249 | //MeshPtr amesh = athene->getMesh();
|
---|
250 | //Mesh::LodDistanceList distances;
|
---|
251 | //distances.push_back(1);
|
---|
252 | //distances.push_back(2);
|
---|
253 | //distances.push_back(3);
|
---|
254 | //distances.push_back(4);
|
---|
255 | ////distances.push_back(5);
|
---|
256 | //amesh->generateLodLevels(distances,ProgressiveMesh::VertexReductionQuota::VRQ_PROPORTIONAL,0.6);
|
---|
257 | //athene->setMeshLodBias(50000.0,0);
|
---|
258 | //tangents
|
---|
259 | //amesh->buildTangentVectors(0,1);
|
---|
260 | MeshPtr kmesh = kupola->getMesh();
|
---|
261 | kmesh->buildTangentVectors(0,1);
|
---|
262 |
|
---|
263 | //init nodes
|
---|
264 | kupolaNode = rootNode->createChildSceneNode( "kupola" );
|
---|
265 | kupolaNode->rotate(Vector3(0,1,0),Radian(Degree(90)));
|
---|
266 | kupolaNode->attachObject( kupola );
|
---|
267 | kupolaNode->scale(25,21,25);
|
---|
268 | //kupolaNode->scale(2,2,2);
|
---|
269 | //kupolaNode->setPosition(0,100,0);
|
---|
270 | SceneNode* atheneNode = rootNode->createChildSceneNode("athene");
|
---|
271 | atheneNode->attachObject(athene);
|
---|
272 | //atheneNode->setScale(3.5,5.5,5.5);
|
---|
273 | atheneNode->setScale(4,4,4);
|
---|
274 | //atheneNode->setScale(0.5,0.5,0.5);
|
---|
275 | AxisAlignedBox bbox = athene->getBoundingBox();
|
---|
276 | atheneNode->setPosition(0, 100,0);
|
---|
277 |
|
---|
278 |
|
---|
279 | goldSphereNode = rootNode->createChildSceneNode( "goldSphere" );
|
---|
280 | //goldSphereNode->scale(0.3,0.3,0.3);
|
---|
281 | goldSphereNode->scale(2,2,2);
|
---|
282 | silverSphereNode = rootNode->createChildSceneNode( "silverSphere" );
|
---|
283 | //silverSphereNode->scale(0.3,0.3,0.3);
|
---|
284 | silverSphereNode->scale(2,2,2);
|
---|
285 | copperSphereNode = rootNode->createChildSceneNode( "copperSphere" );
|
---|
286 | //copperSphereNode->scale(0.3,0.3,0.3);
|
---|
287 | copperSphereNode->scale(2,2,2);
|
---|
288 | aluSphereNode = rootNode->createChildSceneNode( "aluSphere" );
|
---|
289 | //aluSphereNode->scale(0.3,0.3,0.3);
|
---|
290 | aluSphereNode->scale(2,2,2);
|
---|
291 |
|
---|
292 | goldSphereNode->attachObject(goldSphere);
|
---|
293 | silverSphereNode->attachObject(silverSphere);
|
---|
294 | aluSphereNode->attachObject(aluSphere);
|
---|
295 | copperSphereNode->attachObject(copperSphere);
|
---|
296 |
|
---|
297 | goldSphereNode->_updateBounds();
|
---|
298 | silverSphereNode->_updateBounds();
|
---|
299 | aluSphereNode->_updateBounds();
|
---|
300 | copperSphereNode->_updateBounds();
|
---|
301 |
|
---|
302 | kupolaNode->_updateBounds();
|
---|
303 | //atheneNode->_updateBounds();
|
---|
304 |
|
---|
305 | //Init lights
|
---|
306 | Light* mainLight = mSceneMgr->createLight("MainLight");
|
---|
307 | mainLight->setType(Light::LT_POINT);
|
---|
308 | mainLight->setDiffuseColour(ColourValue::White);
|
---|
309 | mainLight->setSpecularColour(ColourValue::White);
|
---|
310 | mainLightNode = rootNode->createChildSceneNode();
|
---|
311 | mainLightNode->setPosition(0,0,0);
|
---|
312 | mainLight->setPosition(mainLightNode->getWorldPosition());
|
---|
313 | mainLightNode->attachObject(mainLight);
|
---|
314 |
|
---|
315 | athene->setMaterialName("GameTools/CubeMap/Localized");
|
---|
316 | aluSphere->setMaterialName("EnvMetals/Alu");
|
---|
317 | copperSphere->setMaterialName("EnvMetals/Copper");
|
---|
318 | goldSphere->setMaterialName("EnvMetals/Gold");
|
---|
319 | silverSphere->setMaterialName("EnvMetals/Silver");
|
---|
320 |
|
---|
321 | OgreIlluminationManager::getSingleton().initTechniques();
|
---|
322 |
|
---|
323 |
|
---|
324 | }
|
---|
325 |
|
---|
326 | void createFrameListener(void)
|
---|
327 | {
|
---|
328 | // This is where we instantiate our own frame listener
|
---|
329 | mFrameListener= new RaytraceDemoListener(mWindow,
|
---|
330 | mCamera,
|
---|
331 | mainLightNode,
|
---|
332 | goldSphereNode,
|
---|
333 | silverSphereNode,
|
---|
334 | copperSphereNode,
|
---|
335 | aluSphereNode);
|
---|
336 | mRoot->addFrameListener(mFrameListener);
|
---|
337 |
|
---|
338 | }
|
---|
339 |
|
---|
340 | };
|
---|
341 |
|
---|