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