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

Revision 3255, 5.7 KB checked in by szirmay, 15 years ago (diff)
Line 
1#include "OgrePhotonMapRenderingRun.h"
2//#include "OgreIlluminationManager.h"
3#include "OgreCubeMapRenderingRun.h"
4
5
6OgrePhotonMapRenderingRun::OgrePhotonMapRenderingRun(OgreSharedRuns* sharedRuns,
7                                                                                                                   String name,
8                                                                                                                   unsigned long startFrame,
9                                                                                                                        unsigned long updateInterval,
10                                                                                                                        unsigned int resolution,
11                                                                                                                   String materialName,
12                                                                                                                    bool useDistance
13                                                                                                                   )
14                                                                                                                   :PhotonMapRenderingRun( startFrame, updateInterval)
15                                                                                                                   , OgreRenderingRun(startFrame, updateInterval)
16                                                                                                                   , RenderingRun(startFrame, updateInterval)
17{
18        this->useDistance = useDistance;
19        this->sharedRuns = sharedRuns;
20        this->name = name;     
21        this->resolution = resolution;
22        this->light = 0;
23        this->materialName = materialName;
24
25        createPhotonMap();     
26}
27
28void OgrePhotonMapRenderingRun::createPhotonMap()
29{
30        TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(name,
31                                                                                                                                                "default",
32                                                                                                                                                TEX_TYPE_2D,
33                                                                                                                                                resolution,
34                                                                                                                                                resolution,
35                                                                                                                                                0,
36                                                                                                                                                0,
37                                                                                                                                                PF_FLOAT32_RGBA,
38                                                                                                                                                TU_RENDERTARGET);
39         photonMapTexture = texPtr.getPointer();
40         photonMapCamera = Root::getSingleton()._getCurrentSceneManager()->createCamera(name + "_CAMERA");
41         //add viewport to rendertarget
42         HardwarePixelBuffer* hpb = (photonMapTexture->getBuffer()).getPointer();
43         RenderTarget* rt = hpb->getRenderTarget();
44         Viewport* v = rt->addViewport(photonMapCamera);
45         v->setOverlaysEnabled(false);
46         rt->setAutoUpdated(false);
47
48         photonMapCamera->setLodBias(0.0001);
49}
50
51void OgrePhotonMapRenderingRun::updateFrame(unsigned long frameNum)
52{
53        refreshLight();
54
55        if(useDistance)
56        {
57                sharedRuns->updateRun(ILLUMRUN_DISTANCE_CUBEMAP, frameNum);
58        }
59
60        Material* mat = (Material*) MaterialManager::getSingleton().getByName(materialName).getPointer();
61        GpuProgramParameters* Fparams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters().getPointer();
62        Fparams->setNamedConstant("lastCenter", sharedRuns->getRootPosition(ILLUMRUN_PHOTONMAP));
63       
64    SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
65        RenderQueue* rq = sm->getRenderQueue();
66        rq->clear();
67
68        ((OgreSharedRuns*)sharedRuns->getRoot(ILLUMRUN_PHOTONMAP))->notifyCamera(photonMapCamera);
69        ((OgreSharedRuns*)sharedRuns->getRoot(ILLUMRUN_PHOTONMAP))->addRenderablesToQueue(rq);
70       
71        setMaterialForRenderables(materialName,rq);
72       
73        RenderTarget* rt = photonMapTexture->getBuffer().getPointer()->getRenderTarget();
74         
75        rt->update();   
76        /*
77        static int framecount = 0;
78        String filename = "photon";
79        filename.append(this->name);
80        //filename.append(StringConverter::toString(framecount));
81        filename.append(".png");
82        rt->writeContentsToFile(filename);
83        framecount++;*/
84       
85       
86        restoreMaterials();
87}
88
89void OgrePhotonMapRenderingRun::refreshLight()
90{
91        ///TODO:
92        ///search nearest light, set light params
93        SceneManager* sm = Root::getSingleton()._getCurrentSceneManager();
94        LightList list;
95        sm->_populateLightList(((OgreSharedRuns*)sharedRuns)->getRootPosition(ILLUMRUN_PHOTONMAP),100000, list);
96       
97        light = *(list.begin());
98
99        sharedRuns->updateBounds();
100
101        if(light!= 0)
102        {
103                if(light->getType() == Light::LT_DIRECTIONAL)
104                {
105                        Vector3 dir = light->getDerivedDirection();
106                        photonMapCamera->setDirection( dir );
107                        Real r = sharedRuns->getRootBoundingSphere(ILLUMRUN_PHOTONMAP).getRadius();
108                        Vector3 center = sharedRuns->getRootPosition(ILLUMRUN_PHOTONMAP);
109                        photonMapCamera->setPosition( center - dir * r);
110                        photonMapCamera->setProjectionType(PT_ORTHOGRAPHIC);
111                        Matrix4 proj;
112                        proj = Matrix4::IDENTITY;
113                        proj.setScale(Vector3(1.0/r, 1.0/r, -1.5/r));
114                        //proj.setTrans(Vector3(0,0,-1));
115                        photonMapCamera->setCustomProjectionMatrix(true,proj);
116                }
117                else
118                {                       
119                        Vector3 pos = light->getDerivedPosition();
120                        Vector3 center = sharedRuns->getRootPosition(ILLUMRUN_PHOTONMAP);
121                        Vector3 dir = center - pos;
122                        Real r = sharedRuns->getRootBoundingSphere(ILLUMRUN_PHOTONMAP).getRadius();
123                        Real d = dir.length();
124                        dir.normalise();
125                        float mindist = 1.5 * r;
126                        if(d < mindist)
127                        {
128                                pos -= dir * (mindist - d) ;
129                                d = mindist;
130                        }
131                        photonMapCamera->setPosition(pos);
132                        Radian alfa = Math::ASin(r/d);
133                        photonMapCamera->setFOVy(2*alfa);
134                        photonMapCamera->setDirection( dir );
135
136                        photonMapCamera->setNearClipDistance(std::max(0.01f, d - r));
137                        photonMapCamera->setFarClipDistance(r + d);     
138                }
139        }
140}
141
142void OgrePhotonMapRenderingRun::distanceCubeMapChanged(RenderingRun* run)
143{
144        Material* mat = (Material*) MaterialManager::getSingleton().getByName(materialName).getPointer();
145        OgreCubeMapRenderingRun* cuberun =(OgreCubeMapRenderingRun*) (run->asOgreRenderingRun());
146        String cubemapname = cuberun->getCubeMapTextureName();
147        mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(cubemapname);
148}
149
150void OgrePhotonMapRenderingRun::distanceCubeMapUpdated(RenderingRun* run)
151{
152        Material* mat = (Material*) MaterialManager::getSingleton().getByName(materialName).getPointer();
153        OgreCubeMapRenderingRun* cuberun =(OgreCubeMapRenderingRun*) (run->asOgreRenderingRun());
154        String cubemapname = cuberun->getCubeMapTextureName();
155        GpuProgramParametersSharedPtr fpParams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
156        Vector3 center = ((OgreSharedRuns*) sharedRuns)->getRootPosition(ILLUMRUN_DISTANCE_CUBEMAP);
157        fpParams->setNamedConstant("lastCenter",center);
158}
159
160void OgrePhotonMapRenderingRun::freeAllResources()
161{
162        this->photonMapTexture = 0;
163        TextureManager::getSingleton().remove(name);
164        Root::getSingleton()._getCurrentSceneManager()->destroyCamera(name + "_CAMERA");       
165}
Note: See TracBrowser for help on using the repository browser.