Jump to content
  • Advertisement
Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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
	glLoadIdentity(); // reset projection matrix
	// calculate aspect ratio of window
	glMatrixMode(GL_MODELVIEW); // set modelview matrix
	glLoadIdentity(); // reset modelview matrix
	this->SWidth = width;
	this->SHeight = height;
	return true;
and for the drawing:

Share this post

Link to post
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 this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!