utils: rewrite gl_perspective() as per OpenGL FAQ 9.085.

This commit is contained in:
Gwenole Beauchesne 2012-01-13 14:03:29 +01:00
parent ba810c2ed6
commit a3c9365f15

View file

@ -205,42 +205,17 @@ gl_set_bgcolor(guint32 color)
* basically is the Mesa implementation of gluPerspective(). * basically is the Mesa implementation of gluPerspective().
*/ */
static void static void
frustum(GLdouble left, GLdouble right, gl_perspective(GLdouble fovy, GLdouble aspect, GLdouble near_val, GLdouble far_val)
GLdouble bottom, GLdouble top,
GLdouble nearval, GLdouble farval)
{ {
GLdouble x, y, a, b, c, d; GLdouble left, right, top, bottom;
GLdouble m[16];
x = (2.0 * nearval) / (right - left); /* Source (Q 9.085):
y = (2.0 * nearval) / (top - bottom); <http://www.opengl.org/resources/faq/technical/transformations.htm> */
a = (right + left) / (right - left); top = tan(fovy * M_PI / 360.0) * near_val;
b = (top + bottom) / (top - bottom); bottom = -top;
c = -(farval + nearval) / ( farval - nearval); left = aspect * bottom;
d = -(2.0 * farval * nearval) / (farval - nearval); right = aspect * top;
glFrustum(left, right, bottom, top, near_val, far_val);
#define M(row,col) m[col*4+row]
M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F;
M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F;
M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d;
M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F;
#undef M
glMultMatrixd(m);
}
static void
gl_perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
GLdouble xmin, xmax, ymin, ymax;
ymax = zNear * tan(fovy * M_PI / 360.0);
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;
/* Don't call glFrustum() because of error semantics (covglu) */
frustum(xmin, xmax, ymin, ymax, zNear, zFar);
} }
/** /**