1 | #include "OgrePMWeightComputeRenderingRun.h"
|
---|
2 | #include "OgreIlluminationManager.h"
|
---|
3 | #include "OgrePMEntryPointMapRenderingRun.h"
|
---|
4 | #include "OgreDepthShadowMapRenderingRun.h"
|
---|
5 |
|
---|
6 | Texture* OgrePMWeightComputeRenderingRun::weightTexture = 0;
|
---|
7 | Texture* OgrePMWeightComputeRenderingRun::allWeightsTexture = 0;
|
---|
8 |
|
---|
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 | {
|
---|
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);
|
---|
48 |
|
---|
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);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 | void OgrePMWeightComputeRenderingRun::updateFrame(unsigned long frameNum)
|
---|
75 | {
|
---|
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)
|
---|
83 | {
|
---|
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 |
|
---|
89 | MaterialPtr mat = MaterialManager::getSingleton().getByName("GTP/PathMap_ComputeWeights");
|
---|
90 | GpuProgramParameters* Fparams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters().getPointer();
|
---|
91 |
|
---|
92 | Vector3 lightPos = light->getDerivedPosition();
|
---|
93 | Vector3 lightDir = light->getDerivedDirection();
|
---|
94 | lightDir.normalise();
|
---|
95 | Radian lightAngle = light->getSpotlightOuterAngle();
|
---|
96 |
|
---|
97 | Fparams->setNamedConstant("nRadionColumns", col);
|
---|
98 | Fparams->setNamedConstant("lightPos", lightPos);
|
---|
99 | Fparams->setNamedConstant("lightDir", lightDir);
|
---|
100 | Fparams->setNamedConstant("lightPower", light->getPowerScale());
|
---|
101 | Fparams->setNamedConstant("lightColor", light->getDiffuseColour());
|
---|
102 | Fparams->setNamedConstant("lightViewProj", SMrun->getLightViewProjMatrix());
|
---|
103 | Fparams->setNamedConstant("lightFarPlane", SMrun->getLightFarPlane());
|
---|
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());
|
---|
110 |
|
---|
111 | mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(
|
---|
112 | PMEPrun->getEntryPointTextureName());
|
---|
113 | mat->getTechnique(0)->getPass(0)->getTextureUnitState(1)->setTextureName(
|
---|
114 | SMrun->getDepthMapTextureName());
|
---|
115 |
|
---|
116 | RenderTarget* rt = allWeightsTexture->getBuffer().getPointer()->getRenderTarget();
|
---|
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 |
|
---|
124 |
|
---|
125 | // rt->writeContentsToFile("allweights.bmp");
|
---|
126 | // rt->writeContentsToFile("allweights.dds");
|
---|
127 |
|
---|
128 | }
|
---|
129 | else if(light->getType() == Light::LT_POINT)
|
---|
130 | {
|
---|
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);
|
---|
163 |
|
---|
164 | //rt->writeContentsToFile("allweights.dds");
|
---|
165 |
|
---|
166 | }
|
---|
167 |
|
---|
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;
|
---|
177 | unsigned int clustercount = OgreIlluminationManager::getSingleton().getPathMapClusterLengthsSize();
|
---|
178 | OgrePMEntryPointMapRenderingRun* PMEPrun = (OgrePMEntryPointMapRenderingRun*)
|
---|
179 | OgreIlluminationManager::getSingleton().getGlobalRun(ILLUMRUN_PM_ENTRYPOINTMAP)->asOgreRenderingRun();
|
---|
180 |
|
---|
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());
|
---|
191 |
|
---|
192 | renderFullscreenQuad(mat->getName(), rt);
|
---|
193 |
|
---|
194 | //rt->writeContentsToFile("clusterweights.bmp");
|
---|
195 | }
|
---|
196 |
|
---|
197 | void OgrePMWeightComputeRenderingRun::freeAllResources()
|
---|
198 | {
|
---|
199 | this->weightTexture = 0;
|
---|
200 | this->allWeightsTexture = 0;
|
---|
201 | TextureManager::getSingleton().remove(name);
|
---|
202 | Root::getSingleton()._getCurrentSceneManager()->destroyCamera(name + "_CAMERA");
|
---|
203 |
|
---|
204 | TextureManager::getSingleton().remove(name + "_ALL");
|
---|
205 | Root::getSingleton()._getCurrentSceneManager()->destroyCamera(name + "_ALL" + "_CAMERA");
|
---|
206 | }
|
---|