#include "Visualization.h" #include "glInterface.h" #include "Camera.h" #include "SceneEntity.h" #include "Bvh.h" #include "RenderState.h" #include "ShadowMapping.h" #include "ViewCellsTree.h" #include "Material.h" #include using namespace std; namespace CHCDemoEngine { void Visualization::RenderBoxForViz(const AxisAlignedBox3 &box) { glBegin(GL_LINE_LOOP); glVertex3d(box.Min().x, box.Max().y, box.Min().z); glVertex3d(box.Max().x, box.Max().y, box.Min().z); glVertex3d(box.Max().x, box.Min().y, box.Min().z); glVertex3d(box.Min().x, box.Min().y, box.Min().z); glEnd(); glBegin(GL_LINE_LOOP); glVertex3d(box.Min().x, box.Min().y, box.Max().z); glVertex3d(box.Max().x, box.Min().y, box.Max().z); glVertex3d(box.Max().x, box.Max().y, box.Max().z); glVertex3d(box.Min().x, box.Max().y, box.Max().z); glEnd(); glBegin(GL_LINE_LOOP); glVertex3d(box.Max().x, box.Min().y, box.Min().z); glVertex3d(box.Max().x, box.Min().y, box.Max().z); glVertex3d(box.Max().x, box.Max().y, box.Max().z); glVertex3d(box.Max().x, box.Max().y, box.Min().z); glEnd(); glBegin(GL_LINE_LOOP); glVertex3d(box.Min().x, box.Min().y, box.Min().z); glVertex3d(box.Min().x, box.Min().y, box.Max().z); glVertex3d(box.Min().x, box.Max().y, box.Max().z); glVertex3d(box.Min().x, box.Max().y, box.Min().z); glEnd(); glBegin(GL_LINE_LOOP); glVertex3d(box.Min().x, box.Min().y, box.Min().z); glVertex3d(box.Max().x, box.Min().y, box.Min().z); glVertex3d(box.Max().x, box.Min().y, box.Max().z); glVertex3d(box.Min().x, box.Min().y, box.Max().z); glEnd(); glBegin(GL_LINE_LOOP); glVertex3d(box.Min().x, box.Max().y, box.Min().z); glVertex3d(box.Max().x, box.Max().y, box.Min().z); glVertex3d(box.Max().x, box.Max().y, box.Max().z); glVertex3d(box.Min().x, box.Max().y, box.Max().z); glEnd(); } /******************************************************/ /* Vizualization implementation */ /******************************************************/ Visualization::Visualization(Bvh *bvh, PerspectiveCamera *camera, Camera *vizCamera, RenderState *renderState): mBvh(bvh), mCamera(camera), mVizCamera(vizCamera), mRenderState(renderState), mFrameId(0), mViewCell(NULL) { } Visualization::~Visualization() { } void Visualization::SetHierarchy(Bvh *bvh) { mBvh = bvh; } void Visualization::SetRenderState(RenderState *state) { mRenderState = state; } void Visualization::SetFrameId(int frameId) { mFrameId = frameId; } void Visualization::SetViewCell(ViewCell *vc) { mViewCell = vc; } static Technique GetVizTechnique() { Technique tech; tech.Init(); //tech.SetLightingEnabled(false); //tech.SetDepthWriteEnabled(false); tech.SetEmmisive(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f)); tech.SetDiffuse(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f)); tech.SetAmbient(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f)); return tech; } void Visualization::Render(bool showShadowFrustra) { stack tStack; tStack.push(mBvh->GetRoot()); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); while (!tStack.empty()) { BvhNode *node = tStack.top(); tStack.pop(); if (!node->IsVirtualLeaf()) { BvhInterior *interior = static_cast(node); tStack.push(interior->GetFront()); tStack.push(interior->GetBack()); } else { if (node->GetLastRenderedFrame() == mFrameId) { int geometrySize; SceneEntity **entities = mBvh->GetGeometry(node, geometrySize); for (int i = 0; i < geometrySize; ++ i) { SceneEntity *ent = entities[i]; if (ent->IsVisible()) ent->Render(mRenderState); } } } } // render current view cell static Technique vcTechnique = GetVizTechnique(); if (mViewCell) { vcTechnique.Render(mRenderState); RenderBoxForViz(mViewCell->GetBox()); } glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); mRenderState->Reset(); glPushAttrib(GL_CURRENT_BIT); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); glDepthMask(GL_FALSE); RenderFrustum(); if (showShadowFrustra) ShadowMap::VisualizeFrustra(); Vector3 pos = mCamera->GetPosition(); // coordinates glColor3f(0.0f, 1.0f, 0.0f); glBegin(GL_LINES); glVertex3d(pos.x, pos.y, pos.z); glVertex3d(pos.x + 100, pos.y, pos.z); glEnd(); glColor3f(0.0f, 0.0f, 1.0f); glBegin(GL_LINES); glVertex3d(pos.x, pos.y, pos.z); glVertex3d(pos.x, pos.y + 100, pos.z); glEnd(); //RenderBoxForViz(mBvh->GetBox()); glDepthMask(GL_TRUE); glPopAttrib(); } void Visualization::RenderFrustum() { glColor3f(1.0f, 0.0f, 0.0f); Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr; mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr); glLineWidth(2); glBegin(GL_LINE_LOOP); glVertex3d(fbl.x, fbl.y, fbl.z); glVertex3d(ftl.x, ftl.y, ftl.z); glVertex3d(ntl.x, ntl.y, ntl.z); glVertex3d(nbl.x, nbl.y, nbl.z); glEnd(); glBegin(GL_LINE_LOOP); glVertex3d(fbr.x, fbr.y, fbr.z); glVertex3d(ftr.x, ftr.y, ftr.z); glVertex3d(ntr.x, ntr.y, ntr.z); glVertex3d(nbr.x, nbr.y, nbr.z); glEnd(); glBegin(GL_LINE_LOOP); glVertex3d(fbr.x, fbr.y, fbr.z); glVertex3d(fbl.x, fbl.y, fbl.z); glVertex3d(nbl.x, nbl.y, nbl.z); glVertex3d(nbr.x, nbr.y, nbr.z); glEnd(); glBegin(GL_LINE_LOOP); glVertex3d(ftr.x, ftr.y, ftr.z); glVertex3d(ftl.x, ftl.y, ftl.z); glVertex3d(ntl.x, ntl.y, ntl.z); glVertex3d(ntr.x, ntr.y, ntr.z); glEnd(); } }