- Timestamp:
- 07/17/08 17:21:39 (16 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling
- Files:
-
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp
r2838 r2844 54 54 mViewOrientation = mBaseOrientation; 55 55 56 cout << "right: " << right << endl;56 /*cout << "right: " << right << endl; 57 57 cout << "up: " << up << endl; 58 58 cout << "dir: " << direction << endl; 59 */ 59 60 } 60 61 … … 248 249 } 249 250 250 cout<<"here2 " << DotProd(h1, Vector3(0, 1, 0)) << " " << mPitch << endl;251 251 CalculateFromPitchAndYaw(); 252 252 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/LODInfo.h
r2839 r2844 10 10 /** Class representing information about an LOD level. 11 11 */ 12 struct LOD Info12 struct LODLevel 13 13 { 14 14 public: 15 16 LODLevel(float squaredDist): mSquaredDistance(squaredDist) {} 17 18 15 19 /// distance of this LOD level 16 20 float mSquaredDistance; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp
r2825 r2844 25 25 mTexture = NULL; 26 26 mAlphaTestEnabled = false; 27 mCullFaceEnabled = true; 27 28 28 29 mAmbientColor = RgbaColor(0.2f, 0.2f, 0.2f, 1.0f); … … 62 63 void Material::Render(RenderState *state) 63 64 { 64 state->SetState(mTexture != NULL, mAlphaTestEnabled );65 state->SetState(mTexture != NULL, mAlphaTestEnabled, mCullFaceEnabled); 65 66 66 67 if (mTexture) -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h
r2822 r2844 64 64 65 65 inline void SetAlphaTestEnabled(bool alpha) { mAlphaTestEnabled = alpha; } 66 inline void SetCullFaceEnabled(bool cull) { mCullFaceEnabled = cull; } 67 66 68 inline bool IsAlphaTestEnabled() const { return mAlphaTestEnabled; } 69 inline bool IsCullFaceEnabled() const { return mCullFaceEnabled; } 70 67 71 /** Renders this material. 68 72 */ … … 88 92 89 93 bool mAlphaTestEnabled; 94 bool mCullFaceEnabled; 90 95 /// the assciated texture 91 96 Texture *mTexture; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderQueue.cpp
r2842 r2844 15 15 16 16 17 inline static bool CompDist 2(RenderQueueBucket *b1, RenderQueueBucket *b2)17 inline static bool CompDist(RenderQueueBucket *b1, RenderQueueBucket *b2) 18 18 { 19 19 return (b1->mMinDistance < b2->mMinDistance); … … 26 26 mMinSizeForSorting(3), 27 27 mCamera(NULL), 28 m Size(0)28 mNumEntries(0) 29 29 { 30 30 } … … 35 35 mMinSizeForSorting(3), 36 36 mCamera(cam), 37 m Size(0)37 mNumEntries(0) 38 38 { 39 39 } … … 58 58 return false; 59 59 60 if (mat->IsCullFaceEnabled() != mBuckets[idx]->mAlphaTestEnabled) 61 return false; 62 60 63 const bool hasTexture = (mat->GetTexture() != NULL); 61 64 … … 78 81 void RenderQueue::Enqueue(SceneEntity *entity) 79 82 { 80 static ShapeContainer shapes; 81 shapes.clear(); 82 83 entity->GetCurrentLODLevel(shapes); 84 85 ShapeContainer::const_iterator sit, sit_end = shapes.end(); 86 87 for (sit = shapes.begin(); sit != sit_end; ++ sit) 83 ShapeContainer::iterator sit, sit_end; 84 85 entity->GetCurrentLODLevel(sit, sit_end); 86 87 for (; sit != sit_end; ++ sit) 88 88 { 89 89 Enqueue(*sit); … … 95 95 { 96 96 RenderQueueBucket *bucket; 97 ++ m Size;97 ++ mNumEntries; 98 98 99 99 if (shape->mRenderQueueBucket) … … 117 117 { 118 118 RenderQueueBucket *bucket = new RenderQueueBucket(); 119 bucket->mMinDistance = -1; 119 120 120 121 Material *mat = shape->GetMaterial(); 121 122 122 123 bucket->mAlphaTestEnabled = mat->IsAlphaTestEnabled(); 124 bucket->mCullFaceEnabled = mat->IsCullFaceEnabled(); 123 125 124 126 const bool hasTexture = (mat->GetTexture() != NULL); … … 131 133 mBuckets.push_back(bucket); 132 134 133 // assume that the incoming nodes are ordered by distance => set min dist134 // on first incoming node135 Vector3 v = shape->GetTransformedBoundingBox().Center() - mCamera->GetPosition();136 const float dist = SqrMagnitude(v);137 mBuckets[i]->mMinDistance = dist;138 139 135 //cout << "num buckets: " << (int)mBuckets.size() << endl; 140 136 } … … 144 140 } 145 141 142 if (bucket->mMinDistance < -0.5f) 143 { 144 // assume that the incoming nodes are ordered by distance 145 // => set min dist on first incoming node 146 const Vector3 v = shape->GetTransformedBoundingBox().Center() - mCamera->GetPosition(); 147 148 const float dist = SqrMagnitude(v); 149 bucket->mMinDistance = dist; 150 } 151 146 152 bucket->mShapes.push_back(shape); 147 153 } … … 151 157 { 152 158 for (size_t i = 0; i < mBuckets.size(); ++ i) 159 { 160 mBuckets[i]->mMinDistance = -1; 153 161 mBuckets[i]->mShapes.clear(); 154 155 mSize = 0; 162 } 163 164 mNumEntries = 0; 156 165 } 157 166 … … 210 219 { 211 220 // sort buckets itself 212 sort(mBuckets.begin(), mBuckets.end(), CompDist 2);221 sort(mBuckets.begin(), mBuckets.end(), CompDist); 213 222 } 214 223 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderQueue.h
r2842 r2844 25 25 bool mHasTexture; 26 26 bool mAlphaTestEnabled; 27 bool mCullFaceEnabled; 27 28 28 29 /// minimal distance to the camera … … 60 61 */ 61 62 void SetCamera(Camera *cam); 62 /** Returns the number entities currently in the queue.63 /** Returns the number of entries currently in the queue. 63 64 */ 64 inline int GetSize() const { return (int)m Size; }65 inline int GetSize() const { return (int)mNumEntries; } 65 66 /** Renders and clears the queue. 66 67 */ … … 95 96 std::vector<RenderQueueBucket *> mBuckets; 96 97 97 int m Size;98 int mNumEntries; 98 99 }; 99 100 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp
r2825 r2844 18 18 RenderState::RenderState(): 19 19 mAlphaTestEnabled(false), 20 mCullFaceEnabled(true), 20 21 mTexturesEnabled(false), 21 22 mMode(RENDER), … … 51 52 glDepthMask(GL_FALSE); 52 53 53 SetState(false, false );54 SetState(false, false, false); 54 55 } 55 56 else // mode returns to render … … 77 78 78 79 79 void RenderState::SetState(bool texturing, bool alphaTest )80 void RenderState::SetState(bool texturing, bool alphaTest, bool cullFace) 80 81 { 82 if (mCullFaceEnabled && !cullFace) 83 { 84 mCullFaceEnabled = false; 85 glDisable(GL_CULL_FACE); 86 } 87 else if (!mCullFaceEnabled && cullFace) 88 { 89 mCullFaceEnabled = true; 90 glEnable(GL_CULL_FACE); 91 } 92 81 93 if (mAlphaTestEnabled && !alphaTest) 82 94 { … … 122 134 mCurrentVboId = -1; 123 135 124 SetState(false, false );136 SetState(false, false, false); 125 137 SetState(RENDER); 126 138 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h
r2825 r2844 44 44 /** Sets the current render state. 45 45 */ 46 void SetState(bool texturing, bool alphaTest );46 void SetState(bool texturing, bool alphaTest, bool cullFace); 47 47 /** Returns wether we are in query or render mode 48 48 */ … … 74 74 75 75 bool mAlphaTestEnabled; 76 bool mCullFaceEnabled; 76 77 bool mTexturesEnabled; 77 78 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp
r2842 r2844 109 109 { 110 110 SceneEntity *ent = entities[i]; 111 ent->UpdateLODs(mCamera); 111 112 112 113 mStats.mNumRenderedTriangles += ent->CountNumTriangles(); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp
r2842 r2844 10 10 #include "Transform3.h" 11 11 #include "Shape.h" 12 #include "LODInfo.h" 12 13 13 14 … … 33 34 SceneEntity *ResourceManager::LoadSceneEntity(igzstream &str) 34 35 { 35 // shape36 int shapeId;37 str.read(reinterpret_cast<char *>(&shapeId), sizeof(int));38 39 Geometry *geom = mGeometryTable[shapeId];40 Material *mat = mMaterialTable[shapeId];41 42 43 36 bool hasTrafo; 44 37 str.read(reinterpret_cast<char *>(&hasTrafo), sizeof(bool)); … … 60 53 Transform3 *trafo = new Transform3(m); 61 54 mTrafos.push_back(trafo); 62 63 // use instancing 55 64 56 sceneGeom = new SceneEntity(trafo); 65 57 66 // create shape 67 Shape *shape = new Shape(geom, mat, sceneGeom); 68 mShapes.push_back(shape); 69 70 sceneGeom->AddShape(shape); 58 /////////////// 59 //-- load lod levels 60 61 int numLODs; 62 str.read(reinterpret_cast<char *>(&numLODs), sizeof(int)); 63 64 //if (numLODs > 1) cout << "numlods: " << numLODs << endl; 65 for (int i = 0; i < numLODs; ++ i) 66 { 67 float distance; 68 str.read(reinterpret_cast<char *>(&distance), sizeof(float)); 69 70 int numShapes; 71 str.read(reinterpret_cast<char *>(&numShapes), sizeof(int)); 72 73 //if (numLODs > 1) cout << "dist: " << distance << " shapes: " << numShapes; 74 75 LODLevel *lodLevel = new LODLevel(distance); 76 77 for (int j = 0; j < numShapes; ++ j) 78 { 79 int shapeId; 80 str.read(reinterpret_cast<char *>(&shapeId), sizeof(int)); 81 82 //if (numLODs > 1) cout << " id " << shapeId; 83 84 Geometry *geom = mGeometryTable[shapeId]; 85 Material *mat = mMaterialTable[shapeId]; 86 87 // create shape 88 Shape *shape = new Shape(geom, mat, sceneGeom); 89 90 mShapes.push_back(shape); 91 92 sceneGeom->AddShape(shape); 93 lodLevel->mShapes.push_back(shape); 94 } 95 96 //if (numLODs > 1) cout << endl; 97 98 sceneGeom->AddLODLevel(lodLevel); 99 } 71 100 72 101 return sceneGeom; … … 90 119 str.read(texname, sizeof(char) * texnameSize); 91 120 92 //cout << "loading texture " << texname << " with len " << texnameSize << " id: " << id << endl;93 121 Texture *tex = new Texture(model_path + texname); 94 122 … … 135 163 136 164 str.read(reinterpret_cast<char *>(&mat->mAlphaTestEnabled), sizeof(bool)); 165 166 //str.read(reinterpret_cast<char *>(&mat->mCullFaceEnabled), sizeof(bool)); 137 167 138 168 // material -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.h
r2842 r2844 34 34 */ 35 35 bool Load(const std::string &filename, SceneEntityContainer &geometry); 36 SceneEntityContainer mSceneEntities; 36 37 37 38 protected: … … 56 57 std::vector<Transform3 *> mTrafos; 57 58 58 SceneEntityContainer mSceneEntities;59 59 ShapeContainer mShapes; 60 60 }; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp
r2842 r2844 1 1 #include "SceneEntity.h" 2 #include "glInterface.h"3 2 #include "Geometry.h" 4 3 #include "Material.h" 5 4 #include "RenderState.h" 6 5 #include "Shape.h" 6 #include "Transform3.h" 7 #include "Camera.h" 8 9 #include "glInterface.h" 7 10 #include <Cg/cg.h> 8 11 #include <Cg/cgGL.h> … … 16 19 17 20 18 SceneEntity::SceneEntity(Transform3 *trafo): mTransform(trafo) 21 SceneEntity::SceneEntity(Transform3 *trafo): 22 mTransform(trafo), mCurrentLODLevel(0) 19 23 { 24 mBox.Initialize(); 20 25 } 21 26 … … 26 31 27 32 28 void SceneEntity:: GetCurrentLODLevel(ShapeContainer &shapes)33 void SceneEntity::UpdateLODs(Camera *cam) 29 34 { 30 ShapeContainer::const_iterator sit, sit_end;35 const Vector3 pos = GetTransformedBoundingBox().Center(); 31 36 32 if (mLODInfos.empty()) 37 const float distance = SqrDistance(pos, cam->GetPosition()); 38 39 mCurrentLODLevel = 0; 40 41 int l = (int)mLODLevels.size(); 42 43 for (int i = 0; i < l; ++ i) 33 44 { 34 sit = mShapes.begin(); sit_end = mShapes.end(); 45 LODLevel *lod = mLODLevels[i]; 46 47 if (lod->mSquaredDistance > distance) 48 break; 49 50 mCurrentLODLevel = i; 35 51 } 36 52 37 float mDistance = 0.0f; 53 /*if (mLODLevels.size() > 1) 54 { 55 Debug << "dist: " << distance << " numshapes: " << mLODLevels[mCurrentLODLevel]->mShapes.size() << " lod: " << mCurrentLODLevel << " of " << mLODLevels.size() << endl; 56 }*/ 57 } 38 58 39 int i = 0; 59 void SceneEntity::GetCurrentLODLevel(ShapeContainer::iterator &start, 60 ShapeContainer::iterator &end) 61 { 62 start = mLODLevels[mCurrentLODLevel]->mShapes.begin(); 63 end = mLODLevels[mCurrentLODLevel]->mShapes.end(); 64 } 40 65 41 LODInfoContainer::const_iterator lid, lid_end = mLODInfos.end();42 66 43 for (lid = mLODInfos.begin(); lid != lid_end; ++ lid) 44 { 45 LODInfo *lodInfo = *lid; 67 void SceneEntity::GetLODLevel(int level, 68 ShapeContainer::iterator &start, 69 ShapeContainer::iterator &end) 70 { 71 LODLevel *lod = mLODLevels[level]; 46 72 47 if (lodInfo->mSquaredDistance > mDistance) 48 { 49 sit = lodInfo->mShapes.begin(); 50 sit_end = lodInfo->mShapes.end(); 51 break; 52 } 53 } 54 55 for (; sit != sit_end; ++ sit) 56 { 57 shapes.push_back(*sit); 58 } 73 start = lod->mShapes.begin(); 74 end = lod->mShapes.end(); 59 75 } 60 76 … … 62 78 void SceneEntity::Render(RenderState *state) 63 79 { 64 //if (mMaterial) mMaterial->Render(state); 65 float mDistance = 0.0f; 80 ShapeContainer::iterator sit, sit_end; 66 81 67 static ShapeContainer shapes; 68 shapes.clear(); 82 /*if (mShapes.size() > 8) 83 { 84 sit = mShapes.begin() + 6; 85 sit_end = mShapes.begin() + 6; 86 } 87 else*/ 88 GetCurrentLODLevel(sit, sit_end); 69 89 70 GetCurrentLODLevel(shapes); 71 72 ShapeContainer::const_iterator sit, sit_end = shapes.end(); 73 74 for (sit = shapes.begin(); sit != sit_end; ++ sit) 90 //if (mShapes.size() > 1) 91 //mShapes[0]->Render(state); 92 for (; sit != sit_end; ++ sit) 75 93 { 76 94 (*sit)->Render(state); … … 82 100 { 83 101 mShapes.push_back(shape); 102 mBox.Include(shape->GetBoundingBox()); 84 103 } 85 104 … … 103 122 104 123 105 int SceneEntity::CountNumTriangles( )124 int SceneEntity::CountNumTriangles(int lodLevel) 106 125 { 107 126 int numTriangles = 0; 108 static ShapeContainer shapes;109 shapes.clear();127 128 ShapeContainer::iterator sit, sit_end; 110 129 111 GetCurrentLODLevel(shapes); 130 if (lodLevel == -1) 131 { 132 GetCurrentLODLevel(sit, sit_end); 133 } 134 else 135 { 136 GetLODLevel(lodLevel, sit, sit_end); 137 } 112 138 113 ShapeContainer::const_iterator sit, sit_end = shapes.end();114 139 115 for ( sit = shapes.begin(); sit != sit_end; ++ sit)140 for (; sit != sit_end; ++ sit) 116 141 { 117 142 numTriangles += (*sit)->GetGeometry()->GetNumTriangles(); … … 122 147 123 148 149 AxisAlignedBox3 SceneEntity::GetTransformedBoundingBox() const 150 { 151 Matrix4x4 *mat = mTransform->GetMatrix(); 152 153 if (!mat) 154 return mBox; 155 156 return Transform(mBox, *mat); 124 157 } 158 159 160 AxisAlignedBox3 SceneEntity::GetBoundingBox() const 161 { 162 return mBox; 163 } 164 165 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.h
r2842 r2844 18 18 class RenderState; 19 19 class Transform3; 20 class Camera; 20 21 21 22 … … 49 50 */ 50 51 void SetTransform(Transform3 *trafo); 51 /** Returns the transformed bounding box.52 */53 //AxisAlignedBox3 GetBoundingBox() const;54 52 /** set frame where we last rendered this node 55 53 */ … … 61 59 */ 62 60 inline Transform3 *GetTransform() const { return mTransform; } 61 /** Counts number of triangles in this entity using the specified lod level 62 with 0 being the highest or the current lod level (if the argument is -1). 63 */ 64 int CountNumTriangles(int lodLevel = 0); 65 /** Returns the bounding box. 66 */ 67 AxisAlignedBox3 GetBoundingBox() const; 68 /** Returns the transformed bounding box. 69 */ 70 AxisAlignedBox3 GetTransformedBoundingBox() const; 71 63 72 64 void GetCurrentLODLevel(ShapeContainer &shapes);65 73 66 int CountNumTriangles();74 //////////////// 67 75 76 /** Internally updates current lod level. 77 */ 78 void UpdateLODs(Camera *cam); 79 /** Returns shapes of specified lod level 80 */ 81 void GetLODLevel(int level, ShapeContainer::iterator &start, ShapeContainer::iterator &end); 82 /** Returns shapes of current lod level 83 */ 84 void GetCurrentLODLevel(ShapeContainer::iterator &start, ShapeContainer::iterator &end); 85 /** Adds a new lod level. 86 */ 87 void AddLODLevel(LODLevel *lod) { mLODLevels.push_back(lod); } 88 /** Returns numbers of lod levels. 89 */ 90 int GetNumLODLevels() const { return (int)mLODLevels.size(); } 68 91 69 92 protected: 70 93 71 94 AxisAlignedBox3 mBox; 72 95 /// transform matrix 73 96 Transform3 *mTransform; 74 97 /// Stores information about the LOD levels 75 LOD InfoContainer mLODInfos;98 LODLevelContainer mLODLevels; 76 99 /// the renderable shapes 77 100 ShapeContainer mShapes; 78 101 /// when this entity was last rendered 79 102 int mLastRendered; 103 104 int mCurrentLODLevel; 80 105 }; 81 106 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.cpp
r2842 r2844 55 55 AxisAlignedBox3 Shape::GetTransformedBoundingBox() const 56 56 { 57 AxisAlignedBox3 box = mGeometry->GetBoundingBox();58 57 Matrix4x4 *mat = mParent->GetTransform()->GetMatrix(); 59 58 60 if (mat) Transform(box, *mat); 59 if (!mat) 60 return mGeometry->GetBoundingBox(); 61 61 62 return box;62 return Transform(mGeometry->GetBoundingBox(), *mat); 63 63 } 64 64 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Vector3.h
r2782 r2844 314 314 inline float SqrMagnitude(const Vector3 &v) 315 315 { 316 316 return v.x * v.x + v.y * v.y + v.z * v.z; 317 317 } 318 318 … … 320 320 inline float Distance(const Vector3 &v1, const Vector3 &v2) 321 321 { 322 return sqrtf(sqrt(v1.x - v2.x) + sqrt(v1.y - v2.y) + sqrt(v1.z - v2.z)); 322 //return sqrtf(sqrt(v1.x - v2.x) + sqrt(v1.y - v2.y) + sqrt(v1.z - v2.z)); 323 return Magnitude(v1 - v2); 323 324 } 324 325 … … 326 327 inline float SqrDistance(const Vector3 &v1, const Vector3 &v2) 327 328 { 328 return sqrt(v1.x - v2.x) + sqrt(v1.y - v2.y) + sqrt(v1.z - v2.z); 329 //return sqrt(v1.x - v2.x) + sqrt(v1.y - v2.y) + sqrt(v1.z - v2.z); 330 return SqrMagnitude(v1 - v2); 329 331 } 330 332 … … 332 334 inline Vector3 Normalize(const Vector3 &A) 333 335 { 334 336 return A * (1.0f / Magnitude(A)); 335 337 } 336 338 … … 338 340 inline float DotProd(const Vector3 &A, const Vector3 &B) 339 341 { 340 342 return A.x * B.x + A.y * B.y + A.z * B.z; 341 343 } 342 344 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
r2843 r2844 854 854 //glEnable(GL_ALPHA_TEST); 855 855 glDisable(GL_ALPHA_TEST); 856 glAlphaFunc(GL_GEQUAL, 0. 5f);856 glAlphaFunc(GL_GEQUAL, 0.8f); 857 857 858 858 glFrontFace(GL_CCW); … … 1195 1195 1196 1196 1197 /*state.Reset(); 1198 state.SetState(RenderState::RENDER); 1199 1200 glEnableClientState(GL_VERTEX_ARRAY); 1201 glEnableClientState(GL_NORMAL_ARRAY); 1202 1203 for (int i = 0; i < loader->mSceneEntities.size(); ++ i) 1204 { 1205 SceneEntity *ent = loader->mSceneEntities[i]; 1206 1207 ent->UpdateLODs(camera); 1208 ent->Render(&state); 1209 }*/ 1210 1197 1211 // actually render the scene geometry using one of the specified algorithms 1198 1212 traverser->RenderScene(); -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.h
r2839 r2844 497 497 typedef std::vector<Shape *> ShapeContainer; 498 498 499 struct LOD Info;500 typedef std::vector<LOD Info *> LODInfoContainer;501 502 } 503 504 #endif 505 506 507 508 509 510 511 512 499 struct LODLevel; 500 typedef std::vector<LODLevel *> LODLevelContainer; 501 502 } 503 504 #endif 505 506 507 508 509 510 511 512
Note: See TracChangeset
for help on using the changeset viewer.