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