1 | #include "OgreDepthShadowRecieverRenderTechnique.h"
|
---|
2 | #include "OgreTechniqueGroup.h"
|
---|
3 | #include "OgreIlluminationManager.h"
|
---|
4 | #include "OgreDepthShadowMapRenderingRun.h"
|
---|
5 |
|
---|
6 | OgreDepthShadowRecieverRenderTechnique::OgreDepthShadowRecieverRenderTechnique(
|
---|
7 | int maxlights,
|
---|
8 | String shadowVertexProgram,
|
---|
9 | String shadowFragmentProgram,
|
---|
10 | bool setLightViewMatrix,
|
---|
11 | bool setLightViewProjMatrix,
|
---|
12 | bool setLightProjFarPlane,
|
---|
13 | String lightViewProjParamName,
|
---|
14 | String lightViewParamName,
|
---|
15 | String lightFarPlaneParamName,
|
---|
16 | Pass* pass,
|
---|
17 | OgreRenderable* parentRenderable,
|
---|
18 | OgreTechniqueGroup* parentTechniqueGroup)
|
---|
19 | :RenderTechnique( parentRenderable, parentTechniqueGroup),
|
---|
20 | OgreRenderTechnique(pass, parentRenderable, parentTechniqueGroup),
|
---|
21 | DepthShadowRecieverRenderTechnique(parentRenderable, parentTechniqueGroup)
|
---|
22 | {
|
---|
23 | this->maxlights = maxlights;
|
---|
24 | this->shadowVertexProgram = shadowVertexProgram ;
|
---|
25 | this->shadowFragmentProgram = shadowFragmentProgram;
|
---|
26 | this->setLightViewMatrix = setLightViewMatrix;
|
---|
27 | this->setLightViewProjMatrix = setLightViewProjMatrix;
|
---|
28 | this->setLightProjFarPlane = setLightProjFarPlane;
|
---|
29 | this->lightViewProjParamName = lightViewProjParamName;
|
---|
30 | this->lightViewParamName = lightViewParamName;
|
---|
31 | this->lightFarPlaneParamName = lightFarPlaneParamName;
|
---|
32 |
|
---|
33 | //insert new passes
|
---|
34 | Ogre::Technique* techn = pass->getParent();
|
---|
35 | Technique::PassIterator it = techn->getPassIterator();
|
---|
36 |
|
---|
37 | int index = 0;
|
---|
38 | while(it.hasMoreElements())
|
---|
39 | {
|
---|
40 | if( it.getNext() == pass)
|
---|
41 | break;
|
---|
42 | index++;
|
---|
43 | it.moveNext();
|
---|
44 | }
|
---|
45 |
|
---|
46 | index++;
|
---|
47 | for(int i = 0; i < maxlights; i++)
|
---|
48 | {
|
---|
49 | int lastpass = techn->getNumPasses();
|
---|
50 | Pass* newpass = techn->createPass();
|
---|
51 | passes.push_back(newpass);
|
---|
52 |
|
---|
53 | newpass->setVertexProgram(shadowVertexProgram);
|
---|
54 | newpass->setFragmentProgram(shadowFragmentProgram);
|
---|
55 |
|
---|
56 | GpuProgramParameters* Vparams = newpass->getVertexProgramParameters().getPointer();
|
---|
57 | Vparams->setNamedAutoConstant("worldViewProj",
|
---|
58 | GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
|
---|
59 | Vparams->setNamedAutoConstant("world",
|
---|
60 | GpuProgramParameters::ACT_WORLD_MATRIX);
|
---|
61 | GpuProgramParameters* Fparams = newpass->getFragmentProgramParameters().getPointer();
|
---|
62 |
|
---|
63 | TextureUnitState* st = newpass->createTextureUnitState();
|
---|
64 | st->setTextureFiltering(TFO_ANISOTROPIC);
|
---|
65 | st->setTextureAddressingMode(TextureUnitState::TAM_BORDER);
|
---|
66 | st->setTextureBorderColour(ColourValue::White);
|
---|
67 |
|
---|
68 | newpass->setSceneBlending(SBT_MODULATE);
|
---|
69 | //newpass->setSceneBlending(SBF_ONE, SBF_ZERO);
|
---|
70 |
|
---|
71 | newpass->setDepthBias(1);
|
---|
72 | techn->movePass(lastpass, index);
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | OgreDepthShadowRecieverRenderTechnique::~OgreDepthShadowRecieverRenderTechnique()
|
---|
78 | {
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | void OgreDepthShadowRecieverRenderTechnique::update(unsigned long frameNum)
|
---|
83 | {
|
---|
84 | LightList lights;
|
---|
85 | Root::getSingleton()._getCurrentSceneManager()->_populateLightList(
|
---|
86 | ((OgreSharedRuns*) sharedRuns)->getRootPosition(),
|
---|
87 | 100000.0,
|
---|
88 | lights);
|
---|
89 |
|
---|
90 | //fill passes
|
---|
91 | unsigned int l = 0;
|
---|
92 | for(unsigned int i = 0; i < passes.size(); i++)
|
---|
93 | {
|
---|
94 | while(lights.size() != l && !lights.at(l)->getCastShadows())
|
---|
95 | l++;
|
---|
96 |
|
---|
97 | if(lights.size() > l)
|
---|
98 | {
|
---|
99 |
|
---|
100 | //create run if not exists
|
---|
101 | OgreIlluminationManager::getSingleton().createPerLightRun(lights.at(l)->getName(),
|
---|
102 | ILLUMRUN_DEPTH_SHADOWMAP);
|
---|
103 | //update light depth map
|
---|
104 | OgreIlluminationManager::getSingleton().updatePerLightRun(lights.at(l)->getName(),
|
---|
105 | ILLUMRUN_DEPTH_SHADOWMAP,
|
---|
106 | frameNum);
|
---|
107 |
|
---|
108 | //set texture to pass
|
---|
109 | OgreDepthShadowMapRenderingRun* depthRun =
|
---|
110 | (OgreDepthShadowMapRenderingRun*) OgreIlluminationManager::getSingleton()
|
---|
111 | .getPerLightRun(lights.at(l)->getName(),
|
---|
112 | ILLUMRUN_DEPTH_SHADOWMAP)->asOgreRenderingRun();
|
---|
113 |
|
---|
114 | passes.at(i)->getTextureUnitState(0)->setTextureName(
|
---|
115 | depthRun->getDepthMapTextureName());
|
---|
116 |
|
---|
117 | //TODO: set matrices
|
---|
118 | GpuProgramParametersSharedPtr fpParams = passes.at(i)->getFragmentProgramParameters();
|
---|
119 | GpuProgramParametersSharedPtr vpParams = passes.at(i)->getVertexProgramParameters();
|
---|
120 |
|
---|
121 | //fpParams->setNamedConstant("lightViewProj", depthRun->getLightViewProjMatrix());
|
---|
122 | if(setLightViewProjMatrix)
|
---|
123 | vpParams->setNamedConstant(lightViewProjParamName, depthRun->getLightViewProjMatrix());
|
---|
124 | if(setLightViewMatrix)
|
---|
125 | vpParams->setNamedConstant(lightViewParamName, depthRun->getLightViewMatrix());
|
---|
126 | if(setLightProjFarPlane)
|
---|
127 | fpParams->setNamedConstant(lightFarPlaneParamName, depthRun->getLightFarPlane());
|
---|
128 |
|
---|
129 |
|
---|
130 | passes.at(i)->setActive(true);
|
---|
131 |
|
---|
132 | }
|
---|
133 | else
|
---|
134 | passes.at(i)->setActive(false);
|
---|
135 |
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | ///Technique parsers
|
---|
141 | namespace DepthShadowRecieverParsers
|
---|
142 | {
|
---|
143 | void parseMaxLights(String& params, RenderTechniqueFactory* factory)
|
---|
144 | {
|
---|
145 | OgreDepthShadowRecieverRenderTechniqueFactory* f = (OgreDepthShadowRecieverRenderTechniqueFactory*) factory;
|
---|
146 | f->maxlights = StringConverter::parseInt(params);
|
---|
147 | }
|
---|
148 |
|
---|
149 | void parseVertexProgram(String& params, RenderTechniqueFactory* factory)
|
---|
150 | {
|
---|
151 | OgreDepthShadowRecieverRenderTechniqueFactory* f = (OgreDepthShadowRecieverRenderTechniqueFactory*) factory;
|
---|
152 | f->shadowVertexProgram = params;
|
---|
153 | }
|
---|
154 |
|
---|
155 | void parseFragmentProgram(String& params, RenderTechniqueFactory* factory)
|
---|
156 | {
|
---|
157 | OgreDepthShadowRecieverRenderTechniqueFactory* f = (OgreDepthShadowRecieverRenderTechniqueFactory*) factory;
|
---|
158 | f->shadowFragmentProgram = params;
|
---|
159 | }
|
---|
160 |
|
---|
161 | void parseSetLightViewProj(String& params, RenderTechniqueFactory* factory)
|
---|
162 | {
|
---|
163 | OgreDepthShadowRecieverRenderTechniqueFactory* f = (OgreDepthShadowRecieverRenderTechniqueFactory*) factory;
|
---|
164 | StringVector vecparams = StringUtil::split(params, " \t");
|
---|
165 | f->setLightViewProjMatrix = StringConverter::parseBool(vecparams[0]);
|
---|
166 | if(f->setLightViewProjMatrix && vecparams.size() > 1)
|
---|
167 | f->lightViewProjParamName = vecparams[1];
|
---|
168 | }
|
---|
169 |
|
---|
170 | void parseSetLightView(String& params, RenderTechniqueFactory* factory)
|
---|
171 | {
|
---|
172 | OgreDepthShadowRecieverRenderTechniqueFactory* f = (OgreDepthShadowRecieverRenderTechniqueFactory*) factory;
|
---|
173 | StringVector vecparams = StringUtil::split(params, " \t");
|
---|
174 | f->setLightViewMatrix = StringConverter::parseBool(vecparams[0]);
|
---|
175 | if(f->setLightViewMatrix && vecparams.size() > 1)
|
---|
176 | f->lightViewParamName = vecparams[1];
|
---|
177 | }
|
---|
178 |
|
---|
179 | void parseSetLightFarPlane(String& params, RenderTechniqueFactory* factory)
|
---|
180 | {
|
---|
181 | OgreDepthShadowRecieverRenderTechniqueFactory* f = (OgreDepthShadowRecieverRenderTechniqueFactory*) factory;
|
---|
182 | StringVector vecparams = StringUtil::split(params, " \t");
|
---|
183 | f->setLightProjFarPlane = StringConverter::parseBool(vecparams[0]);
|
---|
184 | if(f->setLightProjFarPlane && vecparams.size() > 1)
|
---|
185 | f->lightFarPlaneParamName = vecparams[1];
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | OgreDepthShadowRecieverRenderTechniqueFactory::OgreDepthShadowRecieverRenderTechniqueFactory()
|
---|
190 | {
|
---|
191 | typeName = "DepthShadowReciever";
|
---|
192 |
|
---|
193 | using namespace DepthShadowRecieverParsers;
|
---|
194 | //register parsers
|
---|
195 | this->attributeParsers.insert(AttribParserList::value_type("max_light_count", (ILLUM_ATTRIBUTE_PARSER) parseMaxLights));
|
---|
196 | this->attributeParsers.insert(AttribParserList::value_type("vertex_program_name", (ILLUM_ATTRIBUTE_PARSER) parseVertexProgram));
|
---|
197 | this->attributeParsers.insert(AttribParserList::value_type("fragment_program_name", (ILLUM_ATTRIBUTE_PARSER) parseFragmentProgram));
|
---|
198 | this->attributeParsers.insert(AttribParserList::value_type("set_light_viewproj", (ILLUM_ATTRIBUTE_PARSER) parseSetLightViewProj));
|
---|
199 | this->attributeParsers.insert(AttribParserList::value_type("set_light_view", (ILLUM_ATTRIBUTE_PARSER) parseSetLightView));
|
---|
200 | this->attributeParsers.insert(AttribParserList::value_type("set_light_farplane", (ILLUM_ATTRIBUTE_PARSER) parseSetLightFarPlane));
|
---|
201 | }
|
---|
202 |
|
---|
203 | OgreRenderTechnique* OgreDepthShadowRecieverRenderTechniqueFactory::createInstance(
|
---|
204 | IllumTechniqueParams* params,
|
---|
205 | Pass* pass,
|
---|
206 | OgreRenderable* parentRenderable,
|
---|
207 | OgreTechniqueGroup* parentTechniqueGroup)
|
---|
208 | {
|
---|
209 | //reset parameters
|
---|
210 | maxlights = 1;
|
---|
211 | shadowVertexProgram = "GameTools/ShadowMap/ShadowVS";
|
---|
212 | shadowFragmentProgram = "GameTools/ShadowMap/ShadowPS";
|
---|
213 | setLightViewMatrix = false;
|
---|
214 | setLightViewProjMatrix = true;
|
---|
215 | setLightProjFarPlane = false;
|
---|
216 | lightViewProjParamName = "lightViewProj";
|
---|
217 | lightViewParamName = "lightView";
|
---|
218 | lightFarPlaneParamName = "lightFarPlane";
|
---|
219 |
|
---|
220 | parseParams(params);
|
---|
221 |
|
---|
222 | OgreDepthShadowRecieverRenderTechnique* result = new OgreDepthShadowRecieverRenderTechnique(
|
---|
223 | maxlights,
|
---|
224 | shadowVertexProgram,
|
---|
225 | shadowFragmentProgram,
|
---|
226 | setLightViewMatrix ,
|
---|
227 | setLightViewProjMatrix,
|
---|
228 | setLightProjFarPlane,
|
---|
229 | lightViewProjParamName,
|
---|
230 | lightViewParamName,
|
---|
231 | lightFarPlaneParamName,
|
---|
232 | pass,
|
---|
233 | parentRenderable,
|
---|
234 | parentTechniqueGroup);
|
---|
235 |
|
---|
236 | return result;
|
---|
237 | }
|
---|
238 |
|
---|