codemax
codemax

Reputation: 457

what is the actual difference between gluortho2d and glViewport

I made a window sized 800x600. I called

gluOrtho2D(-400,400,-300,300);
glViewport(400,300,400,300);

and I drew a line from (-100,-100) to (100,100). I think I should see a line from (0,0) to (100,100), but I am getting the whole line. Why is this?

Upvotes: 3

Views: 7515

Answers (2)

Daniel Yankowsky
Daniel Yankowsky

Reputation: 7016

In theory, glViewport doesn't cause any clipping (see section 10). Normally, all drawing is clipped to the window. Since you have asked OpenGL to draw into a region of your window, you will need to also tell OpenGL to clip coordinates outside this viewport. For that, you will need glScissor. However, some implementations do clip their drawing to the viewport (see my comment for details).

In addition, your math is wrong. Your projection matrix is 800 units wide by 600 units tall, centered at (0, 0). This is then mapped to a portion of the window that is 400 pixels wide by 300 pixels tall, in the upper-right corner of the window.

If you draw a line from (-100, -100) to (100, 100), it will extend across only a small part of your viewing frustrum. The frustrum is sized to fit in the viewport.

In the image, the blue box is the window, and the red box represents the viewport. The black line should be the line that you drew.

An image describing what the paragraph says. http://img696.imageshack.us/img696/6541/opengl.png

Hope that helps!

Upvotes: 10

drahnr
drahnr

Reputation: 6886

glViewport describes the area of your window which will be drawn by OpenGL. glOrtho or gluOrtho2D define a unit system (OpenGL units) which fit into that (via glViewport defined) area. So your line will be drawn within the Viewport from -100,-100 to 100,100

Upvotes: 0

Related Questions