1 |
|
---|
2 |
|
---|
3 |
|
---|
4 | /*
|
---|
5 | -----------------------------------------------------------------------------
|
---|
6 | This source file is part of OGRE
|
---|
7 | (Object-oriented Graphics Rendering Engine)
|
---|
8 | For the latest info, see http://www.ogre3d.org/
|
---|
9 |
|
---|
10 | Copyright (c) 2000-2005 The OGRE Team
|
---|
11 | Also see acknowledgements in Readme.html
|
---|
12 |
|
---|
13 | You may use this sample code for anything you like, it is not covered by the
|
---|
14 | LGPL like the rest of the engine.
|
---|
15 | -----------------------------------------------------------------------------
|
---|
16 | */
|
---|
17 |
|
---|
18 | /**
|
---|
19 | \file
|
---|
20 | Lighting.h
|
---|
21 | \brief
|
---|
22 | Shows lighting support in OGRE. Also demonstrates the use of billboards
|
---|
23 | and controllers for automatic time-relative behaviour.
|
---|
24 | */
|
---|
25 |
|
---|
26 |
|
---|
27 | #include "ExampleApplication.h"
|
---|
28 | #include "OgreIlluminationManager.h"
|
---|
29 |
|
---|
30 | float causize = 10;
|
---|
31 | float cauintens = 40;
|
---|
32 |
|
---|
33 | // Listener class for frame updates
|
---|
34 | class RaytraceDemoListener : public ExampleFrameListener
|
---|
35 | {
|
---|
36 | protected:
|
---|
37 |
|
---|
38 | Light* light;
|
---|
39 | SceneNode* lightNode;
|
---|
40 | SceneNode* sphereNode;
|
---|
41 | unsigned long framecount;
|
---|
42 |
|
---|
43 | public:
|
---|
44 | RaytraceDemoListener(RenderWindow* window, Camera* maincam,SceneNode* lightNode, SceneNode* sphereNode, Light* l)
|
---|
45 | :ExampleFrameListener(window, maincam)
|
---|
46 |
|
---|
47 | {
|
---|
48 | framecount = 0;
|
---|
49 | this->sphereNode = sphereNode;
|
---|
50 | this->lightNode = lightNode;
|
---|
51 | light = l;
|
---|
52 | }
|
---|
53 | bool processUnbufferedKeyInput(const FrameEvent& evt)
|
---|
54 | {
|
---|
55 | if (mInputDevice->isKeyDown(KC_SUBTRACT))
|
---|
56 | {
|
---|
57 | Vector3 lPos = lightNode->getPosition();
|
---|
58 | Vector3 lDir = sphereNode->getPosition() - lPos;
|
---|
59 | lDir.normalise();
|
---|
60 | lPos -= lDir;
|
---|
61 | lightNode->setPosition(lPos);
|
---|
62 | light->setDirection(lDir);
|
---|
63 | }
|
---|
64 | if (mInputDevice->isKeyDown(KC_ADD))
|
---|
65 | {
|
---|
66 |
|
---|
67 | Vector3 lPos = lightNode->getPosition();
|
---|
68 | Vector3 lDir = sphereNode->getPosition() - lPos;
|
---|
69 | lDir.normalise();
|
---|
70 | lPos += lDir;
|
---|
71 | lightNode->setPosition(lPos);
|
---|
72 | light->setDirection(lDir);
|
---|
73 | }
|
---|
74 | if (mInputDevice->isKeyDown(KC_MULTIPLY))
|
---|
75 | {
|
---|
76 | Vector3 lPos = lightNode->getPosition();
|
---|
77 | Vector3 lDir = sphereNode->getPosition() - lPos;
|
---|
78 | lDir.normalise();
|
---|
79 | Vector3 right = lDir.crossProduct(Vector3(0,1,0));
|
---|
80 | Vector3 up = lDir.crossProduct(right);
|
---|
81 | lPos += up,
|
---|
82 | lightNode->setPosition(lPos);
|
---|
83 | light->setDirection(lDir);
|
---|
84 | }
|
---|
85 | if (mInputDevice->isKeyDown(KC_DIVIDE))
|
---|
86 | {
|
---|
87 | Vector3 lPos = lightNode->getPosition();
|
---|
88 | Vector3 lDir = sphereNode->getPosition() - lPos;
|
---|
89 | lDir.normalise();
|
---|
90 | Vector3 right = lDir.crossProduct(Vector3(0,1,0));
|
---|
91 | Vector3 up = lDir.crossProduct(right);
|
---|
92 | lPos -= up,
|
---|
93 | lightNode->setPosition(lPos);
|
---|
94 | light->setDirection(lDir);
|
---|
95 |
|
---|
96 | }
|
---|
97 | if (mInputDevice->isKeyDown(KC_HOME))
|
---|
98 | {
|
---|
99 | Vector3 lPos = lightNode->getPosition();
|
---|
100 | Vector3 lDir = sphereNode->getPosition() - lPos;
|
---|
101 | lDir.normalise();
|
---|
102 | Vector3 right = lDir.crossProduct(Vector3(0,1,0));
|
---|
103 | Vector3 up = lDir.crossProduct(right);
|
---|
104 | lPos += right,
|
---|
105 | lightNode->setPosition(lPos);
|
---|
106 | light->setDirection(lDir);
|
---|
107 | }
|
---|
108 | if (mInputDevice->isKeyDown(KC_END))
|
---|
109 | {
|
---|
110 | Vector3 lPos = lightNode->getPosition();
|
---|
111 | Vector3 lDir = sphereNode->getPosition() - lPos;
|
---|
112 | lDir.normalise();
|
---|
113 | Vector3 right = lDir.crossProduct(Vector3(0,1,0));
|
---|
114 | Vector3 up = lDir.crossProduct(right);
|
---|
115 | lPos -= right,
|
---|
116 | lightNode->setPosition(lPos);
|
---|
117 | light->setDirection(lDir);
|
---|
118 |
|
---|
119 | }
|
---|
120 | return ExampleFrameListener::processUnbufferedKeyInput(evt);
|
---|
121 | }
|
---|
122 | bool frameStarted(const FrameEvent& evt)
|
---|
123 | {
|
---|
124 | static bool first = true;
|
---|
125 |
|
---|
126 |
|
---|
127 | // lightNode->setPosition( 100.0 * Vector3(cos(-(float)framecount/500.0),2.0,sin(-(float)framecount/500.0)));
|
---|
128 |
|
---|
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 |
|
---|
145 | protected:
|
---|
146 | SceneNode* mainLightNode;
|
---|
147 | SceneNode* sphereNode;
|
---|
148 |
|
---|
149 | void chooseSceneManager()
|
---|
150 | {
|
---|
151 | //mSceneMgr = mRoot->createSceneManager("OctreeSceneManager");
|
---|
152 | mSceneMgr = mRoot->createSceneManager("OcclusionCullingSceneManager");
|
---|
153 | }
|
---|
154 |
|
---|
155 | void loadResources(void)
|
---|
156 | {
|
---|
157 |
|
---|
158 | // Initialise, parse scripts etc
|
---|
159 | ResourceGroupManager::getSingleton().initialiseResourceGroup("Bootstrap");
|
---|
160 | ResourceGroupManager::getSingleton().initialiseResourceGroup("General");
|
---|
161 | ResourceGroupManager::getSingleton().initialiseResourceGroup("GTP_Basic");
|
---|
162 | ResourceGroupManager::getSingleton().initialiseResourceGroup("PostProc");
|
---|
163 | ResourceGroupManager::getSingleton().initialiseResourceGroup("GTP_EnvMap");
|
---|
164 | ResourceGroupManager::getSingleton().initialiseResourceGroup("GTP_Caustic");
|
---|
165 | }
|
---|
166 |
|
---|
167 | void createPostproc()
|
---|
168 | {
|
---|
169 | CompositorManager::getSingleton().addCompositor(mWindow->getViewport(0),
|
---|
170 | "GTP/PostProc/Glow");
|
---|
171 | CompositorManager::getSingleton().setCompositorEnabled(mWindow->getViewport(0),
|
---|
172 | "GTP/PostProc/Glow", true);
|
---|
173 |
|
---|
174 | CompositorManager::getSingleton().addCompositor(mWindow->getViewport(0),
|
---|
175 | "GTP/PostProc/ToneMap");
|
---|
176 | CompositorManager::getSingleton().setCompositorEnabled(mWindow->getViewport(0),
|
---|
177 | "GTP/PostProc/ToneMap", true);
|
---|
178 | }
|
---|
179 |
|
---|
180 | Light* light;
|
---|
181 | void createScene(void)
|
---|
182 | {
|
---|
183 | std::string occlusion_cfg("OcclusionCulling.cfg");
|
---|
184 | mSceneMgr->setWorldGeometry(occlusion_cfg);
|
---|
185 |
|
---|
186 | //createPostproc();
|
---|
187 | OgreIlluminationManager::getSingleton().setBlurShadowMap(true);
|
---|
188 | OgreIlluminationManager::getSingleton().setUseLISPSM(false);
|
---|
189 |
|
---|
190 | OgreIlluminationManager::getSingleton().setMainCamera(mCamera);
|
---|
191 | OgreIlluminationManager::getSingleton().setMainViewport(mWindow->getViewport(0));
|
---|
192 |
|
---|
193 | OgreIlluminationManager::getSingleton().setMaxJoinRadius(420);
|
---|
194 |
|
---|
195 | Root::getSingleton()._setCurrentSceneManager(mSceneMgr);
|
---|
196 |
|
---|
197 | mCamera->setPosition(0,100,240);
|
---|
198 | mCamera->setFOVy(Radian(Degree(80)));
|
---|
199 | //mCamera->setFarClipDistance(1000);
|
---|
200 | // Set ambient light
|
---|
201 | mSceneMgr->setAmbientLight(ColourValue(1, 1, 1));
|
---|
202 |
|
---|
203 | // Skybox
|
---|
204 | mSceneMgr->setSkyBox(true, "Examples/EveningSkyBox");
|
---|
205 |
|
---|
206 | SceneNode* rootNode = mSceneMgr->getRootSceneNode();
|
---|
207 |
|
---|
208 | Entity* sphere = mSceneMgr->createEntity("sphere", "head.mesh");
|
---|
209 |
|
---|
210 | // Material* mat = (Material*) MaterialManager::getSingleton().getByName("GTP/Caustic/Glass").getPointer();
|
---|
211 | // Material* newMat = mat->clone(mat->getName() + "_new").getPointer();
|
---|
212 |
|
---|
213 | sphere->setMaterialName("GTP/Caustic/Glass_PointSprite");
|
---|
214 | sphereNode = rootNode->createChildSceneNode();
|
---|
215 | //sphereNode->setScale(0.1, 0.1, 0.1);//sphere
|
---|
216 | sphereNode->setPosition(0, 50, 0);
|
---|
217 | sphereNode->attachObject(sphere);
|
---|
218 | sphereNode->_updateBounds();
|
---|
219 |
|
---|
220 | Entity* room = mSceneMgr->createEntity("room", "atlascube.mesh");
|
---|
221 | // mat = (Material*) MaterialManager::getSingleton().getByName("TestPlane").getPointer();
|
---|
222 | // newMat = mat->clone(mat->getName() + "_new").getPointer();
|
---|
223 |
|
---|
224 | room->setMaterialName("TestPlane");
|
---|
225 | SceneNode *roomNode = rootNode->createChildSceneNode();
|
---|
226 | //roomNode->setScale(0.1, 0.1, 0.1);
|
---|
227 | roomNode->setPosition(0, 50, 0);
|
---|
228 | roomNode->attachObject(room);
|
---|
229 |
|
---|
230 |
|
---|
231 | Light* mainLight = mSceneMgr->createLight("MainLight");
|
---|
232 | light = mainLight;
|
---|
233 | mainLight->setType(Light::LT_POINT);
|
---|
234 | mainLight->setDiffuseColour(ColourValue::White);
|
---|
235 | mainLight->setSpecularColour(ColourValue::White);
|
---|
236 | mainLightNode = rootNode->createChildSceneNode();
|
---|
237 | mainLightNode->setPosition(100,100,0);
|
---|
238 | //mainLight->setPosition(mainLightNode->getWorldPosition());
|
---|
239 | mainLightNode->attachObject(mainLight);
|
---|
240 | BillboardSet* bbs = mSceneMgr->createBillboardSet("bb", 1);
|
---|
241 | bbs->setDefaultDimensions(10,10);
|
---|
242 | bbs->createBillboard(Vector3::ZERO, ColourValue::White);
|
---|
243 | bbs->setMaterialName("Flare");
|
---|
244 | mainLightNode->attachObject(bbs);
|
---|
245 |
|
---|
246 | //createPlane("ground", "TestPlane", Vector3(0,0,0), Vector2(500,500));
|
---|
247 | OgreIlluminationManager::getSingleton().initTechniques();
|
---|
248 | }
|
---|
249 | void createPlane(Ogre::String _name, Ogre::String _material, Ogre::Vector3 _pos, Ogre::Vector2 _size, Ogre::Vector2 _subdivisions = Ogre::Vector2(1,1)) {
|
---|
250 |
|
---|
251 | Plane _plane;
|
---|
252 | _plane.normal = Vector3::UNIT_Y;
|
---|
253 | _plane.d = 0;
|
---|
254 |
|
---|
255 |
|
---|
256 | MeshManager::getSingleton().createPlane(_name + ".plane",
|
---|
257 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
|
---|
258 | _plane,_size.x,_size.y,1,1,true,1,_subdivisions.x,_subdivisions.y,Vector3::UNIT_Z);
|
---|
259 |
|
---|
260 | Entity *_entity = mSceneMgr->createEntity(_name + ".entity", _name + ".plane");
|
---|
261 | _entity->setMaterialName(_material);
|
---|
262 |
|
---|
263 | SceneNode* _node = mSceneMgr->getRootSceneNode()->createChildSceneNode(_name + ".node");
|
---|
264 | _node->attachObject(_entity);
|
---|
265 | _node->setPosition(_pos);
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 |
|
---|
270 | void createFrameListener(void)
|
---|
271 | {
|
---|
272 | // This is where we instantiate our own frame listener
|
---|
273 | mFrameListener= new RaytraceDemoListener(mWindow,
|
---|
274 | mCamera,
|
---|
275 | mainLightNode,
|
---|
276 | sphereNode,
|
---|
277 | light
|
---|
278 | );
|
---|
279 | mFrameListener->setPriority(1);
|
---|
280 | OgreIlluminationManager::getSingleton().setPriority(2);
|
---|
281 | mRoot->addFrameListener(&OgreIlluminationManager::getSingleton());
|
---|
282 | mRoot->addFrameListener(mFrameListener);
|
---|
283 |
|
---|
284 | }
|
---|
285 |
|
---|
286 | };
|
---|
287 |
|
---|