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 |
|
---|
21 | SceneEntity::SceneEntity(Transform3 *trafo):
|
---|
22 | mTransform(trafo), mCurrentLODLevel(0), mLODLastUpdated(-1)
|
---|
23 | {
|
---|
24 | mBox.Initialize();
|
---|
25 | }
|
---|
26 |
|
---|
27 |
|
---|
28 | SceneEntity::~SceneEntity()
|
---|
29 | {
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | void 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 |
|
---|
56 | int 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 |
|
---|
68 | void 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 |
|
---|
82 | void 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 |
|
---|
93 | void 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 |
|
---|
106 | void SceneEntity::AddShape(Shape *shape)
|
---|
107 | {
|
---|
108 | mShapes.push_back(shape);
|
---|
109 | mBox.Include(shape->GetBoundingBox());
|
---|
110 |
|
---|
111 | mCenter = mBox.Center();
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | void SceneEntity::SetTransform(Transform3 *trafo)
|
---|
116 | {
|
---|
117 | mTransform = trafo;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | void SceneEntity::SetLastRendered(int lastRendered)
|
---|
122 | {
|
---|
123 | mLastRendered = lastRendered;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | int SceneEntity::GetLastRendered() const
|
---|
128 | {
|
---|
129 | return mLastRendered;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | int 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 |
|
---|
148 | AxisAlignedBox3 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 |
|
---|
159 | AxisAlignedBox3 SceneEntity::GetBoundingBox() const
|
---|
160 | {
|
---|
161 | return mBox;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | Vector3 SceneEntity::GetCenter() const
|
---|
166 | {
|
---|
167 | Matrix4x4 *mat = mTransform->GetMatrix();
|
---|
168 |
|
---|
169 | if (!mat) return mCenter;
|
---|
170 |
|
---|
171 | return (*mat) * mCenter;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | } |
---|