[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"
|
---|
| 7 |
|
---|
[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,
|
---|
| 71 | Camera *camera,
|
---|
| 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 |
|
---|
[2767] | 106 | void Visualization::Render()
|
---|
| 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);
|
---|
[2790] | 151 |
|
---|
[2806] | 152 | RenderFrustum();
|
---|
| 153 | //RenderBoxForViz(mBvh->GetBox());
|
---|
[2790] | 154 |
|
---|
| 155 | glPopAttrib();
|
---|
[2767] | 156 | }
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 | void Visualization::RenderFrustum()
|
---|
| 160 | {
|
---|
[2788] | 161 | glColor3f(1.0f, 0.0f, 0.0f);
|
---|
| 162 |
|
---|
[2767] | 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(ftl.x, ftl.y, ftl.z);
|
---|
| 171 | glVertex3d(ntl.x, ntl.y, ntl.z);
|
---|
| 172 | glVertex3d(nbl.x, nbl.y, nbl.z);
|
---|
| 173 | glEnd();
|
---|
| 174 |
|
---|
| 175 | glBegin(GL_LINE_LOOP);
|
---|
| 176 | glVertex3d(fbr.x, fbr.y, fbr.z);
|
---|
| 177 | glVertex3d(ftr.x, ftr.y, ftr.z);
|
---|
| 178 | glVertex3d(ntr.x, ntr.y, ntr.z);
|
---|
| 179 | glVertex3d(nbr.x, nbr.y, nbr.z);
|
---|
| 180 | glEnd();
|
---|
| 181 |
|
---|
| 182 | glBegin(GL_LINE_LOOP);
|
---|
| 183 | glVertex3d(fbr.x, fbr.y, fbr.z);
|
---|
| 184 | glVertex3d(fbl.x, fbl.y, fbl.z);
|
---|
| 185 | glVertex3d(nbl.x, nbl.y, nbl.z);
|
---|
| 186 | glVertex3d(nbr.x, nbr.y, nbr.z);
|
---|
| 187 | glEnd();
|
---|
| 188 |
|
---|
| 189 | glBegin(GL_LINE_LOOP);
|
---|
| 190 | glVertex3d(ftr.x, ftr.y, ftr.z);
|
---|
| 191 | glVertex3d(ftl.x, ftl.y, ftl.z);
|
---|
| 192 | glVertex3d(ntl.x, ntl.y, ntl.z);
|
---|
| 193 | glVertex3d(ntr.x, ntr.y, ntr.z);
|
---|
| 194 | glEnd();
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 |
|
---|
| 198 | } |
---|