diff options
author | Ian Romanick <ian.d.romanick@intel.com> | 2009-10-25 22:30:22 -0700 |
---|---|---|
committer | Ian Romanick <ian.d.romanick@intel.com> | 2009-10-25 22:30:22 -0700 |
commit | a44406cdfb1568b497ddf2176660f4fadf792085 (patch) | |
tree | 24eac3cae2439bba62c2423c3a384a99a60538bb | |
parent | 0a375b908418ea64bd7a817413b4b14d74fca6ab (diff) |
Add several functions to calculate projection matrixes.
This commit adds: gluFrustum6f, gluOrtho4f, and gluOrtho6f. These
functions match glFrustum, gluOrtho2d (from classic GLU), and glOrtho.
-rw-r--r-- | include/glu3.h | 102 | ||||
-rw-r--r-- | src/matrix.c | 52 |
2 files changed, 154 insertions, 0 deletions
diff --git a/include/glu3.h b/include/glu3.h index 43fcd8b..1c31c15 100644 --- a/include/glu3.h +++ b/include/glu3.h @@ -436,6 +436,44 @@ void gluLookAt4v(GLUmat4 *result, const GLUvec4 *eye, const GLUvec4 *center, const GLUvec4 *up); /** + * \name Projection matrix + * + * Functions that generate various common projection matrixes. + */ +/*@{*/ +/** + * Generate a perspective projection matrix + * + * \param result Location to store the calculated matrix + * \param left Coordinate for the left clipping plane + * \param right Coordinate for the right clipping plane + * \param top Coordinate for the top clipping plane + * \param bottom Coordinate for the bottom clipping plane + * \param near Distance to the near plane + * \param far Distance to the far plane + * + * The matrix calculated is: + * + * \f{eqnarray*}{ + * M &=& + * \left( \begin{tabular}{cccc} + * ${2 * near} \over {right - left}$ & $0$ & $ {{right + left} \over {right - left}}$ & $0$\\ + * $0$ & ${2 * near} \over {top - bottom}$ & $ {{top + bottom} \over {top - bottom}}$ & $0$ \\ + * $0$ & $0$ & $-{{far + near} \over {far - near}}$ & $-{{2 * far * near} \over {far - near}}$ \\ + * $0$ & $0$ & $-1$ & $0$ \\ + * \end{tabular} \right) \\ + * \f} + * + * If \c left = \c right, \c top = \c bottom, or \c near = \c far, the function + * returns without writing any value to \c result. + * + * If either\c near or \c far are negative, the function returns without + * writing any value to \c result. + */ +void gluFrustum6f(GLUmat4 *result, GLfloat left, GLfloat right, GLfloat top, + GLfloat bottom, GLfloat near, GLfloat far); + +/** * Calculate a perspective projection matrix * * \param result Storage for the resulting matrix. @@ -463,6 +501,70 @@ void gluPerspective4f(GLUmat4 *result, GLfloat fovy, GLfloat aspect, GLfloat near, GLfloat far); /** + * Generate an orthographic projection matrix + * + * \param result Location to store the calculated matrix + * \param left Coordinate for the left clipping plane + * \param right Coordinate for the right clipping plane + * \param top Coordinate for the top clipping plane + * \param bottom Coordinate for the bottom clipping plane + * + * The matrix calculated is: + * + * \f{eqnarray*}{ + * M &=& + * \left( \begin{tabular}{cccc} + * $2 \over {right - left}$ & $0$ & $0$ & $-{{right + left} \over {right - left}}$ \\ + * $0$ & $2 \over {top - bottom}$ & $0$ & $-{{top + bottom} \over {top - bottom}}$ \\ + * $0$ & $0$ & $-1$ & $0$ \\ + * $0$ & $0$ & $0$ & $1$ \\ + * \end{tabular} \right) \\ + * \f} + * + * If \c left = \c right or \c top = \c bottom the function returns without + * writing any value to \c result. + * + * This function is identical to calling \c gluOrtho6f with \c near = -1 and + * \c far = 1. + * + * \sa gluOrtho6f + */ +void gluOrtho4f(GLUmat4 *result, GLfloat left, GLfloat right, GLfloat top, + GLfloat bottom); + +/** + * Generate an orthographic projection matrix + * + * \param result Location to store the calculated matrix + * \param left Coordinate for the left clipping plane + * \param right Coordinate for the right clipping plane + * \param top Coordinate for the top clipping plane + * \param bottom Coordinate for the bottom clipping plane + * \param near Distance to the near plane + * \param far Distance to the far plane + * + * The matrix calculated is: + * + * \f{eqnarray*}{ + * M &=& + * \left( \begin{tabular}{cccc} + * $2 \over {right - left}$ & $0$ & $0$ & $-{{right + left} \over {right - left}}$ \\ + * $0$ & $2 \over {top - bottom}$ & $0$ & $-{{top + bottom} \over {top - bottom}}$ \\ + * $0$ & $0$ & $-2 \over {far - near}$ & $-{{far + near} \over {far - near}}$ \\ + * $0$ & $0$ & $0$ & $1$ \\ + * \end{tabular} \right) \\ + * \f} + * + * If \c left = \c right, \c top = \c bottom, or \c near = \c far, the function + * returns without writing any value to \c result. + * + * \s gluOrtho4f + */ +void gluOrtho6f(GLUmat4 *result, GLfloat left, GLfloat right, GLfloat top, + GLfloat bottom, GLfloat near, GLfloat far); +/*@}*/ + +/** * Calculate the transpose of a matrix. */ void gluTranspose4m(GLUmat4 *result, const GLUmat4 *m); diff --git a/src/matrix.c b/src/matrix.c index 280d315..9fb356d 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -156,6 +156,31 @@ void gluRotate4v(GLUmat4 *result, const GLUvec4 *_axis, GLfloat angle) void +gluFrustum6f(GLUmat4 *result, + GLfloat left, GLfloat right, GLfloat top, GLfloat bottom, + GLfloat near, GLfloat far) +{ + if ((right == left) || (top == bottom) || (near == far) + || (near < 0.0) || (far < 0.0)) + return; + + + memcpy(result, &gluIdentityMatrix, sizeof(gluIdentityMatrix)); + + result->col[0].values[0] = (2.0 * near) / (right - left); + result->col[1].values[1] = (2.0 * near) / (top - bottom); + + result->col[2].values[0] = (right + left) / (right - left); + result->col[2].values[1] = (top + bottom) / (top - bottom); + result->col[2].values[2] = -(far + near) / (far - near); + result->col[2].values[3] = -1.0; + + result->col[3].values[2] = -(2.0 * far * near) / (far - near); + result->col[3].values[3] = 0.0; +} + + +void gluPerspective4f(GLUmat4 *result, GLfloat fovy, GLfloat aspect, GLfloat near, GLfloat far) { @@ -177,3 +202,30 @@ gluPerspective4f(GLUmat4 *result, result->col[3].values[2] = -2.0 * near * far / dz; result->col[3].values[3] = 0.0; } + + +void +gluOrtho6f(GLUmat4 *result, + GLfloat left, GLfloat right, GLfloat top, GLfloat bottom, + GLfloat near, GLfloat far) +{ + if ((right == left) || (top == bottom) || (near == far)) + return; + + (void) memcpy(result, & gluIdentityMatrix, sizeof(*result)); + result->col[0].values[0] = 2.0 / (right - left); + result->col[1].values[1] = 2.0 / (top - bottom); + result->col[2].values[2] = -2.0 / (far - near); + + result->col[3].values[0] = -(right + left) / (right - left); + result->col[3].values[1] = -(top + bottom) / (top - bottom); + result->col[3].values[2] = -(far + near) / (far - near); +} + + +void +gluOrtho4f(GLUmat4 *result, GLfloat left, GLfloat right, GLfloat top, + GLfloat bottom) +{ + gluOrtho6f(result, left, right, top, bottom, -1.0, 1.0); +} |