[2767] | 1 | #include "Visualization.h"
|
---|
| 2 | #include "glInterface.h"
|
---|
| 3 | #include "Camera.h"
|
---|
| 4 | #include "SceneEntity.h"
|
---|
| 5 | #include "Bvh.h"
|
---|
[2787] | 6 | #include "RenderState.h"
|
---|
[2911] | 7 | #include "ShadowMapping.h"
|
---|
[2767] | 8 | #include <stack>
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | using namespace std;
|
---|
| 12 |
|
---|
| 13 |
|
---|
[2776] | 14 | namespace CHCDemoEngine
|
---|
[2767] | 15 | {
|
---|
| 16 |
|
---|
[2788] | 17 |
|
---|
[2790] | 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 |
|
---|
[2767] | 65 | /******************************************************/
|
---|
[2806] | 66 | /* Vizualization implementation */
|
---|
[2767] | 67 | /******************************************************/
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | Visualization::Visualization(Bvh *bvh,
|
---|
[3062] | 71 | PerspectiveCamera *camera,
|
---|
[2767] | 72 | Camera *vizCamera,
|
---|
| 73 | RenderState *renderState):
|
---|
[2787] | 74 | mBvh(bvh),
|
---|
| 75 | mCamera(camera),
|
---|
| 76 | mVizCamera(vizCamera),
|
---|
| 77 | mRenderState(renderState),
|
---|
| 78 | mFrameId(0)
|
---|
[2767] | 79 | {
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 |
|
---|
[2796] | 83 | Visualization::~Visualization()
|
---|
| 84 | {
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
[2767] | 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 |
|
---|
[2787] | 100 | void Visualization::SetFrameId(int frameId)
|
---|
| 101 | {
|
---|
| 102 | mFrameId = frameId;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
[3063] | 106 | void Visualization::Render(bool showShadowFrustra)
|
---|
[2767] | 107 | {
|
---|
| 108 | stack<BvhNode *> tStack;
|
---|
| 109 | tStack.push(mBvh->GetRoot());
|
---|
| 110 |
|
---|
[2787] | 111 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
| 112 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 113 |
|
---|
[2767] | 114 | while (!tStack.empty())
|
---|
| 115 | {
|
---|
| 116 | BvhNode *node = tStack.top();
|
---|
| 117 | tStack.pop();
|
---|
| 118 |
|
---|
[2771] | 119 | if (!node->IsVirtualLeaf())
|
---|
[2767] | 120 | {
|
---|
| 121 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 122 |
|
---|
| 123 | tStack.push(interior->GetFront());
|
---|
| 124 | tStack.push(interior->GetBack());
|
---|
| 125 | }
|
---|
| 126 | else
|
---|
| 127 | {
|
---|
[2787] | 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 | }
|
---|
[2767] | 139 | }
|
---|
| 140 | }
|
---|
[2787] | 141 |
|
---|
| 142 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
| 143 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
[2767] | 144 |
|
---|
| 145 |
|
---|
[2806] | 146 | mRenderState->Reset();
|
---|
| 147 |
|
---|
[2790] | 148 | glPushAttrib(GL_CURRENT_BIT);
|
---|
[2806] | 149 | glDisable(GL_LIGHTING);
|
---|
| 150 | glDisable(GL_DEPTH_TEST);
|
---|
[2929] | 151 | glDepthMask(GL_FALSE);
|
---|
[2790] | 152 |
|
---|
[2913] | 153 | RenderFrustum();
|
---|
[2891] | 154 |
|
---|
[3063] | 155 | if (showShadowFrustra)
|
---|
| 156 | ShadowMap::VisualizeFrustra();
|
---|
[2911] | 157 |
|
---|
[2891] | 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 |
|
---|
[2806] | 173 | //RenderBoxForViz(mBvh->GetBox());
|
---|
[2929] | 174 |
|
---|
| 175 | glDepthMask(GL_TRUE);
|
---|
[2790] | 176 | glPopAttrib();
|
---|
[2767] | 177 | }
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | void Visualization::RenderFrustum()
|
---|
| 181 | {
|
---|
[2788] | 182 | glColor3f(1.0f, 0.0f, 0.0f);
|
---|
| 183 |
|
---|
[2767] | 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 | } |
---|