[2189] | 1 | #include "OgrePMWeightComputeRenderingRun.h"
|
---|
| 2 | #include "OgreIlluminationManager.h"
|
---|
| 3 | #include "OgrePMEntryPointMapRenderingRun.h"
|
---|
[2218] | 4 | #include "OgreDepthShadowMapRenderingRun.h"
|
---|
[2189] | 5 |
|
---|
[2333] | 6 | Texture* OgrePMWeightComputeRenderingRun::weightTexture = 0;
|
---|
| 7 | Texture* OgrePMWeightComputeRenderingRun::allWeightsTexture = 0;
|
---|
| 8 |
|
---|
[2189] | 9 | OgrePMWeightComputeRenderingRun::OgrePMWeightComputeRenderingRun(String name, String LightName)
|
---|
| 10 | : OgreRenderingRun(1, 1)
|
---|
| 11 | , RenderingRun(1, 1)
|
---|
| 12 | {
|
---|
| 13 | this->name = name;
|
---|
| 14 | weights = 0;
|
---|
| 15 |
|
---|
| 16 | this->light = Root::getSingleton()._getCurrentSceneManager()->getLight(LightName);
|
---|
| 17 | createWeightMap();
|
---|
| 18 |
|
---|
| 19 | OgreIlluminationManager::getSingleton().createGlobalRun(ILLUMRUN_PM_ENTRYPOINTMAP);
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | void OgrePMWeightComputeRenderingRun::createWeightMap()
|
---|
| 23 | {
|
---|
[2333] | 24 | if(allWeightsTexture == 0)
|
---|
| 25 | {
|
---|
| 26 | unsigned int entryPointCnt = OgreIlluminationManager::getSingleton().getPathMapEntryPoints().size();
|
---|
| 27 | int width = entryPointCnt / 4096;
|
---|
| 28 | int height = 4096;
|
---|
| 29 | TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(name + "_ALL",
|
---|
| 30 | "default",
|
---|
| 31 | TEX_TYPE_2D,
|
---|
| 32 | width,
|
---|
| 33 | height,
|
---|
| 34 | 0,
|
---|
| 35 | 0,
|
---|
| 36 | PF_FLOAT16_RGBA,
|
---|
| 37 | TU_RENDERTARGET);
|
---|
| 38 | allWeightsTexture = texPtr.getPointer();
|
---|
| 39 | //add viewport to rendertarget
|
---|
| 40 | HardwarePixelBuffer* hpb = (allWeightsTexture->getBuffer()).getPointer();
|
---|
| 41 | RenderTarget* rt = hpb->getRenderTarget();
|
---|
| 42 | Camera* wc = Root::getSingleton()._getCurrentSceneManager()->createCamera(name + "_ALL_CAMERA");
|
---|
| 43 | Viewport* v = rt->addViewport(wc);
|
---|
| 44 | v->setBackgroundColour(ColourValue::Black);
|
---|
| 45 | v->setClearEveryFrame(false);
|
---|
| 46 | v->setOverlaysEnabled(false);
|
---|
| 47 | rt->setAutoUpdated(false);
|
---|
[2200] | 48 |
|
---|
[2333] | 49 | unsigned int clustercount = OgreIlluminationManager::getSingleton().getPathMapClusterLengthsSize();
|
---|
| 50 | width = clustercount;
|
---|
| 51 | height = 1;
|
---|
| 52 | texPtr = Ogre::TextureManager::getSingleton().createManual(name,
|
---|
| 53 | "default",
|
---|
| 54 | TEX_TYPE_2D,
|
---|
| 55 | width,
|
---|
| 56 | height,
|
---|
| 57 | 0,
|
---|
| 58 | 0,
|
---|
| 59 | PF_FLOAT16_RGBA,
|
---|
| 60 | TU_RENDERTARGET);
|
---|
| 61 | weightTexture = texPtr.getPointer();
|
---|
| 62 | //add viewport to rendertarget
|
---|
| 63 | hpb = (weightTexture->getBuffer()).getPointer();
|
---|
| 64 | rt = hpb->getRenderTarget();
|
---|
| 65 | wc = Root::getSingleton()._getCurrentSceneManager()->createCamera(name + "_CAMERA");
|
---|
| 66 | v = rt->addViewport(wc);
|
---|
| 67 | v->setOverlaysEnabled(false);
|
---|
| 68 | rt->setAutoUpdated(false);
|
---|
[2420] | 69 | }
|
---|
[2189] | 70 | }
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | void OgrePMWeightComputeRenderingRun::updateFrame(unsigned long frameNum)
|
---|
| 75 | {
|
---|
[2200] | 76 | OgreIlluminationManager::getSingleton().updateGlobalRun(ILLUMRUN_PM_ENTRYPOINTMAP, frameNum);
|
---|
| 77 | OgrePMEntryPointMapRenderingRun* PMEPrun = (OgrePMEntryPointMapRenderingRun*)
|
---|
| 78 | OgreIlluminationManager::getSingleton().getGlobalRun(ILLUMRUN_PM_ENTRYPOINTMAP)->asOgreRenderingRun();
|
---|
| 79 | unsigned int entryPointCnt = OgreIlluminationManager::getSingleton().getPathMapEntryPoints().size();
|
---|
| 80 | int col = entryPointCnt / 4096;
|
---|
| 81 |
|
---|
| 82 | if(light->getType() == Light::LT_SPOTLIGHT)
|
---|
[2189] | 83 | {
|
---|
[2218] | 84 | OgreIlluminationManager::getSingleton().createPerLightRun(light->getName(), ILLUMRUN_DEPTH_SHADOWMAP);
|
---|
| 85 | OgreIlluminationManager::getSingleton().updatePerLightRun(light->getName(), ILLUMRUN_DEPTH_SHADOWMAP, frameNum);
|
---|
| 86 | OgreDepthShadowMapRenderingRun* SMrun = (OgreDepthShadowMapRenderingRun*) OgreIlluminationManager::getSingleton()
|
---|
| 87 | .getPerLightRun(light->getName(), ILLUMRUN_DEPTH_SHADOWMAP)->asOgreRenderingRun();
|
---|
| 88 |
|
---|
[2200] | 89 | MaterialPtr mat = MaterialManager::getSingleton().getByName("GTP/PathMap_ComputeWeights");
|
---|
| 90 | GpuProgramParameters* Fparams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters().getPointer();
|
---|
[2189] | 91 |
|
---|
[2333] | 92 | Vector3 lightPos = light->getDerivedPosition();
|
---|
| 93 | Vector3 lightDir = light->getDerivedDirection();
|
---|
[2410] | 94 | lightDir.normalise();
|
---|
[2333] | 95 | Radian lightAngle = light->getSpotlightOuterAngle();
|
---|
[2200] | 96 |
|
---|
[2189] | 97 | Fparams->setNamedConstant("nRadionColumns", col);
|
---|
| 98 | Fparams->setNamedConstant("lightPos", lightPos);
|
---|
| 99 | Fparams->setNamedConstant("lightDir", lightDir);
|
---|
[2249] | 100 | Fparams->setNamedConstant("lightPower", light->getPowerScale());
|
---|
[2333] | 101 | Fparams->setNamedConstant("lightColor", light->getDiffuseColour());
|
---|
[2218] | 102 | Fparams->setNamedConstant("lightViewProj", SMrun->getLightViewProjMatrix());
|
---|
| 103 | Fparams->setNamedConstant("lightFarPlane", SMrun->getLightFarPlane());
|
---|
[2333] | 104 | Fparams->setNamedConstant("lightAttenuation", Vector4(light->getAttenuationRange(),
|
---|
| 105 | light->getAttenuationConstant(),
|
---|
| 106 | light->getAttenuationLinear(),
|
---|
| 107 | light->getAttenuationQuadric()));
|
---|
| 108 | Fparams->setNamedConstant("lightAngleCos", Math::Cos(lightAngle.valueRadians()/2.0));
|
---|
| 109 | Fparams->setNamedConstant("spotLightFalloff", light->getSpotlightFalloff());
|
---|
[2200] | 110 |
|
---|
[2189] | 111 | mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(
|
---|
| 112 | PMEPrun->getEntryPointTextureName());
|
---|
[2218] | 113 | mat->getTechnique(0)->getPass(0)->getTextureUnitState(1)->setTextureName(
|
---|
| 114 | SMrun->getDepthMapTextureName());
|
---|
[2189] | 115 |
|
---|
[2200] | 116 | RenderTarget* rt = allWeightsTexture->getBuffer().getPointer()->getRenderTarget();
|
---|
[2333] | 117 | if(frameNum != rt->getLastFrameNumber())
|
---|
| 118 | rt->getViewport(0)->setClearEveryFrame(true);
|
---|
| 119 | else
|
---|
| 120 | rt->getViewport(0)->setClearEveryFrame(false);
|
---|
| 121 |
|
---|
| 122 | renderFullscreenQuad(mat->getName(), rt);
|
---|
| 123 |
|
---|
[2189] | 124 |
|
---|
[2218] | 125 | // rt->writeContentsToFile("allweights.bmp");
|
---|
[2209] | 126 | // rt->writeContentsToFile("allweights.dds");
|
---|
| 127 |
|
---|
[2189] | 128 | }
|
---|
[2333] | 129 | else if(light->getType() == Light::LT_POINT)
|
---|
[2200] | 130 | {
|
---|
[2333] | 131 | OgreIlluminationManager::getSingleton().createPerLightRun(light->getName(), ILLUMRUN_DEPTH_SHADOWMAP);
|
---|
| 132 | OgreIlluminationManager::getSingleton().updatePerLightRun(light->getName(), ILLUMRUN_DEPTH_SHADOWMAP, frameNum);
|
---|
| 133 | OgreDepthShadowMapRenderingRun* SMrun = (OgreDepthShadowMapRenderingRun*) OgreIlluminationManager::getSingleton()
|
---|
| 134 | .getPerLightRun(light->getName(), ILLUMRUN_DEPTH_SHADOWMAP)->asOgreRenderingRun();
|
---|
| 135 |
|
---|
| 136 | MaterialPtr mat = MaterialManager::getSingleton().getByName("GTP/PathMap_ComputeWeights_Point");
|
---|
| 137 | GpuProgramParameters* Fparams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters().getPointer();
|
---|
| 138 |
|
---|
| 139 | Vector3 lightPos = light->getDerivedPosition();
|
---|
| 140 |
|
---|
| 141 | Fparams->setNamedConstant("nRadionColumns", col);
|
---|
| 142 | Fparams->setNamedConstant("lightPos", lightPos);
|
---|
| 143 | Fparams->setNamedConstant("lightPower", light->getPowerScale());
|
---|
| 144 | Fparams->setNamedConstant("lightColor", light->getDiffuseColour());
|
---|
| 145 | Fparams->setNamedConstant("lightFarPlane", SMrun->getLightFarPlane());
|
---|
| 146 | Fparams->setNamedConstant("lightAttenuation", Vector4(light->getAttenuationRange(),
|
---|
| 147 | light->getAttenuationConstant(),
|
---|
| 148 | light->getAttenuationLinear(),
|
---|
| 149 | light->getAttenuationQuadric()));
|
---|
| 150 |
|
---|
| 151 | mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(
|
---|
| 152 | PMEPrun->getEntryPointTextureName());
|
---|
| 153 | mat->getTechnique(0)->getPass(0)->getTextureUnitState(1)->setTextureName(
|
---|
| 154 | SMrun->getDepthMapTextureName());
|
---|
| 155 |
|
---|
| 156 | RenderTarget* rt = allWeightsTexture->getBuffer().getPointer()->getRenderTarget();
|
---|
| 157 | if(frameNum != rt->getLastFrameNumber())
|
---|
| 158 | rt->getViewport(0)->setClearEveryFrame(true);
|
---|
| 159 | else
|
---|
| 160 | rt->getViewport(0)->setClearEveryFrame(false);
|
---|
| 161 |
|
---|
| 162 | renderFullscreenQuad(mat->getName(), rt);
|
---|
[2420] | 163 |
|
---|
| 164 | //rt->writeContentsToFile("allweights.dds");
|
---|
[2333] | 165 |
|
---|
[2200] | 166 | }
|
---|
[2214] | 167 |
|
---|
[2333] | 168 | }
|
---|
| 169 |
|
---|
| 170 | void OgrePMWeightComputeRenderingRun::sumWeights(unsigned long frameNum)
|
---|
| 171 | {
|
---|
| 172 | RenderTarget* rt = weightTexture->getBuffer().getPointer()->getRenderTarget();
|
---|
| 173 | if(frameNum == rt->getLastFrameNumber())return;
|
---|
| 174 |
|
---|
| 175 | unsigned int entryPointCnt = OgreIlluminationManager::getSingleton().getPathMapEntryPoints().size();
|
---|
| 176 | int col = entryPointCnt / 4096;
|
---|
[2189] | 177 | unsigned int clustercount = OgreIlluminationManager::getSingleton().getPathMapClusterLengthsSize();
|
---|
[2333] | 178 | OgrePMEntryPointMapRenderingRun* PMEPrun = (OgrePMEntryPointMapRenderingRun*)
|
---|
| 179 | OgreIlluminationManager::getSingleton().getGlobalRun(ILLUMRUN_PM_ENTRYPOINTMAP)->asOgreRenderingRun();
|
---|
| 180 |
|
---|
[2200] | 181 | MaterialPtr mat = MaterialManager::getSingleton().getByName("GTP/PathMap_SumWeights");
|
---|
| 182 | GpuProgramParameters* Fparams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters().getPointer();
|
---|
| 183 | Fparams->setNamedConstant("nRadionColumns", col);
|
---|
| 184 | Fparams->setNamedConstant("clusterCount", (float) clustercount);
|
---|
| 185 | mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(
|
---|
| 186 | PMEPrun->getEntryPointTextureName());
|
---|
| 187 | mat->getTechnique(0)->getPass(0)->getTextureUnitState(1)->setTextureName(
|
---|
| 188 | PMEPrun->getClusterLengthTextureName());
|
---|
| 189 | mat->getTechnique(0)->getPass(0)->getTextureUnitState(2)->setTextureName(
|
---|
| 190 | allWeightsTexture->getName());
|
---|
[2189] | 191 |
|
---|
[2200] | 192 | renderFullscreenQuad(mat->getName(), rt);
|
---|
[2420] | 193 |
|
---|
| 194 | //rt->writeContentsToFile("clusterweights.bmp");
|
---|
[2189] | 195 | }
|
---|
| 196 |
|
---|
[2320] | 197 | void OgrePMWeightComputeRenderingRun::freeAllResources()
|
---|
| 198 | {
|
---|
[2321] | 199 | this->weightTexture = 0;
|
---|
| 200 | this->allWeightsTexture = 0;
|
---|
[2320] | 201 | TextureManager::getSingleton().remove(name);
|
---|
| 202 | Root::getSingleton()._getCurrentSceneManager()->destroyCamera(name + "_CAMERA");
|
---|
[2189] | 203 |
|
---|
[2320] | 204 | TextureManager::getSingleton().remove(name + "_ALL");
|
---|
| 205 | Root::getSingleton()._getCurrentSceneManager()->destroyCamera(name + "_ALL" + "_CAMERA");
|
---|
| 206 | }
|
---|