[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"
|
---|
[2787] | 8 |
|
---|
[2767] | 9 | #include <stack>
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | using namespace std;
|
---|
| 13 |
|
---|
| 14 |
|
---|
[2776] | 15 | namespace CHCDemoEngine
|
---|
[2767] | 16 | {
|
---|
| 17 |
|
---|
[2788] | 18 |
|
---|
[2790] | 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 |
|
---|
[2767] | 66 | /******************************************************/
|
---|
[2806] | 67 | /* Vizualization implementation */
|
---|
[2767] | 68 | /******************************************************/
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | Visualization::Visualization(Bvh *bvh,
|
---|
| 72 | Camera *camera,
|
---|
| 73 | Camera *vizCamera,
|
---|
| 74 | RenderState *renderState):
|
---|
[2787] | 75 | mBvh(bvh),
|
---|
| 76 | mCamera(camera),
|
---|
| 77 | mVizCamera(vizCamera),
|
---|
| 78 | mRenderState(renderState),
|
---|
| 79 | mFrameId(0)
|
---|
[2767] | 80 | {
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 |
|
---|
[2796] | 84 | Visualization::~Visualization()
|
---|
| 85 | {
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 |
|
---|
[2767] | 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 |
|
---|
[2787] | 101 | void Visualization::SetFrameId(int frameId)
|
---|
| 102 | {
|
---|
| 103 | mFrameId = frameId;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 |
|
---|
[2767] | 107 | void Visualization::Render()
|
---|
| 108 | {
|
---|
| 109 | stack<BvhNode *> tStack;
|
---|
| 110 | tStack.push(mBvh->GetRoot());
|
---|
| 111 |
|
---|
[2787] | 112 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
| 113 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 114 |
|
---|
[2767] | 115 | while (!tStack.empty())
|
---|
| 116 | {
|
---|
| 117 | BvhNode *node = tStack.top();
|
---|
| 118 | tStack.pop();
|
---|
| 119 |
|
---|
[2771] | 120 | if (!node->IsVirtualLeaf())
|
---|
[2767] | 121 | {
|
---|
| 122 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 123 |
|
---|
| 124 | tStack.push(interior->GetFront());
|
---|
| 125 | tStack.push(interior->GetBack());
|
---|
| 126 | }
|
---|
| 127 | else
|
---|
| 128 | {
|
---|
[2787] | 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 | }
|
---|
[2767] | 140 | }
|
---|
| 141 | }
|
---|
[2787] | 142 |
|
---|
| 143 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
| 144 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
[2767] | 145 |
|
---|
| 146 |
|
---|
[2806] | 147 | mRenderState->Reset();
|
---|
| 148 |
|
---|
[2790] | 149 | glPushAttrib(GL_CURRENT_BIT);
|
---|
[2806] | 150 | glDisable(GL_LIGHTING);
|
---|
| 151 | glDisable(GL_DEPTH_TEST);
|
---|
[2929] | 152 | glDepthMask(GL_FALSE);
|
---|
[2790] | 153 |
|
---|
[2913] | 154 | RenderFrustum();
|
---|
[2891] | 155 |
|
---|
[2963] | 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 | } |
---|