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 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | Visualization::~Visualization()
|
---|
84 | {
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | void Visualization::SetHierarchy(Bvh *bvh)
|
---|
89 | {
|
---|
90 | mBvh = bvh;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | void Visualization::SetRenderState(RenderState *state)
|
---|
95 | {
|
---|
96 | mRenderState = state;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | void Visualization::SetFrameId(int frameId)
|
---|
101 | {
|
---|
102 | mFrameId = frameId;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | void Visualization::Render()
|
---|
107 | {
|
---|
108 | stack<BvhNode *> tStack;
|
---|
109 | tStack.push(mBvh->GetRoot());
|
---|
110 |
|
---|
111 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
112 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
113 |
|
---|
114 | while (!tStack.empty())
|
---|
115 | {
|
---|
116 | BvhNode *node = tStack.top();
|
---|
117 | tStack.pop();
|
---|
118 |
|
---|
119 | if (!node->IsVirtualLeaf())
|
---|
120 | {
|
---|
121 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
122 |
|
---|
123 | tStack.push(interior->GetFront());
|
---|
124 | tStack.push(interior->GetBack());
|
---|
125 | }
|
---|
126 | else
|
---|
127 | {
|
---|
128 | if (node->GetLastRenderedFrame() == mFrameId)
|
---|
129 | {
|
---|
130 | int geometrySize;
|
---|
131 | SceneEntity **entities = mBvh->GetGeometry(node, geometrySize);
|
---|
132 |
|
---|
133 | for (int i = 0; i < geometrySize; ++ i)
|
---|
134 | {
|
---|
135 | SceneEntity *ent = entities[i];
|
---|
136 | ent->Render(mRenderState);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
143 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
144 |
|
---|
145 |
|
---|
146 | mRenderState->Reset();
|
---|
147 |
|
---|
148 | glPushAttrib(GL_CURRENT_BIT);
|
---|
149 | glDisable(GL_LIGHTING);
|
---|
150 | glDisable(GL_DEPTH_TEST);
|
---|
151 |
|
---|
152 | RenderFrustum();
|
---|
153 | //RenderBoxForViz(mBvh->GetBox());
|
---|
154 |
|
---|
155 | glPopAttrib();
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | void Visualization::RenderFrustum()
|
---|
160 | {
|
---|
161 | glColor3f(1.0f, 0.0f, 0.0f);
|
---|
162 |
|
---|
163 | Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
|
---|
164 | mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
|
---|
165 |
|
---|
166 | glLineWidth(2);
|
---|
167 |
|
---|
168 | glBegin(GL_LINE_LOOP);
|
---|
169 | glVertex3d(fbl.x, fbl.y, fbl.z);
|
---|
170 | glVertex3d(fbr.x, fbr.y, fbr.z);
|
---|
171 | glVertex3d(ftr.x, ftr.y, ftr.z);
|
---|
172 | glVertex3d(ftl.x, ftl.y, ftl.z);
|
---|
173 | glEnd();
|
---|
174 |
|
---|
175 | glBegin(GL_LINE_LOOP);
|
---|
176 | glVertex3d(nbl.x, nbl.y, nbl.z);
|
---|
177 | glVertex3d(nbr.x, nbr.y, nbr.z);
|
---|
178 | glVertex3d(ntr.x, ntr.y, ntr.z);
|
---|
179 | glVertex3d(ntl.x, ntl.y, ntl.z);
|
---|
180 | glEnd();
|
---|
181 |
|
---|
182 | glBegin(GL_LINE_LOOP);
|
---|
183 | glVertex3d(fbl.x, fbl.y, fbl.z);
|
---|
184 | glVertex3d(ftl.x, ftl.y, ftl.z);
|
---|
185 | glVertex3d(ntl.x, ntl.y, ntl.z);
|
---|
186 | glVertex3d(nbl.x, nbl.y, nbl.z);
|
---|
187 | glEnd();
|
---|
188 |
|
---|
189 | glBegin(GL_LINE_LOOP);
|
---|
190 | glVertex3d(fbr.x, fbr.y, fbr.z);
|
---|
191 | glVertex3d(ftr.x, ftr.y, ftr.z);
|
---|
192 | glVertex3d(ntr.x, ntr.y, ntr.z);
|
---|
193 | glVertex3d(nbr.x, nbr.y, nbr.z);
|
---|
194 | glEnd();
|
---|
195 |
|
---|
196 | glBegin(GL_LINE_LOOP);
|
---|
197 | glVertex3d(fbr.x, fbr.y, fbr.z);
|
---|
198 | glVertex3d(fbl.x, fbl.y, fbl.z);
|
---|
199 | glVertex3d(nbl.x, nbl.y, nbl.z);
|
---|
200 | glVertex3d(nbr.x, nbr.y, nbr.z);
|
---|
201 | glEnd();
|
---|
202 |
|
---|
203 | glBegin(GL_LINE_LOOP);
|
---|
204 | glVertex3d(ftr.x, ftr.y, ftr.z);
|
---|
205 | glVertex3d(ftl.x, ftl.y, ftl.z);
|
---|
206 | glVertex3d(ntl.x, ntl.y, ntl.z);
|
---|
207 | glVertex3d(ntr.x, ntr.y, ntr.z);
|
---|
208 | glEnd();
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | } |
---|