source: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgrePhotonMapRenderingRun.cpp @ 808

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