Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use glOrtho() in OpenGL?

Tags:

c++

c

opengl

I can't understand the usage of glOrtho. Can someone explain what it is used for?

Is it used to set the range of x y and z coordinates limit?

glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); 

It means that the x, y and z range is from -1 to 1?

like image 692
ufk Avatar asked Apr 03 '10 13:04

ufk


People also ask

What does glOrtho do in OpenGL?

glOrtho describes a transformation that produces a parallel projection.

What is glOrtho()?

The glOrtho subroutine describes a perspective matrix that produces a parallel projection. (Left, Bottom, -Near) and (Right, Top, -Near) specify the points on the near clipping plane that are mapped to the lower left and upper right corners of the window, respectively, assuming that the eye is located at (0, 0, 0).

What is the function of gluLookAt () and glOrtho ()?

gluLookAt sets up the world to view space transformation, glOrtho does view to orthographic projection space transformation and glFrustum does view to perspective projection space transformation.

How does gluOrtho2D function work?

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


1 Answers

Have a look at this picture: Graphical Projections enter image description here

The glOrtho command produces an "Oblique" projection that you see in the bottom row. No matter how far away vertexes are in the z direction, they will not recede into the distance.

I use glOrtho every time I need to do 2D graphics in OpenGL (such as health bars, menus etc) using the following code every time the window is resized:

glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0f, windowWidth, windowHeight, 0.0f, 0.0f, 1.0f); 

This will remap the OpenGL coordinates into the equivalent pixel values (X going from 0 to windowWidth and Y going from 0 to windowHeight). Note that I've flipped the Y values because OpenGL coordinates start from the bottom left corner of the window. So by flipping, I get a more conventional (0,0) starting at the top left corner of the window rather.

Note that the Z values are clipped from 0 to 1. So be careful when you specify a Z value for your vertex's position, it will be clipped if it falls outside that range. Otherwise if it's inside that range, it will appear to have no effect on the position except for Z tests.

like image 168
Mikepote Avatar answered Sep 16 '22 16:09

Mikepote