1 | #include "OgreRenderingrun.h"
|
---|
2 | #include "OgreIlluminationManager.h"
|
---|
3 |
|
---|
4 | MovablePlane* OgreRenderingRun::fullScreenQuad = NULL;
|
---|
5 | Entity* OgreRenderingRun::fullScreenQuadEntity = NULL;
|
---|
6 | SceneNode* OgreRenderingRun::fullScreenQuadNode = NULL;
|
---|
7 | BillboardSet* OgreRenderingRun::pixelSprites = NULL;
|
---|
8 | Entity* OgreRenderingRun::fullscreenGrid = NULL;
|
---|
9 | String OgreRenderingRun::spriteSetName = "";
|
---|
10 |
|
---|
11 | class MaterialSetVisitor : public QueuedRenderableVisitor
|
---|
12 | {
|
---|
13 | private:
|
---|
14 | String materialName;
|
---|
15 | String triggerName;
|
---|
16 | std::map<Renderable*, String>* visibleObjects;
|
---|
17 | public:
|
---|
18 |
|
---|
19 | MaterialSetVisitor(String& materialName, std::map<Renderable*, String>* visibleObjects, String trigger = "")
|
---|
20 | {
|
---|
21 | this->materialName = materialName;
|
---|
22 | this->visibleObjects = visibleObjects;
|
---|
23 | }
|
---|
24 |
|
---|
25 | void visit(const Renderable* r)
|
---|
26 | {
|
---|
27 | Renderable* rend = (Renderable*) r; //sorry for removing the const
|
---|
28 |
|
---|
29 | if( visibleObjects->count(rend) == 0)
|
---|
30 | {
|
---|
31 | (*visibleObjects)[rend] = rend->getMaterial().getPointer()->getName();
|
---|
32 | String materialToSet = materialName;
|
---|
33 | if(triggerName != "")//we have a trigger here
|
---|
34 | {
|
---|
35 | String triggeredMaterial =
|
---|
36 | ((OgreTechniqueGroup*)rend->getRenderTechniqueGroup())
|
---|
37 | ->getMaterialNameForTrigger(triggerName);
|
---|
38 | if(triggeredMaterial != "")//a trigger is associated
|
---|
39 | materialToSet = triggeredMaterial;
|
---|
40 | }
|
---|
41 | rend->setMaterialName(materialToSet);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | bool visit(const Pass* p){return true;}
|
---|
45 | void visit(const RenderablePass* rp)
|
---|
46 | {
|
---|
47 | Renderable* rend = rp->renderable;
|
---|
48 |
|
---|
49 | if( visibleObjects->count(rend) == 0)
|
---|
50 | {
|
---|
51 | (*visibleObjects)[rend] = rend->getMaterial().getPointer()->getName();
|
---|
52 | String materialToSet = materialName;
|
---|
53 | if(triggerName != "")//we have a trigger here
|
---|
54 | {
|
---|
55 | String triggeredMaterial =
|
---|
56 | ((OgreTechniqueGroup*)rend->getRenderTechniqueGroup())
|
---|
57 | ->getMaterialNameForTrigger(triggerName);
|
---|
58 | if(triggeredMaterial != "")//a trigger is associated
|
---|
59 | materialToSet = triggeredMaterial;
|
---|
60 | }
|
---|
61 | rend->setMaterialName(materialToSet);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | };
|
---|
66 |
|
---|
67 | Vector3 OgreRenderingRun::getCubeMapFaceDirection(unsigned char faceId)
|
---|
68 | {
|
---|
69 | switch(faceId)
|
---|
70 | {
|
---|
71 | case 0: //positive X
|
---|
72 | return Vector3(1,0,0);
|
---|
73 | case 1: //negative X
|
---|
74 | return Vector3(-1,0,0);
|
---|
75 | case 2: //positiove Y
|
---|
76 | return Vector3(0,1,0);
|
---|
77 | case 3: //negative Y
|
---|
78 | return Vector3(0,-1,0);
|
---|
79 | case 4: //positiove Z
|
---|
80 | return Vector3(0,0,-1);
|
---|
81 | case 5: //negative Z
|
---|
82 | return Vector3(0,0,1);
|
---|
83 | default:
|
---|
84 | return Vector3(0,0,0);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | Texture* OgreRenderingRun::createCubeRenderTexture(String name, const Vector3 position, unsigned int resolution, PixelFormat format, int numMips, ColourValue clearColor)
|
---|
89 | {
|
---|
90 | //create texture
|
---|
91 | TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual( name,
|
---|
92 | "default",
|
---|
93 | TEX_TYPE_CUBE_MAP,
|
---|
94 | resolution,
|
---|
95 | resolution,
|
---|
96 | 0,
|
---|
97 | numMips,
|
---|
98 | format,
|
---|
99 | TU_RENDERTARGET);
|
---|
100 | Texture* result = texPtr.getPointer();
|
---|
101 |
|
---|
102 | //add viewports to faces
|
---|
103 | for(unsigned int i = 0; i < 6; i++)
|
---|
104 | {
|
---|
105 | Camera* cam = Root::getSingleton()._getCurrentSceneManager()
|
---|
106 | ->createCamera(name + "CUBEMAPFACE_" + StringConverter::toString(i) + "_CAMERA");
|
---|
107 |
|
---|
108 | cam->setPosition(position);
|
---|
109 | cam->setAspectRatio(1);
|
---|
110 | cam->setFOVy(Radian(Degree(90)));
|
---|
111 | cam->setNearClipDistance(0.1);
|
---|
112 | cam->setFarClipDistance(OgreIlluminationManager::getSingleton().
|
---|
113 | getMainCamera()->getFarClipDistance());
|
---|
114 |
|
---|
115 | //set camera orientation
|
---|
116 | switch(i)
|
---|
117 | {
|
---|
118 | case 2:
|
---|
119 | cam->setOrientation(Quaternion(Radian(Degree(90)),Vector3(1,0,0)));
|
---|
120 | break;
|
---|
121 | case 3:
|
---|
122 | cam->setOrientation(Quaternion(Radian(Degree(90)),Vector3(-1,0,0)));
|
---|
123 | break;
|
---|
124 | default:
|
---|
125 | cam->setDirection(getCubeMapFaceDirection(i));
|
---|
126 | break;
|
---|
127 | }
|
---|
128 |
|
---|
129 | HardwarePixelBuffer* hpb = (result->getBuffer(i, 0)).getPointer();
|
---|
130 | RenderTarget* rt = hpb->getRenderTarget(0);
|
---|
131 | Viewport* v = rt->addViewport(cam);
|
---|
132 | v->setOverlaysEnabled(false);
|
---|
133 | rt->setAutoUpdated(false);
|
---|
134 | v->setBackgroundColour(clearColor);
|
---|
135 | Root::getSingleton()._getCurrentSceneManager()->getRenderQueue()->clear();
|
---|
136 | Root::getSingleton()._getCurrentSceneManager()->setFindVisibleObjects(false);
|
---|
137 | rt->update();
|
---|
138 | Root::getSingleton()._getCurrentSceneManager()->setFindVisibleObjects(true);
|
---|
139 |
|
---|
140 | Root::getSingleton().getRenderSystem()->_setViewport(v);
|
---|
141 | Root::getSingleton().getRenderSystem()->clearFrameBuffer( FBT_COLOUR, clearColor);
|
---|
142 | }
|
---|
143 |
|
---|
144 | return result;
|
---|
145 |
|
---|
146 | }
|
---|
147 |
|
---|
148 | void OgreRenderingRun::setMaterialForRenderables(String& materialName, RenderQueue* rq, bool solidsonly, String triggerName)
|
---|
149 | {
|
---|
150 | SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
|
---|
151 |
|
---|
152 | RenderQueue::QueueGroupIterator queueIt = rq->_getQueueGroupIterator();
|
---|
153 |
|
---|
154 | while (queueIt.hasMoreElements())
|
---|
155 | {
|
---|
156 | RenderQueueGroup* pGroup = queueIt.getNext();
|
---|
157 |
|
---|
158 | RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
|
---|
159 |
|
---|
160 | while (groupIt.hasMoreElements())
|
---|
161 | {
|
---|
162 | RenderPriorityGroup* pPriorityGrp = groupIt.getNext();
|
---|
163 | const QueuedRenderableCollection& solids = pPriorityGrp->getSolidsBasic();
|
---|
164 | const QueuedRenderableCollection& transparents = pPriorityGrp->getTransparents();
|
---|
165 | MaterialSetVisitor visitor(materialName, &visibleObjects, triggerName);
|
---|
166 | solids.acceptVisitor(&visitor, QueuedRenderableCollection::OM_PASS_GROUP);
|
---|
167 | if(!solidsonly)
|
---|
168 | transparents.acceptVisitor(&visitor, QueuedRenderableCollection::OM_SORT_ASCENDING);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | //refill renderqueue
|
---|
173 | rq->clear();
|
---|
174 |
|
---|
175 | std::map<Renderable*, String>::iterator it = visibleObjects.begin();
|
---|
176 | std::map<Renderable*, String>::iterator itend = visibleObjects.end();
|
---|
177 |
|
---|
178 | while(it != itend)
|
---|
179 | {
|
---|
180 | rq->addRenderable((*it).first);
|
---|
181 | it++;
|
---|
182 | }
|
---|
183 |
|
---|
184 | sm->setFindVisibleObjects(false);
|
---|
185 | }
|
---|
186 |
|
---|
187 | void OgreRenderingRun::setMaterialForVisibles(String& materialName, Camera* cam, bool shadowcastersonly, bool solidsonly, String triggerName)
|
---|
188 | {
|
---|
189 | SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
|
---|
190 |
|
---|
191 | RenderQueue* rq = sm->getRenderQueue();
|
---|
192 | rq->clear();
|
---|
193 |
|
---|
194 | RenderQueue::QueueGroupIterator groupIter = rq->_getQueueGroupIterator();
|
---|
195 | while (groupIter.hasMoreElements())
|
---|
196 | {
|
---|
197 | RenderQueueGroup* g = groupIter.getNext();
|
---|
198 | g->defaultOrganisationMode();
|
---|
199 | }
|
---|
200 |
|
---|
201 | //sm->_findVisibleObjectsOC(cam, cam->getViewport(), false, false, shadowcastersonly);
|
---|
202 | sm->_findVisibleObjects(cam, shadowcastersonly);
|
---|
203 |
|
---|
204 | setMaterialForRenderables(materialName, sm->getRenderQueue(), solidsonly, triggerName);
|
---|
205 | }
|
---|
206 |
|
---|
207 | void OgreRenderingRun::restoreMaterials()
|
---|
208 | {
|
---|
209 | SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
|
---|
210 |
|
---|
211 | std::map<Renderable*, String>::iterator it = visibleObjects.begin();
|
---|
212 | std::map<Renderable*, String>::iterator itend = visibleObjects.end();
|
---|
213 |
|
---|
214 | while(it != itend)
|
---|
215 | {
|
---|
216 | ((*it).first)->setMaterialName((*it).second);
|
---|
217 | it++;
|
---|
218 | }
|
---|
219 |
|
---|
220 | visibleObjects.clear();
|
---|
221 |
|
---|
222 | sm->setFindVisibleObjects(true);
|
---|
223 | }
|
---|
224 |
|
---|
225 | void OgreRenderingRun::renderFullscreenQuad(String materialName, RenderTarget* target)
|
---|
226 | {
|
---|
227 | SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
|
---|
228 |
|
---|
229 | if(fullScreenQuad == NULL)
|
---|
230 | {
|
---|
231 | fullScreenQuad = new MovablePlane("PASS_FULLSCREENQUAD_PLANE");
|
---|
232 | fullScreenQuad->d = 0;
|
---|
233 | fullScreenQuad->normal = Vector3::UNIT_Z;
|
---|
234 | MeshManager::getSingleton().createPlane("PASS_FULLSCREENQUAD_MESH",
|
---|
235 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
|
---|
236 | *fullScreenQuad, 2, 2,
|
---|
237 | 1, 1, true, 1, 1, 1, Vector3::UNIT_Y);
|
---|
238 | fullScreenQuadEntity = sm->createEntity( "PASS_FULLSCREENQUAD_ENTITY", "PASS_FULLSCREENQUAD_MESH" );
|
---|
239 | sm->getRootSceneNode()->attachObject(fullScreenQuadEntity);
|
---|
240 | fullScreenQuadEntity->setVisible(false);
|
---|
241 | }
|
---|
242 |
|
---|
243 | fullScreenQuadEntity->setMaterialName(materialName);
|
---|
244 | sm->getRenderQueue()->clear();
|
---|
245 | sm->getRenderQueue()->addRenderable(fullScreenQuadEntity->getSubEntity(0));
|
---|
246 | sm->setFindVisibleObjects(false);
|
---|
247 | target->update();
|
---|
248 | sm->setFindVisibleObjects(true);
|
---|
249 | }
|
---|
250 |
|
---|
251 | void OgreRenderingRun::renderFullscreenGrid(String& materialName, RenderTarget* rt, int width, int height)
|
---|
252 | {
|
---|
253 | SceneManager* sm = Root::getSingleton()._getCurrentSceneManager();
|
---|
254 |
|
---|
255 | if(fullscreenGrid == NULL)
|
---|
256 | {
|
---|
257 | MeshPtr mesh = MeshManager::getSingleton().createManual("FullScreenGrid",ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
258 | SubMesh* subMesh = mesh->createSubMesh();
|
---|
259 |
|
---|
260 | mesh->sharedVertexData = new VertexData();
|
---|
261 | VertexData* vertexData = mesh->sharedVertexData;
|
---|
262 |
|
---|
263 | // define the vertex format
|
---|
264 | VertexDeclaration* vertexDecl = vertexData->vertexDeclaration;
|
---|
265 | size_t currOffset = 0;
|
---|
266 | // positions
|
---|
267 | vertexDecl->addElement(0, currOffset, VET_FLOAT3, VES_POSITION);
|
---|
268 | currOffset += VertexElement::getTypeSize(VET_FLOAT3);
|
---|
269 |
|
---|
270 | // allocate the vertex buffer
|
---|
271 | vertexData->vertexCount = width * height;
|
---|
272 | HardwareVertexBufferSharedPtr vBuf = HardwareBufferManager::getSingleton().createVertexBuffer(vertexDecl->getVertexSize(0), vertexData->vertexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);
|
---|
273 | VertexBufferBinding* binding = vertexData->vertexBufferBinding;
|
---|
274 | binding->setBinding(0, vBuf);
|
---|
275 | float* pVertex = static_cast<float*>(vBuf->lock(HardwareBuffer::HBL_DISCARD));
|
---|
276 |
|
---|
277 | // allocate index buffer
|
---|
278 | subMesh->indexData->indexCount = 6 * (width - 1) * (height - 1);
|
---|
279 | subMesh->indexData->indexBuffer = HardwareBufferManager::getSingleton().createIndexBuffer(HardwareIndexBuffer::IT_16BIT, subMesh->indexData->indexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);
|
---|
280 | HardwareIndexBufferSharedPtr iBuf = subMesh->indexData->indexBuffer;
|
---|
281 | unsigned short* pIndices = static_cast<unsigned short*>(iBuf->lock(HardwareBuffer::HBL_DISCARD));
|
---|
282 |
|
---|
283 | unsigned short wVerticeIndex = 0 ;
|
---|
284 |
|
---|
285 |
|
---|
286 | for( int j = 0; j < height; j++ )
|
---|
287 | {
|
---|
288 | for( int i = 0; i < width; i++ )
|
---|
289 | { //positions
|
---|
290 | float x = ((float)i + 0.5) / (float) width;
|
---|
291 | float y = ((float)j + 0.5) / (float) height;
|
---|
292 | *pVertex++ = x;
|
---|
293 | *pVertex++ = y;
|
---|
294 | *pVertex++ = 0;
|
---|
295 |
|
---|
296 | //indexes
|
---|
297 | if( j < height - 1 && i < width -1 )
|
---|
298 | {
|
---|
299 | *pIndices++ = j * width + i;
|
---|
300 | *pIndices++ = (j + 1) * width + i;
|
---|
301 | *pIndices++ = j * width + i + 1;
|
---|
302 | *pIndices++ = j * width + i + 1;
|
---|
303 | *pIndices++ = (j + 1) * width + i;
|
---|
304 | *pIndices++ = (j + 1) * width + i + 1;
|
---|
305 | }
|
---|
306 |
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | // Unlock
|
---|
311 | vBuf->unlock();
|
---|
312 | iBuf->unlock();
|
---|
313 | // Generate face list
|
---|
314 | subMesh->useSharedVertices = true;
|
---|
315 |
|
---|
316 | // this line makes clear the mesh is loaded (avoids memory leaks)
|
---|
317 | mesh->load();
|
---|
318 |
|
---|
319 | fullscreenGrid = sm->createEntity("FullScreenGridEntity", "FullScreenGrid");
|
---|
320 | SceneNode* n = sm->getRootSceneNode()->createChildSceneNode("FullScreenGridNode");
|
---|
321 | n->attachObject(fullscreenGrid);
|
---|
322 | fullscreenGrid->setVisible(false);
|
---|
323 | }
|
---|
324 |
|
---|
325 | fullscreenGrid->setMaterialName(materialName);
|
---|
326 |
|
---|
327 | sm->getRenderQueue()->clear();
|
---|
328 | sm->getRenderQueue()->addRenderable(fullscreenGrid->getSubEntity(0));
|
---|
329 | sm->setFindVisibleObjects(false);
|
---|
330 | rt->update();
|
---|
331 | sm->setFindVisibleObjects(true);
|
---|
332 |
|
---|
333 | }
|
---|
334 |
|
---|
335 | void OgreRenderingRun::renderPixelSprites(String& materialName, RenderTarget* rt, int width, int height)
|
---|
336 | {
|
---|
337 | SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
|
---|
338 |
|
---|
339 | if(pixelSprites == 0)
|
---|
340 | {
|
---|
341 | int resolutionX = width;
|
---|
342 | int resolutionY = height;
|
---|
343 | pixelSprites = new BillboardSet(spriteSetName, resolutionX * resolutionY, true);
|
---|
344 | pixelSprites->setBillboardsInWorldSpace(true);
|
---|
345 | pixelSprites->setCullIndividually(false);
|
---|
346 | pixelSprites->setPointRenderingEnabled(true);
|
---|
347 | //pixelSprites->setDefaultDimensions(1.0 / (float) resolutionX, 1.0 / resolutionY);
|
---|
348 | pixelSprites->beginBillboards();
|
---|
349 | for(int i = 0; i < resolutionX; i++)
|
---|
350 | {
|
---|
351 | for(int j = 0; j < resolutionY; j++)
|
---|
352 | {
|
---|
353 | Billboard bb( Vector3(
|
---|
354 | ( (float)i + 0.5 )/ (float) resolutionX, //position x
|
---|
355 | ( (float)j + 0.5 )/ (float) resolutionY, //position y
|
---|
356 | 0),
|
---|
357 | pixelSprites);
|
---|
358 | bb.setDimensions(1.0 / (float) resolutionX, 1.0 / resolutionY);
|
---|
359 | pixelSprites->injectBillboard(bb);
|
---|
360 | }
|
---|
361 | }
|
---|
362 | pixelSprites->endBillboards();
|
---|
363 |
|
---|
364 | sm->getRootSceneNode()->attachObject(pixelSprites);
|
---|
365 |
|
---|
366 | pixelSprites->setVisible(false);
|
---|
367 | }
|
---|
368 |
|
---|
369 | pixelSprites->setMaterialName(materialName);
|
---|
370 |
|
---|
371 | sm->getRenderQueue()->clear();
|
---|
372 | sm->getRenderQueue()->addRenderable(pixelSprites);
|
---|
373 | sm->setFindVisibleObjects(false);
|
---|
374 | rt->update();
|
---|
375 | sm->setFindVisibleObjects(true);
|
---|
376 |
|
---|
377 | }
|
---|
378 |
|
---|