source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/source/OgreIllumModule/src/RenderingRuns/OgreFocusingMapRenderingRun.cpp @ 3255

Revision 3255, 3.4 KB checked in by szirmay, 15 years ago (diff)
Line 
1#include "OgreFocusingMapRenderingRun.h"
2
3
4void OgreFocusingMapRenderingRun::getMinMax(Vector3& min, Vector3& max)
5{       
6        unsigned int buffersize = focusingMapSize * focusingMapSize * 4;
7        float* floatbuffer = new float[buffersize];
8        PixelBox lockBox(focusingMapSize, focusingMapSize, 1, PF_FLOAT32_RGBA, floatbuffer);
9        focusingTexture->getBuffer()->blitToMemory(lockBox);
10       
11    float minX = 0;
12        float minY = 0;
13        float minZ = 0;
14        float maxX = 0;
15        float maxY = 0;
16        float maxZ = 0;
17
18        bool first = true;
19       
20        for(unsigned int i = 0; i < buffersize; i+= 4)
21        {
22                float x = floatbuffer[i];
23                float y = floatbuffer[i + 1];
24                float z = floatbuffer[i + 2];
25                float valid = floatbuffer[i + 3];
26
27                if(valid)
28                {
29                        if(first)
30                        {
31                                minX = x;
32                                minY = y;
33                                minZ = z;
34                                maxX = x;
35                                maxY = y;
36                                maxZ = z;
37
38                                first = false;
39                        }
40                        else
41                        {
42                                if(x < minX)minX = x;
43                                if(y < minY)minY = y;
44                                if(z < minZ)minZ = z;
45
46                                if(x > maxX)maxX = x;
47                                if(y > maxY)maxY = y;
48                                if(z > maxZ)maxZ = z;
49                        }
50                }
51        }
52
53        min.x = minX; min.y = minY; min.z = minZ;
54        max.x = maxX; max.y = maxY; max.z = maxZ;
55
56        delete[] floatbuffer;
57}
58
59OgreFocusingMapRenderingRun::OgreFocusingMapRenderingRun(       String name,
60                                                                                                                        Matrix4 lightMatrix,
61                                                                                                                        unsigned int focusingMapSize
62                                                                                                                   )
63                                                                                                                   : OgreRenderingRun(1, 1)
64                                                                                                                   , RenderingRun(1, 1)
65{
66        this->name = name;
67        this->lightMatrix = lightMatrix;
68        this->focusingMapSize = focusingMapSize;       
69       
70        createFocusingMap();   
71}
72
73void OgreFocusingMapRenderingRun::setCameraMatrices(const Matrix4 &view, const Matrix4 &projection)
74{
75        camera->setCustomViewMatrix(true, view);
76        camera->setCustomProjectionMatrix(true, projection);
77}
78
79void OgreFocusingMapRenderingRun::createFocusingMap()
80{
81        int width = focusingMapSize;
82        int height = focusingMapSize;
83        TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(name,
84                                                                                                                                                "default",
85                                                                                                                                                TEX_TYPE_2D,
86                                                                                                                                                width,
87                                                                                                                                                height,
88                                                                                                                                                0,
89                                                                                                                                                0,
90                                                                                                                                                PF_FLOAT32_RGBA,
91                                                                                                                                                TU_RENDERTARGET);
92        focusingTexture = texPtr.getPointer();
93         //add viewport to rendertarget
94         HardwarePixelBuffer* hpb = (focusingTexture->getBuffer()).getPointer();
95         RenderTarget* rt = hpb->getRenderTarget();
96         this->camera = Root::getSingleton()._getCurrentSceneManager()->createCamera(name + "_CAMERA");
97         Viewport* v = rt->addViewport(camera);
98         v->setOverlaysEnabled(false);
99         rt->setAutoUpdated(false);
100}
101
102
103
104void OgreFocusingMapRenderingRun::updateFrame(unsigned long frameNum)
105{
106       
107       
108        MaterialPtr mat = MaterialManager::getSingleton().getByName("GTP/Basic/Focusing");
109        GpuProgramParameters* Vparams = mat->getTechnique(0)->getPass(0)->getVertexProgramParameters().getPointer();
110        Vparams->setNamedConstant("LightViewProj", lightMatrix);
111               
112        setMaterialForVisibles(String("GTP/Basic/Focusing"), camera);
113
114        RenderTarget* rt = focusingTexture->getBuffer().getPointer()->getRenderTarget();
115
116        ColourValue backColor(0,0,0,0);
117        rt->getViewport(0)->setBackgroundColour(backColor);
118               
119        rt->update();   
120
121        //rt->writeContentsToFile("focusingmap.dds");
122       
123        restoreMaterials();     
124}
125
126void OgreFocusingMapRenderingRun::freeAllResources()
127{
128        this->focusingTexture = 0;
129        TextureManager::getSingleton().remove(name);
130        Root::getSingleton()._getCurrentSceneManager()->destroyCamera(name + "_CAMERA");       
131}
132
Note: See TracBrowser for help on using the repository browser.