1 | #include "Visualization.h"
|
---|
2 | #include "glInterface.h"
|
---|
3 | #include "Camera.h"
|
---|
4 | #include "SceneEntity.h"
|
---|
5 | #include "Bvh.h"
|
---|
6 | #include "RenderState.h"
|
---|
7 |
|
---|
8 | #include <stack>
|
---|
9 |
|
---|
10 |
|
---|
11 | using namespace std;
|
---|
12 |
|
---|
13 |
|
---|
14 | namespace CHCDemoEngine
|
---|
15 | {
|
---|
16 |
|
---|
17 |
|
---|
18 | static void RenderBoxForViz(const AxisAlignedBox3 &box)
|
---|
19 | {
|
---|
20 | glBegin(GL_LINE_LOOP);
|
---|
21 | glVertex3d(box.Min().x, box.Max().y, box.Min().z);
|
---|
22 | glVertex3d(box.Max().x, box.Max().y, box.Min().z);
|
---|
23 | glVertex3d(box.Max().x, box.Min().y, box.Min().z);
|
---|
24 | glVertex3d(box.Min().x, box.Min().y, box.Min().z);
|
---|
25 | glEnd();
|
---|
26 |
|
---|
27 | glBegin(GL_LINE_LOOP);
|
---|
28 | glVertex3d(box.Min().x, box.Min().y, box.Max().z);
|
---|
29 | glVertex3d(box.Max().x, box.Min().y, box.Max().z);
|
---|
30 | glVertex3d(box.Max().x, box.Max().y, box.Max().z);
|
---|
31 | glVertex3d(box.Min().x, box.Max().y, box.Max().z);
|
---|
32 | glEnd();
|
---|
33 |
|
---|
34 | glBegin(GL_LINE_LOOP);
|
---|
35 | glVertex3d(box.Max().x, box.Min().y, box.Min().z);
|
---|
36 | glVertex3d(box.Max().x, box.Min().y, box.Max().z);
|
---|
37 | glVertex3d(box.Max().x, box.Max().y, box.Max().z);
|
---|
38 | glVertex3d(box.Max().x, box.Max().y, box.Min().z);
|
---|
39 | glEnd();
|
---|
40 |
|
---|
41 | glBegin(GL_LINE_LOOP);
|
---|
42 | glVertex3d(box.Min().x, box.Min().y, box.Min().z);
|
---|
43 | glVertex3d(box.Min().x, box.Min().y, box.Max().z);
|
---|
44 | glVertex3d(box.Min().x, box.Max().y, box.Max().z);
|
---|
45 | glVertex3d(box.Min().x, box.Max().y, box.Min().z);
|
---|
46 | glEnd();
|
---|
47 |
|
---|
48 | glBegin(GL_LINE_LOOP);
|
---|
49 | glVertex3d(box.Min().x, box.Min().y, box.Min().z);
|
---|
50 | glVertex3d(box.Max().x, box.Min().y, box.Min().z);
|
---|
51 | glVertex3d(box.Max().x, box.Min().y, box.Max().z);
|
---|
52 | glVertex3d(box.Min().x, box.Min().y, box.Max().z);
|
---|
53 | glEnd();
|
---|
54 |
|
---|
55 | glBegin(GL_LINE_LOOP);
|
---|
56 | glVertex3d(box.Min().x, box.Max().y, box.Min().z);
|
---|
57 | glVertex3d(box.Max().x, box.Max().y, box.Min().z);
|
---|
58 | glVertex3d(box.Max().x, box.Max().y, box.Max().z);
|
---|
59 | glVertex3d(box.Min().x, box.Max().y, box.Max().z);
|
---|
60 |
|
---|
61 | glEnd();
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | /******************************************************/
|
---|
66 | /* Vizualization implementation */
|
---|
67 | /******************************************************/
|
---|
68 |
|
---|
69 |
|
---|
70 | Visualization::Visualization(Bvh *bvh,
|
---|
71 | Camera *camera,
|
---|
72 | Camera *vizCamera,
|
---|
73 | RenderState *renderState):
|
---|
74 | mBvh(bvh),
|
---|
75 | mCamera(camera),
|
---|
76 | mVizCamera(vizCamera),
|
---|
77 | mRenderState(renderState),
|
---|
78 | mFrameId(0)
|
---|
79 | {
|
---|
80 | mSphere = (GLUquadric *)gluNewQuadric();
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | Visualization::~Visualization()
|
---|
85 | {
|
---|
86 | //DEL_PTR(mSphere);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | void Visualization::SetHierarchy(Bvh *bvh)
|
---|
91 | {
|
---|
92 | mBvh = bvh;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | void Visualization::SetRenderState(RenderState *state)
|
---|
97 | {
|
---|
98 | mRenderState = state;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | void Visualization::SetFrameId(int frameId)
|
---|
103 | {
|
---|
104 | mFrameId = frameId;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | void Visualization::Render()
|
---|
109 | {
|
---|
110 | stack<BvhNode *> tStack;
|
---|
111 | tStack.push(mBvh->GetRoot());
|
---|
112 |
|
---|
113 | glDisable(GL_LIGHTING);
|
---|
114 |
|
---|
115 | RenderViewPoint();
|
---|
116 | RenderFrustum();
|
---|
117 | RenderBoxForViz(mBvh->GetBox());
|
---|
118 |
|
---|
119 | glEnable(GL_LIGHTING);
|
---|
120 |
|
---|
121 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
122 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
123 |
|
---|
124 | while (!tStack.empty())
|
---|
125 | {
|
---|
126 | BvhNode *node = tStack.top();
|
---|
127 | tStack.pop();
|
---|
128 |
|
---|
129 | if (!node->IsVirtualLeaf())
|
---|
130 | {
|
---|
131 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
132 |
|
---|
133 | tStack.push(interior->GetFront());
|
---|
134 | tStack.push(interior->GetBack());
|
---|
135 | }
|
---|
136 | else
|
---|
137 | {
|
---|
138 | if (node->GetLastRenderedFrame() == mFrameId)
|
---|
139 | {
|
---|
140 | int geometrySize;
|
---|
141 | SceneEntity **entities = mBvh->GetGeometry(node, geometrySize);
|
---|
142 |
|
---|
143 | for (int i = 0; i < geometrySize; ++ i)
|
---|
144 | {
|
---|
145 | SceneEntity *ent = entities[i];
|
---|
146 | ent->Render(mRenderState);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | //leaves.push_back(static_cast<BvhLeaf *>(node));
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | mRenderState->Reset();
|
---|
154 |
|
---|
155 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
156 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | void Visualization::RenderViewPoint()
|
---|
161 | {
|
---|
162 | glPushMatrix();
|
---|
163 | Vector3 pos = mCamera->GetPosition();
|
---|
164 | pos.z += 100;
|
---|
165 | glTranslatef(pos.x, pos.y, pos.z);
|
---|
166 |
|
---|
167 | glScalef(5.0f, 5.0f, 5.0f);
|
---|
168 | glPushAttrib(GL_CURRENT_BIT);
|
---|
169 |
|
---|
170 | glColor3f(1.0f, 0.0f, 0.0f);
|
---|
171 |
|
---|
172 | gluSphere((::GLUquadric *)mSphere,
|
---|
173 | 2e-3f * Magnitude(mBvh->GetBox().Size()), 6, 6);
|
---|
174 |
|
---|
175 | glPopAttrib();
|
---|
176 | glPopMatrix();
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | void Visualization::RenderFrustum()
|
---|
181 | {
|
---|
182 | glColor3f(1.0f, 0.0f, 0.0f);
|
---|
183 |
|
---|
184 | Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
|
---|
185 | mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
|
---|
186 |
|
---|
187 | glLineWidth(2);
|
---|
188 |
|
---|
189 | glBegin(GL_LINE_LOOP);
|
---|
190 | glVertex3d(fbl.x, fbl.y, fbl.z);
|
---|
191 | glVertex3d(fbr.x, fbr.y, fbr.z);
|
---|
192 | glVertex3d(ftr.x, ftr.y, ftr.z);
|
---|
193 | glVertex3d(ftl.x, ftl.y, ftl.z);
|
---|
194 | glEnd();
|
---|
195 |
|
---|
196 | glBegin(GL_LINE_LOOP);
|
---|
197 | glVertex3d(nbl.x, nbl.y, nbl.z);
|
---|
198 | glVertex3d(nbr.x, nbr.y, nbr.z);
|
---|
199 | glVertex3d(ntr.x, ntr.y, ntr.z);
|
---|
200 | glVertex3d(ntl.x, ntl.y, ntl.z);
|
---|
201 | glEnd();
|
---|
202 |
|
---|
203 | glBegin(GL_LINE_LOOP);
|
---|
204 | glVertex3d(fbl.x, fbl.y, fbl.z);
|
---|
205 | glVertex3d(ftl.x, ftl.y, ftl.z);
|
---|
206 | glVertex3d(ntl.x, ntl.y, ntl.z);
|
---|
207 | glVertex3d(nbl.x, nbl.y, nbl.z);
|
---|
208 | glEnd();
|
---|
209 |
|
---|
210 | glBegin(GL_LINE_LOOP);
|
---|
211 | glVertex3d(fbr.x, fbr.y, fbr.z);
|
---|
212 | glVertex3d(ftr.x, ftr.y, ftr.z);
|
---|
213 | glVertex3d(ntr.x, ntr.y, ntr.z);
|
---|
214 | glVertex3d(nbr.x, nbr.y, nbr.z);
|
---|
215 | glEnd();
|
---|
216 |
|
---|
217 | glBegin(GL_LINE_LOOP);
|
---|
218 | glVertex3d(fbr.x, fbr.y, fbr.z);
|
---|
219 | glVertex3d(fbl.x, fbl.y, fbl.z);
|
---|
220 | glVertex3d(nbl.x, nbl.y, nbl.z);
|
---|
221 | glVertex3d(nbr.x, nbr.y, nbr.z);
|
---|
222 | glEnd();
|
---|
223 |
|
---|
224 | glBegin(GL_LINE_LOOP);
|
---|
225 | glVertex3d(ftr.x, ftr.y, ftr.z);
|
---|
226 | glVertex3d(ftl.x, ftl.y, ftl.z);
|
---|
227 | glVertex3d(ntl.x, ntl.y, ntl.z);
|
---|
228 | glVertex3d(ntr.x, ntr.y, ntr.z);
|
---|
229 | glEnd();
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | } |
---|