Results 1 to 2 of 2

Thread: OpenGL wrong?

  1. #1
    Newbie Newbie
    Join Date
    Sep 2013
    Posts
    1

    Exclamation OpenGL wrong?

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

  2. #2
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896
    Please use [code][/code] 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.

Similar Threads

  1. Opengl GL wrong portview position
    By Washiro in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-23-2012, 10:26 AM
  2. [C++/SDL/OpenGL] Wrong mouse information
    By PauliusM in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-05-2012, 05:11 AM
  3. What's wrong with my opengl fbo program
    By zhoudibin in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 05-23-2006, 10:30 PM
  4. What could be wrong with OpenGL calls? (ATI)
    By MickeyMouse in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 05-16-2004, 03:09 PM
  5. Wrong OpenGL initialization?
    By Shagrath in forum OpenGL: Linux
    Replies: 0
    Last Post: 02-06-2001, 12:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean