1 | #include "OgreCausticReceiverRenderTechnique.h"
|
---|
2 | #include "OgreTechniqueGroup.h"
|
---|
3 | #include "OgreIlluminationManager.h"
|
---|
4 | #include "OgreCausticCubeMapRenderingRun.h"
|
---|
5 | #include "OgreCubeMapRenderingRun.h"
|
---|
6 |
|
---|
7 | OgreCausticReceiverRenderTechnique::OgreCausticReceiverRenderTechnique(
|
---|
8 | int maxcasters,
|
---|
9 | String causticVertexProgram,
|
---|
10 | String causticFragmentProgram,
|
---|
11 | SceneBlendFactor passBlendingSRC,
|
---|
12 | SceneBlendFactor passBlendingDEST,
|
---|
13 | bool createNewPasses,
|
---|
14 | int startTextureUnitID,
|
---|
15 | String casterCenterVariableName,
|
---|
16 | String attenuationVariableName,
|
---|
17 | Pass* pass,
|
---|
18 | OgreRenderable* parentRenderable,
|
---|
19 | OgreTechniqueGroup* parentTechniqueGroup)
|
---|
20 | :RenderTechnique( parentRenderable, parentTechniqueGroup),
|
---|
21 | OgreRenderTechnique(pass, parentRenderable, parentTechniqueGroup),
|
---|
22 | CausticReceiverRenderTechnique(parentRenderable, parentTechniqueGroup)
|
---|
23 | {
|
---|
24 | this->passBlendingSRC = passBlendingSRC;
|
---|
25 | this->passBlendingDEST = passBlendingDEST;
|
---|
26 | this->maxcasters = maxcasters;
|
---|
27 | this->causticVertexProgram = causticVertexProgram;
|
---|
28 | this->causticFragmentProgram = causticFragmentProgram;
|
---|
29 | this->createNewPasses = createNewPasses;
|
---|
30 | this->startTextureUnitID = startTextureUnitID;
|
---|
31 | this->casterCenterVariableName = casterCenterVariableName;
|
---|
32 | this->attenuationVariableName = attenuationVariableName;
|
---|
33 |
|
---|
34 | if(createNewPasses)
|
---|
35 | {
|
---|
36 | //insert new passes
|
---|
37 | Ogre::Technique* techn = pass->getParent();
|
---|
38 | Technique::PassIterator it = techn->getPassIterator();
|
---|
39 |
|
---|
40 | int index = 0;
|
---|
41 | while(it.hasMoreElements())
|
---|
42 | {
|
---|
43 | if( it.getNext() == pass)
|
---|
44 | break;
|
---|
45 | index++;
|
---|
46 | it.moveNext();
|
---|
47 | }
|
---|
48 |
|
---|
49 | index++;
|
---|
50 | for(int i = 0; i < maxcasters; i++)
|
---|
51 | {
|
---|
52 | int lastpass = techn->getNumPasses();
|
---|
53 | Pass* newpass = techn->createPass();
|
---|
54 | passes.push_back(newpass);
|
---|
55 |
|
---|
56 | newpass->setVertexProgram(causticVertexProgram);
|
---|
57 | newpass->setFragmentProgram(causticFragmentProgram);
|
---|
58 |
|
---|
59 | GpuProgramParameters* Vparams = newpass->getVertexProgramParameters().getPointer();
|
---|
60 | Vparams->setNamedAutoConstant("WorldViewProj",
|
---|
61 | GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
|
---|
62 | Vparams->setNamedAutoConstant("World",
|
---|
63 | GpuProgramParameters::ACT_WORLD_MATRIX);
|
---|
64 | GpuProgramParameters* Fparams = newpass->getFragmentProgramParameters().getPointer();
|
---|
65 | //Fparams->setNamedConstant("cubeMapCameraPosition", Vector3(0,0,0));
|
---|
66 |
|
---|
67 | TextureUnitState* st = newpass->createTextureUnitState();
|
---|
68 | TextureUnitState* st2 = newpass->createTextureUnitState();
|
---|
69 |
|
---|
70 | st->setTextureFiltering(TFO_BILINEAR);
|
---|
71 | st2->setTextureFiltering(TFO_BILINEAR);
|
---|
72 |
|
---|
73 | newpass->setSceneBlending(passBlendingSRC, passBlendingDEST);
|
---|
74 | newpass->setDepthBias(1);
|
---|
75 |
|
---|
76 | techn->movePass(lastpass, index);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | OgreCausticReceiverRenderTechnique::~OgreCausticReceiverRenderTechnique()
|
---|
83 | {
|
---|
84 |
|
---|
85 | }
|
---|
86 |
|
---|
87 | void OgreCausticReceiverRenderTechnique::update(unsigned long frameNum)
|
---|
88 | {
|
---|
89 | //find nearest casters
|
---|
90 | causticCasters.clear();
|
---|
91 | OgreIlluminationManager::getSingleton().getNearestCausticCasters(
|
---|
92 | ((OgreSharedRuns*)sharedRuns)->getRootPosition(),
|
---|
93 | &causticCasters,
|
---|
94 | maxcasters);
|
---|
95 |
|
---|
96 | //fill passes
|
---|
97 | for(unsigned int i = 0; i < maxcasters; i++)
|
---|
98 | {
|
---|
99 | if(causticCasters.size() > i)
|
---|
100 | {
|
---|
101 | //update caustic caster
|
---|
102 | causticCasters.at(i)->updateRun(ILLUMRUN_PHOTONMAP, frameNum);
|
---|
103 | causticCasters.at(i)->updateRun(ILLUMRUN_CAUSTIC_CUBEMAP, frameNum);
|
---|
104 |
|
---|
105 | //set texture to pass
|
---|
106 | OgreCausticCubeMapRenderingRun* cauCubeRun =
|
---|
107 | (OgreCausticCubeMapRenderingRun*) causticCasters.at(i)->
|
---|
108 | getRun(ILLUMRUN_CAUSTIC_CUBEMAP)->asOgreRenderingRun();
|
---|
109 |
|
---|
110 | Pass* passToSet = this->pass;
|
---|
111 | int textureUnit = startTextureUnitID + i;
|
---|
112 | if(createNewPasses)
|
---|
113 | {
|
---|
114 | passToSet = passes.at(i);
|
---|
115 | passToSet->setActive(true);
|
---|
116 | textureUnit = 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | passToSet->getTextureUnitState(textureUnit)->setTextureName(
|
---|
120 | cauCubeRun->getCausticCubeMapTextureName());
|
---|
121 |
|
---|
122 | //set caster position nad caustic attenuation
|
---|
123 | GpuProgramParameters* Fparams = passToSet->getFragmentProgramParameters().getPointer();
|
---|
124 | String centerVarName = casterCenterVariableName;
|
---|
125 | String attenuationVarName = attenuationVariableName;
|
---|
126 | if(!createNewPasses)
|
---|
127 | {
|
---|
128 | centerVarName += String("[") + StringConverter::toString(i) + String("]");
|
---|
129 | attenuationVarName += String("[") + StringConverter::toString(i) + String("]");
|
---|
130 | }
|
---|
131 | Fparams->setNamedConstant(centerVarName,
|
---|
132 | causticCasters.at(i)->getRootPosition(ILLUMRUN_CAUSTIC_CUBEMAP));
|
---|
133 | Fparams->setNamedConstant(attenuationVarName,
|
---|
134 | cauCubeRun->getAttenuation());
|
---|
135 | }
|
---|
136 | else if(createNewPasses)
|
---|
137 | passes.at(i)->setActive(false);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | namespace CausticReceiverParsers
|
---|
143 | {
|
---|
144 | ///Technique parsers
|
---|
145 | void parseMaxCasters(String& params, RenderTechniqueFactory* factory)
|
---|
146 | {
|
---|
147 | OgreCausticReceiverRenderTechniqueFactory* f = (OgreCausticReceiverRenderTechniqueFactory*) factory;
|
---|
148 | f->maxcasters = StringConverter::parseInt(params);
|
---|
149 | }
|
---|
150 |
|
---|
151 | void parseVertexProgram(String& params, RenderTechniqueFactory* factory)
|
---|
152 | {
|
---|
153 | OgreCausticReceiverRenderTechniqueFactory* f = (OgreCausticReceiverRenderTechniqueFactory*) factory;
|
---|
154 | f->causticVertexProgram = params;
|
---|
155 | }
|
---|
156 |
|
---|
157 | void parseFragmentProgram(String& params, RenderTechniqueFactory* factory)
|
---|
158 | {
|
---|
159 | OgreCausticReceiverRenderTechniqueFactory* f = (OgreCausticReceiverRenderTechniqueFactory*) factory;
|
---|
160 | f->causticFragmentProgram = params;
|
---|
161 | }
|
---|
162 |
|
---|
163 | SceneBlendFactor convertBlendFactor(const String& param)
|
---|
164 | {
|
---|
165 | if (param == "one")
|
---|
166 | return SBF_ONE;
|
---|
167 | else if (param == "zero")
|
---|
168 | return SBF_ZERO;
|
---|
169 | else if (param == "dest_colour")
|
---|
170 | return SBF_DEST_COLOUR;
|
---|
171 | else if (param == "src_colour")
|
---|
172 | return SBF_SOURCE_COLOUR;
|
---|
173 | else if (param == "one_minus_dest_colour")
|
---|
174 | return SBF_ONE_MINUS_DEST_COLOUR;
|
---|
175 | else if (param == "one_minus_src_colour")
|
---|
176 | return SBF_ONE_MINUS_SOURCE_COLOUR;
|
---|
177 | else if (param == "dest_alpha")
|
---|
178 | return SBF_DEST_ALPHA;
|
---|
179 | else if (param == "src_alpha")
|
---|
180 | return SBF_SOURCE_ALPHA;
|
---|
181 | else if (param == "one_minus_dest_alpha")
|
---|
182 | return SBF_ONE_MINUS_DEST_ALPHA;
|
---|
183 | else if (param == "one_minus_src_alpha")
|
---|
184 | return SBF_ONE_MINUS_SOURCE_ALPHA;
|
---|
185 | }
|
---|
186 |
|
---|
187 | void parsePassBlending(String& params, RenderTechniqueFactory* factory)
|
---|
188 | {
|
---|
189 | OgreCausticReceiverRenderTechniqueFactory* f = (OgreCausticReceiverRenderTechniqueFactory*) factory;
|
---|
190 | StringVector vecparams = StringUtil::split(params, " \t");
|
---|
191 | if(vecparams.size() == 1)
|
---|
192 | {
|
---|
193 | if (vecparams[0] == "none")
|
---|
194 | {
|
---|
195 | f->passBlendingSRC = SBF_ONE;
|
---|
196 | f->passBlendingDEST = SBF_ZERO;
|
---|
197 | }
|
---|
198 | if (vecparams[0] == "add")
|
---|
199 | {
|
---|
200 | f->passBlendingSRC = SBF_ONE;
|
---|
201 | f->passBlendingDEST = SBF_ONE;
|
---|
202 | }
|
---|
203 | else if (vecparams[0] == "modulate")
|
---|
204 | {
|
---|
205 | f->passBlendingSRC = SBF_DEST_COLOUR;
|
---|
206 | f->passBlendingDEST = SBF_ZERO;
|
---|
207 | }
|
---|
208 | else if (vecparams[0] == "colour_blend")
|
---|
209 | {
|
---|
210 | f->passBlendingSRC = SBF_SOURCE_COLOUR;
|
---|
211 | f->passBlendingDEST = SBF_ONE_MINUS_SOURCE_COLOUR;
|
---|
212 | }
|
---|
213 | else if (vecparams[0] == "alpha_blend")
|
---|
214 | {
|
---|
215 | f->passBlendingSRC = SBF_SOURCE_ALPHA;
|
---|
216 | f->passBlendingDEST = SBF_ONE_MINUS_SOURCE_ALPHA;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | else if (vecparams.size() == 2)
|
---|
220 | {
|
---|
221 | f->passBlendingSRC = convertBlendFactor(vecparams[0]);
|
---|
222 | f->passBlendingDEST = convertBlendFactor(vecparams[1]) ;
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | void parseCreateNewPasses(String& params, RenderTechniqueFactory* factory)
|
---|
227 | {
|
---|
228 | OgreCausticReceiverRenderTechniqueFactory* f = (OgreCausticReceiverRenderTechniqueFactory*) factory;
|
---|
229 | f->createNewPasses = StringConverter::parseBool(params);
|
---|
230 | }
|
---|
231 |
|
---|
232 | void parseStartTexID(String& params, RenderTechniqueFactory* factory)
|
---|
233 | {
|
---|
234 | OgreCausticReceiverRenderTechniqueFactory* f = (OgreCausticReceiverRenderTechniqueFactory*) factory;
|
---|
235 | f->startTextureUnitID = StringConverter::parseInt(params);
|
---|
236 | }
|
---|
237 |
|
---|
238 | void parseCenterVarName(String& params, RenderTechniqueFactory* factory)
|
---|
239 | {
|
---|
240 | OgreCausticReceiverRenderTechniqueFactory* f = (OgreCausticReceiverRenderTechniqueFactory*) factory;
|
---|
241 | f->casterCenterVariableName = params;
|
---|
242 | }
|
---|
243 |
|
---|
244 | void parseAttenuationVarName(String& params, RenderTechniqueFactory* factory)
|
---|
245 | {
|
---|
246 | OgreCausticReceiverRenderTechniqueFactory* f = (OgreCausticReceiverRenderTechniqueFactory*) factory;
|
---|
247 | f->attenuationVariableName = params;
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | OgreCausticReceiverRenderTechniqueFactory::OgreCausticReceiverRenderTechniqueFactory()
|
---|
252 | {
|
---|
253 | typeName = "CausticReceiver";
|
---|
254 |
|
---|
255 | using namespace CausticReceiverParsers;
|
---|
256 | //register parsers
|
---|
257 | this->attributeParsers.insert(AttribParserList::value_type("max_caster_count", (ILLUM_ATTRIBUTE_PARSER) parseMaxCasters));
|
---|
258 | this->attributeParsers.insert(AttribParserList::value_type("vertex_program_name", (ILLUM_ATTRIBUTE_PARSER) parseVertexProgram));
|
---|
259 | this->attributeParsers.insert(AttribParserList::value_type("fragment_program_name", (ILLUM_ATTRIBUTE_PARSER) parseFragmentProgram));
|
---|
260 | this->attributeParsers.insert(AttribParserList::value_type("pass_blending", (ILLUM_ATTRIBUTE_PARSER) parsePassBlending));
|
---|
261 | this->attributeParsers.insert(AttribParserList::value_type("new_passes", (ILLUM_ATTRIBUTE_PARSER) parseCreateNewPasses));
|
---|
262 | this->attributeParsers.insert(AttribParserList::value_type("start_tex_id", (ILLUM_ATTRIBUTE_PARSER) parseStartTexID));
|
---|
263 | this->attributeParsers.insert(AttribParserList::value_type("center_var_name", (ILLUM_ATTRIBUTE_PARSER) parseCenterVarName));
|
---|
264 | this->attributeParsers.insert(AttribParserList::value_type("attenuation_var_name", (ILLUM_ATTRIBUTE_PARSER) parseAttenuationVarName));
|
---|
265 | }
|
---|
266 |
|
---|
267 | OgreRenderTechnique* OgreCausticReceiverRenderTechniqueFactory::createInstance(
|
---|
268 | IllumTechniqueParams* params,
|
---|
269 | Pass* pass,
|
---|
270 | OgreRenderable* parentRenderable,
|
---|
271 | OgreTechniqueGroup* parentTechniqueGroup)
|
---|
272 | {
|
---|
273 | //reset parameters
|
---|
274 | maxcasters = 1;
|
---|
275 | causticVertexProgram = "GTP/Basic/Shaded_VS";
|
---|
276 | causticFragmentProgram = "GTP/Caustic/GatherCaustic_Cube_PS";
|
---|
277 | passBlendingSRC = SBF_DEST_COLOUR;
|
---|
278 | passBlendingDEST = SBF_ONE;
|
---|
279 | createNewPasses = true;
|
---|
280 | startTextureUnitID = 0;
|
---|
281 | casterCenterVariableName = "cubeMapCameraPosition";
|
---|
282 | String attenuationVariableName = "attenuation";
|
---|
283 |
|
---|
284 | parseParams(params);
|
---|
285 |
|
---|
286 | OgreCausticReceiverRenderTechnique* result = new OgreCausticReceiverRenderTechnique(
|
---|
287 | maxcasters,
|
---|
288 | causticVertexProgram,
|
---|
289 | causticFragmentProgram,
|
---|
290 | passBlendingSRC,
|
---|
291 | passBlendingDEST,
|
---|
292 | createNewPasses,
|
---|
293 | startTextureUnitID,
|
---|
294 | casterCenterVariableName,
|
---|
295 | attenuationVariableName,
|
---|
296 | pass,
|
---|
297 | parentRenderable,
|
---|
298 | parentTechniqueGroup);
|
---|
299 |
|
---|
300 | return result;
|
---|
301 | }
|
---|