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

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