- Timestamp:
- 09/07/08 13:44:28 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
r2899 r2911 4 4 #include "RenderTraverser.h" 5 5 #include "Light.h" 6 #include "Polygon3.h" 7 #include "Polyhedron.h" 8 6 9 #include <IL/il.h> 7 10 #include <assert.h> 8 11 12 9 13 using namespace std; 10 14 … … 12 16 namespace CHCDemoEngine 13 17 { 18 19 20 static Polyhedron *polyhedron = NULL; 14 21 15 22 … … 97 104 98 105 99 void ShadowMap::ComputeShadowMap(RenderTraverser *renderer) 106 void ShadowMap::DrawPolys() 107 { 108 if (!polyhedron) return; 109 110 cout << "here3" << endl; 111 112 for (size_t i = 0; i < polyhedron->NumPolygons(); ++ i) 113 { 114 float r = (float)i / polyhedron->NumPolygons(); 115 float g = 1; 116 float b = 1; 117 118 glColor3f(r, g, b); 119 120 glBegin(GL_LINE_LOOP); 121 122 Polygon3 *poly = polyhedron->GetPolygons()[i]; 123 124 for (size_t j = 0; j < poly->mVertices.size(); ++ j) 125 { 126 Vector3 v = poly->mVertices[j]; 127 glVertex3d(v.x, v.y, v.z); 128 } 129 130 glEnd(); 131 } 132 } 133 134 135 void ShadowMap::CalcFocussedFrustum(const Matrix4x4 &camProjView, 136 const Matrix4x4 &lightProjView, 137 Matrix4x4 &focussed) 138 { 139 /////////////////// 140 //-- First step: calc clipped frustum 141 142 DEL_PTR(polyhedron); 143 polyhedron = mCamera->CalcClippedFrustum(mSceneBox); 144 145 if (polyhedron) 146 { 147 // second step: focus light frustum on clipped frustum 148 Frustum lightFrustum(lightProjView); 149 150 for (int i = 0; i < 6; ++ i) 151 { 152 // the clipping planes look outward the frustum, 153 // so distances > 0 mean that a point is outside 154 const float invLength = 1.0f / Magnitude(lightFrustum.mClipPlanes[i].mNormal); 155 156 lightFrustum.mClipPlanes[i].mD *= invLength; 157 lightFrustum.mClipPlanes[i].mNormal *= invLength; 158 } 159 160 //lightFrustum.EnclosePolyhedron(*polyhedron); 161 lightFrustum.ExtractTransformation(focussed); 162 } 163 } 164 165 166 void ShadowMap::ComputeShadowMap(RenderTraverser *renderer, const Matrix4x4 &projView) 100 167 { 101 168 const float xlen = Magnitude(mSceneBox.Diagonal() * 0.5f); … … 107 174 Vector3 pos = mSceneBox.Center(); 108 175 176 Matrix4x4 camView; 177 mCamera->GetModelViewMatrix(camView); 178 109 179 pos -= mLight->GetDirection() * Magnitude(mSceneBox.Diagonal() * 0.5f); 110 mShadowCam->SetPosition(pos);180 //mShadowCam->SetPosition(pos); 111 181 112 182 mFbo->Bind(); … … 153 223 mShadowCam->GetProjectionMatrix(lightProj); 154 224 225 Matrix4x4 oldLightProjView = lightView * lightProj; 226 Matrix4x4 newLightProj; 227 228 CalcFocussedFrustum(projView, lightProj, newLightProj); 229 //CalcFocussedFrustum(projView, oldLightProjView, newLightProj); 230 231 Matrix4x4 dummyMat; 232 glGetFloatv(GL_PROJECTION_MATRIX, (float *)dummyMat.x); 233 234 235 cout << "old:\n" << dummyMat << endl; 236 237 238 glMatrixMode(GL_PROJECTION); 239 glLoadMatrixf((float *)newLightProj.x); 240 241 glMatrixMode(GL_MODELVIEW); 242 //glLoadIdentity(); 243 244 Matrix4x4 dummyMat3; 245 glGetFloatv(GL_PROJECTION_MATRIX, (float *)dummyMat3.x); 246 247 cout << "new:\n" << newLightProj << endl; 248 249 lightView = camView; 250 mLightProjView = lightView * newLightProj; 251 //mLightProjView = newLightProj; 252 253 //cout << "old: \n" << oldLightProjView << " new: \n " << mLightProjView << endl; 254 255 155 256 static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f, 156 257 0.0f, 0.5f, 0.0f, 0.5f, 157 258 0.0f, 0.0f, 0.5f, 0.5f, 158 0.0f, 0.0f, 0.0f, 1.0f); //bias from [-1, 1] to [0, 1] 159 160 Matrix4x4 lightProjView = lightView * lightProj; 161 mTextureMatrix = lightProjView * biasMatrix; 259 0.0f, 0.0f, 0.0f, 1.0f); 260 261 mTextureMatrix = mLightProjView * biasMatrix; 262 //mTextureMatrix = oldLightProjView * biasMatrix; 263 264 162 265 163 266 … … 176 279 177 280 glPopAttrib(); 178 179 /*float *data = new float[mSize * mSize];281 /* 282 float *data = new float[mSize * mSize]; 180 283 181 284 GrabDepthBuffer(data, mFbo->GetDepthTex()); … … 183 286 184 287 delete [] data; 185 */288 186 289 PrintGLerror("shadow map"); 187 290 */ 188 291 FrameBufferObject::Release(); 189 292 }
Note: See TracChangeset
for help on using the changeset viewer.