Hi, guys. Currently I am researching the optimised algorithms of building triangulations of 3D surfaces. And I faces the problem with visualization of the 3D triangulation. I tried to do it with OpenGL, but I couldn't set the point of view correctly. Can anybody help me?
This is my MFC dialog for visualization:
The problem is in Init() and DrawScene(). m_triangulation is my own class with all information about mesh,and the part with drawing is correct, I only need to set the poin of view rightly.
This is my MFC dialog for visualization:
Code:
#include "stdafx.h" #include "Terrain.h" #include "TerrainDisplay3D.h" #include <GL/gl.h> #include <GL/glaux.h> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define WIDTH 550 #define HEIGHT 550 #define SPEED 1 CTerrainDisplay3D::CTerrainDisplay3D(CWnd* pParent /*=NULL*/) : CDialog(CTerrainDisplay3D::IDD, pParent) { //{{AFX_DATA_INIT(CTerrainDisplay3D) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } void CTerrainDisplay3D::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTerrainDisplay3D) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CTerrainDisplay3D, CDialog) //{{AFX_MSG_MAP(CTerrainDisplay3D) ON_WM_TIMER() ON_WM_CLOSE() ON_WM_DESTROY() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTerrainDisplay3D message handlers BOOL CTerrainDisplay3D::PreCreateWindow(CREATESTRUCT &cs) { cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN; return CDialog::PreCreateWindow(cs); } BOOL CTerrainDisplay3D::OnInitDialog() { CDialog::OnInitDialog(); SetWindowPos(&wndTop, 0, 0, WIDTH, HEIGHT, SWP_NOMOVE); pDC = GetDC(); CenterWindow(); Init(); SetTimer(1,SPEED, NULL); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CTerrainDisplay3D::Init() { CRect rect; HGLRC hrc; if (!bSetupPixelFormat()) return; hrc = wglCreateContext(pDC->GetSafeHdc()); ASSERT(hrc != NULL); wglMakeCurrent(pDC->GetSafeHdc(), hrc); GetClientRect(&rect); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glShadeModel(GL_SMOOTH); glViewport(0,0,WIDTH,HEIGHT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0); glMatrixMode(GL_MODELVIEW); gluLookAt (4, 4, 5, 4, 4, 0, 0, 0, 0); } BOOL CTerrainDisplay3D::bSetupPixelFormat() { static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd 1, // version number PFD_DRAW_TO_WINDOW | // support window PFD_SUPPORT_OPENGL | // support OpenGL PFD_DOUBLEBUFFER, // double buffered PFD_TYPE_RGBA, // RGBA type 24, // 24-bit color depth 0, 0, 0, 0, 0, 0, // color bits ignored 0, // no alpha buffer 0, // shift bit ignored 0, // no accumulation buffer 0, 0, 0, 0, // accum bits ignored 32, // 32-bit z-buffer 0, // no stencil buffer 0, // no auxiliary buffer PFD_MAIN_PLANE, // main layer 0, // reserved 0, 0, 0 // layer masks ignored }; int pixelformat; if ((pixelformat = ChoosePixelFormat(pDC->GetSafeHdc(), &pfd)) == 0) { MessageBox("ChoosePixelFormat failed"); return FALSE; } if (SetPixelFormat(pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE) { MessageBox("SetPixelFormat failed"); return FALSE; } return TRUE; } void CTerrainDisplay3D::OnTimer(UINT nIDEvent) { DrawScene(); CDialog::OnTimer(nIDEvent); } void CTerrainDisplay3D::DrawScene() { glPushMatrix(); float m = WIDTH / m_triangulation.getSize(); int c = 0; int Ax,Ay,Bx,By,Cx,Cy; while (c < m_triangulation.triangles.size()){ Ax = m_triangulation.triangles[c].x; Ay = m_triangulation.triangles[c].y; c++; Bx = m_triangulation.triangles[c].x; By = m_triangulation.triangles[c].y; c++; Cx = m_triangulation.triangles[c].x; Cy = m_triangulation.triangles[c].y; c++; glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glBegin(GL_TRIANGLE_STRIP); glColor3ub(255,255,255); // белый glVertex3f(Ax, Ay, m_triangulation.heightField[Ax][Ay]); glVertex3f(Bx, By, m_triangulation.heightField[Bx][By]); glVertex3f(Cx, Cy, m_triangulation.heightField[Cx][Cy]); glEnd(); } glPopMatrix(); SwapBuffers(pDC->m_hDC); } void CTerrainDisplay3D::OnClose() { HGLRC hrc; KillTimer(1); hrc = ::wglGetCurrentContext(); ::wglMakeCurrent(NULL, NULL); if (hrc) ::wglDeleteContext(hrc); CDialog::OnClose(); } void CTerrainDisplay3D::OnDestroy() { CDialog::OnDestroy(); HGLRC hrc; KillTimer(1); hrc = ::wglGetCurrentContext(); ::wglMakeCurrent(NULL, NULL); if (hrc) ::wglDeleteContext(hrc); }
Comment