Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not understanding the gluOrtho2D function

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?

like image 918
ajaysinghnegi Avatar asked Apr 07 '18 07:04

ajaysinghnegi


People also ask

What is the function of gluOrtho2D?

The gluOrtho2D function sets up a two-dimensional orthographic viewing region. This is equivalent to calling glOrtho with zNear = -1 and zFar = 1.

What is the use of gluOrtho2D 0640 0480 );?

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

What is orthographic projection matrix?

In computer graphics, one of the most common matrices used for orthographic projection can be defined by a 6-tuple, (left, right, bottom, top, near, far), which defines the clipping planes. These planes form a box with the minimum corner at (left, bottom, -near) and the maximum corner at (right, top, -far).


2 Answers

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
like image 192
Rabbid76 Avatar answered Sep 22 '22 08:09

Rabbid76


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.

like image 28
derhass Avatar answered Sep 20 '22 08:09

derhass