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 |
|
---|
14 | using namespace std;
|
---|
15 |
|
---|
16 |
|
---|
17 | namespace CHCDemoEngine
|
---|
18 | {
|
---|
19 |
|
---|
20 | bool SceneEntity::sUseLODs = true;
|
---|
21 |
|
---|
22 |
|
---|
23 | SceneEntity::SceneEntity(Transform3 *trafo):
|
---|
24 | mTransform(trafo), mCurrentLODLevel(0), mLODLastUpdated(-1)
|
---|
25 | {
|
---|
26 | mBox.Initialize();
|
---|
27 | }
|
---|
28 |
|
---|
29 |
|
---|
30 | SceneEntity::~SceneEntity()
|
---|
31 | {
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | void 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 |
|
---|
60 | int 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 |
|
---|
72 | void 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 |
|
---|
86 | void 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 |
|
---|
97 | void 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 |
|
---|
110 | void SceneEntity::AddShape(Shape *shape)
|
---|
111 | {
|
---|
112 | mShapes.push_back(shape);
|
---|
113 | mBox.Include(shape->GetBoundingBox());
|
---|
114 |
|
---|
115 | mCenter = mBox.Center();
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | void SceneEntity::SetTransform(Transform3 *trafo)
|
---|
120 | {
|
---|
121 | mTransform = trafo;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | void SceneEntity::SetLastRendered(int lastRendered)
|
---|
126 | {
|
---|
127 | mLastRendered = lastRendered;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | int SceneEntity::GetLastRendered() const
|
---|
132 | {
|
---|
133 | return mLastRendered;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | int 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 |
|
---|
152 | AxisAlignedBox3 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 |
|
---|
162 | AxisAlignedBox3 SceneEntity::GetBoundingBox() const
|
---|
163 | {
|
---|
164 | return mBox;
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | Vector3 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 | } |
---|