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

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