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?


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.

| improve this answer | |
  • \$\begingroup\$ 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. \$\endgroup\$ – user1640736 Mar 4 '13 at 18:43
  • \$\begingroup\$ 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. \$\endgroup\$ – Alex Shepard Mar 4 '13 at 19:00
  • \$\begingroup\$ So that means i set those values however i want using glOrtho? \$\endgroup\$ – user1640736 Mar 4 '13 at 19:19
  • \$\begingroup\$ *_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 \$\endgroup\$ – Alex Shepard Mar 4 '13 at 19:22
  • \$\begingroup\$ Thnx a lot for this answere :D Helped me a LOT! \$\endgroup\$ – 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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