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