# Defining an OpenGL unit

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:

given:

glViewport(screen_left,screen_top,width,height);
glOrtho(negative_x,positive_x,negative_y,positive_y,positive_z,negative_z);


where:

Screen coordinates

screen_left // the x coordinate that your window is created at
screen_top // the y coordinate that your window is created at 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 Be sure to note that screen coordinates are not opengl coordinates

then:

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.

• 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.