ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

glmath.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "math/glmath.h"
00024 
00025 namespace Math {
00026 
00027 // function based on gluUnProject from Mesa 5.0 glu GPLv2+ licensed sources
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 // function based on gluPerspective from Mesa 5.0 glu GPLv2+ licensed sources
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 &center, 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 }


Generated on Sat Apr 20 2019 05:02:44 for ResidualVM by doxygen 1.7.1
curved edge   curved edge