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 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 |
|
---|
55 | int 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 |
|
---|
67 | void 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 |
|
---|
81 | void 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 |
|
---|
92 | void 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 |
|
---|
105 | void SceneEntity::AddShape(Shape *shape)
|
---|
106 | {
|
---|
107 | mShapes.push_back(shape);
|
---|
108 | mBox.Include(shape->GetBoundingBox());
|
---|
109 |
|
---|
110 | mCenter = mBox.Center();
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | void SceneEntity::SetTransform(Transform3 *trafo)
|
---|
115 | {
|
---|
116 | mTransform = trafo;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | void SceneEntity::SetLastRendered(int lastRendered)
|
---|
121 | {
|
---|
122 | mLastRendered = lastRendered;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | int SceneEntity::GetLastRendered() const
|
---|
127 | {
|
---|
128 | return mLastRendered;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | int 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 |
|
---|
147 | AxisAlignedBox3 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 |
|
---|
158 | AxisAlignedBox3 SceneEntity::GetBoundingBox() const
|
---|
159 | {
|
---|
160 | return mBox;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | Vector3 SceneEntity::GetCenter() const
|
---|
165 | {
|
---|
166 | Matrix4x4 *mat = mTransform->GetMatrix();
|
---|
167 |
|
---|
168 | if (!mat) return mCenter;
|
---|
169 |
|
---|
170 | return (*mat) * mCenter;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | } |
---|