#include "OgreFocusingMapRenderingRun.h" void OgreFocusingMapRenderingRun::getMinMax(Vector3& min, Vector3& max) { unsigned int buffersize = focusingMapSize * focusingMapSize * 4; float* floatbuffer = new float[buffersize]; PixelBox lockBox(focusingMapSize, focusingMapSize, 1, PF_FLOAT32_RGBA, floatbuffer); focusingTexture->getBuffer()->blitToMemory(lockBox); float minX = 0; float minY = 0; float minZ = 0; float maxX = 0; float maxY = 0; float maxZ = 0; bool first = true; for(unsigned int i = 0; i < buffersize; i+= 4) { float x = floatbuffer[i]; float y = floatbuffer[i + 1]; float z = floatbuffer[i + 2]; float valid = floatbuffer[i + 3]; if(valid) { if(first) { minX = x; minY = y; minZ = z; maxX = x; maxY = y; maxZ = z; first = false; } else { if(x < minX)minX = x; if(y < minY)minY = y; if(z < minZ)minZ = z; if(x > maxX)maxX = x; if(y > maxY)maxY = y; if(z > maxZ)maxZ = z; } } } min.x = minX; min.y = minY; min.z = minZ; max.x = maxX; max.y = maxY; max.z = maxZ; delete[] floatbuffer; } OgreFocusingMapRenderingRun::OgreFocusingMapRenderingRun( String name, Matrix4 lightMatrix, unsigned int focusingMapSize ) : OgreRenderingRun(1, 1) , RenderingRun(1, 1) { this->name = name; this->lightMatrix = lightMatrix; this->focusingMapSize = focusingMapSize; createFocusingMap(); } void OgreFocusingMapRenderingRun::setCameraMatrices(const Matrix4 &view, const Matrix4 &projection) { camera->setCustomViewMatrix(true, view); camera->setCustomProjectionMatrix(true, projection); } void OgreFocusingMapRenderingRun::createFocusingMap() { int width = focusingMapSize; int height = focusingMapSize; TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(name, "default", TEX_TYPE_2D, width, height, 0, 0, PF_FLOAT32_RGBA, TU_RENDERTARGET); focusingTexture = texPtr.getPointer(); //add viewport to rendertarget HardwarePixelBuffer* hpb = (focusingTexture->getBuffer()).getPointer(); RenderTarget* rt = hpb->getRenderTarget(); this->camera = Root::getSingleton()._getCurrentSceneManager()->createCamera(name + "_CAMERA"); Viewport* v = rt->addViewport(camera); v->setOverlaysEnabled(false); rt->setAutoUpdated(false); } void OgreFocusingMapRenderingRun::updateFrame(unsigned long frameNum) { MaterialPtr mat = MaterialManager::getSingleton().getByName("GTP/Basic/Focusing"); GpuProgramParameters* Vparams = mat->getTechnique(0)->getPass(0)->getVertexProgramParameters().getPointer(); Vparams->setNamedConstant("LightViewProj", lightMatrix); setMaterialForVisibles(String("GTP/Basic/Focusing"), camera); RenderTarget* rt = focusingTexture->getBuffer().getPointer()->getRenderTarget(); ColourValue backColor(0,0,0,0); rt->getViewport(0)->setBackgroundColour(backColor); rt->update(); //rt->writeContentsToFile("focusingmap.dds"); restoreMaterials(); } void OgreFocusingMapRenderingRun::freeAllResources() { this->focusingTexture = 0; TextureManager::getSingleton().remove(name); Root::getSingleton()._getCurrentSceneManager()->destroyCamera(name + "_CAMERA"); }