# Difference between revisions of "GluPerspective code"

$glRotateBit (talk | contribs) m (fovyInDegrees must be divided by 2 which is done during conversion to radians (M_PI / 360.0)! See: http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml and also look into the original source!) |
|||

Line 10: | Line 10: | ||

<source lang="c"> | <source lang="c"> | ||

− | // | + | // Matrix will receive the calculated perspective matrix. |

− | //You would have to upload to your shader | + | // You would have to upload to your shader |

// or use glLoadMatrixf if you aren't using shaders. | // or use glLoadMatrixf if you aren't using shaders. | ||

void glhPerspectivef2(float *matrix, float fovyInDegrees, float aspectRatio, | void glhPerspectivef2(float *matrix, float fovyInDegrees, float aspectRatio, | ||

Line 19: | Line 19: | ||

float temp, temp2, temp3, temp4; | float temp, temp2, temp3, temp4; | ||

ymax = znear * tanf(fovyInDegrees * M_PI / 360.0); | ymax = znear * tanf(fovyInDegrees * M_PI / 360.0); | ||

− | //ymin = -ymax; | + | // ymin = -ymax; |

− | //xmin = -ymax * aspectRatio; | + | // xmin = -ymax * aspectRatio; |

xmax = ymax * aspectRatio; | xmax = ymax * aspectRatio; | ||

glhFrustumf2(matrix, -xmax, xmax, -ymax, ymax, znear, zfar); | glhFrustumf2(matrix, -xmax, xmax, -ymax, ymax, znear, zfar); |

## Latest revision as of 11:53, 3 January 2018

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 * 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;
}
```