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