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 |
|
---|
11 | using namespace std;
|
---|
12 |
|
---|
13 |
|
---|
14 | namespace CHCDemoEngine
|
---|
15 | {
|
---|
16 |
|
---|
17 | int sCurrentId = 0;
|
---|
18 | bool SceneEntity::sUseLODs = true;
|
---|
19 | int SceneEntity::sCurrentVisibleId = -1;
|
---|
20 |
|
---|
21 | SceneEntity::SceneEntity(Transform3 *trafo):
|
---|
22 | mTransform(trafo),
|
---|
23 | mCurrentLODLevel(0),
|
---|
24 | mLODLastUpdated(-1),
|
---|
25 | mId(sCurrentId ++),
|
---|
26 | mVisibleId(0)
|
---|
27 | {
|
---|
28 | mBox.Initialize();
|
---|
29 | }
|
---|
30 |
|
---|
31 |
|
---|
32 | SceneEntity::~SceneEntity()
|
---|
33 | {
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | void 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 |
|
---|
57 | int 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 |
|
---|
69 | void 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 |
|
---|
83 | void 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 |
|
---|
92 | void 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 |
|
---|
116 | void SceneEntity::AddShape(Shape *shape)
|
---|
117 | {
|
---|
118 | mShapes.push_back(shape);
|
---|
119 | mBox.Include(shape->GetBoundingBox());
|
---|
120 |
|
---|
121 | mCenter = mBox.Center();
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | void SceneEntity::SetTransform(Transform3 *trafo)
|
---|
126 | {
|
---|
127 | mTransform = trafo;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | void SceneEntity::SetLastRenderedFrame(int lastRenderedFrame)
|
---|
132 | {
|
---|
133 | mLastRenderedFrame = lastRenderedFrame;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | int SceneEntity::GetLastRenderedFrame() const
|
---|
138 | {
|
---|
139 | return mLastRenderedFrame;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | int 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 |
|
---|
156 | AxisAlignedBox3 SceneEntity::GetWorldBoundingBox() const
|
---|
157 | {
|
---|
158 | if (mTransform->IsIdentity()) return mBox;
|
---|
159 | Matrix4x4 mat = mTransform->GetMatrix();
|
---|
160 |
|
---|
161 | return Transform(mBox, mat);
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | AxisAlignedBox3 SceneEntity::GetBoundingBox() const
|
---|
166 | {
|
---|
167 | return mBox;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | Vector3 SceneEntity::GetWorldCenter() const
|
---|
172 | {
|
---|
173 | if (mTransform->IsIdentity()) return mCenter;
|
---|
174 | return mTransform->GetMatrix() * mCenter;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | void 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 |
|
---|
189 | AxisAlignedBox3 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 |
|
---|
211 | AxisAlignedBox3 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 | } |
---|