source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp @ 3034

Revision 3034, 3.1 KB checked in by mattausch, 16 years ago (diff)
Line 
1#include "SceneEntity.h"
2#include "Geometry.h"
3#include "Material.h"
4#include "RenderState.h"
5#include "Shape.h"
6#include "Transform3.h"
7#include "Camera.h"
8
9#include "glInterface.h"
10#include <Cg/cg.h>
11#include <Cg/cgGL.h>
12
13
14using namespace std;
15
16
17namespace CHCDemoEngine
18{
19
20bool SceneEntity::sUseLODs = true;
21
22
23SceneEntity::SceneEntity(Transform3 *trafo):
24mTransform(trafo), mCurrentLODLevel(0), mLODLastUpdated(-1)
25{
26        mBox.Initialize();
27}
28
29
30SceneEntity::~SceneEntity()
31{
32}
33
34
35void SceneEntity::UpdateLODs(const Vector3 &viewPoint)
36{
37        mCurrentLODLevel = 0;
38
39        if (!sUseLODs) return;
40
41        const Vector3 pos = GetCenter();
42
43        const float dist = SqrDistance(pos, viewPoint);
44
45        // note: we assume that lods are ordered from smallest distance to largest
46        const int l = (int)mLODLevels.size();
47
48        for (int i = 0; i < l; ++ i)
49        {
50                LODLevel *lod = mLODLevels[i];
51
52                if (lod->GetSquaredDistance() > dist)
53                        break;
54
55                mCurrentLODLevel = i;
56        }
57}
58
59
60int SceneEntity::GetCurrentLODLevel()
61{
62        if (mLODLastUpdated != LODLevel::sFrameId)
63        {
64                mLODLastUpdated = LODLevel::sFrameId;
65                UpdateLODs(LODLevel::sViewPoint);
66        }
67
68        return mCurrentLODLevel;
69}
70
71
72void SceneEntity::GetCurrentLODLevel(ShapeContainer::iterator &start,
73                                                                         ShapeContainer::iterator &end)
74{
75        if (mLODLastUpdated != LODLevel::sFrameId)
76        {
77                mLODLastUpdated = LODLevel::sFrameId;
78                UpdateLODs(LODLevel::sViewPoint);
79        }
80
81        start = mLODLevels[mCurrentLODLevel]->GetShapes().begin();
82        end   = mLODLevels[mCurrentLODLevel]->GetShapes().end();
83}
84
85
86void SceneEntity::GetLODLevel(int level,
87                                                          ShapeContainer::iterator &start,
88                                                          ShapeContainer::iterator &end)
89{
90        LODLevel *lod = mLODLevels[level];
91
92        start = lod->GetShapes().begin();
93        end = lod->GetShapes().end();
94}
95
96
97void SceneEntity::Render(RenderState *state)
98{
99        ShapeContainer::iterator sit, sit_end;
100
101        GetCurrentLODLevel(sit, sit_end);
102
103        for (; sit != sit_end; ++ sit)
104        {
105                (*sit)->Render(state);
106        }
107}
108
109
110void SceneEntity::AddShape(Shape *shape)
111{
112        mShapes.push_back(shape);
113        mBox.Include(shape->GetBoundingBox());
114
115        mCenter = mBox.Center();
116}
117
118
119void SceneEntity::SetTransform(Transform3 *trafo)
120{
121        mTransform = trafo;
122}
123
124
125void SceneEntity::SetLastRendered(int lastRendered)
126{
127        mLastRendered = lastRendered;
128}
129
130
131int SceneEntity::GetLastRendered() const
132{
133        return mLastRendered;
134}
135
136
137int SceneEntity::CountNumTriangles(int lodLevel)
138{
139        int numTriangles = 0;
140       
141        ShapeContainer::iterator sit, sit_end;
142
143        if (lodLevel == -1)
144        {
145                lodLevel = GetCurrentLODLevel();
146        }
147
148        return mLODLevels[lodLevel]->GetNumTriangles();
149}
150
151
152AxisAlignedBox3 SceneEntity::GetTransformedBoundingBox() const
153{
154        if (mTransform->IsIdentity()) return mBox;
155
156        Matrix4x4 mat = mTransform->GetMatrix();
157
158        return Transform(mBox, mat);
159}
160
161
162AxisAlignedBox3 SceneEntity::GetBoundingBox() const
163{
164        return mBox;
165}
166
167
168Vector3 SceneEntity::GetCenter() const
169{
170        if (mTransform->IsIdentity())
171                return mCenter;
172
173        Matrix4x4 mat = mTransform->GetMatrix();
174
175        return mat * mCenter;
176}
177
178
179}
Note: See TracBrowser for help on using the repository browser.