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