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)
RevLine 
[790]1#include "OgreDepthShadowMapRenderingRun.h"
[1055]2#include "OgreIlluminationManager.h"
[790]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)
[874]13                                                                                                                   , OgreRenderingRun(1, 1)
14                                                                                                                   , RenderingRun(1, 1)
[790]15{
16        this->light = light;
17        this->sharedRuns = sharedRuns;
[1055]18        names[0] = name + "1"; 
19        names[1] = name + "2"; 
[790]20        this->materialName = materialName;
21
22        createDepthMap();       
23}
24
25void OgreDepthShadowMapRenderingRun::createDepthMap()
26{
[1103]27        for(int i = 0; i < 1; i++)
[1055]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        }
[790]47}
48
49void OgreDepthShadowMapRenderingRun::updateFrame(unsigned long frameNum)
50{
[1103]51        refreshLight(frameNum);
[790]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{
[1103]71        setMaterialForVisibles(materialName, depthMapCameras[0], false);       
[1055]72        RenderTarget* rt = depthMapTextures[0]->getBuffer().getPointer()->getRenderTarget();   
73        rt->update();
74        restoreMaterials();
[1103]75        //rt->writeContentsToFile("shadowmap1.dds");
[1055]76}
[790]77
[1055]78void setnear()
79{
[790]80
81}
82
[1103]83void OgreDepthShadowMapRenderingRun::refreshLight(unsigned long frameNum)
[790]84{
85       
86        if(light!= 0)
87        {
88                if(light->getType() == Light::LT_DIRECTIONAL)
89                {
[1055]90                        Camera* viewcam = OgreIlluminationManager::getSingleton().getMainCamera();
91                       
[1103]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();
[1055]98                       
[1103]99                        Vector3 min;
100                        Vector3 max;
101                        frun->getMinMax(min, max);
[1055]102
[1103]103                        Vector3 lightpos = light->getPosition();
104                        Vector3 lightdir = light->getDirection();
[1055]105                       
[1103]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;
[1055]114
115
[1103]116                        depthMapCameras[0]->setDirection( lightdir );
117                        depthMapCameras[0]->setPosition( lightpos );
[1055]118
[1103]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
[790]129                }
130                else if(light->getType() == Light::LT_SPOTLIGHT)
131                {
[1055]132        /*              Vector3 dir = light->getDirection();
[790]133                        depthMapCamera->setDirection( dir );
134                        Vector3 pos = light->getParentNode()->getWorldPosition();
135                        depthMapCamera->setPosition(pos);
136                        depthMapCamera->setProjectionType(PT_PERSPECTIVE);             
[1055]137                        depthMapCamera->setFOVy(light->getSpotlightOuterAngle());*/
[790]138                }
139                else//point light
140                {
[1055]141        /*              Vector3 pos = light->getParentNode()->getWorldPosition();
[790]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");
[1055]151                */     
[790]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
[1055]163Matrix4 OgreDepthShadowMapRenderingRun::getLightViewMatrix(int i)
[790]164{
[1055]165        return depthMapCameras[i]->getViewMatrix();
[790]166}
167
[1055]168Matrix4 OgreDepthShadowMapRenderingRun::getLightViewProjMatrix(int i)
[790]169{
[1055]170        return depthMapCameras[i]->getProjectionMatrix() * depthMapCameras[i]->getViewMatrix();
[790]171}
172
Note: See TracBrowser for help on using the repository browser.