Ignore:
Timestamp:
Feb 16, 2011, 7:04:08 PM (10 years ago)
Author:
sam
Message:

Implement float4x4::frustum() and float4x4::perspective() to build
projection matrices.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/matrix.cpp

    r648 r652  
    6363}
    6464
     65template<> float4x4 float4x4::frustum(float left, float right, float bottom,
     66                                      float top, float near, float far)
     67{
     68    float invrl = (right != left) ? 1.0f / right - left : 0.0f;
     69    float invtb = (top != bottom) ? 1.0f / top - bottom : 0.0f;
     70    float invfn = (far != near) ? 1.0f / far - near : 0.0f;
     71
     72    float4x4 ret(0.0f);
     73    ret[0][0] = 2.0f * near * invrl;
     74    ret[1][1] = 2.0f * near * invtb;
     75    ret[2][0] = (right + left) * invrl;
     76    ret[2][1] = (top + bottom) * invtb;
     77    ret[2][2] = - (far + near) * invfn;
     78    ret[2][3] = -1.0f;
     79    ret[3][2] = -2.0f * far * near * invfn;
     80    return ret;
     81}
     82
     83template<> float4x4 float4x4::perspective(float theta, float width,
     84                                          float height, float near, float far)
     85{
     86    float t1 = tanf(theta / 2.0f);
     87    float t2 = t1 * height / width;
     88
     89    return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
     90}
     91
Note: See TracChangeset for help on using the changeset viewer.