Changeset 2911 for GTP/trunk/App/Demos
- Timestamp:
- 09/07/08 13:44:28 (16 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling
- Files:
-
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
r2901 r2911 284 284 </File> 285 285 <File 286 RelativePath=".\src\Polygon3.cpp" 287 > 288 </File> 289 <File 290 RelativePath=".\src\Polygon3.h" 291 > 292 </File> 293 <File 294 RelativePath=".\src\Polyhedron.cpp" 295 > 296 </File> 297 <File 298 RelativePath=".\src\Polyhedron.h" 299 > 300 </File> 301 <File 286 302 RelativePath=".\src\SampleGenerator.cpp" 287 303 > -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp
r2802 r2911 1 #include "AxisAlignedBox3.h" 2 #include "Plane3.h" 3 #include "Polygon3.h" 4 #include "Polyhedron.h" 5 1 6 #include <cassert> 2 7 #include <iostream> 3 #include "AxisAlignedBox3.h"4 #include "Plane3.h"5 6 8 7 9 using namespace std; … … 50 52 Minimize(mMin, newpt); 51 53 Maximize(mMax, newpt); 54 } 55 56 57 void AxisAlignedBox3::Include(const Polygon3 &newpoly) 58 { 59 VertexArray::const_iterator it, it_end = newpoly.mVertices.end(); 60 61 for (it = newpoly.mVertices.begin(); it != it_end; ++ it) 62 Include(*it); 63 } 64 65 66 void AxisAlignedBox3::Include(const PolygonContainer &polys) 67 { 68 PolygonContainer::const_iterator it, it_end = polys.end(); 69 70 for (it = polys.begin(); it != it_end; ++ it) 71 Include(*(*it)); 52 72 } 53 73 … … 1026 1046 } 1027 1047 1028 } 1029 1048 1049 struct VertexData 1050 { 1051 Vector3 mVertex; 1052 float mAngle; 1053 1054 VertexData(Vector3 vtx, float angle): mVertex(vtx), mAngle(angle) 1055 {} 1056 1057 bool operator<(const VertexData &b) const 1058 { 1059 return mAngle > b.mAngle; 1060 } 1061 }; 1062 1063 // TODO: use a table to avoid normal and distance computations 1064 Polygon3 *AxisAlignedBox3::CrossSection(const Plane3 &plane) const 1065 { 1066 Polygon3 *planePoly = new Polygon3(); 1067 1068 int side[8]; 1069 bool onFrontSide = false, onBackSide = false; 1070 1071 Vector3 vtx; 1072 1073 ////////////// 1074 //-- compute classification of vertices 1075 1076 for (int i = 0; i < 8; ++i) 1077 { 1078 GetVertex(i, vtx); 1079 side[i] = plane.Side(vtx); 1080 if (side[i] > 0) 1081 onFrontSide = true; 1082 else if (side[i] < 0) 1083 onBackSide = true; 1084 else // vertex coincident => push_back 1085 planePoly->mVertices.push_back(vtx); 1086 } 1087 1088 /////////// 1089 //-- find intersections 1090 1091 if (onFrontSide && onBackSide) 1092 { 1093 Vector3 ptA, ptB; 1094 for (int i = 0; i < 12; ++ i) 1095 { 1096 int aIdx, bIdx; 1097 GetEdge(i, aIdx, bIdx); 1098 1099 ptA = GetVertex(aIdx); 1100 ptB = GetVertex(bIdx); 1101 1102 int sideA = side[aIdx]; 1103 int sideB = side[bIdx]; 1104 1105 if (((sideA > 0) && (sideB < 0)) || (sideA < 0) && (sideB > 0)) 1106 planePoly->mVertices.push_back(plane.FindIntersection(ptA, ptB)); 1107 } 1108 } 1109 1110 // order intersections 1111 if (planePoly->mVertices.size() > 3) 1112 { 1113 Vector3 centerOfMass(0); 1114 1115 int i; 1116 // compute center of mass 1117 for (i = 0; i < (int)planePoly->mVertices.size(); ++ i) 1118 centerOfMass += planePoly->mVertices[i]; 1119 1120 centerOfMass /= (float)planePoly->mVertices.size(); 1121 1122 vector<VertexData> vertexData; 1123 Vector3 refVec = Normalize(centerOfMass - planePoly->mVertices[0]); 1124 1125 // compute angle to reference point 1126 for (i = 1; i < (int)planePoly->mVertices.size(); ++ i) 1127 { 1128 float angle = 1129 Angle(refVec, centerOfMass - planePoly->mVertices[i], plane.mNormal); 1130 1131 vertexData.push_back(VertexData(planePoly->mVertices[i], angle)); 1132 } 1133 1134 std::stable_sort(vertexData.begin(), vertexData.end()); 1135 1136 // update vertices 1137 for (i = 1; i < (int)planePoly->mVertices.size(); ++ i) 1138 planePoly->mVertices[i] = vertexData[i - 1].mVertex; 1139 } 1140 else if (planePoly->mVertices.size() == 3) 1141 { 1142 // fix orientation if needed 1143 if (DotProd(planePoly->GetNormal(), plane.mNormal) < 0) 1144 { 1145 Vector3 v = planePoly->mVertices[1]; 1146 planePoly->mVertices[1] = planePoly->mVertices[2]; 1147 planePoly->mVertices[2] = v; 1148 } 1149 } 1150 1151 return planePoly; 1152 } 1153 1154 1155 Plane3 AxisAlignedBox3::GetPlane(const int face) const 1156 { 1157 switch (face) 1158 { 1159 1160 case 0: 1161 return Plane3(Vector3(-1, 0, 0), mMin); 1162 case 1: 1163 return Plane3(Vector3(1, 0, 0), mMax); 1164 case 2: 1165 return Plane3(Vector3(0, -1, 0), mMin); 1166 case 3: 1167 return Plane3(Vector3(0, 1, 0), mMax); 1168 case 4: 1169 return Plane3(Vector3(0, 0, -1), mMin); 1170 case 5: 1171 return Plane3(Vector3(0, 0, 1), mMax); 1172 } 1173 1174 // should not come here 1175 return Plane3(); 1176 } 1177 1178 1179 int AxisAlignedBox3::Side(const Plane3 &plane) const 1180 { 1181 int s = 0; 1182 // if exactly one of the vertices lies on the plane 1183 // and the others are on the same side, the plane is tangent 1184 // to the box on either side and not intersecting 1185 for (int i = 0; i < 8; ++ i) 1186 { 1187 Vector3 pt; 1188 GetVertex(i, pt); 1189 1190 int side = plane.Side(pt); 1191 1192 if (side != 0) 1193 { 1194 if (side == -s) 1195 return 0; // sign changed => intersects 1196 1197 s = side; 1198 } 1199 } 1200 1201 return s; 1202 } 1203 1204 1205 Polyhedron *AxisAlignedBox3::CalcIntersection(Polyhedron *polyhedron) const 1206 { 1207 Polyhedron *oldPolyhedron = new Polyhedron(*polyhedron); 1208 1209 Polyhedron *newPolyhedron = NULL; 1210 1211 for (int i = 0; i < 6; ++ i) 1212 { 1213 Polyhedron *newPolyhedron = oldPolyhedron->CalcIntersection(GetPlane(i)); 1214 DEL_PTR(oldPolyhedron); 1215 1216 if (!newPolyhedron) 1217 return NULL; 1218 } 1219 1220 return newPolyhedron; 1221 } 1222 1223 1224 } 1225 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.h
r2802 r2911 5 5 #include "Matrix4x4.h" 6 6 #include "Vector3.h" 7 #include "common.h" 8 # 7 9 8 10 … … 12 14 struct Triangle3; 13 15 class Plane3; 16 class Polygon3; 17 class Polyhedron; 18 14 19 15 20 /** Axis alignedd box class. … … 79 84 void Include(const Vector3 &newpt); 80 85 void Include(const AxisAlignedBox3 &bbox); 86 void Include(const Polygon3 &newpoly); 87 void Include(const PolygonContainer &polys); 88 81 89 /** Expand the axis-aligned box to include given values in particular axis. 82 90 */ … … 85 93 */ 86 94 bool Includes(const AxisAlignedBox3 &b) const; 95 87 96 /** Returns true if this point is inside box. 88 97 */ … … 167 176 */ 168 177 float GetMinVisibleDistance(const Plane3 &near) const; 178 /** Returns cross section of the plane as a polygon. 179 */ 180 Polygon3 *AxisAlignedBox3::CrossSection(const Plane3 &plane) const; 181 /** Returns the supporting plane of this face. 182 */ 183 Plane3 AxisAlignedBox3::GetPlane(const int face) const; 184 /** Returns 185 0 if box intersects plane 186 1 if box in front of plane 187 -1 if box behind plane 188 */ 189 int Side(const Plane3 &plane) const; 190 /** Calculates the intersection of the polyhedron with the box. 191 */ 192 Polyhedron *CalcIntersection(Polyhedron *polyhedron) const; 169 193 170 194 … … 178 202 friend inline bool OverlapS(const AxisAlignedBox3 &,const AxisAlignedBox3 &); 179 203 180 /** Overlap returns 1 if the two axis-aligned boxes overlap for a given 181 epsilon.If eps > 0.0, then the boxes has to have the real intersection204 /** Overlap returns 1 if the two axis-aligned boxes overlap for a given epsilon. 205 If eps > 0.0, then the boxes has to have the real intersection 182 206 box, if eps < 0.0, then the boxes need not intersect really, they 183 207 can be at eps distance in the projection. -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
r2897 r2911 65 65 66 66 static Plane3 sNearPlane; 67 static Camera::Frustum sFrustum;67 static Frustum sFrustum; 68 68 69 69 /// these values are valid for all nodes … … 364 364 365 365 366 static void CalcNPVertexIndices(const Frustum &frustum, int *indices) 367 { 368 for (int i = 0; i < 6; ++ i) 369 { 370 // n-vertex 371 indices[i * 2 + 0] = AxisAlignedBox3::GetIndexNearestVertex(frustum.mClipPlanes[i].mNormal); 372 // p-vertex 373 indices[i * 2 + 1] = AxisAlignedBox3::GetIndexFarthestVertex(frustum.mClipPlanes[i].mNormal); 374 } 375 } 376 377 366 378 void Bvh::InitFrame(Camera *cam) 367 379 { … … 370 382 371 383 cam->CalcFrustum(sFrustum); 372 sFrustum.CalcNPVertexIndices(sClipPlaneAABBVertexIndices);384 CalcNPVertexIndices(sFrustum, sClipPlaneAABBVertexIndices); 373 385 374 386 // store near plane -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp
r2893 r2911 2 2 #include "Camera.h" 3 3 #include "glInterface.h" 4 4 #include "Polygon3.h" 5 #include "Matrix4x4.h" 6 #include "Polyhedron.h" 5 7 6 8 namespace CHCDemoEngine … … 107 109 108 110 matProjectionView = matViewing * matProjectionView; 109 110 float planes[6][4]; 111 112 113 ////////// 114 //-- extract the plane equations 115 116 for (int i = 0; i < 4; ++ i) 117 { 118 planes[0][i] = matProjectionView.x[i][3] - matProjectionView.x[i][0]; // right plane 119 planes[1][i] = matProjectionView.x[i][3] + matProjectionView.x[i][0]; // left plane 120 planes[2][i] = matProjectionView.x[i][3] + matProjectionView.x[i][1]; // bottom plane 121 planes[3][i] = matProjectionView.x[i][3] - matProjectionView.x[i][1]; // top plane 122 planes[4][i] = matProjectionView.x[i][3] - matProjectionView.x[i][2]; // far plane 123 planes[5][i] = matProjectionView.x[i][3] + matProjectionView.x[i][2]; // near plane 124 } 125 111 112 frustum = Frustum(matProjectionView); 126 113 127 114 //////////// … … 132 119 // the clipping planes look outward the frustum, 133 120 // so distances > 0 mean that a point is outside 134 float fInvLength = -1.0f / 135 sqrt(planes[i][0] * planes[i][0] + 136 planes[i][1] * planes[i][1] + 137 planes[i][2] * planes[i][2]); 138 139 planes[i][0] *= fInvLength; 140 planes[i][1] *= fInvLength; 141 planes[i][2] *= fInvLength; 142 planes[i][3] *= fInvLength; 143 144 frustum.mClipPlanes[i].mNormal = Vector3(planes[i][0], planes[i][1], planes[i][2]); 145 frustum.mClipPlanes[i].mD = planes[i][3]; 146 } 147 } 148 149 150 void Camera::Frustum::CalcNPVertexIndices(int *indices) 151 { 152 for (int i = 0; i < 6; ++ i) 153 { 154 // n-vertex 155 indices[i * 2 + 0] = AxisAlignedBox3::GetIndexNearestVertex(mClipPlanes[i].mNormal); 156 // p-vertex 157 indices[i * 2 + 1] = AxisAlignedBox3::GetIndexFarthestVertex(mClipPlanes[i].mNormal); 121 const float invLength = -1.0f / Magnitude(frustum.mClipPlanes[i].mNormal); 122 123 frustum.mClipPlanes[i].mD *= invLength; 124 frustum.mClipPlanes[i].mNormal *= invLength; 158 125 } 159 126 } … … 174 141 175 142 176 void Camera::ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 177 Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) 143 void Camera::ComputePointsInternal(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 144 Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr, 145 const Vector3 &view, const Vector3 &right, const Vector3 &up, 146 const Vector3 &pos) const 178 147 { 179 148 float z_near = mNear; … … 188 157 const float h_far = w_far / GetAspect(); 189 158 190 const Vector3 view = GetDirection(); 191 const Vector3 fc = mPosition + view * z_far; 192 193 const Vector3 up = GetUpVector(); 194 const Vector3 right = GetRightVector(); 195 159 const Vector3 fc = pos + view * z_far; 160 196 161 Vector3 t1, t2; 197 162 … … 204 169 fbr = fc - t1 + t2; 205 170 206 const Vector3 nc = mPosition+ view * z_near;171 const Vector3 nc = pos + view * z_near; 207 172 208 173 t1 = h_near * 0.5f * up; … … 216 181 217 182 183 void Camera::ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 184 Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) const 185 { 186 ComputePointsInternal(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr, 187 GetDirection(), GetRightVector(), GetUpVector(), GetPosition()); 188 } 189 190 191 void Camera::ComputeBasePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 192 Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) const 193 { 194 ComputePointsInternal(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr, 195 GetBaseDirection(), GetBaseRightVector(), GetBaseUpVector(), Vector3(0, 0, 0)); 196 } 197 198 199 Polyhedron *Camera::CalcClippedFrustum(const AxisAlignedBox3 &box) const 200 { 201 Vector3 ftl, ftr, fbl, fbr; 202 Vector3 ntl, ntr, nbl, nbr; 203 204 VertexArray sides[6]; 205 206 ComputeBasePoints(ftl, ftr, fbl, fbr,ntl, ntr, nbl, nbr); 207 //ComputePoints(ftl, ftr, fbl, fbr,ntl, ntr, nbl, nbr); 208 209 for (int i = 0; i < 6; i ++) 210 sides[i].resize(4); 211 212 // left, right 213 sides[0][0] = ntl; sides[0][1] = ftl; sides[0][2] = fbl; sides[0][3] = nbl; 214 sides[1][0] = ntr; sides[1][1] = ftr; sides[1][2] = fbr; sides[1][3] = nbr; 215 // bottom, top 216 sides[2][0] = ntl; sides[2][1] = ftl; sides[2][2] = ftr; sides[2][3] = ntr; 217 sides[3][0] = nbl; sides[3][1] = fbl; sides[3][2] = fbr; sides[3][3] = nbr; 218 // near, far 219 sides[4][0] = ntl; sides[4][1] = ntr; sides[4][2] = nbr; sides[4][3] = nbl; 220 sides[5][0] = ntl; sides[5][1] = ntr; sides[5][2] = nbr; sides[5][3] = nbl; 221 222 223 ////////// 224 //-- compute polyhedron 225 226 PolygonContainer polygons; 227 228 for (int i = 0; i < 6; ++ i) 229 { 230 Polygon3 *poly = new Polygon3(sides[i]); 231 polygons.push_back(poly); 232 } 233 234 Polyhedron *p = new Polyhedron(polygons); 235 236 //Polyhedron *clippedPolyhedron = box.CalcIntersection(po); 237 //DEL_PTR(p); 238 239 //return clippedPolyhedron; 240 return p; 241 } 242 243 218 244 void Camera::SetOrtho(bool ortho) 219 245 { … … 240 266 Vector3 ndir = -Normalize(dir); 241 267 242 //mPitch = atan2(dir.x, dir.z);243 //mYaw = atan2(dir.y, sqrt((dir.x * dir.x) + (dir.z * dir.z)));244 245 //mPitch = atan2(dir.x, dir.y);246 268 mPitch = -atan2(ndir.x, ndir.y); 247 269 mYaw = atan2(ndir.z, sqrt((ndir.x * ndir.x) + (ndir.y * ndir.y))); … … 277 299 Vector3 Camera::GetRightVector() const 278 300 { 279 return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]); 280 281 } 282 283 284 } 285 301 return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]); 302 } 303 304 305 Vector3 Camera::GetBaseDirection() const 306 { 307 return -Vector3(mBaseOrientation.x[0][2], mBaseOrientation.x[1][2], mBaseOrientation.x[2][2]); 308 } 309 310 311 Vector3 Camera::GetBaseUpVector() const 312 { 313 return Vector3(mBaseOrientation.x[0][1], mBaseOrientation.x[1][1], mBaseOrientation.x[2][1]); 314 } 315 316 317 Vector3 Camera::GetBaseRightVector() const 318 { 319 return Vector3(mBaseOrientation.x[0][0], mBaseOrientation.x[1][0], mBaseOrientation.x[2][0]); 320 } 321 322 323 Frustum::Frustum(const Matrix4x4 &trafo) 324 { 325 ////////// 326 //-- extract the plane equations 327 328 for (int i = 0; i < 4; ++ i) 329 { 330 mClipPlanes[Frustum::RIGHT_PLANE][i] = trafo.x[i][3] - trafo.x[i][0]; 331 mClipPlanes[Frustum::LEFT_PLANE][i] = trafo.x[i][3] + trafo.x[i][0]; 332 mClipPlanes[Frustum::BOTTOM_PLANE][i] = trafo.x[i][3] + trafo.x[i][1]; 333 mClipPlanes[Frustum::TOP_PLANE][i] = trafo.x[i][3] - trafo.x[i][1]; 334 mClipPlanes[Frustum::FAR_PLANE][i] = trafo.x[i][3] - trafo.x[i][2]; 335 mClipPlanes[Frustum::NEAR_PLANE][i] = trafo.x[i][3] + trafo.x[i][2]; 336 } 337 } 338 339 340 void Frustum::EnclosePolyhedron(const Polyhedron &polyhedron) 341 { 342 VertexArray vertices; 343 polyhedron.CollectVertices(vertices); 344 345 for (int i = 0; i < 6; ++ i) 346 { 347 Plane3 &plane = mClipPlanes[i]; 348 349 //cout << "p" << i << " " << plane.mD << endl; 350 351 VertexArray::const_iterator it, it_end = vertices.end(); 352 353 float minDist = 1e20; 354 355 for (it = vertices.begin(); it != it_end; ++ it) 356 { 357 float dist = plane.Distance(*it); 358 359 if (dist < minDist) 360 { 361 cout << "minDist: " << dist << endl; 362 minDist = dist; 363 } 364 } 365 366 plane.mD += minDist; 367 } 368 } 369 370 371 void Frustum::ExtractTransformation(Matrix4x4 &m) const 372 { 373 for (int i = 0; i < 4; ++ i) 374 { 375 m.x[i][0] = (mClipPlanes[LEFT_PLANE][i] - mClipPlanes[RIGHT_PLANE][i]) * 0.5f; 376 m.x[i][1] = (mClipPlanes[BOTTOM_PLANE][i] - mClipPlanes[TOP_PLANE][i]) * 0.5f; 377 m.x[i][2] = (mClipPlanes[NEAR_PLANE][i] - mClipPlanes[FAR_PLANE][i]) * 0.5f; 378 m.x[i][3] = (mClipPlanes[LEFT_PLANE][i] + mClipPlanes[RIGHT_PLANE][i]) * 0.5f; 379 } 380 } 381 382 383 384 } 385 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h
r2838 r2911 10 10 { 11 11 12 class Matrix4x4; 13 class Polyhedron; 14 15 /** Class representing a frustum. 16 */ 17 class Frustum 18 { 19 friend class Camera; 20 21 public: 22 23 Frustum() {}; 24 25 Frustum(const Matrix4x4 &trafo); 26 27 enum { RIGHT_PLANE, LEFT_PLANE, BOTTOM_PLANE, TOP_PLANE, FAR_PLANE, NEAR_PLANE}; 28 /** Resizes the current frustum so that it fully encloses the given polyhedron 29 */ 30 void EnclosePolyhedron(const Polyhedron &polyhedron); 31 32 void ExtractTransformation(Matrix4x4 &m) const; 33 34 35 /// the 6 clip planes 36 Plane3 mClipPlanes[6]; 37 }; 38 12 39 13 40 class Camera 14 41 { 15 42 public: 16 17 /** Small struct representing a frustum.18 */19 struct Frustum20 {21 /// the 6 clip planes22 Plane3 mClipPlanes[6];23 24 void CalcNPVertexIndices(int *indices);25 };26 43 27 44 Camera(); … … 33 50 34 51 inline Vector3 GetPosition() const { return mPosition; } 52 35 53 Vector3 GetDirection() const; 36 54 Vector3 GetUpVector() const; 37 55 Vector3 GetRightVector() const; 56 57 Vector3 GetBaseDirection() const; 58 Vector3 GetBaseUpVector() const; 59 Vector3 GetBaseRightVector() const; 38 60 39 61 inline float GetFov() const { return mFovy; } … … 51 73 */ 52 74 void GetModelViewMatrix(Matrix4x4 &mat); 53 75 /** Calculates a frustum from the projection and the modelview matrix. 76 */ 54 77 void CalcFrustum(Frustum &frustum); 55 78 /** Computes the extremal points of this frustum. 56 79 */ 57 80 void ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 58 Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr); 59 81 Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) const; 82 /** Computes the extremal points of the base frustum. 83 */ 84 void ComputeBasePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 85 Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr) const; 60 86 /** Returns the near plane. 61 87 */ … … 83 109 void SetDirection(const Vector3 &direction); 84 110 111 /** Calculates the intersection of the frustum with the box, 112 returns resultin polyhedron as array of polygons. 113 */ 114 Polyhedron *CalcClippedFrustum(const AxisAlignedBox3 &box) const; 85 115 86 116 … … 91 121 void CalculateFromPitchAndYaw(); 92 122 123 void ComputePointsInternal(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr, 124 Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr, 125 const Vector3 &view, const Vector3 &right, const Vector3 &up, 126 const Vector3 &pos) const; 93 127 94 128 //////////////// -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Plane3.h
r2853 r2911 8 8 { 9 9 10 /** 3D Plane.10 /** Class describing a 3D Plane. 11 11 */ 12 12 class Plane3 … … 35 35 /** Returns 1 if v is on front side, -1 if on back side, 0 if on plane. 36 36 */ 37 int Side(const Vector3 &v, constfloat threshold = 1e-6) const;37 int Side(const Vector3 &v, float threshold = 1e-6) const; 38 38 /** Finds intersection of line segment between points a and b with plane. 39 39 @param a start point … … 52 52 float FindT(const Vector3 &a, const Vector3 &b) const; 53 53 54 operator const float*() const 55 { 56 return (const float*) this; 57 } 58 59 float& operator[] (const int inx) 60 { 61 return (&mNormal.x)[inx]; 62 } 63 64 const float &operator[] (const int inx) const 65 { 66 return *(&mNormal.x + inx); 67 } 54 68 55 69 /////////// … … 59 73 60 74 friend bool PlaneIntersection(const Plane3 &p1, const Plane3 &p2); 75 61 76 62 77 friend std::ostream &operator<<(std::ostream &s, const Plane3 &p) … … 73 88 }; 74 89 90 75 91 } 76 92 #endif -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SampleGenerator.h
r2903 r2911 28 28 }; 29 29 30 /** Class generating random samples on a disc or a sphere, respectively.30 /** Class generating random samples on a disc or in a sphere, respectively. 31 31 */ 32 32 class SampleGenerator … … 61 61 62 62 63 /** This class generates random samples on a disc respecting 64 the poisson disc condition (all samples at least distance d apart) 65 according to some d related to the number of samples. 66 */ 63 67 class PoissonDiscSampleGenerator: public SampleGenerator 64 68 { … … 74 78 }; 75 79 76 80 /** This class generates random spherical samples. 81 */ 77 82 class SphericalSampleGenerator: public SampleGenerator 78 83 { -
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 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
r2897 r2911 34 34 /** Computes the shadow map 35 35 */ 36 void ComputeShadowMap(RenderTraverser *traverser );36 void ComputeShadowMap(RenderTraverser *traverser, const Matrix4x4 &projView); 37 37 /** Returns computed shadow texture. 38 38 */ … … 53 53 54 54 55 static void DrawPolys(); 56 57 55 58 protected: 59 60 void CalcFocussedFrustum(const Matrix4x4 &camProjView, 61 const Matrix4x4 &lightProjView, 62 Matrix4x4 &focussed); 63 56 64 57 65 /// the scene bounding box … … 69 77 /// the scene camera 70 78 Camera *mCamera; 79 80 Matrix4x4 mLightProjView; 71 81 }; 72 82 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Vector3.h
r2844 r2911 2 2 #define _Vector3_h__ 3 3 4 #include <iostream> 4 5 5 #include "common.h" 6 6 #include <math.h> -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Visualization.cpp
r2891 r2911 5 5 #include "Bvh.h" 6 6 #include "RenderState.h" 7 #include "ShadowMapping.h" 7 8 8 9 #include <stack> … … 150 151 glDisable(GL_DEPTH_TEST); 151 152 152 RenderFrustum(); 153 //RenderFrustum(); 154 155 ShadowMap::DrawPolys(); 153 156 154 157 Vector3 pos = mCamera->GetPosition(); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
r2905 r2911 44 44 static Environment env; 45 45 46 #define MAX_DEPTH_CONST myfar //10.0f;46 #define MAX_DEPTH_CONST 10.0f 47 47 48 48 // fbo … … 336 336 camera = new Camera(winWidth, winHeight, fov); 337 337 camera->SetNear(nearDist); 338 camera->SetFar(1000 );338 camera->SetFar(100000); 339 339 340 340 camera->SetDirection(camDir); … … 933 933 934 934 // the scene is rendered withouth any shading 935 shadowMap->ComputeShadowMap(shadowTraverser );935 shadowMap->ComputeShadowMap(shadowTraverser, matProjectionView); 936 936 937 937 // change back state … … 1545 1545 1546 1546 // hack: set far plane for viz 1547 camera->SetFar(0.35f * Magnitude(box.Diagonal())); 1547 //camera->SetFar(0.35f * Magnitude(box.Diagonal())); 1548 camera->SetFar(1); 1548 1549 1549 1550 const float offs = box.Size().x * 0.3f; … … 1593 1594 visualization->Render(); 1594 1595 1595 1596 1596 1597 1597 // reset previous settings -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.h
r2903 r2911 26 26 class OcclusionQuery; 27 27 class SceneEntity; 28 29 class Shape; 30 struct LODLevel; 31 class Polygon3; 32 28 33 29 34 … … 483 488 typedef std::queue<BvhNode *> BvhNodeQueue; 484 489 490 491 typedef std::vector<Shape *> ShapeContainer; 492 typedef std::vector<LODLevel *> LODLevelContainer; 493 typedef std::vector<Polygon3 *> PolygonContainer; 494 485 495 static std::ofstream Debug("debug.log"); 486 496 … … 488 498 #define INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES 1000 489 499 490 class Shape; 491 typedef std::vector<Shape *> ShapeContainer; 492 493 struct LODLevel; 494 typedef std::vector<LODLevel *> LODLevelContainer; 495 496 } 497 498 #endif 499 500 501 502 503 504 505 506 500 501 } 502 503 #endif 504 505 506 507 508 509 510 511 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
r2895 r2911 102 102 103 103 float4 lightSpacePos = mul(shadowMatrix, position); 104 lightSpacePos /= lightSpacePos.w; 104 105 105 106 float shadowDepth[9]; … … 123 124 OUT.color = col; 124 125 125 float depth = lightSpacePos.z / lightSpacePos.w;126 float depth = lightSpacePos.z; 126 127 127 128 float d = 0.0f; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
r2904 r2911 88 88 (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample); 89 89 90 #if 0 90 91 // if surface normal perpenticular to view dir, approx. half of the samples will not count 91 92 // => compensate for this (on the other hand, projected sampling area could be larger!) 92 //const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));93 //total_ao += cos_angle * distance_intensity * view_correction;94 93 const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); 94 total_ao += cos_angle * distance_intensity * view_correction; 95 #endif 95 96 total_ao += cos_angle * distance_intensity; 96 97 //total_ao /= j;98 97 } 99 98
Note: See TracChangeset
for help on using the changeset viewer.