C – How to use glortho () in OpenGL?

I can’t understand the usage of glOrtho. Can someone explain its usage?

Is it used to set the limits of x, y and z coordinates?

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

This means that the range of x, y and z is- 1 to 1?

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

The glOrtho command produces an “oblique” projection, which you can see on the bottom line. No matter how far the vertices are in the z direction, they will not fall back into the distance.

I use glOrtho every time I need to do 2D graphics in OpenGL (e.g. health bar, menu, etc.)
Use the following code each time you resize the window:

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

This will remap the OpenGL coordinates to the equivalent pixel values ​​(X from 0 to windowWidth, Y from 0 to windowHeight). Please note that I have The Y value is flipped, because the OpenGL coordinates start from the lower left corner of the window. So by flipping, I get a more regular (0,0) starting from the upper left corner of the window.

< p>I cannot understand the usage of glOrtho. Can someone explain its usage?

Is it used to set the limits of x, y and z coordinates?

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

This means that the range of x, y and z is- 1 to 1?

Look at this picture: Graphical Projections
enter image description here

The glOrtho command produces an “oblique” projection, which you can see on the bottom row. No matter how far the vertices are in the z direction, they are all Will not fall back to the distance.

Every time I need to do 2D graphics in OpenGL, I will use glOrtho (such as health bar, menu, etc.)
I use the following code every time I resize the window:< /p>

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, windowWidth, windowHeight, 0.0f, 0.0f, 1.0f);< /pre>

This will remap the OpenGL coordinates to the equivalent pixel values ​​(X from 0 to windowWidth, Y from 0 to windowHeight). Note that I have flipped the Y value because the OpenGL coordinates are from the bottom left of the window Starting at the corner. So by flipping, I get a more regular (0,0) starting from the upper left corner of the window.

Leave a Comment

Your email address will not be published.