# Difference between revisions of "GluPerspective code"

Blue Prawn (talk | contribs) m (breaking long line) |
$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 18: | Line 18: | ||

float ymax, xmax; | float ymax, xmax; | ||

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

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

//ymin = -ymax; | //ymin = -ymax; | ||

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

## Latest revision as of 08:13, 7 June 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 * 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;
}
```