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