1 | #include "OgrePhotonMapRenderingRun.h"
|
---|
2 | //#include "OgreIlluminationManager.h"
|
---|
3 | #include "OgreDistanceCubeMapRenderingRun.h"
|
---|
4 |
|
---|
5 |
|
---|
6 |
|
---|
7 | OgrePhotonMapRenderingRun::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 |
|
---|
29 | void 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 |
|
---|
52 | void 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(this->name);
|
---|
81 | //filename.append(StringConverter::toString(framecount));
|
---|
82 | filename.append(".png");
|
---|
83 | rt->writeContentsToFile(filename);
|
---|
84 | framecount++;*/
|
---|
85 |
|
---|
86 | restoreMaterials();
|
---|
87 | }
|
---|
88 |
|
---|
89 | void 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 | if(light!= 0)
|
---|
100 | {
|
---|
101 | if(light->getType() == Light::LT_DIRECTIONAL)
|
---|
102 | {
|
---|
103 | Vector3 dir = light->getDirection();
|
---|
104 | photonMapCamera->setDirection( dir );
|
---|
105 | Real r = sharedRuns->getRootBoundingSphere(ILLUMRUN_PHOTONMAP).getRadius();
|
---|
106 | photonMapCamera->setPosition( sharedRuns->getRootPosition(ILLUMRUN_PHOTONMAP) - dir * r);
|
---|
107 | photonMapCamera->setProjectionType(PT_ORTHOGRAPHIC);
|
---|
108 | Matrix4 proj;
|
---|
109 | proj = Matrix4::IDENTITY;
|
---|
110 | proj.setScale(Vector3(1.0/r, 1.0/r, -1.0/r));
|
---|
111 | proj.setTrans(Vector3(0,0,-1));
|
---|
112 | photonMapCamera->setCustomProjectionMatrix(true,proj);
|
---|
113 | }
|
---|
114 | else
|
---|
115 | {
|
---|
116 | Vector3 pos = light->getParentSceneNode()->getPosition();
|
---|
117 | Vector3 dir = sharedRuns->getRootPosition(ILLUMRUN_PHOTONMAP) - pos;
|
---|
118 | Real r = sharedRuns->getRootBoundingSphere(ILLUMRUN_PHOTONMAP).getRadius();
|
---|
119 | Real d = dir.length();
|
---|
120 | dir.normalise();
|
---|
121 | float mindist = 1.5 * r;
|
---|
122 | if(d < mindist)
|
---|
123 | {
|
---|
124 | pos -= dir * (mindist - d) ;
|
---|
125 | d = mindist;
|
---|
126 | }
|
---|
127 | photonMapCamera->setPosition(pos);
|
---|
128 | Radian alfa = Math::ASin(r/d);
|
---|
129 | photonMapCamera->setFOVy(2*alfa);
|
---|
130 | photonMapCamera->setDirection( dir );
|
---|
131 |
|
---|
132 | photonMapCamera->setNearClipDistance(std::max(0.01f, d - r));
|
---|
133 | photonMapCamera->setFarClipDistance(r + d);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | void OgrePhotonMapRenderingRun::distanceCubeMapChanged(RenderingRun* run)
|
---|
139 | {
|
---|
140 | Material* mat = (Material*) MaterialManager::getSingleton().getByName(materialName).getPointer();
|
---|
141 | OgreDistanceCubeMapRenderingRun* cuberun =(OgreDistanceCubeMapRenderingRun*) (run->asOgreRenderingRun());
|
---|
142 | String cubemapname = cuberun->getDistanceCubeMapTextureName();
|
---|
143 | mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(cubemapname);
|
---|
144 | }
|
---|
145 |
|
---|
146 | void OgrePhotonMapRenderingRun::distanceCubeMapUpdated(RenderingRun* run)
|
---|
147 | {
|
---|
148 | Material* mat = (Material*) MaterialManager::getSingleton().getByName(materialName).getPointer();
|
---|
149 | OgreDistanceCubeMapRenderingRun* cuberun =(OgreDistanceCubeMapRenderingRun*) (run->asOgreRenderingRun());
|
---|
150 | String cubemapname = cuberun->getDistanceCubeMapTextureName();
|
---|
151 | GpuProgramParametersSharedPtr fpParams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
|
---|
152 | Vector3 center = ((OgreSharedRuns*) sharedRuns)->getRootPosition(ILLUMRUN_DISTANCE_CUBEMAP);
|
---|
153 | fpParams->setNamedConstant("lastCenter",center);
|
---|
154 | }
|
---|
155 | |
---|