00001
00002
00003
00004 #include <math.h>
00005 #include "gl_matrix.h"
00006
00007
00008
00009 void gl::matrix4::build_frustum(const GLfloat left, const GLfloat right, const GLfloat bottom,
00010 const GLfloat top, const GLfloat near, const GLfloat far){
00011
00012
00013 GLfloat r_minus_l = right - left;
00014 GLfloat t_minus_b = top - bottom;
00015 GLfloat f_minus_n = far - near;
00016 GLfloat two_x_near = 2.0*near;
00017
00018
00019 m_data[0][0] = two_x_near/r_minus_l;
00020 m_data[1][0] = 0.0;
00021 m_data[2][0] = 0.0;
00022 m_data[3][0] = 0.0;
00023
00024
00025 m_data[0][1] = 0.0;
00026 m_data[1][1] = two_x_near/t_minus_b;
00027 m_data[2][1] = 0.0;
00028 m_data[3][1] = 0.0;
00029
00030
00031 m_data[0][2] = (right+left)/r_minus_l;
00032 m_data[1][2] = (top+bottom)/t_minus_b;
00033 m_data[2][2] = -(far+near)/f_minus_n;
00034 m_data[3][2] = -1.0;
00035
00036
00037 m_data[0][3] = 0.0;
00038 m_data[1][3] = 0.0;
00039 m_data[2][3] = (-two_x_near*far)/f_minus_n;
00040 m_data[3][3] = 0.0;
00041 }
00042
00043
00044
00045 void gl::matrix4::build_perspective(const GLfloat field_of_view, const GLfloat aspect_ratio,
00046 const GLfloat near, const GLfloat far){
00047 GLfloat x_min, x_max, y_min, y_max;
00048 y_max = near * tan(field_of_view*M_PI/360.0);
00049 y_min = -y_max;
00050 x_min = y_min*aspect_ratio;
00051 x_max = y_max*aspect_ratio;
00052 build_frustum(x_min, x_max, y_min, y_max, near, far);
00053 }
00054
00055
00056
00057 void gl::matrix4::build_look_at(const math::vector3 eye, const math::vector3 center, const math::vector3 up){
00058 math::vector3 X, Y, Z;
00059
00060
00061
00062 Z = eye - center;
00063 Z.normalize();
00064
00065
00066 X = math::cross(up, Z);
00067 X.normalize();
00068
00069
00070 Y = math::cross(Z, X);
00071 Y.normalize();
00072
00073
00074 m_data[0][0] = X.x();
00075 m_data[1][0] = Y.x();
00076 m_data[2][0] = Z.x();
00077 m_data[3][0] = 0.0;
00078
00079
00080 m_data[0][1] = X.y();
00081 m_data[1][1] = Y.y();
00082 m_data[2][1] = Z.y();
00083 m_data[3][1] = 0.0;
00084
00085
00086 m_data[0][2] = X.z();
00087 m_data[1][2] = Y.z();
00088 m_data[2][2] = Z.z();
00089 m_data[3][2] = 0.0;
00090
00091
00092 m_data[0][3] = -1.0*math::dot(X, eye);
00093 m_data[1][3] = -1.0*math::dot(Y, eye);
00094 m_data[2][3] = -1.0*math::dot(Z, eye);
00095 m_data[3][3] = 1.0;
00096 }
00097
00098
00099
00100 gl::matrix4& gl::matrix4::operator= (const math::matrix4 &m){
00101 int i, j;
00102 for (i=0; i<4; i++)
00103 for (j=0; j<4; j++)
00104 m_data[i][j] = m(i,j);
00105 return *this;
00106 }
00107
00108
00109
00110 gl::matrix4::operator const GLfloat*(void){
00111 int i,j;
00112 for (i=0; i<4; i++)
00113 for (j=0; j<4; j++)
00114 m_transposed_data[i][j] = m_data[j][i];
00115 return (const GLfloat*)(m_transposed_data);
00116 }
00117