# Difference between revisions of "GluPerspective code"

(New page: GLU - the OpenGL Utility library is an additional library that contains a handful of functions for additional tasks.<br> It is traditional and can be found in a lot of tutorials and exampl...) |
$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!) |
||

(8 intermediate revisions by 4 users not shown) | |||

Line 1: | Line 1: | ||

GLU - the OpenGL Utility library is an additional library that contains a handful of functions for additional tasks.<br> | GLU - the OpenGL Utility library is an additional library that contains a handful of functions for additional tasks.<br> | ||

− | It | + | 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.<br> |

+ | 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.<br> | ||

gluPerspective's source code is very simple and can be replaced easily.<br> | gluPerspective's source code is very simple and can be replaced easily.<br> | ||

− | The code here comes from glh library (OpenGL Helper Library) | + | The code here comes from glh library (OpenGL Helper Library), LGPL license |

− | + | https://sourceforge.net/projects/glhlib<br> | |

<br> | <br> | ||

It has glhPerspectivef2 and glhPerspectived2 (a float and double version).<br> | It has glhPerspectivef2 and glhPerspectived2 (a float and double version).<br> | ||

− | Here is the float version of the code, slightly simplified :<br> | + | Here is the float version of the code, slightly simplified:<br> |

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

− | + | //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); | ||

+ | } | ||

+ | </source> | ||

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

− | + | 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; | ||

+ | } | ||

+ | </source> | ||

+ | |||

+ | [[Category:Examples]] |

## Latest revision as of 09: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;
}
```