| 160 | } |
| 161 | |
| 162 | void LightShadowMap::calcLightMatrices( MatrixF &outLightMatrix, const Frustum &viewFrustum ) |
| 163 | { |
| 164 | // Create light matrix, set projection |
| 165 | |
| 166 | switch ( mLight->getType() ) |
| 167 | { |
| 168 | case LightInfo::Vector : |
| 169 | { |
| 170 | const ShadowMapParams *p = mLight->getExtended<ShadowMapParams>(); |
| 171 | |
| 172 | // Calculate the bonding box of the shadowed area |
| 173 | // we're interested in... this is the shadow box |
| 174 | // transformed by the frustum transform. |
| 175 | Box3F viewBB( -p->shadowDistance, -p->shadowDistance, -p->shadowDistance, |
| 176 | p->shadowDistance, p->shadowDistance, p->shadowDistance ); |
| 177 | viewFrustum.getTransform().mul( viewBB ); |
| 178 | |
| 179 | // Calculate a light "projection" matrix. |
| 180 | MatrixF lightMatrix = MathUtils::createOrientFromDir(mLight->getDirection()); |
| 181 | outLightMatrix = lightMatrix; |
| 182 | static MatrixF rotMat(EulerF( (M_PI_F / 2.0f), 0.0f, 0.0f)); |
| 183 | lightMatrix.mul( rotMat ); |
| 184 | |
| 185 | // This is the box in lightspace |
| 186 | Box3F lightViewBB(viewBB); |
| 187 | lightMatrix.mul(lightViewBB); |
| 188 | |
| 189 | // Now, let's position our light based on the lightViewBB |
| 190 | Point3F newLightPos(viewBB.getCenter()); |
| 191 | F32 sceneDepth = lightViewBB.maxExtents.z - lightViewBB.minExtents.z; |
| 192 | newLightPos += mLight->getDirection() * ((-sceneDepth / 2.0f)-1.0f); // -1 for the nearplane |
| 193 | outLightMatrix.setPosition(newLightPos); |
| 194 | |
| 195 | // Update light info |
| 196 | mLight->setRange( sceneDepth ); |
| 197 | mLight->setPosition( newLightPos ); |
| 198 | |
| 199 | // Set our ortho projection |
| 200 | F32 width = (lightViewBB.maxExtents.x - lightViewBB.minExtents.x) / 2.0f; |
| 201 | F32 height = (lightViewBB.maxExtents.y - lightViewBB.minExtents.y) / 2.0f; |
| 202 | |
| 203 | width = getMax(width, height); |
| 204 | |
| 205 | GFX->setOrtho(-width, width, -width, width, 1.0f, sceneDepth, true); |
| 206 | |
| 207 | |
| 208 | // TODO: Width * 2... really isn't that pixels being used as |
| 209 | // meters? Is a real physical metric of scene depth better? |
| 210 | //SceneManager::setVisibleDistance(width * 2.0f); |
| 211 | |
| 212 | #if 0 |
| 213 | DebugDrawer::get()->drawFrustum(viewFrustum, ColorF(1.0f, 0.0f, 0.0f)); |
| 214 | DebugDrawer::get()->drawBox(viewBB.minExtents, viewBB.maxExtents, ColorF(0.0f, 1.0f, 0.0f)); |
| 215 | DebugDrawer::get()->drawBox(lightViewBB.minExtents, lightViewBB.maxExtents, ColorF(0.0f, 0.0f, 1.0f)); |
| 216 | DebugDrawer::get()->drawBox(newLightPos - Point3F(1,1,1), newLightPos + Point3F(1,1,1), ColorF(1,1,0)); |
| 217 | DebugDrawer::get()->drawLine(newLightPos, newLightPos + mLight.mDirection*3.0f, ColorF(0,1,1)); |
| 218 | |
| 219 | Point3F a(newLightPos); |
nothing calls this directly
no test coverage detected