user2793834
user2793834

Reputation: 1

Diagonal OpenGL lines lose vertical component?

I try to draw a diagonal but OpenGL shows it as a horizontal line, I don't know why.

Here is my code:

Setup OpenGL:

if (!SetWindowPixelFormat(hDC))
    return 0;

if (!CreateViewGLContext(hDC))
    return 0;


BOOL 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;
}

Set frustum:

void OnSize(UINT nType, int cx, int cy)
{
    double fWidth = 35.209655761718750;
    double fHeight = 22.964477539062500;
    double xc = 506654.83659999998;
    double yc = 7805600.7220999999;

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

void 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();
}

Draw 5 points below must be on a diagonal but OpenGL shows as a horizontal line:

void OnDraw(CDC* pDC)
{
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);
}

Upvotes: 0

Views: 156

Answers (1)

datenwolf
datenwolf

Reputation: 162309

You're simply and clearly running into a precision problem here. Floating point values are only approximate. You got a fixed number of significant digits (the mantissa) and an exponent telling the order of magnitude. Now you've got two very different orders of magnitude here: The order of magnitude the absolute values your numbers have (which are large-ish) and the order of magnitude those numbers vary (which are small-ish). If you throw 4 multiplications and 4 additions onto them (which is what happens when a vertex component gets transformed by matrix multiplication) your very smallish value variations will get lost within the numerical errors accumulating doing floating point operations. In your case your numbers eventually round off to the same integer, but if you're unlucky they actually jitter around.

It doesn't matter if you use single or double precision math here: Either a GPU doesn't support double precision at all or must be forced into it, which usually involves a lot of internal bit juggling to emulate it. When you're working with a GPU you effectively are limited to working with single precision.

Now I really wonder why your numbers are so odd? What is it what you try to draw, that you have such a large range of orders of magnitude in it?

Upvotes: 1

Related Questions