Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "math/glmath.h"
00024
00025 namespace Math {
00026
00027
00028 bool gluMathUnProject(Vector3d win, const Matrix4 &mvpMatrix, const Common::Rect &viewport, Vector3d &obj) {
00029 Matrix4 A;
00030 Vector4d in, out;
00031
00032 in.x() = (win.x() - viewport.left) * 2 / viewport.width() - 1.0;
00033 in.y() = (win.y() - viewport.top) * 2 / viewport.height() - 1.0;
00034 in.z() = 2 * win.z() - 1.0;
00035 in.w() = 1.0;
00036
00037 A = mvpMatrix;
00038 A.inverse();
00039
00040 out = A.transform(in);
00041 if (out.w() == 0.0)
00042 return false;
00043
00044 obj.x() = out.x() / out.w();
00045 obj.y() = out.y() / out.w();
00046 obj.z() = out.z() / out.w();
00047
00048 return true;
00049 }
00050
00051
00052 Matrix4 makePerspectiveMatrix(double fovy, double aspect, double zNear, double zFar) {
00053 double xmin, xmax, ymin, ymax;
00054
00055 ymax = zNear * tan(fovy * M_PI / 360.0);
00056 ymin = -ymax;
00057 xmin = ymin * aspect;
00058 xmax = ymax * aspect;
00059
00060 return makeFrustumMatrix(xmin, xmax, ymin, ymax, zNear, zFar);
00061 }
00062
00063 Matrix4 makeFrustumMatrix(double left, double right, double bottom, double top, double zNear, double zFar) {
00064 Matrix4 proj;
00065 proj(0, 0) = (2.0f * zNear) / (right - left);
00066 proj(1, 1) = (2.0f * zNear) / (top - bottom);
00067 proj(2, 0) = (right + left) / (right - left);
00068 proj(2, 1) = (top + bottom) / (top - bottom);
00069 proj(2, 2) = -(zFar + zNear) / (zFar - zNear);
00070 proj(2, 3) = -1.0f;
00071 proj(3, 2) = -(2.0f * zFar * zNear) / (zFar - zNear);
00072 proj(3, 3) = 0.0f;
00073
00074 return proj;
00075 }
00076
00077 Matrix4 makeLookAtMatrix(const Vector3d &eye, const Vector3d ¢er, const Vector3d &up) {
00078 Vector3d f = (center - eye).getNormalized();
00079 Vector3d u = up.getNormalized();
00080 Vector3d s = Vector3d::crossProduct(f, u).getNormalized();
00081 u = Vector3d::crossProduct(s, f);
00082
00083 Math::Matrix4 look;
00084 look(0, 0) = s.x();
00085 look(1, 0) = s.y();
00086 look(2, 0) = s.z();
00087 look(0, 1) = u.x();
00088 look(1, 1) = u.y();
00089 look(2, 1) = u.z();
00090 look(0, 2) = -f.x();
00091 look(1, 2) = -f.y();
00092 look(2, 2) = -f.z();
00093 look(3, 3) = 1.0;
00094
00095 return look;
00096 }
00097
00098 }