1 | #include "OgrePhotonMapRenderingRun.h"
|
---|
2 | //#include "OgreIlluminationManager.h"
|
---|
3 | #include "OgreCubeMapRenderingRun.h"
|
---|
4 |
|
---|
5 |
|
---|
6 | OgrePhotonMapRenderingRun::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 |
|
---|
28 | void 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 |
|
---|
51 | void 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 | restoreMaterials();
|
---|
86 | }
|
---|
87 |
|
---|
88 | void 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 | sharedRuns->updateBounds();
|
---|
99 |
|
---|
100 | if(light!= 0)
|
---|
101 | {
|
---|
102 | if(light->getType() == Light::LT_DIRECTIONAL)
|
---|
103 | {
|
---|
104 | Vector3 dir = light->getDerivedDirection();
|
---|
105 | photonMapCamera->setDirection( dir );
|
---|
106 | Real r = sharedRuns->getRootBoundingSphere(ILLUMRUN_PHOTONMAP).getRadius();
|
---|
107 | Vector3 center = sharedRuns->getRootPosition(ILLUMRUN_PHOTONMAP);
|
---|
108 | photonMapCamera->setPosition( center - dir * r);
|
---|
109 | photonMapCamera->setProjectionType(PT_ORTHOGRAPHIC);
|
---|
110 | Matrix4 proj;
|
---|
111 | proj = Matrix4::IDENTITY;
|
---|
112 | proj.setScale(Vector3(1.0/r, 1.0/r, -1.5/r));
|
---|
113 | //proj.setTrans(Vector3(0,0,-1));
|
---|
114 | photonMapCamera->setCustomProjectionMatrix(true,proj);
|
---|
115 | }
|
---|
116 | else
|
---|
117 | {
|
---|
118 | Vector3 pos = light->getDerivedPosition();
|
---|
119 | Vector3 center = sharedRuns->getRootPosition(ILLUMRUN_PHOTONMAP);
|
---|
120 | Vector3 dir = center - pos;
|
---|
121 | Real r = sharedRuns->getRootBoundingSphere(ILLUMRUN_PHOTONMAP).getRadius();
|
---|
122 | Real d = dir.length();
|
---|
123 | dir.normalise();
|
---|
124 | float mindist = 1.5 * r;
|
---|
125 | if(d < mindist)
|
---|
126 | {
|
---|
127 | pos -= dir * (mindist - d) ;
|
---|
128 | d = mindist;
|
---|
129 | }
|
---|
130 | photonMapCamera->setPosition(pos);
|
---|
131 | Radian alfa = Math::ASin(r/d);
|
---|
132 | photonMapCamera->setFOVy(2*alfa);
|
---|
133 | photonMapCamera->setDirection( dir );
|
---|
134 |
|
---|
135 | photonMapCamera->setNearClipDistance(std::max(0.01f, d - r));
|
---|
136 | photonMapCamera->setFarClipDistance(r + d);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | void OgrePhotonMapRenderingRun::distanceCubeMapChanged(RenderingRun* run)
|
---|
142 | {
|
---|
143 | Material* mat = (Material*) MaterialManager::getSingleton().getByName(materialName).getPointer();
|
---|
144 | OgreCubeMapRenderingRun* cuberun =(OgreCubeMapRenderingRun*) (run->asOgreRenderingRun());
|
---|
145 | String cubemapname = cuberun->getCubeMapTextureName();
|
---|
146 | mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(cubemapname);
|
---|
147 | }
|
---|
148 |
|
---|
149 | void OgrePhotonMapRenderingRun::distanceCubeMapUpdated(RenderingRun* run)
|
---|
150 | {
|
---|
151 | Material* mat = (Material*) MaterialManager::getSingleton().getByName(materialName).getPointer();
|
---|
152 | OgreCubeMapRenderingRun* cuberun =(OgreCubeMapRenderingRun*) (run->asOgreRenderingRun());
|
---|
153 | String cubemapname = cuberun->getCubeMapTextureName();
|
---|
154 | GpuProgramParametersSharedPtr fpParams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
|
---|
155 | Vector3 center = ((OgreSharedRuns*) sharedRuns)->getRootPosition(ILLUMRUN_DISTANCE_CUBEMAP);
|
---|
156 | fpParams->setNamedConstant("lastCenter",center);
|
---|
157 | }
|
---|
158 |
|
---|
159 | void OgrePhotonMapRenderingRun::freeAllResources()
|
---|
160 | {
|
---|
161 | this->photonMapTexture = 0;
|
---|
162 | TextureManager::getSingleton().remove(name);
|
---|
163 | Root::getSingleton()._getCurrentSceneManager()->destroyCamera(name + "_CAMERA");
|
---|
164 | } |
---|