Skip to content
Search

GlFrustum

lastmod 2024-11-17created 2024-11-145 edits

A function that creates a perspective projection.

Synopsis

void glFrustum( 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.

GlFrustum.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 (frustum), affine transformations are applied, and then the projection transformation (here a perspective transformation, from 3D to 2D) onto the clipped plane takes place. The transformation matrix of this perspective projection is as follows.

Perspective_matrix.jpeg

glOrtho(orthographic projection) vs glFrustum(perspective projection)

A point in affine space is p(x, y, z, w) with w = 1. The orthographic projection matrix keeps w = 1, whereas the perspective projection matrix does not. Because of this, orthographic projection preserves parallelism and ratios, which are characteristics of affine transformations, but perspective projection does not.

Easier version

To make glFrustum() easier to use, there is the gluPerspective function in GLU, but the perspective projection matrix it creates differs from the matrix above.

Example

GlFrustum_-_example_result.jpeg

Below are the screenshot and source code of a very simple example program using perspective projection. You can feel the 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();

    glFrustum(-1.0f, 1.0f, -1.0f, 1.0, 1.0f, 10.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();
}

See also

Favorite site

author Zer0 sitemap