# OpenGL Making the upper left corner position 0,0 and the bootm left on pos width,height?

This topic is 3855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I want to use OpenGL for some 2d drawing because of it's speed and features. I'm trying to set up the scene so when I draw a line from pos 0,0, to width,height it will actualy draw one on the screen at the same positions as well. If I don't set any perspective i get the 0,0 position on the center of the screen and but scaled down and If I use orthographic or perspective I won't see anything at all. Some source code:
        glViewport(0, 0, width, height); // reset the viewport to new dimensions
glMatrixMode(GL_PROJECTION); // set projection matrix current matrix
// calculate aspect ratio of window
gluPerspective(52.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);
glMatrixMode(GL_MODELVIEW); // set modelview matrix
this->SWidth = width;
this->SHeight = height;
return true;

and for the drawing:
        glPushMatrix();
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
glPopMatrix();


##### Share on other sites
Look into gluOrtho2D (it's nearly equivalent to glOrtho2D, btw).

Replace your gluPerspective call with something like this:

gluOrtho2D(0.0, resolution_x_width, resolution_y_height, 0.0);

Note that the 3rd parameter resolution_y corresponds to GLdouble bottom, and the 4th param 0 corresponds to GLdouble top.

That should give let you specify vertices in pixels, where (0.5, 0.5) is the center of the upper-left pixel, and (639.5, 479.5) is the lower-right pixel center (assuming a resolution of 640x480).

Edit: Reason I said (0.5, 0.5) instead of (0, 0) is... For lines, points, and other geometry that has no 'volume' (or rather it is 0), you usually want to specify pixel centers, otherwise it's up to the OpenGL implmenentation to decide between the neighbouring pixels. But for geometry with volume, like triangles, quads, etc. you want to specify pixel corners, so a quad from (0,0) to (5,5) is a 5 by 5 pixel quad. This is all assuming you used the gluOrtho2D function to setup your GL_PROJECTION matrix, of course. Just wanted to give you a heads-up if you are interested in perfect (sub-)pixel precision.

##### Share on other sites
Thank you... just by replacing that line everything works fine (and deleted the setviewport).

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 103
• 11
• ### Forum Statistics

• Total Topics
632976
• Total Posts
3009672
• ### Who's Online (See full list)

There are no registered users currently online

×