Difference between revisions of "GluPerspective code"

From OpenGL.org
Jump to: navigation, search
(precising some points)
m (code source tag)
Line 9: Line 9:
 
Here is the float version of the code, slightly simplified:<br>
 
Here is the float version of the code, slightly simplified:<br>
  
  //matrix will receive the calculated perspective matrix.
+
<source lang="c">
  //You would have to upload to your shader or use glLoadMatrixf if you aren't using shaders.
+
//matrix will receive the calculated perspective matrix.
  void glhPerspectivef2(float *matrix, float fovyInDegrees, float aspectRatio, float znear, float zfar)
+
//You would have to upload to your shader or use glLoadMatrixf if you aren't using shaders.
  {
+
void glhPerspectivef2(float *matrix, float fovyInDegrees, float aspectRatio,
      float ymax, xmax;
+
                      float znear, float zfar)
      float temp, temp2, temp3, temp4;
+
{
      ymax = znear * tanf(fovyInDegrees * 2.0 * M_PI / 360.0);
+
    float ymax, xmax;
      //ymin = -ymax;
+
    float temp, temp2, temp3, temp4;
      //xmin = -ymax * aspectRatio;
+
    ymax = znear * tanf(fovyInDegrees * 2.0 * M_PI / 360.0);
      xmax = ymax * aspectRatio;
+
    //ymin = -ymax;
      glhFrustumf2(matrix, -xmax, xmax, -ymax, ymax, znear, zfar);
+
    //xmin = -ymax * aspectRatio;
  }
+
    xmax = ymax * aspectRatio;
 +
    glhFrustumf2(matrix, -xmax, xmax, -ymax, ymax, znear, zfar);
 +
}
 +
</source>
  
  void glhFrustumf2(float *matrix, float left, float right, float bottom, float top, float znear, float zfar)
+
<source lang="c">
  {
+
void glhFrustumf2(float *matrix, float left, float right, float bottom, float top,
      float temp, temp2, temp3, temp4;
+
                  float znear, float zfar)
      temp = 2.0 * znear;
+
{
      temp2 = right - left;
+
    float temp, temp2, temp3, temp4;
      temp3 = top - bottom;
+
    temp = 2.0 * znear;
      temp4 = zfar - znear;
+
    temp2 = right - left;
      matrix[0] = temp / temp2;
+
    temp3 = top - bottom;
      matrix[1] = 0.0;
+
    temp4 = zfar - znear;
      matrix[2] = 0.0;
+
    matrix[0] = temp / temp2;
      matrix[3] = 0.0;
+
    matrix[1] = 0.0;
      matrix[4] = 0.0;
+
    matrix[2] = 0.0;
      matrix[5] = temp / temp3;
+
    matrix[3] = 0.0;
      matrix[6] = 0.0;
+
    matrix[4] = 0.0;
      matrix[7] = 0.0;
+
    matrix[5] = temp / temp3;
      matrix[8] = (right + left) / temp2;
+
    matrix[6] = 0.0;
      matrix[9] = (top + bottom) / temp3;
+
    matrix[7] = 0.0;
      matrix[10] = (-zfar - znear) / temp4;
+
    matrix[8] = (right + left) / temp2;
      matrix[11] = -1.0;
+
    matrix[9] = (top + bottom) / temp3;
      matrix[12] = 0.0;
+
    matrix[10] = (-zfar - znear) / temp4;
      matrix[13] = 0.0;
+
    matrix[11] = -1.0;
      matrix[14] = (-temp * zfar) / temp4;
+
    matrix[12] = 0.0;
      matrix[15] = 0.0;
+
    matrix[13] = 0.0;
  }
+
    matrix[14] = (-temp * zfar) / temp4;
 +
    matrix[15] = 0.0;
 +
}
 +
</source>
  
 
[[Category:Examples]]
 
[[Category:Examples]]

Revision as of 21:14, 24 May 2010

GLU - the OpenGL Utility library is an additional library that contains a handful of functions for additional tasks.
It was traditional and can be found in a lot of tutorials and examples. But GLU uses a lot of OpenGL features that are deprecated now.
The internal projection matrix should not be used anymore, so the programmer should use a replacement for gluPerspective, that instead of updating the internal projection matrix will return this matrix.
gluPerspective's source code is very simple and can be replaced easily.
The code here comes from glh library (OpenGL Helper Library), LGPL license https://sourceforge.net/projects/glhlib

It has glhPerspectivef2 and glhPerspectived2 (a float and double version).
Here is the float version of the code, slightly simplified:

//matrix will receive the calculated perspective matrix.
//You would have to upload to your shader or use glLoadMatrixf if you aren't using shaders.
void glhPerspectivef2(float *matrix, float fovyInDegrees, float aspectRatio,
                      float znear, float zfar)
{
    float ymax, xmax;
    float temp, temp2, temp3, temp4;
    ymax = znear * tanf(fovyInDegrees * 2.0 * M_PI / 360.0);
    //ymin = -ymax;
    //xmin = -ymax * aspectRatio;
    xmax = ymax * aspectRatio;
    glhFrustumf2(matrix, -xmax, xmax, -ymax, ymax, znear, zfar);
}
void glhFrustumf2(float *matrix, float left, float right, float bottom, float top,
                  float znear, float zfar)
{
    float temp, temp2, temp3, temp4;
    temp = 2.0 * znear;
    temp2 = right - left;
    temp3 = top - bottom;
    temp4 = zfar - znear;
    matrix[0] = temp / temp2;
    matrix[1] = 0.0;
    matrix[2] = 0.0;
    matrix[3] = 0.0;
    matrix[4] = 0.0;
    matrix[5] = temp / temp3;
    matrix[6] = 0.0;
    matrix[7] = 0.0;
    matrix[8] = (right + left) / temp2;
    matrix[9] = (top + bottom) / temp3;
    matrix[10] = (-zfar - znear) / temp4;
    matrix[11] = -1.0;
    matrix[12] = 0.0;
    matrix[13] = 0.0;
    matrix[14] = (-temp * zfar) / temp4;
    matrix[15] = 0.0;
}