GlOrtho
lastmod 2024-11-17created 2024-11-146 edits
A function that creates an orthographic projection
Synopsis
void glOrtho( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far )
The meaning of this function's arguments is explained in detail in the figure below.
GlOrtho.jpeg
In the figure above, the plane defined by (left, bottom) - (right, top) is the clipped region, and the 2D picture is drawn onto this plane. That is, the 3D polygons we have in mind are drawn in the hexahedron behind it, affine transformations are applied, and then the projection transformation (here an orthographic transformation, from 3D to 2D) onto the clipped plane takes place. The transformation matrix of this orthographic projection is as follows.
Orthographic_matrix.jpeg
glOrtho(orthographic projection) vs glFrustum(perspective projection)
See GlFrustum#glOrtho (orthographic projection) vs glFrustum (perspective projection).
Example
GlOrtho_-_example_result.jpeg
Below are the screenshot and source code of a very simple example program using orthographic projection. There is no sense of perspective.
void RenderWindow::OnSize(WPARAM wParam, LPARAM lParam)
{
GLsizei width = LOWORD(lParam);
GLsizei height = HIWORD(lParam);
if (height == 0)
height = 1;
glViewport( 0, 0, width, height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-3.0f, 3.0f, -3.0f, 3.0, 1.0f, 100.0f); //!
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
BOOL RenderWindow::InitGL(void)
{
Window::InitGL();
glEnable(GL_LIGHTING);
GLfloat lightPos[4] = { 0.5f, 0.5f, 0.5f, 1.0f };
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glEnable(GL_LIGHT0);
return TRUE;
}
void RenderWindow::RenderGLScene(void)
{
Window::RenderGLScene();
glPushMatrix();
glTranslatef(0.0f, 0.0f, -2.0f);
glRotatef(33.0f, 1.0f, 0.0f, 0.0f);
glRotatef(33.0f, 0.0f, 1.0f, 0.0f);
glColor3f(0.5f, 0.5f, 0.5f);
glutSolidTeapot(0.4f);
glPopMatrix();
glPushMatrix();
glTranslatef(1.0f, 0.0f, -4.0f);
glRotatef(33.0f, 1.0f, 0.0f, 0.0f);
glRotatef(33.0f, 0.0f, 1.0f, 0.0f);
glColor3f(0.3f, 0.3f, 0.3f);
glutSolidTeapot(0.4f);
glPopMatrix();
glPushMatrix();
glTranslatef(-1.0f, 0.0f, -6.0f);
glRotatef(33.0f, 1.0f, 0.0f, 0.0f);
glRotatef(33.0f, 0.0f, 1.0f, 0.0f);
glColor3f(0.1f, 0.1f, 0.1f);
glutSolidTeapot(0.4f);
glPopMatrix();
}