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

Revision 3331, 4.3 KB checked in by mattausch, 16 years ago (diff)

performance testing

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#include "glInterface.h"
9
10
11using namespace std;
12
13
14namespace CHCDemoEngine
15{
16
17int sCurrentId = 0;
18bool SceneEntity::sUseLODs = true;
19int SceneEntity::sCurrentVisibleId = -1;
20
21SceneEntity::SceneEntity(Transform3 *trafo):
22mTransform(trafo),
23mCurrentLODLevel(0),
24mLODLastUpdated(-1),
25mId(sCurrentId ++),
26mVisibleId(0)
27{
28        mBox.Initialize();
29}
30
31
32SceneEntity::~SceneEntity()
33{
34}
35
36
37void SceneEntity::UpdateLODs(const Vector3 &viewPoint)
38{
39        mCurrentLODLevel = 0;
40
41        if (!sUseLODs) return;
42
43        const Vector3 pos = GetWorldCenter();
44        const float dist = SqrDistance(pos, viewPoint);
45
46        // note: we assume that lods are ordered from smallest distance to largest
47        const int l = (int)mLODLevels.size();
48
49        for (int i = 0; i < l; ++ i)
50        {
51                if (mLODLevels[i].GetSquaredDistance() > dist) break;
52                mCurrentLODLevel = i;
53        }
54}
55
56
57int SceneEntity::GetCurrentLODLevel()
58{
59        if (mLODLastUpdated != LODLevel::sFrameId)
60        {
61                mLODLastUpdated = LODLevel::sFrameId;
62                UpdateLODs(LODLevel::sViewPoint);
63        }
64
65        return mCurrentLODLevel;
66}
67
68
69void SceneEntity::GetCurrentLODLevel(ShapeContainer::iterator &start,
70                                                                         ShapeContainer::iterator &end)
71{
72        if (mLODLastUpdated != LODLevel::sFrameId)
73        {
74                mLODLastUpdated = LODLevel::sFrameId;
75                UpdateLODs(LODLevel::sViewPoint);
76        }
77
78        start = mLODLevels[mCurrentLODLevel].GetShapes().begin();
79        end   = mLODLevels[mCurrentLODLevel].GetShapes().end();
80}
81
82
83void SceneEntity::GetLODLevel(int level,
84                                                          ShapeContainer::iterator &start,
85                                                          ShapeContainer::iterator &end)
86{
87        start = mLODLevels[level].GetShapes().begin();
88        end = mLODLevels[level].GetShapes().end();
89}
90
91
92void SceneEntity::Render(RenderState *state)
93{
94        ShapeContainer::iterator sit, sit_end;
95
96        if (!mLODLevels.empty())
97        {
98                GetCurrentLODLevel(sit, sit_end);
99        }
100        else
101        {
102                sit = mShapes.begin(); sit_end = mShapes.end();
103        }
104
105        Prepare(state);
106
107        for (; sit != sit_end; ++ sit)
108        {
109                (*sit)->Render(state, this);
110        }
111
112        mTransform->Unload(state);
113}
114
115
116void SceneEntity::AddShape(Shape *shape)
117{
118        mShapes.push_back(shape);
119        mBox.Include(shape->GetBoundingBox());
120
121        mCenter = mBox.Center();
122}
123
124
125void SceneEntity::SetTransform(Transform3 *trafo)
126{
127        mTransform = trafo;
128}
129
130
131void SceneEntity::SetLastRenderedFrame(int lastRenderedFrame)
132{
133        mLastRenderedFrame = lastRenderedFrame;
134}
135
136
137int SceneEntity::GetLastRenderedFrame() const
138{
139        return mLastRenderedFrame;
140}
141
142
143int SceneEntity::CountNumTriangles(int lodLevel)
144{
145        int numTriangles = 0;
146       
147        ShapeContainer::iterator sit, sit_end;
148
149        if (lodLevel == -1)
150                lodLevel = GetCurrentLODLevel();
151
152        return mLODLevels[lodLevel].GetNumTriangles();
153}
154
155
156AxisAlignedBox3 SceneEntity::GetWorldBoundingBox() const
157{
158        if (mTransform->IsIdentity()) return mBox;
159        Matrix4x4 mat = mTransform->GetMatrix();
160
161        return Transform(mBox, mat);
162}
163
164
165AxisAlignedBox3 SceneEntity::GetBoundingBox() const
166{
167        return mBox;
168}
169
170
171Vector3 SceneEntity::GetWorldCenter() const
172{
173        if (mTransform->IsIdentity()) return mCenter;
174        return mTransform->GetMatrix() * mCenter;
175}
176
177
178void SceneEntity::Prepare(RenderState *state)
179{
180        /*int id[] = {(mId / (255 * 255)) % 255, (mId / 255) % 255, mId % 255, 0};
181
182        GLfloat fogColor[4] = {(float)id[0] / 255.0f, (float)id[1] / 255.0f, (float)id[2] / 255.0f, .0f};
183        glFogfv(GL_FOG_COLOR, fogColor);*/
184
185        mTransform->Load(state);
186}
187
188
189AxisAlignedBox3 SceneEntity::ComputeBoundingBox(SceneEntity **entities, int numEntities)
190{
191        AxisAlignedBox3 box;
192       
193        if (numEntities <= 0)
194        {       // no box => just initialize
195                box.Initialize();
196        }
197        else
198        {
199                box = entities[0]->GetWorldBoundingBox();
200
201                for (int i = 1; i < numEntities; ++ i)
202                {
203                        box.Include(entities[i]->GetWorldBoundingBox());
204                }
205        }
206
207        return box;
208}
209
210
211AxisAlignedBox3 SceneEntity::ComputeBoundingBox(const SceneEntityContainer &entities)
212{
213        AxisAlignedBox3 box;
214       
215        if (entities.empty())
216        {       // no box => just initialize
217                box.Initialize();
218        }
219        else
220        {
221                box = entities[0]->GetWorldBoundingBox();
222
223                for (size_t i = 1; i < entities.size(); ++ i)
224                {
225                        box.Include(entities[i]->GetWorldBoundingBox());
226                }
227        }
228
229        return box;
230}
231
232
233
234
235}
Note: See TracBrowser for help on using the repository browser.