1 | #include "OgreDepthShadowMapRenderingRun.h"
|
---|
2 | #include "OgreIlluminationManager.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | OgreDepthShadowMapRenderingRun::OgreDepthShadowMapRenderingRun(OgreSharedRuns* sharedRuns,
|
---|
6 | String name,
|
---|
7 | Light* light,
|
---|
8 | unsigned int resolutionX,
|
---|
9 | unsigned int resolutionY,
|
---|
10 | String materialName
|
---|
11 | )
|
---|
12 | :DepthShadowMapRenderingRun(resolutionX, resolutionY)
|
---|
13 | , OgreRenderingRun(1, 1)
|
---|
14 | , RenderingRun(1, 1)
|
---|
15 | {
|
---|
16 | this->light = light;
|
---|
17 | this->sharedRuns = sharedRuns;
|
---|
18 | names[0] = name + "1";
|
---|
19 | names[1] = name + "2";
|
---|
20 | this->materialName = materialName;
|
---|
21 |
|
---|
22 | createDepthMap();
|
---|
23 | }
|
---|
24 |
|
---|
25 | void OgreDepthShadowMapRenderingRun::createDepthMap()
|
---|
26 | {
|
---|
27 | for(int i = 0; i < 2; i++)
|
---|
28 | {
|
---|
29 | TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(names[i],
|
---|
30 | "default",
|
---|
31 | TEX_TYPE_2D,
|
---|
32 | resolutionX,
|
---|
33 | resolutionY,
|
---|
34 | 0,
|
---|
35 | 0,
|
---|
36 | PF_FLOAT16_RGBA,
|
---|
37 | TU_RENDERTARGET);
|
---|
38 | depthMapTextures[i] = texPtr.getPointer();
|
---|
39 | depthMapCameras[i] = Root::getSingleton()._getCurrentSceneManager()->createCamera(names[i] + "_CAMERA");
|
---|
40 | //add viewport to rendertarget
|
---|
41 | HardwarePixelBuffer* hpb = (depthMapTextures[i]->getBuffer()).getPointer();
|
---|
42 | RenderTarget* rt = hpb->getRenderTarget();
|
---|
43 | Viewport* v = rt->addViewport(depthMapCameras[i]);
|
---|
44 | v->setOverlaysEnabled(false);
|
---|
45 | rt->setAutoUpdated(false);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | void OgreDepthShadowMapRenderingRun::updateFrame(unsigned long frameNum)
|
---|
50 | {
|
---|
51 | refreshLight();
|
---|
52 |
|
---|
53 | if(light->getType() == Light::LT_POINT)
|
---|
54 | {
|
---|
55 | updateDepthMap();
|
---|
56 | }
|
---|
57 | else
|
---|
58 | {
|
---|
59 | updateDepthMap();
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | }
|
---|
64 |
|
---|
65 | void OgreDepthShadowMapRenderingRun::updateDepthCubeFace(int facenum)
|
---|
66 | {
|
---|
67 | }
|
---|
68 |
|
---|
69 | void OgreDepthShadowMapRenderingRun::updateDepthMap()
|
---|
70 | {
|
---|
71 | setMaterialForVisibles(materialName, depthMapCameras[0], true);
|
---|
72 | RenderTarget* rt = depthMapTextures[0]->getBuffer().getPointer()->getRenderTarget();
|
---|
73 | rt->update();
|
---|
74 | //restoreMaterials();
|
---|
75 | //rt->writeContentsToFile("shadowmap1.dds");
|
---|
76 |
|
---|
77 | //setMaterialForVisibles(materialName, depthMapCameras[1], true);
|
---|
78 | RenderTarget* rt2 = depthMapTextures[1]->getBuffer().getPointer()->getRenderTarget();
|
---|
79 | rt2->update();
|
---|
80 | restoreMaterials();
|
---|
81 | //rt2->writeContentsToFile("shadowmap2.dds");
|
---|
82 |
|
---|
83 |
|
---|
84 | }
|
---|
85 |
|
---|
86 | void setnear()
|
---|
87 | {
|
---|
88 |
|
---|
89 | }
|
---|
90 |
|
---|
91 | void OgreDepthShadowMapRenderingRun::refreshLight()
|
---|
92 | {
|
---|
93 |
|
---|
94 | if(light!= 0)
|
---|
95 | {
|
---|
96 | if(light->getType() == Light::LT_DIRECTIONAL)
|
---|
97 | {
|
---|
98 | //Vector3 dir = light->getDirection();
|
---|
99 | Vector3 dir = Vector3(0,-1,-1);
|
---|
100 | Vector3 pos;
|
---|
101 | float size;
|
---|
102 |
|
---|
103 | Camera* viewcam = OgreIlluminationManager::getSingleton().getMainCamera();
|
---|
104 |
|
---|
105 | //Matrix4 camView = viewcam->getViewMatrix();
|
---|
106 | //Matrix4 camViewI = camView.inverse();
|
---|
107 | //const AxisAlignedBox bt = viewcam->getWorldBoundingBox(true);
|
---|
108 | //AxisAlignedBox b = bt;
|
---|
109 | //b.transform(camView);
|
---|
110 | //SceneManager* sm = Root::getSingleton()._getCurrentSceneManager();
|
---|
111 | //SceneNode* node = sm->getRootSceneNode();
|
---|
112 | //const AxisAlignedBox bs(-35,-5 ,-75, 35, 15, 75);// = node->_getWorldAABB();
|
---|
113 | //AxisAlignedBox intersection = b.intersection(bs);
|
---|
114 | //pos = intersection.getCenter();
|
---|
115 | //size = (intersection.getMaximum() - pos).length();
|
---|
116 |
|
---|
117 | //TODO: where to put light
|
---|
118 | size = 30;
|
---|
119 | pos = Vector3(0,0,0);
|
---|
120 |
|
---|
121 | depthMapCameras[0]->setDirection( dir );
|
---|
122 | pos = pos - size * dir;
|
---|
123 | depthMapCameras[0]->setPosition( pos );
|
---|
124 | depthMapCameras[0]->setProjectionType(PT_ORTHOGRAPHIC);
|
---|
125 | Matrix4 fproj;
|
---|
126 | fproj = depthMapCameras[0]->getProjectionMatrix();
|
---|
127 | fproj = Matrix4::IDENTITY;
|
---|
128 | fproj.setScale(Vector3(1.0/20, 1.0/25, -1.0/100));
|
---|
129 | //fproj.setTrans(Vector3(0,0,-50));
|
---|
130 | depthMapCameras[0]->setCustomProjectionMatrix(true,fproj);
|
---|
131 |
|
---|
132 |
|
---|
133 | //camera2
|
---|
134 | dir.normalise();
|
---|
135 | size = abs(dir.crossProduct(Vector3(0,0,1)).length() * 2);
|
---|
136 | Vector3 viewDir = viewcam->getDirection();
|
---|
137 | float y = viewDir.y;
|
---|
138 | viewDir.y = std::min(0.0f, y);
|
---|
139 | viewDir.normalise();
|
---|
140 | Vector3 down(0,-1,0);
|
---|
141 | float cosA = viewDir.dotProduct(down);
|
---|
142 | Vector3 newDir = viewDir + down * 0.4;
|
---|
143 | newDir.normalise();
|
---|
144 | pos = viewcam->getPosition();
|
---|
145 | size *= pos.y * 0.5 + 1;
|
---|
146 | pos += newDir * viewcam->getPosition().y
|
---|
147 | / newDir.dotProduct(down);
|
---|
148 | //pos.y = 0;
|
---|
149 |
|
---|
150 |
|
---|
151 |
|
---|
152 | depthMapCameras[1]->setDirection( dir );
|
---|
153 | pos = pos - 30 * dir;
|
---|
154 | depthMapCameras[1]->setPosition( pos );
|
---|
155 | depthMapCameras[1]->setProjectionType(PT_ORTHOGRAPHIC);
|
---|
156 | Matrix4 fproj2;
|
---|
157 | fproj2 = depthMapCameras[1]->getProjectionMatrix();
|
---|
158 | fproj2 = Matrix4::IDENTITY;
|
---|
159 | fproj2.setScale(Vector3(1.0/size, 1.0/size, -1.0/100));
|
---|
160 | //fproj.setTrans(Vector3(0,0,-50));
|
---|
161 | depthMapCameras[1]->setCustomProjectionMatrix(true,fproj2);
|
---|
162 |
|
---|
163 |
|
---|
164 | /*LISPSM
|
---|
165 | Vector3 viewdir = viewcam->getDirection();
|
---|
166 | Vector3 lightdir = dir;
|
---|
167 |
|
---|
168 | float dotProd = viewdir.dotProduct(lightdir);
|
---|
169 | float sinGamma;
|
---|
170 | sinGamma = sqrt(1.0-dotProd*dotProd);
|
---|
171 | g_fsinGamma = sinGamma;
|
---|
172 |
|
---|
173 | Vector3 newVdir = viewdir;
|
---|
174 | Matrix4 proj;*/
|
---|
175 |
|
---|
176 | }
|
---|
177 | else if(light->getType() == Light::LT_SPOTLIGHT)
|
---|
178 | {
|
---|
179 | /* Vector3 dir = light->getDirection();
|
---|
180 | depthMapCamera->setDirection( dir );
|
---|
181 | Vector3 pos = light->getParentNode()->getWorldPosition();
|
---|
182 | depthMapCamera->setPosition(pos);
|
---|
183 | depthMapCamera->setProjectionType(PT_PERSPECTIVE);
|
---|
184 | depthMapCamera->setFOVy(light->getSpotlightOuterAngle());*/
|
---|
185 | }
|
---|
186 | else//point light
|
---|
187 | {
|
---|
188 | /* Vector3 pos = light->getParentNode()->getWorldPosition();
|
---|
189 | Vector3 dir = -pos;
|
---|
190 | depthMapCamera->setDirection( dir );
|
---|
191 | depthMapCamera->setPosition(pos);
|
---|
192 | depthMapCamera->setProjectionType(PT_PERSPECTIVE);
|
---|
193 | depthMapCamera->setFOVy(Radian(Degree(140)));
|
---|
194 | depthMapCamera->setNearClipDistance(1);
|
---|
195 | // depthMapCamera->setFarClipDistance(200);
|
---|
196 |
|
---|
197 | //OGRE_EXCEPT(0, "NOT implemented for Pointlight", "OgreDepthShadowMapRenderingRun::refreshLight");
|
---|
198 | */
|
---|
199 | /*Vector3 pos = light->getParentNode()->getWorldPosition();
|
---|
200 | Vector3 dir = -pos;
|
---|
201 | depthMapCamera->setDirection( dir );
|
---|
202 | depthMapCamera->setPosition(pos);
|
---|
203 | depthMapCamera->setProjectionType(PT_ORTHOGRAPHIC);
|
---|
204 | depthMapCamera->*/
|
---|
205 |
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | Matrix4 OgreDepthShadowMapRenderingRun::getLightViewMatrix(int i)
|
---|
211 | {
|
---|
212 | return depthMapCameras[i]->getViewMatrix();
|
---|
213 | }
|
---|
214 |
|
---|
215 | Matrix4 OgreDepthShadowMapRenderingRun::getLightViewProjMatrix(int i)
|
---|
216 | {
|
---|
217 | return depthMapCameras[i]->getProjectionMatrix() * depthMapCameras[i]->getViewMatrix();
|
---|
218 | }
|
---|
219 |
|
---|