Not understanding the gluOrtho2D function
Asked Answered
D

2

9

I am not able to what gluOrtho2D() function is doing? Is it fixing the origin at some particular point on the OpenGL window or something else?

This is because gluOrtho2D(1,1,1,1) fixes the origin at the middle of the window.

If it is not fixing the origin at some point then is there any way that I can fix the origin because I have read that there is no such thing called "OpenGL Window coordinates"?

I read that gluOrtho2D(0,640,480,0) fixes the origin at the top left corner of the window but how do I know where is it shifting if other values are sent as parameters?

Danford answered 7/4, 2018 at 7:36 Comment(1)
This answer may be usefulShanteshantee
O
9

gluOrtho2D setup an Orthographic projection matrix, with a given left, right, top and bottom, but a fixed near and far plane of -1 respectively 1.

gluOrtho2D( left, right, bottom, top );

This means that

gluOrtho2D(0,640,480,0)

creates a projection matrix, which maps 0 to the left border of the viewport, 640 to the right, 480 to the bottom and 0 to the top.

The values for left, right, bottom, top, near and far define a box. All the geometry which is inside the volume of the box is "visible" on the viewport.


The projection matrix describes the mapping from 3D points of a scene, to 2D points of the viewport. It transforms from eye space to the clip space, and the coordinates in the clip space are transformed to the normalized device coordinates (NDC) by dividing with the w component of the clip coordinates. The NDC are in range (-1,-1,-1) to (1,1,1).
Every geometry which is out of the clippspace is clipped.

At Orthographic Projection the coordinates in the eye space are linearly mapped to normalized device coordinates and the clip space coordinates are equal the normalized device coordinates, because the w component is 1 (for a Cartesian coordinate).

Orthographic Projection Matrix:

r = right, l = left, b = bottom, t = top, n = near, f = far 

2/(r-l)         0               0               0
0               2/(t-b)         0               0
0               0               -2/(f-n)        0
-(r+l)/(r-l)    -(t+b)/(t-b)    -(f+n)/(f-n)    1
Outofdate answered 7/4, 2018 at 7:46 Comment(0)
H
2

In addition to @Rabbid76's answer:

This is because gluOrtho2D(1,1,1,1) fixes the origin at the middle of the window.

No. gluOrtho2D(1,1,1,1) does not do that. It does generate a GL_INVALID_VALUE error and leaves the current matrix untouched. It is not allowed to set left=right or top=bottom, since that does not make any geoemtric sense, and would also result in a division by zero in the matrix.

Since you most likely have the identity matrix loaded on the matrix stack before calling gluOrtho2D, what you will end up with is just the idendity matrix, which means that your view volume is an axis-aligned cube -1 <= x,y,z <= 1.

Hesitation answered 21/11, 2018 at 0:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.