source: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgrePMWeightComputeRenderingRun.cpp @ 2214

Revision 2214, 6.4 KB checked in by szirmay, 17 years ago (diff)
Line 
1#include "OgrePMWeightComputeRenderingRun.h"
2#include "OgreIlluminationManager.h"
3#include "OgrePMEntryPointMapRenderingRun.h"
4
5OgrePMWeightComputeRenderingRun::OgrePMWeightComputeRenderingRun(String name, String LightName)
6                                                                                                                   : OgreRenderingRun(1, 1)
7                                                                                                                   , RenderingRun(1, 1)
8{
9        this->name = name;
10        weights = 0;
11       
12        this->light = Root::getSingleton()._getCurrentSceneManager()->getLight(LightName);
13        createWeightMap();
14
15        OgreIlluminationManager::getSingleton().createGlobalRun(ILLUMRUN_PM_ENTRYPOINTMAP);
16}
17
18void OgrePMWeightComputeRenderingRun::createWeightMap()
19{
20       
21        unsigned int entryPointCnt = OgreIlluminationManager::getSingleton().getPathMapEntryPoints().size();
22        int width = entryPointCnt / 4096;
23        int height = 4096;
24        TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(name + "_ALL",
25                                                                                                                                                "default",
26                                                                                                                                                TEX_TYPE_2D,
27                                                                                                                                                width,
28                                                                                                                                                height,
29                                                                                                                                                0,
30                                                                                                                                                0,
31                                                                                                                                                PF_FLOAT32_R,
32                                                                                                                                                TU_RENDERTARGET);
33         allWeightsTexture = texPtr.getPointer();
34         //add viewport to rendertarget
35         HardwarePixelBuffer* hpb = (allWeightsTexture->getBuffer()).getPointer();
36         RenderTarget* rt = hpb->getRenderTarget();
37         Camera* wc = Root::getSingleton()._getCurrentSceneManager()->createCamera(name + "_ALL_CAMERA");
38         Viewport* v = rt->addViewport(wc);
39         v->setOverlaysEnabled(false);
40         rt->setAutoUpdated(false);
41
42        unsigned int clustercount = OgreIlluminationManager::getSingleton().getPathMapClusterLengthsSize();
43        width = clustercount;
44        height = 1;
45        texPtr = Ogre::TextureManager::getSingleton().createManual(name,
46                                                                                                                                "default",
47                                                                                                                                TEX_TYPE_2D,
48                                                                                                                                width,
49                                                                                                                                height,
50                                                                                                                                0,
51                                                                                                                                0,
52                                                                                                                                PF_FLOAT32_R,
53                                                                                                                                TU_RENDERTARGET);
54         weightTexture = texPtr.getPointer();
55         //add viewport to rendertarget
56         hpb = (weightTexture->getBuffer()).getPointer();
57         rt = hpb->getRenderTarget();
58         wc = Root::getSingleton()._getCurrentSceneManager()->createCamera(name + "_CAMERA");
59         v = rt->addViewport(wc);
60         v->setOverlaysEnabled(false);
61         rt->setAutoUpdated(false);
62}
63
64
65
66void OgrePMWeightComputeRenderingRun::updateFrame(unsigned long frameNum)
67{
68        OgreIlluminationManager::getSingleton().updateGlobalRun(ILLUMRUN_PM_ENTRYPOINTMAP, frameNum);
69        OgrePMEntryPointMapRenderingRun* PMEPrun = (OgrePMEntryPointMapRenderingRun*)
70                OgreIlluminationManager::getSingleton().getGlobalRun(ILLUMRUN_PM_ENTRYPOINTMAP)->asOgreRenderingRun();
71        unsigned int entryPointCnt = OgreIlluminationManager::getSingleton().getPathMapEntryPoints().size();
72        int col = entryPointCnt / 4096;
73               
74        if(light->getType() == Light::LT_SPOTLIGHT)
75        {
76                MaterialPtr mat = MaterialManager::getSingleton().getByName("GTP/PathMap_ComputeWeights");
77
78                GpuProgramParameters* Fparams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters().getPointer();
79                       
80                        Vector3 lightPos = light->getPosition();
81                        Vector3 lightDir = light->getDirection();
82                //      Radian lightAngle = light->getSpotlightOuterAngle();
83                       
84                        //Fparams->setNamedConstant("lightTransform", lightMatrix);
85                        Fparams->setNamedConstant("nRadionColumns", col);
86                        Fparams->setNamedConstant("lightPos", lightPos);
87                        Fparams->setNamedConstant("lightDir", lightDir);
88                       
89                        mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(
90                                                PMEPrun->getEntryPointTextureName());
91                               
92                        RenderTarget* rt = allWeightsTexture->getBuffer().getPointer()->getRenderTarget();
93                        renderFullscreenQuad(mat->getName(), rt);
94
95                        rt->writeContentsToFile("allweights.bmp");
96                //      rt->writeContentsToFile("allweights.dds");
97                       
98                        float* allweights = new float[entryPointCnt / 4096 * 4096];
99                        PixelBox lockBox(entryPointCnt / 4096, 4096, 1, PF_FLOAT32_R, allweights);
100                        allWeightsTexture->getBuffer()->blitToMemory(lockBox);
101
102                        ///////////////////DEBUG
103                        static BillboardSet* entryPointBBSet = 0;
104                        if(entryPointBBSet == 0)
105                        {
106                                SceneManager* sm = Root::getSingleton()._getCurrentSceneManager();
107                                entryPointBBSet = sm->createBillboardSet("PATHMAP_ENTRYPOINTS", entryPointCnt);
108                                entryPointBBSet->setBillboardType(BBT_POINT);
109                                entryPointBBSet->setBillboardsInWorldSpace(true);
110                                entryPointBBSet->setPointRenderingEnabled(true);
111                                for(unsigned int i = 0; i < entryPointCnt; i++)
112                                {
113                                        PathMapEntryPoint EP = OgreIlluminationManager::getSingleton().getPathMapEntryPoints().at(i);
114                                        Vector3 billboardPosition = EP.position;
115                                       
116                                        float uCoord = allweights[i];
117                                        float  vCoord = 0;
118                                        //float uCoord = ((float) (i % col) + 0.5f) / (float) col;
119                                        //float vCoord = ((float)(i / col) + 0.5f) / 4096.0f;
120                                        entryPointBBSet->createBillboard(billboardPosition, ColourValue(uCoord, vCoord, 0, 0));
121                                }
122                                entryPointBBSet->setMaterialName("GTP/PM/EPBillboards");
123                                MaterialPtr mater = MaterialManager::getSingleton().getByName("GTP/PM/EPBillboards");
124                                mater->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(allWeightsTexture->getName());
125                                sm->getRootSceneNode()->createChildSceneNode()->attachObject(entryPointBBSet);
126                        }
127
128                        delete[] allweights;
129
130/////////////////
131        }
132        else
133        {
134                ///not implemented
135        }
136
137        unsigned int clustercount = OgreIlluminationManager::getSingleton().getPathMapClusterLengthsSize();
138
139        MaterialPtr mat = MaterialManager::getSingleton().getByName("GTP/PathMap_SumWeights");
140        GpuProgramParameters* Fparams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters().getPointer();
141        Fparams->setNamedConstant("nRadionColumns", col);
142        Fparams->setNamedConstant("clusterCount", (float) clustercount);
143        mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(
144                                                PMEPrun->getEntryPointTextureName());
145        mat->getTechnique(0)->getPass(0)->getTextureUnitState(1)->setTextureName(
146                                                PMEPrun->getClusterLengthTextureName());
147        mat->getTechnique(0)->getPass(0)->getTextureUnitState(2)->setTextureName(
148                                                allWeightsTexture->getName());
149       
150        RenderTarget* rt = weightTexture->getBuffer().getPointer()->getRenderTarget();
151        renderFullscreenQuad(mat->getName(), rt);
152
153        //rt->writeContentsToFile("weights.bmp");       
154        //rt->writeContentsToFile("weights.dds");
155
156        float* weights = new float[clustercount];
157        PixelBox lockBox(clustercount, 1, 1, PF_FLOAT32_R, weights);
158        allWeightsTexture->getBuffer()->blitToMemory(lockBox);
159
160        delete[] weights;
161}
162
163
Note: See TracBrowser for help on using the repository browser.