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