[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"
|
---|
[3245] | 8 | #include "ViewCellsTree.h"
|
---|
| 9 | #include "Material.h"
|
---|
| 10 |
|
---|
| 11 |
|
---|
[2767] | 12 | #include <stack>
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | using namespace std;
|
---|
| 16 |
|
---|
| 17 |
|
---|
[2776] | 18 | namespace CHCDemoEngine
|
---|
[2767] | 19 | {
|
---|
| 20 |
|
---|
[2788] | 21 |
|
---|
[3245] | 22 | void Visualization::RenderBoxForViz(const AxisAlignedBox3 &box)
|
---|
[2790] | 23 | {
|
---|
| 24 | glBegin(GL_LINE_LOOP);
|
---|
| 25 | glVertex3d(box.Min().x, box.Max().y, box.Min().z);
|
---|
| 26 | glVertex3d(box.Max().x, box.Max().y, box.Min().z);
|
---|
| 27 | glVertex3d(box.Max().x, box.Min().y, box.Min().z);
|
---|
| 28 | glVertex3d(box.Min().x, box.Min().y, box.Min().z);
|
---|
| 29 | glEnd();
|
---|
| 30 |
|
---|
| 31 | glBegin(GL_LINE_LOOP);
|
---|
| 32 | glVertex3d(box.Min().x, box.Min().y, box.Max().z);
|
---|
| 33 | glVertex3d(box.Max().x, box.Min().y, box.Max().z);
|
---|
| 34 | glVertex3d(box.Max().x, box.Max().y, box.Max().z);
|
---|
| 35 | glVertex3d(box.Min().x, box.Max().y, box.Max().z);
|
---|
| 36 | glEnd();
|
---|
| 37 |
|
---|
| 38 | glBegin(GL_LINE_LOOP);
|
---|
| 39 | glVertex3d(box.Max().x, box.Min().y, box.Min().z);
|
---|
| 40 | glVertex3d(box.Max().x, box.Min().y, box.Max().z);
|
---|
| 41 | glVertex3d(box.Max().x, box.Max().y, box.Max().z);
|
---|
| 42 | glVertex3d(box.Max().x, box.Max().y, box.Min().z);
|
---|
| 43 | glEnd();
|
---|
| 44 |
|
---|
| 45 | glBegin(GL_LINE_LOOP);
|
---|
| 46 | glVertex3d(box.Min().x, box.Min().y, box.Min().z);
|
---|
| 47 | glVertex3d(box.Min().x, box.Min().y, box.Max().z);
|
---|
| 48 | glVertex3d(box.Min().x, box.Max().y, box.Max().z);
|
---|
| 49 | glVertex3d(box.Min().x, box.Max().y, box.Min().z);
|
---|
| 50 | glEnd();
|
---|
| 51 |
|
---|
| 52 | glBegin(GL_LINE_LOOP);
|
---|
| 53 | glVertex3d(box.Min().x, box.Min().y, box.Min().z);
|
---|
| 54 | glVertex3d(box.Max().x, box.Min().y, box.Min().z);
|
---|
| 55 | glVertex3d(box.Max().x, box.Min().y, box.Max().z);
|
---|
| 56 | glVertex3d(box.Min().x, box.Min().y, box.Max().z);
|
---|
| 57 | glEnd();
|
---|
| 58 |
|
---|
| 59 | glBegin(GL_LINE_LOOP);
|
---|
| 60 | glVertex3d(box.Min().x, box.Max().y, box.Min().z);
|
---|
| 61 | glVertex3d(box.Max().x, box.Max().y, box.Min().z);
|
---|
| 62 | glVertex3d(box.Max().x, box.Max().y, box.Max().z);
|
---|
| 63 | glVertex3d(box.Min().x, box.Max().y, box.Max().z);
|
---|
| 64 |
|
---|
| 65 | glEnd();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
[2767] | 69 | /******************************************************/
|
---|
[2806] | 70 | /* Vizualization implementation */
|
---|
[2767] | 71 | /******************************************************/
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | Visualization::Visualization(Bvh *bvh,
|
---|
[3062] | 75 | PerspectiveCamera *camera,
|
---|
[2767] | 76 | Camera *vizCamera,
|
---|
| 77 | RenderState *renderState):
|
---|
[2787] | 78 | mBvh(bvh),
|
---|
| 79 | mCamera(camera),
|
---|
| 80 | mVizCamera(vizCamera),
|
---|
| 81 | mRenderState(renderState),
|
---|
[3245] | 82 | mFrameId(0),
|
---|
| 83 | mViewCell(NULL)
|
---|
[2767] | 84 | {
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
[2796] | 88 | Visualization::~Visualization()
|
---|
| 89 | {
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 |
|
---|
[2767] | 93 | void Visualization::SetHierarchy(Bvh *bvh)
|
---|
| 94 | {
|
---|
| 95 | mBvh = bvh;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | void Visualization::SetRenderState(RenderState *state)
|
---|
| 100 | {
|
---|
| 101 | mRenderState = state;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 |
|
---|
[2787] | 105 | void Visualization::SetFrameId(int frameId)
|
---|
| 106 | {
|
---|
| 107 | mFrameId = frameId;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 |
|
---|
[3245] | 111 | void Visualization::SetViewCell(ViewCell *vc)
|
---|
| 112 | {
|
---|
| 113 | mViewCell = vc;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | static Technique GetVizTechnique()
|
---|
| 118 | {
|
---|
| 119 | Technique tech;
|
---|
| 120 | tech.Init();
|
---|
| 121 |
|
---|
| 122 | //tech.SetLightingEnabled(false);
|
---|
| 123 | //tech.SetDepthWriteEnabled(false);
|
---|
| 124 |
|
---|
| 125 | tech.SetEmmisive(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f));
|
---|
| 126 | tech.SetDiffuse(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f));
|
---|
| 127 | tech.SetAmbient(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f));
|
---|
| 128 |
|
---|
| 129 | return tech;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 |
|
---|
[3063] | 133 | void Visualization::Render(bool showShadowFrustra)
|
---|
[2767] | 134 | {
|
---|
| 135 | stack<BvhNode *> tStack;
|
---|
| 136 | tStack.push(mBvh->GetRoot());
|
---|
| 137 |
|
---|
[2787] | 138 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
| 139 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 140 |
|
---|
[2767] | 141 | while (!tStack.empty())
|
---|
| 142 | {
|
---|
| 143 | BvhNode *node = tStack.top();
|
---|
| 144 | tStack.pop();
|
---|
| 145 |
|
---|
[2771] | 146 | if (!node->IsVirtualLeaf())
|
---|
[2767] | 147 | {
|
---|
| 148 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 149 |
|
---|
| 150 | tStack.push(interior->GetFront());
|
---|
| 151 | tStack.push(interior->GetBack());
|
---|
| 152 | }
|
---|
| 153 | else
|
---|
| 154 | {
|
---|
[2787] | 155 | if (node->GetLastRenderedFrame() == mFrameId)
|
---|
| 156 | {
|
---|
| 157 | int geometrySize;
|
---|
| 158 | SceneEntity **entities = mBvh->GetGeometry(node, geometrySize);
|
---|
| 159 |
|
---|
| 160 | for (int i = 0; i < geometrySize; ++ i)
|
---|
| 161 | {
|
---|
| 162 | SceneEntity *ent = entities[i];
|
---|
[3245] | 163 |
|
---|
| 164 | if (ent->IsVisible())
|
---|
| 165 | ent->Render(mRenderState);
|
---|
[2787] | 166 | }
|
---|
| 167 | }
|
---|
[2767] | 168 | }
|
---|
| 169 | }
|
---|
[2787] | 170 |
|
---|
[3245] | 171 | // render current view cell
|
---|
| 172 | static Technique vcTechnique = GetVizTechnique();
|
---|
| 173 |
|
---|
| 174 | if (mViewCell)
|
---|
| 175 | {
|
---|
| 176 | vcTechnique.Render(mRenderState);
|
---|
[3246] | 177 | RenderBoxForViz(mViewCell->GetBox());
|
---|
[3245] | 178 | }
|
---|
| 179 |
|
---|
[2787] | 180 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
| 181 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
[2767] | 182 |
|
---|
| 183 |
|
---|
[2806] | 184 | mRenderState->Reset();
|
---|
| 185 |
|
---|
[2790] | 186 | glPushAttrib(GL_CURRENT_BIT);
|
---|
[2806] | 187 | glDisable(GL_LIGHTING);
|
---|
| 188 | glDisable(GL_DEPTH_TEST);
|
---|
[2929] | 189 | glDepthMask(GL_FALSE);
|
---|
[2790] | 190 |
|
---|
[2913] | 191 | RenderFrustum();
|
---|
[2891] | 192 |
|
---|
[3063] | 193 | if (showShadowFrustra)
|
---|
| 194 | ShadowMap::VisualizeFrustra();
|
---|
[2911] | 195 |
|
---|
[2891] | 196 | Vector3 pos = mCamera->GetPosition();
|
---|
| 197 |
|
---|
| 198 | // coordinates
|
---|
| 199 | glColor3f(0.0f, 1.0f, 0.0f);
|
---|
| 200 | glBegin(GL_LINES);
|
---|
| 201 | glVertex3d(pos.x, pos.y, pos.z);
|
---|
| 202 | glVertex3d(pos.x + 100, pos.y, pos.z);
|
---|
| 203 | glEnd();
|
---|
| 204 |
|
---|
| 205 | glColor3f(0.0f, 0.0f, 1.0f);
|
---|
| 206 | glBegin(GL_LINES);
|
---|
| 207 | glVertex3d(pos.x, pos.y, pos.z);
|
---|
| 208 | glVertex3d(pos.x, pos.y + 100, pos.z);
|
---|
| 209 | glEnd();
|
---|
| 210 |
|
---|
[2806] | 211 | //RenderBoxForViz(mBvh->GetBox());
|
---|
[2929] | 212 |
|
---|
| 213 | glDepthMask(GL_TRUE);
|
---|
[2790] | 214 | glPopAttrib();
|
---|
[2767] | 215 | }
|
---|
| 216 |
|
---|
| 217 |
|
---|
| 218 | void Visualization::RenderFrustum()
|
---|
| 219 | {
|
---|
[2788] | 220 | glColor3f(1.0f, 0.0f, 0.0f);
|
---|
| 221 |
|
---|
[2767] | 222 | Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
|
---|
| 223 | mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
|
---|
| 224 |
|
---|
| 225 | glLineWidth(2);
|
---|
| 226 |
|
---|
| 227 | glBegin(GL_LINE_LOOP);
|
---|
| 228 | glVertex3d(fbl.x, fbl.y, fbl.z);
|
---|
| 229 | glVertex3d(ftl.x, ftl.y, ftl.z);
|
---|
| 230 | glVertex3d(ntl.x, ntl.y, ntl.z);
|
---|
| 231 | glVertex3d(nbl.x, nbl.y, nbl.z);
|
---|
| 232 | glEnd();
|
---|
| 233 |
|
---|
| 234 | glBegin(GL_LINE_LOOP);
|
---|
| 235 | glVertex3d(fbr.x, fbr.y, fbr.z);
|
---|
| 236 | glVertex3d(ftr.x, ftr.y, ftr.z);
|
---|
| 237 | glVertex3d(ntr.x, ntr.y, ntr.z);
|
---|
| 238 | glVertex3d(nbr.x, nbr.y, nbr.z);
|
---|
| 239 | glEnd();
|
---|
| 240 |
|
---|
| 241 | glBegin(GL_LINE_LOOP);
|
---|
| 242 | glVertex3d(fbr.x, fbr.y, fbr.z);
|
---|
| 243 | glVertex3d(fbl.x, fbl.y, fbl.z);
|
---|
| 244 | glVertex3d(nbl.x, nbl.y, nbl.z);
|
---|
| 245 | glVertex3d(nbr.x, nbr.y, nbr.z);
|
---|
| 246 | glEnd();
|
---|
| 247 |
|
---|
| 248 | glBegin(GL_LINE_LOOP);
|
---|
| 249 | glVertex3d(ftr.x, ftr.y, ftr.z);
|
---|
| 250 | glVertex3d(ftl.x, ftl.y, ftl.z);
|
---|
| 251 | glVertex3d(ntl.x, ntl.y, ntl.z);
|
---|
| 252 | glVertex3d(ntr.x, ntr.y, ntr.z);
|
---|
| 253 | glEnd();
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 |
|
---|
| 257 | } |
---|