1 | #include "OgrePathMapRenderTechnique.h"
|
---|
2 | #include "OgreTechniqueGroup.h"
|
---|
3 | #include "OgreIlluminationManager.h"
|
---|
4 | #include "OgrePMWeightComputeRenderingRun.h"
|
---|
5 |
|
---|
6 |
|
---|
7 |
|
---|
8 | OgrePathMapRenderTechnique::OgrePathMapRenderTechnique(
|
---|
9 | Pass* pass,
|
---|
10 | OgreRenderable* parentRenderable,
|
---|
11 | OgreTechniqueGroup* parentTechniqueGroup)
|
---|
12 | :RenderTechnique( parentRenderable, parentTechniqueGroup),
|
---|
13 | OgreRenderTechnique(pass, parentRenderable, parentTechniqueGroup)
|
---|
14 | {
|
---|
15 | this->clusters = OgreIlluminationManager::getSingleton().getPathMapClusters(parentRenderable->getName());
|
---|
16 |
|
---|
17 | //insert new pass
|
---|
18 | Ogre::Technique* techn = pass->getParent();
|
---|
19 | Technique::PassIterator it = techn->getPassIterator();
|
---|
20 |
|
---|
21 | int index = 0;
|
---|
22 | while(it.hasMoreElements())
|
---|
23 | {
|
---|
24 | if( it.getNext() == pass)
|
---|
25 | break;
|
---|
26 | index++;
|
---|
27 | it.moveNext();
|
---|
28 | }
|
---|
29 |
|
---|
30 | Pass* newpass = pathMapPass = techn->createPass();
|
---|
31 |
|
---|
32 | newpass->setVertexProgram("GTP/PathMap_VS");
|
---|
33 | newpass->setFragmentProgram("GTP/PathMap_PS");
|
---|
34 |
|
---|
35 | //bind vertex program parameters
|
---|
36 | GpuProgramParameters* Vparams = newpass->getVertexProgramParameters().getPointer();
|
---|
37 | Vparams->setNamedAutoConstant("WorldViewProj", GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
|
---|
38 | //bind fragment program parameters
|
---|
39 | GpuProgramParameters* Fparams = newpass->getFragmentProgramParameters().getPointer();
|
---|
40 | int prmxres = clusters->pathMapResolution * clusters->count;
|
---|
41 | int prmyres = clusters->pathMapResolution;
|
---|
42 | while(prmxres > 4096)
|
---|
43 | {
|
---|
44 | prmxres /= 2;
|
---|
45 | prmyres *= 2;
|
---|
46 | }
|
---|
47 | int prmnt[2] = {prmxres / clusters->pathMapResolution, prmyres / clusters->pathMapResolution};
|
---|
48 |
|
---|
49 | float halfPixel[2] = {0.5 / prmxres, 0.5 / prmyres};
|
---|
50 | Vector4 pathMapParameters(prmnt[0],prmnt[1],halfPixel[0],halfPixel[1]);
|
---|
51 | Fparams->setNamedConstant("prmAtlasTilesHalfPixel",pathMapParameters);
|
---|
52 |
|
---|
53 | unsigned int clustercount = OgreIlluminationManager::getSingleton().getPathMapClusterLengthsSize();
|
---|
54 | Fparams->setNamedConstant("allClusterCount", (float) clustercount);
|
---|
55 | PathMapClusters* clusters = OgreIlluminationManager::getSingleton().getPathMapClusters(parentOgreRenderable->getName());
|
---|
56 | Fparams->setNamedConstant("clusterCount", (float) clusters->count);
|
---|
57 |
|
---|
58 | TextureUnitState* st = newpass->createTextureUnitState();
|
---|
59 | st->setTextureFiltering(TFO_BILINEAR);
|
---|
60 | st->setTextureAddressingMode(TextureUnitState::TAM_BORDER);
|
---|
61 | st->setTextureBorderColour(ColourValue::Green);
|
---|
62 | st->setTextureName(clusters->pathMapTextureFilename);
|
---|
63 |
|
---|
64 | createWeightIndexTexture();
|
---|
65 |
|
---|
66 | st = newpass->createTextureUnitState();
|
---|
67 | st->setTextureFiltering(TFO_NONE);
|
---|
68 | st->setTextureAddressingMode(TextureUnitState::TAM_BORDER);
|
---|
69 | st->setTextureBorderColour(ColourValue::Red);
|
---|
70 | st->setTextureName(weightIndexTexture->getName());
|
---|
71 |
|
---|
72 | st = newpass->createTextureUnitState();
|
---|
73 | st->setTextureFiltering(TFO_NONE);
|
---|
74 | st->setTextureAddressingMode(TextureUnitState::TAM_BORDER);
|
---|
75 | st->setTextureBorderColour(ColourValue::Blue);
|
---|
76 |
|
---|
77 | st = newpass->createTextureUnitState();
|
---|
78 | st->setTextureFiltering(TFO_BILINEAR);
|
---|
79 | st->setTextureAddressingMode(TextureUnitState::TAM_WRAP);
|
---|
80 | st->setTextureBorderColour(ColourValue::Blue);
|
---|
81 | st->setTextureName(pass->getTextureUnitState(0)->getTextureName());
|
---|
82 |
|
---|
83 | //newpass->setSceneBlending(SBT_MODULATE);
|
---|
84 | newpass->setSceneBlending(SBT_ADD);
|
---|
85 | //newpass->setSceneBlending(SBF_ONE, SBF_ZERO);
|
---|
86 | newpass->setDepthBias(1);
|
---|
87 |
|
---|
88 |
|
---|
89 |
|
---|
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 | OgrePathMapRenderTechniqueFactory::OgrePathMapRenderTechniqueFactory()
|
---|
161 | {
|
---|
162 | typeName = "PathMap";
|
---|
163 | }
|
---|
164 |
|
---|
165 | OgreRenderTechnique* OgrePathMapRenderTechniqueFactory::createInstance(
|
---|
166 | IllumTechniqueParams* params,
|
---|
167 | Pass* pass,
|
---|
168 | OgreRenderable* parentRenderable,
|
---|
169 | OgreTechniqueGroup* parentTechniqueGroup)
|
---|
170 | {
|
---|
171 | parseParams(params);
|
---|
172 |
|
---|
173 | OgrePathMapRenderTechnique* result = new OgrePathMapRenderTechnique(
|
---|
174 | pass,
|
---|
175 | parentRenderable,
|
---|
176 | parentTechniqueGroup);
|
---|
177 |
|
---|
178 | return result;
|
---|
179 | }
|
---|
180 |
|
---|