PDA

View Full Version : Rendering 2D stuff with [0,0] at top left corner



ThePeto
10-16-2008, 03:29 PM
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().

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);

Rendering 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 (http://pastebin.com/m7dd3ecac)
Pic of my app: imageshack.us (http://img513.imageshack.us/img513/1232/98492360ud8.png)

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

Ilian Dinev
10-16-2008, 06:52 PM
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.

ThePeto
10-17-2008, 12:17 AM
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.


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?

_NK47
10-17-2008, 01:36 AM
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?