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