source: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderTechniques/OgrePathMapRenderTechnique.cpp @ 2218

Revision 2218, 6.4 KB checked in by szirmay, 17 years ago (diff)
Line 
1#include "OgrePathMapRenderTechnique.h"
2#include "OgreTechniqueGroup.h"
3#include "OgreIlluminationManager.h"
4#include "OgrePMWeightComputeRenderingRun.h"
5
6
7
8OgrePathMapRenderTechnique::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        newpass->setSceneBlending(SBT_MODULATE);
78        //newpass->setSceneBlending(SBF_ONE, SBF_ZERO);
79        newpass->setDepthBias(1);
80       
81
82       
83}
84
85OgrePathMapRenderTechnique::~OgrePathMapRenderTechnique()
86{
87
88}
89
90void OgrePathMapRenderTechnique::createWeightIndexTexture()
91{
92        PathMapClusters* clusters = OgreIlluminationManager::getSingleton().getPathMapClusters(parentOgreRenderable->getName());
93        int width = clusters->count;
94        TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(this->parentOgreRenderable->getName() + "_PMWeightIndexTexture",
95                                                                                                                                                "default",
96                                                                                                                                                TEX_TYPE_2D,
97                                                                                                                                                width,
98                                                                                                                                                1,
99                                                                                                                                                0,
100                                                                                                                                                0,
101                                                                                                                                                PF_FLOAT32_R,
102                                                                                                                                                TU_DYNAMIC_WRITE_ONLY);
103        weightIndexTexture = texPtr.getPointer();
104
105        float *weightIndices = new float[width];
106        PixelBox lockBox(width, 1, 1, PF_FLOAT32_R, weightIndices);
107        for(int j = 0; j< clusters->count; j++)
108                        weightIndices[j] = clusters->clusters[j];
109        weightIndexTexture->getBuffer()->blitFromMemory(lockBox);
110        delete[] weightIndices;
111}
112
113void OgrePathMapRenderTechnique::update(unsigned long frameNum)
114{       
115        LightList lights;
116        SceneManager* sm = Root::getSingleton()._getCurrentSceneManager();
117        sm->_populateLightList(Vector3(0,0,0), 1000, lights); //TODO
118//TODO set weights
119        /*
120        unsigned int entryPointCount = OgreIlluminationManager::getSingleton().getPathMapEntryPoints().size();
121        PathMapClusters* clusters = OgreIlluminationManager::getSingleton().getPathMapClusters(parentOgreRenderable->getName());
122        int weightTextureWidth = clusters->count / 4;
123        */
124        for(int i = 0 ; i < 1; i++)
125        {
126                String lightName = lights.at(0)->getName();
127                OgreIlluminationManager::getSingleton().createPerLightRun(lightName, ILLUMRUN_PM_WEIGHTMAP);
128                OgreIlluminationManager::getSingleton().updatePerLightRun(lightName, ILLUMRUN_PM_WEIGHTMAP, frameNum);
129                OgrePMWeightComputeRenderingRun* PMWeightRun = (OgrePMWeightComputeRenderingRun*)
130                        OgreIlluminationManager::getSingleton().getPerLightRun(lightName, ILLUMRUN_PM_WEIGHTMAP)->asOgreRenderingRun();
131
132                TextureUnitState* st = pathMapPass->getTextureUnitState(2);
133                st->setTextureName(PMWeightRun->getPMWeightTetureName());
134
135                /*
136                TexturePtr tex = TextureManager::getSingleton().getByName(PMWeightRun->getPMWeightTetureName());
137                float *allClusterWeights = new float[entryPointCount];
138                PixelBox lockBox(entryPointCount, 1, 1, PF_FLOAT32_R, allClusterWeights);
139                tex->getBuffer()->blitToMemory(lockBox);
140
141                float *weightIndices = new float[clusters->count];
142                for(int j = 0; j< clusters->count; j++)
143                        weightIndices[j] = allClusterWeights[clusters->clusters[j]];
144               
145                PixelBox lockBox2(weightTextureWidth, 1, 1, PF_FLOAT32_RGBA, weightIndices);
146                weightIndexTexture->getBuffer()->blitFromMemory(lockBox2);
147
148                TextureUnitState* st = pathMapPass->getTextureUnitState(1);
149                st->setTextureName(weightIndexTexture->getName());      */                     
150        }
151}
152
153OgrePathMapRenderTechniqueFactory::OgrePathMapRenderTechniqueFactory()
154{
155        typeName = "PathMap";
156}
157
158OgreRenderTechnique* OgrePathMapRenderTechniqueFactory::createInstance(
159                                                                                IllumTechniqueParams* params,
160                                                                                Pass* pass,
161                                                                                OgreRenderable* parentRenderable,
162                                                                                OgreTechniqueGroup* parentTechniqueGroup)
163{       
164        parseParams(params);
165
166        OgrePathMapRenderTechnique* result = new OgrePathMapRenderTechnique(
167                                                                                                pass,
168                                                                                                parentRenderable,
169                                                                                                parentTechniqueGroup); 
170
171        return result;
172}
173
Note: See TracBrowser for help on using the repository browser.