source: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp @ 1103

Revision 1103, 5.0 KB checked in by szirmay, 18 years ago (diff)
Line 
1#include "OgreDepthShadowMapRenderingRun.h"
2#include "OgreIlluminationManager.h"
3
4
5OgreDepthShadowMapRenderingRun::OgreDepthShadowMapRenderingRun(OgreSharedRuns* sharedRuns,
6                                                                                                                                String name,
7                                                                                                                                Light* light,
8                                                                                                                                unsigned int resolutionX,
9                                                                                                                                unsigned int resolutionY,
10                                                                                                                                String materialName
11                                                                                                                   )
12                                                                                                                   :DepthShadowMapRenderingRun(resolutionX, resolutionY)
13                                                                                                                   , OgreRenderingRun(1, 1)
14                                                                                                                   , RenderingRun(1, 1)
15{
16        this->light = light;
17        this->sharedRuns = sharedRuns;
18        names[0] = name + "1"; 
19        names[1] = name + "2"; 
20        this->materialName = materialName;
21
22        createDepthMap();       
23}
24
25void OgreDepthShadowMapRenderingRun::createDepthMap()
26{
27        for(int i = 0; i < 1; i++)
28        {
29                TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(names[i],
30                                                                                                                                                        "default",
31                                                                                                                                                        TEX_TYPE_2D,
32                                                                                                                                                        resolutionX,
33                                                                                                                                                        resolutionY,
34                                                                                                                                                        0,
35                                                                                                                                                        0,
36                                                                                                                                                        PF_FLOAT16_RGBA,
37                                                                                                                                                        TU_RENDERTARGET);
38                depthMapTextures[i] = texPtr.getPointer();
39                depthMapCameras[i] = Root::getSingleton()._getCurrentSceneManager()->createCamera(names[i] + "_CAMERA");
40                //add viewport to rendertarget
41                HardwarePixelBuffer* hpb = (depthMapTextures[i]->getBuffer()).getPointer();
42                RenderTarget* rt = hpb->getRenderTarget();
43                Viewport* v = rt->addViewport(depthMapCameras[i]);
44                v->setOverlaysEnabled(false);
45                rt->setAutoUpdated(false);
46        }
47}
48
49void OgreDepthShadowMapRenderingRun::updateFrame(unsigned long frameNum)
50{
51        refreshLight(frameNum);
52
53        if(light->getType() == Light::LT_POINT)
54        {
55                updateDepthMap();
56        }
57        else
58        {
59                updateDepthMap();
60        }
61       
62       
63}
64
65void OgreDepthShadowMapRenderingRun::updateDepthCubeFace(int facenum)
66{
67}
68
69void OgreDepthShadowMapRenderingRun::updateDepthMap()
70{
71        setMaterialForVisibles(materialName, depthMapCameras[0], false);       
72        RenderTarget* rt = depthMapTextures[0]->getBuffer().getPointer()->getRenderTarget();   
73        rt->update();
74        restoreMaterials();
75        //rt->writeContentsToFile("shadowmap1.dds");
76}
77
78void setnear()
79{
80
81}
82
83void OgreDepthShadowMapRenderingRun::refreshLight(unsigned long frameNum)
84{
85       
86        if(light!= 0)
87        {
88                if(light->getType() == Light::LT_DIRECTIONAL)
89                {
90                        Camera* viewcam = OgreIlluminationManager::getSingleton().getMainCamera();
91                       
92                        OgreIlluminationManager::getSingleton().updatePerLightRun(light->getName(),
93                                                                                                                                                ILLUMRUN_FOCUSING_MAP,
94                                                                                                                                                frameNum);
95
96                        OgreFocusingMapRenderingRun* frun = (OgreFocusingMapRenderingRun*)
97                                OgreIlluminationManager::getSingleton().getPerLightRun(light->getName(), ILLUMRUN_FOCUSING_MAP)->asOgreRenderingRun();
98                       
99                        Vector3 min;
100                        Vector3 max;
101                        frun->getMinMax(min, max);
102
103                        Vector3 lightpos = light->getPosition();
104                        Vector3 lightdir = light->getDirection();
105                       
106                        Vector3 center = (min + max) / 2.0;
107                        Vector3 scale = max - min;
108                        scale.x = 2.0 / scale.x;
109                        scale.y = 2.0 / scale.y;
110                        scale.z = 2.0 / scale.z;
111                        center.x *= scale.x;
112                        center.y *= scale.y;
113                        center.z *= scale.z;
114
115
116                        depthMapCameras[0]->setDirection( lightdir );
117                        depthMapCameras[0]->setPosition( lightpos );
118
119                        Matrix4 projection = Matrix4::IDENTITY;
120                        projection.setTrans(center);
121                        projection.setScale(scale);
122                        depthMapCameras[0]->setCustomProjectionMatrix(true, projection);       
123/*
124                        projection = Matrix4::IDENTITY;
125                        projection.setScale(Vector3(1.0 / 100.0, 1.0 / 100.0, -1.0 / 100.0));
126                        depthMapCameras[0]->setCustomProjectionMatrix(true, projection);
127                        depthMapCameras[0]->setPosition( Vector3(50,50,50) );*/
128
129                }
130                else if(light->getType() == Light::LT_SPOTLIGHT)
131                {
132        /*              Vector3 dir = light->getDirection();
133                        depthMapCamera->setDirection( dir );
134                        Vector3 pos = light->getParentNode()->getWorldPosition();
135                        depthMapCamera->setPosition(pos);
136                        depthMapCamera->setProjectionType(PT_PERSPECTIVE);             
137                        depthMapCamera->setFOVy(light->getSpotlightOuterAngle());*/
138                }
139                else//point light
140                {
141        /*              Vector3 pos = light->getParentNode()->getWorldPosition();
142                        Vector3 dir = -pos;
143                        depthMapCamera->setDirection( dir );
144                        depthMapCamera->setPosition(pos);
145                        depthMapCamera->setProjectionType(PT_PERSPECTIVE);
146                        depthMapCamera->setFOVy(Radian(Degree(140)));
147                        depthMapCamera->setNearClipDistance(1);
148                        // depthMapCamera->setFarClipDistance(200);
149       
150                        //OGRE_EXCEPT(0, "NOT implemented for Pointlight", "OgreDepthShadowMapRenderingRun::refreshLight");
151                */     
152                        /*Vector3 pos = light->getParentNode()->getWorldPosition();
153                        Vector3 dir = -pos;
154                        depthMapCamera->setDirection( dir );
155                        depthMapCamera->setPosition(pos);
156                        depthMapCamera->setProjectionType(PT_ORTHOGRAPHIC);
157                        depthMapCamera->*/
158                       
159                }
160        }
161}
162
163Matrix4 OgreDepthShadowMapRenderingRun::getLightViewMatrix(int i)
164{
165        return depthMapCameras[i]->getViewMatrix();
166}
167
168Matrix4 OgreDepthShadowMapRenderingRun::getLightViewProjMatrix(int i)
169{
170        return depthMapCameras[i]->getProjectionMatrix() * depthMapCameras[i]->getViewMatrix();
171}
172
Note: See TracBrowser for help on using the repository browser.