#include "OgreDepthShadowMapRenderingRun.h" #include "OgreIlluminationManager.h" OgreDepthShadowMapRenderingRun::OgreDepthShadowMapRenderingRun(OgreSharedRuns* sharedRuns, String name, Light* light, unsigned int resolutionX, unsigned int resolutionY, String materialName ) :DepthShadowMapRenderingRun(resolutionX, resolutionY) , OgreRenderingRun(1, 1) , RenderingRun(1, 1) { this->light = light; this->sharedRuns = sharedRuns; names[0] = name + "1"; names[1] = name + "2"; this->materialName = materialName; createDepthMap(); } void OgreDepthShadowMapRenderingRun::createDepthMap() { for(int i = 0; i < 1; i++) { TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(names[i], "default", TEX_TYPE_2D, resolutionX, resolutionY, 0, 0, PF_FLOAT16_RGBA, TU_RENDERTARGET); depthMapTextures[i] = texPtr.getPointer(); depthMapCameras[i] = Root::getSingleton()._getCurrentSceneManager()->createCamera(names[i] + "_CAMERA"); //add viewport to rendertarget HardwarePixelBuffer* hpb = (depthMapTextures[i]->getBuffer()).getPointer(); RenderTarget* rt = hpb->getRenderTarget(); Viewport* v = rt->addViewport(depthMapCameras[i]); v->setOverlaysEnabled(false); rt->setAutoUpdated(false); } } void OgreDepthShadowMapRenderingRun::updateFrame(unsigned long frameNum) { refreshLight(frameNum); if(light->getType() == Light::LT_POINT) { updateDepthMap(); } else { updateDepthMap(); } } void OgreDepthShadowMapRenderingRun::updateDepthCubeFace(int facenum) { } void OgreDepthShadowMapRenderingRun::updateDepthMap() { setMaterialForVisibles(materialName, depthMapCameras[0], false); RenderTarget* rt = depthMapTextures[0]->getBuffer().getPointer()->getRenderTarget(); rt->update(); restoreMaterials(); //rt->writeContentsToFile("shadowmap1.dds"); } void setnear() { } void OgreDepthShadowMapRenderingRun::refreshLight(unsigned long frameNum) { if(light!= 0) { if(light->getType() == Light::LT_DIRECTIONAL) { Camera* viewcam = OgreIlluminationManager::getSingleton().getMainCamera(); OgreIlluminationManager::getSingleton().updatePerLightRun(light->getName(), ILLUMRUN_FOCUSING_MAP, frameNum); OgreFocusingMapRenderingRun* frun = (OgreFocusingMapRenderingRun*) OgreIlluminationManager::getSingleton().getPerLightRun(light->getName(), ILLUMRUN_FOCUSING_MAP)->asOgreRenderingRun(); Vector3 min; Vector3 max; frun->getMinMax(min, max); Vector3 lightpos = light->getPosition(); Vector3 lightdir = light->getDirection(); Vector3 center = (min + max) / 2.0; Vector3 scale = max - min; scale.x = 2.0 / scale.x; scale.y = 2.0 / scale.y; scale.z = 2.0 / scale.z; center.x *= scale.x; center.y *= scale.y; center.z *= scale.z; depthMapCameras[0]->setDirection( lightdir ); depthMapCameras[0]->setPosition( lightpos ); Matrix4 projection = Matrix4::IDENTITY; projection.setTrans(center); projection.setScale(scale); depthMapCameras[0]->setCustomProjectionMatrix(true, projection); /* projection = Matrix4::IDENTITY; projection.setScale(Vector3(1.0 / 100.0, 1.0 / 100.0, -1.0 / 100.0)); depthMapCameras[0]->setCustomProjectionMatrix(true, projection); depthMapCameras[0]->setPosition( Vector3(50,50,50) );*/ } else if(light->getType() == Light::LT_SPOTLIGHT) { /* Vector3 dir = light->getDirection(); depthMapCamera->setDirection( dir ); Vector3 pos = light->getParentNode()->getWorldPosition(); depthMapCamera->setPosition(pos); depthMapCamera->setProjectionType(PT_PERSPECTIVE); depthMapCamera->setFOVy(light->getSpotlightOuterAngle());*/ } else//point light { /* Vector3 pos = light->getParentNode()->getWorldPosition(); Vector3 dir = -pos; depthMapCamera->setDirection( dir ); depthMapCamera->setPosition(pos); depthMapCamera->setProjectionType(PT_PERSPECTIVE); depthMapCamera->setFOVy(Radian(Degree(140))); depthMapCamera->setNearClipDistance(1); // depthMapCamera->setFarClipDistance(200); //OGRE_EXCEPT(0, "NOT implemented for Pointlight", "OgreDepthShadowMapRenderingRun::refreshLight"); */ /*Vector3 pos = light->getParentNode()->getWorldPosition(); Vector3 dir = -pos; depthMapCamera->setDirection( dir ); depthMapCamera->setPosition(pos); depthMapCamera->setProjectionType(PT_ORTHOGRAPHIC); depthMapCamera->*/ } } } Matrix4 OgreDepthShadowMapRenderingRun::getLightViewMatrix(int i) { return depthMapCameras[i]->getViewMatrix(); } Matrix4 OgreDepthShadowMapRenderingRun::getLightViewProjMatrix(int i) { return depthMapCameras[i]->getProjectionMatrix() * depthMapCameras[i]->getViewMatrix(); }