]> git.tdb.fi Git - libs/gl.git/commitdiff
Use the matrix classes internally
authorMikko Rasa <tdb@tdb.fi>
Wed, 3 Nov 2010 06:59:39 +0000 (06:59 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 3 Nov 2010 06:59:39 +0000 (06:59 +0000)
Fix an error in Matrix::perspective

source/camera.cpp
source/camera.h
source/matrix.cpp
source/shadowmap.cpp

index ad1a3e946a1055c5367fc4a9c4592e171c90dfc1..e2952a19f0f38b52448e1da719f3e0dd9cbd290a 100644 (file)
@@ -8,7 +8,6 @@ Distributed under the LGPL
 #include <cmath>
 #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
index a2d2993afeedd83500e0f1a490f3f100b91a3f2f..cea9c35ef9d02ae2e91769eb6bba3d8e1159c8a6 100644 (file)
@@ -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();
index d8f018aaf4835814c4736ac389f8e7426dd6279d..a4b60dc604a17fb5b95bbd7a5262628b26259408 100644 (file)
@@ -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);
 }
 
index bedfcfd51c0f1e2aa4d1f5bd374e1c5b8a342620..c8730426e6a557b113440914bef162239bc02751 100644 (file)
@@ -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);