1 | #include "OgrePathMapRenderTechnique.h"
|
---|
2 | #include "OgreTechniqueGroup.h"
|
---|
3 | #include "OgreIlluminationManager.h"
|
---|
4 | #include "OgrePMWeightComputeRenderingRun.h"
|
---|
5 |
|
---|
6 |
|
---|
7 |
|
---|
8 | OgrePathMapRenderTechnique::OgrePathMapRenderTechnique(
|
---|
9 | SceneBlendFactor passBlendingSRC,
|
---|
10 | SceneBlendFactor passBlendingDEST,
|
---|
11 | Pass* pass,
|
---|
12 | OgreRenderable* parentRenderable,
|
---|
13 | OgreTechniqueGroup* parentTechniqueGroup)
|
---|
14 | :RenderTechnique( parentRenderable, parentTechniqueGroup),
|
---|
15 | OgreRenderTechnique(pass, parentRenderable, parentTechniqueGroup)
|
---|
16 | {
|
---|
17 | this->passBlendingSRC = passBlendingSRC;
|
---|
18 | this->passBlendingDEST = passBlendingDEST;
|
---|
19 |
|
---|
20 | this->clusters = OgreIlluminationManager::getSingleton().getPathMapClusters(parentRenderable->getName());
|
---|
21 |
|
---|
22 | //insert new pass
|
---|
23 | Ogre::Technique* techn = pass->getParent();
|
---|
24 | Technique::PassIterator it = techn->getPassIterator();
|
---|
25 |
|
---|
26 | int index = 0;
|
---|
27 | while(it.hasMoreElements())
|
---|
28 | {
|
---|
29 | if( it.getNext() == pass)
|
---|
30 | break;
|
---|
31 | index++;
|
---|
32 | it.moveNext();
|
---|
33 | }
|
---|
34 |
|
---|
35 | Pass* newpass = pathMapPass = techn->createPass();
|
---|
36 |
|
---|
37 | newpass->setVertexProgram("GTP/PathMap_VS");
|
---|
38 | newpass->setFragmentProgram("GTP/PathMap_PS");
|
---|
39 |
|
---|
40 | //bind vertex program parameters
|
---|
41 | GpuProgramParameters* Vparams = newpass->getVertexProgramParameters().getPointer();
|
---|
42 | Vparams->setNamedAutoConstant("WorldViewProj", GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
|
---|
43 | //bind fragment program parameters
|
---|
44 | GpuProgramParameters* Fparams = newpass->getFragmentProgramParameters().getPointer();
|
---|
45 | int prmxres = clusters->pathMapResolution * clusters->count;
|
---|
46 | int prmyres = clusters->pathMapResolution;
|
---|
47 | while(prmxres > 4096)
|
---|
48 | {
|
---|
49 | prmxres /= 2;
|
---|
50 | prmyres *= 2;
|
---|
51 | }
|
---|
52 | int prmnt[2] = {prmxres / clusters->pathMapResolution, prmyres / clusters->pathMapResolution};
|
---|
53 |
|
---|
54 | float halfPixel[2] = {0.5 / prmxres, 0.5 / prmyres};
|
---|
55 | Vector4 pathMapParameters(prmnt[0],prmnt[1],halfPixel[0],halfPixel[1]);
|
---|
56 | Fparams->setNamedConstant("prmAtlasTilesHalfPixel",pathMapParameters);
|
---|
57 |
|
---|
58 | unsigned int clustercount = OgreIlluminationManager::getSingleton().getPathMapClusterLengthsSize();
|
---|
59 | Fparams->setNamedConstant("allClusterCount", (float) clustercount);
|
---|
60 | PathMapClusters* clusters = OgreIlluminationManager::getSingleton().getPathMapClusters(parentOgreRenderable->getName());
|
---|
61 | Fparams->setNamedConstant("clusterCount", (float) clusters->count);
|
---|
62 |
|
---|
63 | TextureUnitState* st = newpass->createTextureUnitState();
|
---|
64 | st->setTextureFiltering(TFO_BILINEAR);
|
---|
65 | st->setTextureAddressingMode(TextureUnitState::TAM_BORDER);
|
---|
66 | st->setTextureBorderColour(ColourValue::Green);
|
---|
67 | st->setTextureName(clusters->pathMapTextureFilename);
|
---|
68 |
|
---|
69 | createWeightIndexTexture();
|
---|
70 |
|
---|
71 | st = newpass->createTextureUnitState();
|
---|
72 | st->setTextureFiltering(TFO_NONE);
|
---|
73 | st->setTextureAddressingMode(TextureUnitState::TAM_BORDER);
|
---|
74 | st->setTextureBorderColour(ColourValue::Red);
|
---|
75 | st->setTextureName(weightIndexTexture->getName());
|
---|
76 |
|
---|
77 | st = newpass->createTextureUnitState();
|
---|
78 | st->setTextureFiltering(TFO_NONE);
|
---|
79 | st->setTextureAddressingMode(TextureUnitState::TAM_BORDER);
|
---|
80 | st->setTextureBorderColour(ColourValue::Blue);
|
---|
81 |
|
---|
82 | st = newpass->createTextureUnitState();
|
---|
83 | st->setTextureFiltering(TFO_BILINEAR);
|
---|
84 | st->setTextureAddressingMode(TextureUnitState::TAM_WRAP);
|
---|
85 | st->setTextureBorderColour(ColourValue::Blue);
|
---|
86 | st->setTextureName(pass->getTextureUnitState(0)->getTextureName());
|
---|
87 |
|
---|
88 | newpass->setSceneBlending(passBlendingSRC, passBlendingDEST);
|
---|
89 | newpass->setDepthBias(1);
|
---|
90 | }
|
---|
91 |
|
---|
92 | OgrePathMapRenderTechnique::~OgrePathMapRenderTechnique()
|
---|
93 | {
|
---|
94 |
|
---|
95 | }
|
---|
96 |
|
---|
97 | void OgrePathMapRenderTechnique::createWeightIndexTexture()
|
---|
98 | {
|
---|
99 | PathMapClusters* clusters = OgreIlluminationManager::getSingleton().getPathMapClusters(parentOgreRenderable->getName());
|
---|
100 | int width = clusters->count;
|
---|
101 | TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(this->parentOgreRenderable->getName() + "_PMWeightIndexTexture",
|
---|
102 | "default",
|
---|
103 | TEX_TYPE_2D,
|
---|
104 | width,
|
---|
105 | 1,
|
---|
106 | 0,
|
---|
107 | 0,
|
---|
108 | PF_FLOAT32_R,
|
---|
109 | TU_DYNAMIC_WRITE_ONLY);
|
---|
110 | weightIndexTexture = texPtr.getPointer();
|
---|
111 |
|
---|
112 | float *weightIndices = new float[width];
|
---|
113 | PixelBox lockBox(width, 1, 1, PF_FLOAT32_R, weightIndices);
|
---|
114 | for(int j = 0; j< clusters->count; j++)
|
---|
115 | weightIndices[j] = clusters->clusters[j];
|
---|
116 | weightIndexTexture->getBuffer()->blitFromMemory(lockBox);
|
---|
117 | delete[] weightIndices;
|
---|
118 | }
|
---|
119 |
|
---|
120 | void OgrePathMapRenderTechnique::update(unsigned long frameNum)
|
---|
121 | {
|
---|
122 | LightList lights;
|
---|
123 | SceneManager* sm = Root::getSingleton()._getCurrentSceneManager();
|
---|
124 | sm->_populateLightList(Vector3(0,0,0), 1000, lights); //TODO
|
---|
125 | //TODO set weights
|
---|
126 | /*
|
---|
127 | unsigned int entryPointCount = OgreIlluminationManager::getSingleton().getPathMapEntryPoints().size();
|
---|
128 | PathMapClusters* clusters = OgreIlluminationManager::getSingleton().getPathMapClusters(parentOgreRenderable->getName());
|
---|
129 | int weightTextureWidth = clusters->count / 4;
|
---|
130 | */
|
---|
131 | for(int i = 0 ; i < 1; i++)
|
---|
132 | {
|
---|
133 | String lightName = lights.at(0)->getName();
|
---|
134 | OgreIlluminationManager::getSingleton().createPerLightRun(lightName, ILLUMRUN_PM_WEIGHTMAP);
|
---|
135 | OgreIlluminationManager::getSingleton().updatePerLightRun(lightName, ILLUMRUN_PM_WEIGHTMAP, frameNum);
|
---|
136 | OgrePMWeightComputeRenderingRun* PMWeightRun = (OgrePMWeightComputeRenderingRun*)
|
---|
137 | OgreIlluminationManager::getSingleton().getPerLightRun(lightName, ILLUMRUN_PM_WEIGHTMAP)->asOgreRenderingRun();
|
---|
138 |
|
---|
139 | TextureUnitState* st = pathMapPass->getTextureUnitState(2);
|
---|
140 | st->setTextureName(PMWeightRun->getPMWeightTetureName());
|
---|
141 |
|
---|
142 | /*
|
---|
143 | TexturePtr tex = TextureManager::getSingleton().getByName(PMWeightRun->getPMWeightTetureName());
|
---|
144 | float *allClusterWeights = new float[entryPointCount];
|
---|
145 | PixelBox lockBox(entryPointCount, 1, 1, PF_FLOAT32_R, allClusterWeights);
|
---|
146 | tex->getBuffer()->blitToMemory(lockBox);
|
---|
147 |
|
---|
148 | float *weightIndices = new float[clusters->count];
|
---|
149 | for(int j = 0; j< clusters->count; j++)
|
---|
150 | weightIndices[j] = allClusterWeights[clusters->clusters[j]];
|
---|
151 |
|
---|
152 | PixelBox lockBox2(weightTextureWidth, 1, 1, PF_FLOAT32_RGBA, weightIndices);
|
---|
153 | weightIndexTexture->getBuffer()->blitFromMemory(lockBox2);
|
---|
154 |
|
---|
155 | TextureUnitState* st = pathMapPass->getTextureUnitState(1);
|
---|
156 | st->setTextureName(weightIndexTexture->getName()); */
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | namespace PathMapParsers
|
---|
161 | {
|
---|
162 | SceneBlendFactor convertBlendFactor(const String& param)
|
---|
163 | {
|
---|
164 | if (param == "one")
|
---|
165 | return SBF_ONE;
|
---|
166 | else if (param == "zero")
|
---|
167 | return SBF_ZERO;
|
---|
168 | else if (param == "dest_colour")
|
---|
169 | return SBF_DEST_COLOUR;
|
---|
170 | else if (param == "src_colour")
|
---|
171 | return SBF_SOURCE_COLOUR;
|
---|
172 | else if (param == "one_minus_dest_colour")
|
---|
173 | return SBF_ONE_MINUS_DEST_COLOUR;
|
---|
174 | else if (param == "one_minus_src_colour")
|
---|
175 | return SBF_ONE_MINUS_SOURCE_COLOUR;
|
---|
176 | else if (param == "dest_alpha")
|
---|
177 | return SBF_DEST_ALPHA;
|
---|
178 | else if (param == "src_alpha")
|
---|
179 | return SBF_SOURCE_ALPHA;
|
---|
180 | else if (param == "one_minus_dest_alpha")
|
---|
181 | return SBF_ONE_MINUS_DEST_ALPHA;
|
---|
182 | else if (param == "one_minus_src_alpha")
|
---|
183 | return SBF_ONE_MINUS_SOURCE_ALPHA;
|
---|
184 | }
|
---|
185 |
|
---|
186 | void parsePassBlending(String& params, RenderTechniqueFactory* factory)
|
---|
187 | {
|
---|
188 | OgrePathMapRenderTechniqueFactory* f = (OgrePathMapRenderTechniqueFactory*) factory;
|
---|
189 | StringVector vecparams = StringUtil::split(params, " \t");
|
---|
190 | if(vecparams.size() == 1)
|
---|
191 | {
|
---|
192 | if (vecparams[0] == "none")
|
---|
193 | {
|
---|
194 | f->passBlendingSRC = SBF_ONE;
|
---|
195 | f->passBlendingDEST = SBF_ZERO;
|
---|
196 | }
|
---|
197 | if (vecparams[0] == "add")
|
---|
198 | {
|
---|
199 | f->passBlendingSRC = SBF_ONE;
|
---|
200 | f->passBlendingDEST = SBF_ONE;
|
---|
201 | }
|
---|
202 | else if (vecparams[0] == "modulate")
|
---|
203 | {
|
---|
204 | f->passBlendingSRC = SBF_DEST_COLOUR;
|
---|
205 | f->passBlendingDEST = SBF_ZERO;
|
---|
206 | }
|
---|
207 | else if (vecparams[0] == "colour_blend")
|
---|
208 | {
|
---|
209 | f->passBlendingSRC = SBF_SOURCE_COLOUR;
|
---|
210 | f->passBlendingDEST = SBF_ONE_MINUS_SOURCE_COLOUR;
|
---|
211 | }
|
---|
212 | else if (vecparams[0] == "alpha_blend")
|
---|
213 | {
|
---|
214 | f->passBlendingSRC = SBF_SOURCE_ALPHA;
|
---|
215 | f->passBlendingDEST = SBF_ONE_MINUS_SOURCE_ALPHA;
|
---|
216 | }
|
---|
217 | }
|
---|
218 | else if (vecparams.size() == 2)
|
---|
219 | {
|
---|
220 | f->passBlendingSRC = convertBlendFactor(vecparams[0]);
|
---|
221 | f->passBlendingDEST = convertBlendFactor(vecparams[1]) ;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | OgrePathMapRenderTechniqueFactory::OgrePathMapRenderTechniqueFactory()
|
---|
227 | {
|
---|
228 | typeName = "PathMap";
|
---|
229 | using namespace PathMapParsers;
|
---|
230 | this->attributeParsers.insert(AttribParserList::value_type("pass_blending", (ILLUM_ATTRIBUTE_PARSER) parsePassBlending));
|
---|
231 | }
|
---|
232 |
|
---|
233 | OgreRenderTechnique* OgrePathMapRenderTechniqueFactory::createInstance(
|
---|
234 | IllumTechniqueParams* params,
|
---|
235 | Pass* pass,
|
---|
236 | OgreRenderable* parentRenderable,
|
---|
237 | OgreTechniqueGroup* parentTechniqueGroup)
|
---|
238 | {
|
---|
239 | passBlendingSRC = SBF_ONE;
|
---|
240 | passBlendingDEST = SBF_ONE;
|
---|
241 |
|
---|
242 | parseParams(params);
|
---|
243 |
|
---|
244 | OgrePathMapRenderTechnique* result = new OgrePathMapRenderTechnique(
|
---|
245 | passBlendingSRC,
|
---|
246 | passBlendingDEST,
|
---|
247 | pass,
|
---|
248 | parentRenderable,
|
---|
249 | parentTechniqueGroup);
|
---|
250 |
|
---|
251 | return result;
|
---|
252 | }
|
---|
253 |
|
---|