From: Mikko Rasa Date: Wed, 3 Nov 2010 06:59:39 +0000 (+0000) Subject: Use the matrix classes internally X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=955e7cada42e099016879332e71863e46075d72b;p=libs%2Fgl.git Use the matrix classes internally Fix an error in Matrix::perspective --- diff --git a/source/camera.cpp b/source/camera.cpp index ad1a3e94..e2952a19 100644 --- a/source/camera.cpp +++ b/source/camera.cpp @@ -8,7 +8,6 @@ Distributed under the LGPL #include #include "camera.h" #include "matrix.h" -#include "projection.h" namespace Msp { namespace GL { @@ -21,11 +20,7 @@ Camera::Camera(): position(0, 0, 0), look_dir(0, 0, -1), up_dir(0, 1, 0) -{ - for(unsigned i=0; i<16; ++i) - matrix[i] = (i%5 ? 0 : 1); - matrix[10] = -1; -} +{ } void Camera::set_field_of_view(float f) { @@ -108,14 +103,8 @@ Vector4 Camera::unproject(const Vector4 &p) const void Camera::apply() const { - float h = tan(fov/2)*2*clip_near; - - matrix_mode(PROJECTION); - load_identity(); - frustum_centered(h*aspect, h, clip_near, clip_far); - - matrix_mode(MODELVIEW); - load_matrix(matrix); + MatrixStack::projection() = Matrix::perspective(fov, aspect, clip_near, clip_far); + MatrixStack::modelview() = matrix; } void Camera::compute_matrix() @@ -124,22 +113,30 @@ void Camera::compute_matrix() float y = look_dir.z*up_dir.x-look_dir.x*up_dir.z; float z = look_dir.x*up_dir.y-look_dir.y*up_dir.x; float len = sqrt(x*x+y*y+z*z); + double mdata[16]; + + mdata[0] = x/len; + mdata[4] = y/len; + mdata[8] = z/len; + + mdata[1] = mdata[4]*look_dir.z-mdata[8]*look_dir.y; + mdata[5] = mdata[8]*look_dir.x-mdata[0]*look_dir.z; + mdata[9] = mdata[0]*look_dir.y-mdata[4]*look_dir.x; - matrix[0] = x/len; - matrix[4] = y/len; - matrix[8] = z/len; + mdata[2] = -look_dir.x; + mdata[6] = -look_dir.y; + mdata[10] = -look_dir.z; - matrix[1] = matrix[4]*look_dir.z-matrix[8]*look_dir.y; - matrix[5] = matrix[8]*look_dir.x-matrix[0]*look_dir.z; - matrix[9] = matrix[0]*look_dir.y-matrix[4]*look_dir.x; + mdata[12] = -position.x*mdata[0]-position.y*mdata[4]-position.z*mdata[8]; + mdata[13] = -position.x*mdata[1]-position.y*mdata[5]-position.z*mdata[9]; + mdata[14] = -position.x*mdata[2]-position.y*mdata[6]-position.z*mdata[10]; - matrix[2] = -look_dir.x; - matrix[6] = -look_dir.y; - matrix[10] = -look_dir.z; + mdata[3] = 0; + mdata[7] = 0; + mdata[11] = 0; + mdata[15] = 1; - matrix[12] = -position.x*matrix[0]-position.y*matrix[4]-position.z*matrix[8]; - matrix[13] = -position.x*matrix[1]-position.y*matrix[5]-position.z*matrix[9]; - matrix[14] = -position.x*matrix[2]-position.y*matrix[6]-position.z*matrix[10]; + matrix = mdata; } } // namespace GL diff --git a/source/camera.h b/source/camera.h index a2d2993a..cea9c35e 100644 --- a/source/camera.h +++ b/source/camera.h @@ -8,6 +8,7 @@ Distributed under the LGPL #ifndef MSP_GL_CAMERA_H_ #define MSP_GL_CAMERA_H_ +#include "matrix.h" #include "vector.h" namespace Msp { @@ -24,7 +25,7 @@ private: Vector3 position; Vector3 look_dir; Vector3 up_dir; - float matrix[16]; + Matrix matrix; public: Camera(); diff --git a/source/matrix.cpp b/source/matrix.cpp index d8f018aa..a4b60dc6 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -256,7 +256,7 @@ Matrix Matrix::frustum_centered(double w, double h, double n, double f) Matrix Matrix::perspective(double h, double a, double n, double f) { - double hh = tan(h)*n; + double hh = tan(h/2)*n; return frustum(-hh*a, hh*a, -hh, hh, n, f); } diff --git a/source/shadowmap.cpp b/source/shadowmap.cpp index bedfcfd5..c8730426 100644 --- a/source/shadowmap.cpp +++ b/source/shadowmap.cpp @@ -10,7 +10,6 @@ Distributed under the LGPL #include "light.h" #include "matrix.h" #include "misc.h" -#include "projection.h" #include "scene.h" #include "shadowmap.h" #include "texunit.h" @@ -98,22 +97,15 @@ void ShadowMap::prepare() matrix[15] = 1; { - matrix_mode(PROJECTION); - push_matrix(); - load_identity(); - ortho(-radius, radius, -radius, radius, -radius, radius); - matrix_mode(MODELVIEW); - push_matrix(); - load_matrix(matrix); + MatrixStack::Push push_mv(MatrixStack::modelview()); + MatrixStack::Push push_proj(MatrixStack::projection()); + + MatrixStack::projection() = Matrix::ortho(-radius, radius, -radius, radius, -radius, radius); + MatrixStack::modelview() = matrix; Bind bind_fbo(fbo, true); fbo.clear(DEPTH_BUFFER_BIT); scene.render("shadow"); - - matrix_mode(PROJECTION); - pop_matrix(); - matrix_mode(MODELVIEW); - pop_matrix(); } depth_buf.bind_to(unit);