PDA

View Full Version : OpenGL wrong?



hanh le
09-18-2013, 07:54 PM
I try to draw a diagonal (actually it is points on diagonal) but OpenGL draw it as a horizontal line, I don't know why.
Pls help me, thank so much

Here is my code:

// CTestOGLView message handlers
void CTestOGLView::OnInitialUpdate()
{
CView::OnInitialUpdate();

HDC hDC = ::GetDC(m_hWnd);

CreateOpenGL(hDC);

::ReleaseDC(m_hWnd, hDC);
}

void CTestOGLView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);

if (cx > 0 && cy > 0)
{
double fWidth = 35.209655761718750;
double fHeight = 22.964477539062500;
double xc = 506654.83659999998;
double yc = 7805600.7220999999;

SetFrustum(cx, cy, fWidth, fHeight, xc, yc);
}
}

BOOL CTestOGLView::CreateOpenGL(HDC hDC)
{
if (!SetWindowPixelFormat(hDC))
return 0;

if (!CreateViewGLContext(hDC))
return 0;

//m_hDC = hDC;

return 1;
}

BOOL CTestOGLView::SetWindowPixelFormat(HDC hDC)
{
int iBPP = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);

ZeroMemory(&m_pfd, sizeof(PIXELFORMATDESCRIPTOR));

m_pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
m_pfd.nVersion = 1;
m_pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
m_pfd.iPixelType = PFD_TYPE_RGBA;
m_pfd.cColorBits = iBPP;
m_pfd.cDepthBits = 24;
m_pfd.iLayerType = PFD_MAIN_PLANE;

int pixelIdx = ChoosePixelFormat(hDC, &m_pfd);

if (pixelIdx <= 0)
return FALSE;

if (!SetPixelFormat(hDC, pixelIdx, &m_pfd))
return FALSE;

return TRUE;
}

BOOL CTestOGLView::CreateViewGLContext(HDC hDC, BOOL bSetCurrent)
{
if (!m_hRC)
m_hRC = wglCreateContext(hDC);

if (!m_hRC)
return FALSE;

if (!wglMakeCurrent(hDC, m_hRC))
return FALSE;

return TRUE;
}

void CTestOGLView::SetFrustum(int width, int height, double fWidth, double fHeight, double xc, double yc)
{
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluOrtho2D(xc - fWidth/2, xc + fWidth/2, yc - fHeight/2, yc + fHeight/2);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void CTestOGLView::OnDraw(CDC* pDC)
{
CTestOGLDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_POINTS);

glVertex3d(506655.5111, 7805600.4781, 0);
glVertex3d(506655.3184, 7805600.5478, 0);
glVertex3d(506655.2220, 7805600.5827, 0);
glVertex3d(506654.9330, 7805600.6872, 0);
glVertex3d(506654.8366, 7805600.7221, 0);

glEnd();
glFlush();

SwapBuffers(pDC->m_hDC);
}

carsten neumann
09-20-2013, 11:45 AM
Please use
around source code.

Given the numbers you pass in I wouldn't be too surprised if this is due to limited floating point precision. Note that although glVertex3d allows you to specify coordinates with double precision this gets converted to single precision float for the rest of the OpenGL pipeline.