Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Rendering 2D stuff with [0,0] at top left corner

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2008
    Posts
    2

    Rendering 2D stuff with [0,0] at top left corner

    Hi

    im trying to do some simple basic rendering in 2D. I started with first Nehe tutorial (setting openGL window) and changed it to render in 2D instead of 3D. Many people on internet do it this way so i hope that this code is correct. I didnt touch rest of the code.

    Changes in ReSizeGLScene().
    Code :
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, width, height, 0, 0, 1);
        glMatrixMode(GL_MODELVIEW);

    Rendering code:
    Code :
        glBegin(GL_LINES);
            glColor4ub(255, 0, 0, 255);
            glVertex2i(10, 1);
            glVertex2i(10, 100);
     
            glColor4ub(0, 255, 0, 255);
            glVertex2i(20, 1);
            glVertex2i(25, 1);
        glEnd();
     
        glBegin(GL_POINTS);
            glVertex2i(30, 1);
        glEnd();
     
        glBegin(GL_QUADS);
            glColor4ub(0, 0, 255, 255);
            glVertex2i(35, 1);
            glVertex2i(35 + 10, 1);
            glVertex2i(35 + 10, 1 + 10);
            glVertex2i(35, 1 + 10);
        glEnd();

    My code: pastebin.com
    Pic of my app: imageshack.us

    As you can see red line and blue quad are rendered correctly, but green line and dot should be rendered one pixel lower. Now id like to know why this happens and how can i fix it.
    When point [0,0] is at default position (bottom left - means glOrtho(0, width, 0, height, 0, 1) everything works fine, but i would really appreciate having [0,0] at top left of my window.

    thank you very much for any help or explanation
    Peto

  2. #2
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,261

    Re: Rendering 2D stuff with [0,0] at top left corner

    Put a glTranslatef(0.375,0.375,0); after the glMatrixMode(GL_MODELVIEW);
    Or 0.5,0.5,0 if that doesn't work, either.

  3. #3
    Junior Member Newbie
    Join Date
    Oct 2008
    Posts
    2

    Re: Rendering 2D stuff with [0,0] at top left corn

    Thanks for help but it still doesnt work. I tried both
    glTranslatef(0.375,0.375,0); and same with 0.5 but with same result.

    However when I put it just before my drawing code in DrawGLScene just after glLoadIdentity(); both point and line are drawn as they should.

    Code :
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();
     
        glTranslatef(0, 0.375, 0);
     
        glBegin(GL_LINES);
            glColor4ub(255, 0, 0, 255);
            glVertex2i(10, 1);
            glVertex2i(10, 100);
     
            glColor4ub(0, 255, 0, 255);
            glVertex2i(20, 1);
            glVertex2i(25, 1);
        glEnd();
     
        glBegin(GL_POINTS);
            glVertex2i(30, 1);
        glEnd();
     
        glBegin(GL_QUADS);
            glColor4ub(0, 0, 255, 255);
            glVertex2i(35, 1);
            glVertex2i(35 + 10, 1);
            glVertex2i(35 + 10, 1 + 10);
            glVertex2i(35, 1 + 10);
        glEnd();

    Is that correct and why does it solve my problem? Why do i need to call glTranslate?

  4. #4
    Advanced Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: Rendering 2D stuff with [0,0] at top left corn

    because of the pixel center and the floating point rounding.
    if your 35 coordinate goes throu a tranformation it can vary f.e 34.8.
    adding 0.375 will bring it to 35.175 which is ceiled a 35 coord.
    havent dont it in a long time. any corrections here?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •