/src/ogre/OgreMain/src/OgreSkyRenderer.cpp
Line | Count | Source |
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-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | |
29 | | #include <memory> |
30 | | |
31 | | #include "OgreStableHeaders.h" |
32 | | |
33 | | #include "OgreViewport.h" |
34 | | |
35 | | namespace Ogre { |
36 | | SceneManager::SkyRenderer::SkyRenderer(SceneManager* owner) |
37 | 0 | : mSceneManager(owner), mSceneNode(0), mEnabled(false) |
38 | 0 | { |
39 | 0 | } |
40 | | |
41 | | SceneManager::SkyRenderer::~SkyRenderer() |
42 | 0 | { |
43 | 0 | setEnabled(false); |
44 | 0 | if(mSceneNode) |
45 | 0 | mSceneManager->destroySceneNode(mSceneNode); |
46 | 0 | } |
47 | | |
48 | | void SceneManager::SkyRenderer::setEnabled(bool enable) |
49 | 0 | { |
50 | 0 | if(enable == mEnabled) return; |
51 | 0 | mEnabled = enable; |
52 | 0 | enable ? mSceneManager->addListener(this) : mSceneManager->removeListener(this); |
53 | 0 | } |
54 | | |
55 | | void SceneManager::SkyPlaneRenderer::create( |
56 | | const Plane& plane, |
57 | | const String& materialName, |
58 | | Real gscale, |
59 | | Real tiling, |
60 | | uint8 renderQueue, |
61 | | Real bow, |
62 | | int xsegments, int ysegments, |
63 | | const String& groupName) |
64 | 0 | { |
65 | 0 | String meshName = mSceneManager->mName + "SkyPlane"; |
66 | 0 | mSkyPlane = plane; |
67 | |
|
68 | 0 | MaterialPtr m = MaterialManager::getSingleton().getByName(materialName, groupName); |
69 | 0 | if (!m) |
70 | 0 | { |
71 | 0 | OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, |
72 | 0 | "Sky plane material '" + materialName + "' not found.", |
73 | 0 | "SceneManager::setSkyPlane"); |
74 | 0 | } |
75 | | // Make sure the material doesn't update the depth buffer |
76 | 0 | m->setDepthWriteEnabled(false); |
77 | | // Ensure loaded |
78 | 0 | m->load(); |
79 | | |
80 | | // Set up the plane |
81 | 0 | MeshPtr planeMesh = MeshManager::getSingleton().getByName(meshName, groupName); |
82 | 0 | if (planeMesh) |
83 | 0 | { |
84 | | // Destroy the old one |
85 | 0 | MeshManager::getSingleton().remove(planeMesh); |
86 | 0 | } |
87 | | |
88 | | // Create up vector |
89 | 0 | Vector3 up = plane.normal.crossProduct(Vector3::UNIT_X); |
90 | 0 | if (up == Vector3::ZERO) |
91 | 0 | up = plane.normal.crossProduct(-Vector3::UNIT_Z); |
92 | | |
93 | | // Create skyplane |
94 | 0 | if( bow > 0 ) |
95 | 0 | { |
96 | | // Build a curved skyplane |
97 | 0 | planeMesh = MeshManager::getSingleton().createCurvedPlane( |
98 | 0 | meshName, groupName, plane, gscale * 100, gscale * 100, gscale * bow * 100, |
99 | 0 | xsegments, ysegments, false, 1, tiling, tiling, up); |
100 | 0 | } |
101 | 0 | else |
102 | 0 | { |
103 | 0 | planeMesh = MeshManager::getSingleton().createPlane( |
104 | 0 | meshName, groupName, plane, gscale * 100, gscale * 100, xsegments, ysegments, false, |
105 | 0 | 1, tiling, tiling, up); |
106 | 0 | } |
107 | | |
108 | | // Create entity |
109 | | // Create, use the same name for mesh and entity |
110 | | // manually construct as we don't want this to be destroyed on destroyAllMovableObjects |
111 | 0 | MovableObjectFactory* factory = Root::getSingleton().getMovableObjectFactory(MOT_ENTITY); |
112 | 0 | NameValuePairList params; |
113 | 0 | params["mesh"] = meshName; |
114 | 0 | mSkyPlaneEntity = static_cast<Entity*>(factory->createInstance(meshName, mSceneManager, ¶ms)); |
115 | 0 | mSkyPlaneEntity->setMaterialName(materialName, groupName); |
116 | 0 | mSkyPlaneEntity->setCastShadows(false); |
117 | 0 | mSkyPlaneEntity->setRenderQueueGroup(renderQueue); |
118 | |
|
119 | 0 | MovableObjectCollection* objectMap = mSceneManager->getMovableObjectCollection(MOT_ENTITY); |
120 | 0 | objectMap->map[meshName] = mSkyPlaneEntity; |
121 | | |
122 | | // Create node and attach |
123 | 0 | mSceneNode = mSceneManager->createSceneNode(); |
124 | 0 | mSceneNode->attachObject(mSkyPlaneEntity); |
125 | |
|
126 | 0 | mSkyPlaneGenParameters.skyPlaneBow = bow; |
127 | 0 | mSkyPlaneGenParameters.skyPlaneScale = gscale; |
128 | 0 | mSkyPlaneGenParameters.skyPlaneTiling = tiling; |
129 | 0 | mSkyPlaneGenParameters.skyPlaneXSegments = xsegments; |
130 | 0 | mSkyPlaneGenParameters.skyPlaneYSegments = ysegments; |
131 | 0 | } |
132 | | |
133 | | void SceneManager::SkyBoxRenderer::create( |
134 | | const String& materialName, |
135 | | Real distance, |
136 | | uint8 renderQueue, |
137 | | const Quaternion& orientation, |
138 | | const String& groupName) |
139 | 0 | { |
140 | 0 | MaterialPtr m = MaterialManager::getSingleton().getByName(materialName, groupName); |
141 | 0 | if (!m) |
142 | 0 | { |
143 | 0 | OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, |
144 | 0 | "Sky box material '" + materialName + "' not found.", |
145 | 0 | "SceneManager::setSkyBox"); |
146 | 0 | } |
147 | | // Make sure the material doesn't update the depth buffer |
148 | 0 | m->setDepthWriteEnabled(false); |
149 | | // Ensure loaded |
150 | 0 | m->load(); |
151 | |
|
152 | 0 | bool valid = m->getBestTechnique() && m->getBestTechnique()->getNumPasses(); |
153 | 0 | if(valid) |
154 | 0 | { |
155 | 0 | Pass* pass = m->getBestTechnique()->getPass(0); |
156 | 0 | valid = valid && pass->getNumTextureUnitStates() && |
157 | 0 | pass->getTextureUnitState(0)->getTextureType() == TEX_TYPE_CUBE_MAP; |
158 | 0 | } |
159 | |
|
160 | 0 | if (!valid) |
161 | 0 | { |
162 | 0 | LogManager::getSingleton().logWarning("skybox material " + materialName + |
163 | 0 | " is not supported, defaulting"); |
164 | 0 | m = MaterialManager::getSingleton().getDefaultSettings(); |
165 | 0 | } |
166 | | |
167 | | // Create node |
168 | 0 | mSceneNode = mSceneManager->createSceneNode(); |
169 | | |
170 | | // Create object |
171 | 0 | mSkyBoxObj = std::make_unique<ManualObject>("SkyBox"); |
172 | 0 | mSkyBoxObj->setCastShadows(false); |
173 | 0 | mSceneNode->attachObject(mSkyBoxObj.get()); |
174 | |
|
175 | 0 | mSkyBoxObj->setRenderQueueGroup(renderQueue); |
176 | 0 | mSkyBoxObj->begin(materialName, RenderOperation::OT_TRIANGLE_STRIP, groupName); |
177 | | |
178 | | // rendering cube, only using 14 vertices |
179 | 0 | const Vector3 cube_strip[14] = { |
180 | 0 | {-1.f, 1.f, 1.f}, // Front-top-left |
181 | 0 | {1.f, 1.f, 1.f}, // Front-top-right |
182 | 0 | {-1.f, -1.f, 1.f}, // Front-bottom-left |
183 | 0 | {1.f, -1.f, 1.f}, // Front-bottom-right |
184 | 0 | {1.f, -1.f, -1.f}, // Back-bottom-right |
185 | 0 | {1.f, 1.f, 1.f}, // Front-top-right |
186 | 0 | {1.f, 1.f, -1.f}, // Back-top-right |
187 | 0 | {-1.f, 1.f, 1.f}, // Front-top-left |
188 | 0 | {-1.f, 1.f, -1.f}, // Back-top-left |
189 | 0 | {-1.f, -1.f, 1.f}, // Front-bottom-left |
190 | 0 | {-1.f, -1.f, -1.f}, // Back-bottom-left |
191 | 0 | {1.f, -1.f, -1.f}, // Back-bottom-right |
192 | 0 | {-1.f, 1.f, -1.f}, // Back-top-left |
193 | 0 | {1.f, 1.f, -1.f} // Back-top-right |
194 | 0 | }; |
195 | |
|
196 | 0 | for (const auto& vtx : cube_strip) |
197 | 0 | { |
198 | 0 | mSkyBoxObj->position(orientation * (vtx * distance)); |
199 | | // Note UVs mirrored front/back |
200 | 0 | mSkyBoxObj->textureCoord(vtx.normalisedCopy() * Vector3(1, 1, -1)); |
201 | 0 | } |
202 | |
|
203 | 0 | mSkyBoxObj->end(); |
204 | |
|
205 | 0 | mSkyBoxGenParameters.skyBoxDistance = distance; |
206 | 0 | } |
207 | | |
208 | | void SceneManager::SkyDomeRenderer::create( |
209 | | const String& materialName, |
210 | | Real curvature, |
211 | | Real tiling, |
212 | | Real distance, |
213 | | uint8 renderQueue, |
214 | | const Quaternion& orientation, |
215 | | int xsegments, int ysegments, int ySegmentsToKeep, |
216 | | const String& groupName) |
217 | 0 | { |
218 | 0 | MaterialPtr m = MaterialManager::getSingleton().getByName(materialName, groupName); |
219 | 0 | if (!m) |
220 | 0 | { |
221 | 0 | OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, |
222 | 0 | "Sky dome material '" + materialName + "' not found.", |
223 | 0 | "SceneManager::setSkyDome"); |
224 | 0 | } |
225 | | // Make sure the material doesn't update the depth buffer |
226 | 0 | m->setDepthWriteEnabled(false); |
227 | | // Ensure loaded |
228 | 0 | m->load(); |
229 | | |
230 | | // Create node |
231 | 0 | mSceneNode = mSceneManager->createSceneNode(); |
232 | | |
233 | | // Set up the dome (5 planes) |
234 | 0 | for (int i = 0; i < 5; ++i) |
235 | 0 | { |
236 | 0 | MeshPtr planeMesh = createSkydomePlane((BoxPlane)i, curvature, |
237 | 0 | tiling, distance, orientation, xsegments, ysegments, |
238 | 0 | i!=BP_UP ? ySegmentsToKeep : -1, groupName); |
239 | |
|
240 | 0 | String entName = "SkyDomePlane" + StringConverter::toString(i); |
241 | | |
242 | | // Create entity |
243 | 0 | if (mSkyDomeEntity[i]) |
244 | 0 | { |
245 | | // destroy old one, do it by name for speed |
246 | 0 | mSceneManager->destroyEntity(entName); |
247 | 0 | mSkyDomeEntity[i] = 0; |
248 | 0 | } |
249 | | // construct manually so we don't have problems if destroyAllMovableObjects called |
250 | 0 | MovableObjectFactory* factory = Root::getSingleton().getMovableObjectFactory(MOT_ENTITY); |
251 | |
|
252 | 0 | NameValuePairList params; |
253 | 0 | params["mesh"] = planeMesh->getName(); |
254 | 0 | mSkyDomeEntity[i] = static_cast<Entity*>(factory->createInstance(entName, mSceneManager, ¶ms)); |
255 | 0 | mSkyDomeEntity[i]->setMaterialName(m->getName(), groupName); |
256 | 0 | mSkyDomeEntity[i]->setCastShadows(false); |
257 | 0 | mSkyDomeEntity[i]->setRenderQueueGroup(renderQueue); |
258 | | |
259 | |
|
260 | 0 | MovableObjectCollection* objectMap = mSceneManager->getMovableObjectCollection(MOT_ENTITY); |
261 | 0 | objectMap->map[entName] = mSkyDomeEntity[i]; |
262 | | |
263 | | // Attach to node |
264 | 0 | mSceneNode->attachObject(mSkyDomeEntity[i]); |
265 | 0 | } // for each plane |
266 | |
|
267 | 0 | mSkyDomeGenParameters.skyDomeCurvature = curvature; |
268 | 0 | mSkyDomeGenParameters.skyDomeDistance = distance; |
269 | 0 | mSkyDomeGenParameters.skyDomeTiling = tiling; |
270 | 0 | mSkyDomeGenParameters.skyDomeXSegments = xsegments; |
271 | 0 | mSkyDomeGenParameters.skyDomeYSegments = ysegments; |
272 | 0 | mSkyDomeGenParameters.skyDomeYSegments_keep = ySegmentsToKeep; |
273 | 0 | } |
274 | | |
275 | | MeshPtr SceneManager::SkyDomeRenderer::createSkydomePlane( |
276 | | BoxPlane bp, |
277 | | Real curvature, |
278 | | Real tiling, |
279 | | Real distance, |
280 | | const Quaternion& orientation, |
281 | | int xsegments, int ysegments, int ysegments_keep, |
282 | | const String& groupName) |
283 | 0 | { |
284 | |
|
285 | 0 | Plane plane; |
286 | 0 | String meshName; |
287 | 0 | Vector3 up; |
288 | |
|
289 | 0 | meshName = mSceneManager->mName + "SkyDomePlane_"; |
290 | | // Set up plane equation |
291 | 0 | plane.d = distance; |
292 | 0 | switch(bp) |
293 | 0 | { |
294 | 0 | case BP_FRONT: |
295 | 0 | plane.normal = Vector3::UNIT_Z; |
296 | 0 | up = Vector3::UNIT_Y; |
297 | 0 | meshName += "Front"; |
298 | 0 | break; |
299 | 0 | case BP_BACK: |
300 | 0 | plane.normal = -Vector3::UNIT_Z; |
301 | 0 | up = Vector3::UNIT_Y; |
302 | 0 | meshName += "Back"; |
303 | 0 | break; |
304 | 0 | case BP_LEFT: |
305 | 0 | plane.normal = Vector3::UNIT_X; |
306 | 0 | up = Vector3::UNIT_Y; |
307 | 0 | meshName += "Left"; |
308 | 0 | break; |
309 | 0 | case BP_RIGHT: |
310 | 0 | plane.normal = -Vector3::UNIT_X; |
311 | 0 | up = Vector3::UNIT_Y; |
312 | 0 | meshName += "Right"; |
313 | 0 | break; |
314 | 0 | case BP_UP: |
315 | 0 | plane.normal = -Vector3::UNIT_Y; |
316 | 0 | up = Vector3::UNIT_Z; |
317 | 0 | meshName += "Up"; |
318 | 0 | break; |
319 | 0 | case BP_DOWN: |
320 | | // no down |
321 | 0 | return MeshPtr(); |
322 | 0 | } |
323 | | // Modify by orientation |
324 | 0 | plane.normal = orientation * plane.normal; |
325 | 0 | up = orientation * up; |
326 | | |
327 | | // Check to see if existing plane |
328 | 0 | MeshManager& mm = MeshManager::getSingleton(); |
329 | 0 | MeshPtr planeMesh = mm.getByName(meshName, groupName); |
330 | 0 | if(planeMesh) |
331 | 0 | { |
332 | | // destroy existing |
333 | 0 | mm.remove(planeMesh->getHandle()); |
334 | 0 | } |
335 | | // Create new |
336 | 0 | Real planeSize = distance * 2; |
337 | 0 | planeMesh = mm.createCurvedIllusionPlane(meshName, groupName, plane, |
338 | 0 | planeSize, planeSize, curvature, |
339 | 0 | xsegments, ysegments, false, 1, tiling, tiling, up, |
340 | 0 | orientation, HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY, HardwareBuffer::HBU_STATIC_WRITE_ONLY, |
341 | 0 | false, false, ysegments_keep); |
342 | | |
343 | | //planeMesh->_dumpContents(meshName); |
344 | |
|
345 | 0 | return planeMesh; |
346 | |
|
347 | 0 | } |
348 | | |
349 | | void SceneManager::SkyPlaneRenderer::_updateRenderQueue(RenderQueue* queue) |
350 | 0 | { |
351 | 0 | if (mSkyPlaneEntity->isVisible()) |
352 | 0 | { |
353 | 0 | mSkyPlaneEntity->_updateRenderQueue(queue); |
354 | 0 | } |
355 | 0 | } |
356 | | |
357 | | void SceneManager::SkyBoxRenderer::_updateRenderQueue(RenderQueue* queue) |
358 | 0 | { |
359 | 0 | if (mSkyBoxObj->isVisible()) |
360 | 0 | { |
361 | 0 | mSkyBoxObj->_updateRenderQueue(queue); |
362 | 0 | } |
363 | 0 | } |
364 | | |
365 | | void SceneManager::SkyDomeRenderer::_updateRenderQueue(RenderQueue* queue) |
366 | 0 | { |
367 | 0 | for (uint plane = 0; plane < 5; ++plane) |
368 | 0 | { |
369 | 0 | if (!mSkyDomeEntity[plane]->isVisible()) continue; |
370 | | |
371 | 0 | mSkyDomeEntity[plane]->_updateRenderQueue(queue); |
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | | void SceneManager::SkyRenderer::postFindVisibleObjects(SceneManager* source, IlluminationRenderStage irs, |
376 | | Viewport* vp) |
377 | 0 | { |
378 | | // Queue skies, if viewport seems it |
379 | 0 | if (!vp->getSkiesEnabled() || irs == IRS_RENDER_TO_TEXTURE) |
380 | 0 | return; |
381 | | |
382 | 0 | if(!mEnabled || !mSceneNode) |
383 | 0 | return; |
384 | | |
385 | | // Update nodes |
386 | | // Translate the box by the camera position (constant distance) |
387 | 0 | mSceneNode->setPosition(vp->getCamera()->getDerivedPosition()); |
388 | 0 | _updateRenderQueue(source->getRenderQueue()); |
389 | 0 | } |
390 | | } |