Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have read that you can define your OpenGL unit by using glViewport. As far as I understood if you use something like glViewport(0,0,600,600) then 1 OpenGL unit = 600 pixels. Now my problem is what happens if the 2 sizes aren't equal? How do you calculate your OpenGL unit?

share|improve this question
up vote 8 down vote accepted

glViewport actually defines the size of your window, so glViewport(0,0,600,600) defines a window of 600 x 600.

By default opengl's coordinate system sees 0,0 as the center of your window and 1,1 as the upper right hand. If you want to change that you use either glOrtho or gluPerspective. Using glOrtho to figure the unit/pixel ratio is easy:




Screen coordinates

screen_left // the x coordinate that your window is created at 
// relative to your screen
screen_top // the y coordinate that your window is created at 
// relative to your screen

how screen coords are seen

Opengl Coordinates

negative_x // the left x coordinate that opengl sees
positive_x // the right x coordinate that opengl sees

negative_x // the bottom y coordinate that opengl sees
positive_x // the top y coordinate that opengl sees

Opengl coordinates

Be sure to note that screen coordinates are not opengl coordinates


x_ratio = (positive_x - negative_x)/width;
y_ratio = (positive_y - negative_y)/height;

Try this tutorial's lesson 2 & 3 on setting up the viewport correctly.

share|improve this answer
Thnx for the turotrial,now i think i do understand what Viewport actually does.But giving the fact that I do render a whole screen => my left/top will be 0 which means x_ratio=y_ratio=1 if my math is right. – user1640736 Mar 4 '13 at 18:43
Ah, I should have explained that better. left and top are independent entirely of where your screen is located. I'll edit to see if I can clarify. – Alex Shepard Mar 4 '13 at 19:00
So that means i set those values however i want using glOrtho? – user1640736 Mar 4 '13 at 19:19
*_x and *_y happen to be values that you can pick. If I want my coordinates on screen to be x:(-100,50),y:(-60,20),z(-10,10) I would call glOrtho(-100,50,-60,20,-10,10); At that point its a matter of preference – Alex Shepard Mar 4 '13 at 19:22
Thnx a lot for this answere :D Helped me a LOT! – user1640736 Mar 4 '13 at 19:30

glViewport is just a transformation; see here for an explanation of where it fits into the pipeline and how it's applied. It may be helpful to think of it as defining an area of the window to draw to, but you should also understand that in reality it's something else.

Regarding "units", OpenGL does not define any "units" at all at any stage in it's pipeline, nor does it provide any means by which you may do so. It's up to you to decide how co-ordinates map to "units" in your own program (C/C++/etc) code; so for example if you see a call to glTranslate you get both the choice of and the responsibility for what the values passed to it mean in terms of your own code.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.