Ignore:
Timestamp:
Sep 13, 2010, 1:24:52 AM (10 years ago)
Author:
sam
Message:

Implement Video::SetFov() to allow conic projection.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/video.cpp

    r207 r209  
    77#   include "config.h"
    88#endif
     9
     10#include <math.h>
    911
    1012#ifdef WIN32
     
    4345
    4446    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
     47}
    4548
    46     /* Projection matrix: once and for all */
     49void Video::SetFov(float theta)
     50{
    4751    glMatrixMode(GL_PROJECTION);
    4852    glLoadIdentity();
    49     glOrtho(0, width, 0, height, -(width + height), width + height);
     53
     54    float width = GetWidth();
     55    float height = GetHeight();
     56    float near = -width - height;
     57    float far = width + height;
     58
     59    /* Set the projection matrix */
     60    if (theta < 1e-4f)
     61    {
     62        /* The easy way: purely orthogonal projection. */
     63        glOrtho(0, width, 0, height, near, far);
     64    }
     65    else
     66    {
     67        /* Compute a view that approximates the glOrtho view when theta
     68         * approaches zero. This view ensures that the z=0 plane fills
     69         * the screen. */
     70        float t1 = tanf(theta / 2);
     71        float t2 = t1 * height / width;
     72        float dist = (float)width / (2.0f * t1);
     73
     74        near += dist;
     75        far += dist;
     76
     77        if (near <= 0.0f)
     78        {
     79            far -= (near - 1.0f);
     80            near = 1.0f;
     81        }
     82
     83        glFrustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
     84        glTranslatef(-0.5f * width, -0.5f * height, -dist);
     85    }
     86
     87    /* Reset the model view matrix, just in case */
     88    glMatrixMode(GL_MODELVIEW);
     89    glLoadIdentity();
    5090}
    5191
     
    5494    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    5595
    56     /* Model view matrix: for each frame, just in case */
    57     glMatrixMode(GL_MODELVIEW);
    58     glLoadIdentity();
     96    SetFov(0.0f);
    5997}
    6098
Note: See TracChangeset for help on using the changeset viewer.