]> git.tdb.fi Git - libs/gl.git/commitdiff
Convert Matrix to use floats
authorMikko Rasa <tdb@tdb.fi>
Wed, 27 Nov 2013 09:06:38 +0000 (11:06 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 27 Nov 2013 09:06:38 +0000 (11:06 +0200)
This halves matrix storage size to 64 bytes and reduces overhead from
conversions at certain places.  If an application desires greater
precision, it can use the LinAl::Matrix and Geometry::AffineTransformation
classes internally.

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

index c0b508180b0da9f8676cd12e6f5c74a4a77cede1..b1d639e7c957dbffb3ce73f43256be5e0ba52c8a 100644 (file)
@@ -59,12 +59,12 @@ Animation::AxisInterpolation::AxisInterpolation():
        scale(0)
 { }
 
        scale(0)
 { }
 
-Animation::AxisInterpolation::AxisInterpolation(const double *axis1, const double *axis2)
+Animation::AxisInterpolation::AxisInterpolation(const float *axis1, const float *axis2)
 {
        // Compute a normalized vector halfway between the two endpoints
 {
        // Compute a normalized vector halfway between the two endpoints
-       double half[3];
-       double a1_len = 0;
-       double h_len = 0;
+       float half[3];
+       float a1_len = 0;
+       float h_len = 0;
        for(unsigned i=0; i<3; ++i)
        {
                half[i] = (axis1[i]+axis2[i])/2;
        for(unsigned i=0; i<3; ++i)
        {
                half[i] = (axis1[i]+axis2[i])/2;
@@ -73,8 +73,8 @@ Animation::AxisInterpolation::AxisInterpolation(const double *axis1, const doubl
        }
 
        // Compute correction factors for smooth interpolation
        }
 
        // Compute correction factors for smooth interpolation
-       double cos_half = (axis1[0]*half[0]+axis1[1]*half[1]+axis1[2]*half[2])/sqrt(a1_len*h_len);
-       double angle = acos(cos_half);
+       float cos_half = (axis1[0]*half[0]+axis1[1]*half[1]+axis1[2]*half[2])/sqrt(a1_len*h_len);
+       float angle = acos(cos_half);
        slope = (angle ? angle/tan(angle) : 1);
        scale = cos_half;
 }
        slope = (angle ? angle/tan(angle) : 1);
        scale = cos_half;
 }
@@ -89,8 +89,8 @@ Animation::MatrixInterpolation::MatrixInterpolation(const Matrix &m1, const Matr
        matrix1(&m1),
        matrix2(&m2)
 {
        matrix1(&m1),
        matrix2(&m2)
 {
-       const double *m1_data = matrix1->data();
-       const double *m2_data = matrix2->data();
+       const float *m1_data = matrix1->data();
+       const float *m2_data = matrix2->data();
        for(unsigned i=0; i<3; ++i)
                axes[i] = AxisInterpolation(m1_data+i*4, m2_data+i*4);
 }
        for(unsigned i=0; i<3; ++i)
                axes[i] = AxisInterpolation(m1_data+i*4, m2_data+i*4);
 }
@@ -99,12 +99,12 @@ Matrix Animation::MatrixInterpolation::get(float t) const
 {
        float u = t*2.0f-1.0f;
 
 {
        float u = t*2.0f-1.0f;
 
-       double matrix[16];
+       float matrix[16];
        for(unsigned i=0; i<4; ++i)
        {
        for(unsigned i=0; i<4; ++i)
        {
-               const double *m1_col = matrix1->data()+i*4;
-               const double *m2_col = matrix2->data()+i*4;
-               double *out_col = matrix+i*4;
+               const float *m1_col = matrix1->data()+i*4;
+               const float *m2_col = matrix2->data()+i*4;
+               float *out_col = matrix+i*4;
 
                if(i<3)
                {
 
                if(i<3)
                {
index 58a76583392270e5f1c0dd7a348d0fe10cff23d7..63b13f7224269443e9b562572be9447d263d6faf 100644 (file)
@@ -43,7 +43,7 @@ private:
                float scale;
 
                AxisInterpolation();
                float scale;
 
                AxisInterpolation();
-               AxisInterpolation(const double *, const double *);
+               AxisInterpolation(const float *, const float *);
        };
 
        struct MatrixInterpolation
        };
 
        struct MatrixInterpolation
index df4119a9e20b663503f6a14858a4633ca064a1e9..0202bedf68ff9e2bdd166f9fc57102da6450e8e5 100644 (file)
@@ -79,7 +79,7 @@ Vector3 Camera::project(const Vector3 &p) const
 
 Vector4 Camera::unproject(const Vector4 &p) const
 {
 
 Vector4 Camera::unproject(const Vector4 &p) const
 {
-       Vector4 r = invert(proj_matrix)*LinAl::Vector<double, 4>(p.x, p.y, p.z, 1.0f);
+       Vector4 r = invert(proj_matrix)*Vector4(p.x, p.y, p.z, 1.0f);
        r = object_matrix*Vector4(r.x, r.y, r.z, p.w);
        return r;
 }
        r = object_matrix*Vector4(r.x, r.y, r.z, p.w);
        return r;
 }
@@ -104,11 +104,11 @@ void Camera::update_projection_matrix()
 void Camera::update_object_matrix()
 {
        Vector3 right_dir = normalize(cross(look_dir, up_dir));
 void Camera::update_object_matrix()
 {
        Vector3 right_dir = normalize(cross(look_dir, up_dir));
-       LinAl::Vector<double, 4> columns[4];
-       columns[0] = LinAl::Vector<double, 4>(right_dir, 0.0f);
-       columns[1] = LinAl::Vector<double, 4>(cross(right_dir, look_dir), 0.0f);
-       columns[2] = LinAl::Vector<double, 4>(-look_dir, 0.0f);
-       columns[3] = LinAl::Vector<double, 4>(position, 1.0f);
+       Vector4 columns[4];
+       columns[0] = Vector4(right_dir, 0.0f);
+       columns[1] = Vector4(cross(right_dir, look_dir), 0.0f);
+       columns[2] = Vector4(-look_dir, 0.0f);
+       columns[3] = Vector4(position, 1.0f);
        object_matrix = Matrix::from_columns(columns);
        view_matrix = invert(object_matrix);
 }
        object_matrix = Matrix::from_columns(columns);
        view_matrix = invert(object_matrix);
 }
index cac4a4fcd99afe6d2e110c80030aad39dd9352c1..4bf550aa090ffc44c2734992cd064e578ebb460a 100644 (file)
@@ -14,14 +14,10 @@ Matrix::Matrix():
 { }
 
 Matrix::Matrix(const float *m):
 { }
 
 Matrix::Matrix(const float *m):
-       Base(LinAl::SquareMatrix<float, 4>(m))
-{ }
-
-Matrix::Matrix(const double *m):
        Base(m)
 { }
 
        Base(m)
 { }
 
-Matrix::Matrix(const LinAl::Matrix<double, 4, 4> &other):
+Matrix::Matrix(const LinAl::Matrix<float, 4, 4> &other):
        Base(other)
 { }
 
        Base(other)
 { }
 
@@ -58,7 +54,7 @@ Matrix &Matrix::operator*=(const Matrix &other)
 
 Vector4 Matrix::operator*(const Vector4 &vec) const
 {
 
 Vector4 Matrix::operator*(const Vector4 &vec) const
 {
-       return static_cast<const Base &>(*this)*LinAl::Vector<double, 4>(vec);
+       return static_cast<const Base &>(*this)*LinAl::Vector<float, 4>(vec);
 }
 
 Vector3 Matrix::operator*(const Vector3 &vec) const
 }
 
 Vector3 Matrix::operator*(const Vector3 &vec) const
@@ -66,7 +62,7 @@ Vector3 Matrix::operator*(const Vector3 &vec) const
        return Vector3((*this)*Vector4(vec, 1.0f));
 }
 
        return Vector3((*this)*Vector4(vec, 1.0f));
 }
 
-double Matrix::operator[](unsigned i) const
+float Matrix::operator[](unsigned i) const
 {
        if(i>=16)
                throw out_of_range("Matrix::operator[]");
 {
        if(i>=16)
                throw out_of_range("Matrix::operator[]");
@@ -75,20 +71,20 @@ double Matrix::operator[](unsigned i) const
 
 Matrix Matrix::translation(const Vector3 &t)
 {
 
 Matrix Matrix::translation(const Vector3 &t)
 {
-       return Geometry::AffineTransformation<double, 3>::translation(t).get_matrix();
+       return Geometry::AffineTransformation<float, 3>::translation(t).get_matrix();
 }
 
 Matrix Matrix::rotation(const Angle &a, const Vector3 &x)
 {
 }
 
 Matrix Matrix::rotation(const Angle &a, const Vector3 &x)
 {
-       return Geometry::AffineTransformation<double, 3>::rotation(a, x).get_matrix();
+       return Geometry::AffineTransformation<float, 3>::rotation(a, x).get_matrix();
 }
 
 Matrix Matrix::scaling(const Vector3 &s)
 {
 }
 
 Matrix Matrix::scaling(const Vector3 &s)
 {
-       return Geometry::AffineTransformation<double, 3>::scaling(s).get_matrix();
+       return Geometry::AffineTransformation<float, 3>::scaling(s).get_matrix();
 }
 
 }
 
-Matrix Matrix::ortho(double l, double r, double b, double t, double n, double f)
+Matrix Matrix::ortho(float l, float r, float b, float t, float n, float f)
 {
        if(l==r || b==t || n==f)
                throw invalid_argument("Matrix::ortho");
 {
        if(l==r || b==t || n==f)
                throw invalid_argument("Matrix::ortho");
@@ -103,22 +99,22 @@ Matrix Matrix::ortho(double l, double r, double b, double t, double n, double f)
        return result;
 }
 
        return result;
 }
 
-Matrix Matrix::ortho_centered(double w, double h)
+Matrix Matrix::ortho_centered(float w, float h)
 {
        return ortho(-w/2, w/2, -h/2, h/2, -1, 1);
 }
 
 {
        return ortho(-w/2, w/2, -h/2, h/2, -1, 1);
 }
 
-Matrix Matrix::ortho_bottomleft(double w, double h)
+Matrix Matrix::ortho_bottomleft(float w, float h)
 {
        return ortho(0, w, 0, h, -1, 1);
 }
 
 {
        return ortho(0, w, 0, h, -1, 1);
 }
 
-Matrix Matrix::ortho_topleft(double w, double h)
+Matrix Matrix::ortho_topleft(float w, float h)
 {
        return ortho(0, w, h, 0, -1, 1);
 }
 
 {
        return ortho(0, w, h, 0, -1, 1);
 }
 
-Matrix Matrix::frustum(double l, double r, double b, double t, double n, double f)
+Matrix Matrix::frustum(float l, float r, float b, float t, float n, float f)
 {
        if(l==r || b==t || n<=0 || f<=n)
                throw invalid_argument("Matrix::frustum");
 {
        if(l==r || b==t || n<=0 || f<=n)
                throw invalid_argument("Matrix::frustum");
@@ -131,18 +127,18 @@ Matrix Matrix::frustum(double l, double r, double b, double t, double n, double
        result(2, 2) = -(f+n)/(f-n);
        result(3, 2) = -1;
        result(2, 3) = -2*f*n/(f-n);
        result(2, 2) = -(f+n)/(f-n);
        result(3, 2) = -1;
        result(2, 3) = -2*f*n/(f-n);
-       result(3 ,3) = 0;
+       result(33) = 0;
        return result;
 }
 
        return result;
 }
 
-Matrix Matrix::frustum_centered(double w, double h, double n, double f)
+Matrix Matrix::frustum_centered(float w, float h, float n, float f)
 {
        return frustum(-w/2, w/2, -h/2, h/2, n, f);
 }
 
 {
        return frustum(-w/2, w/2, -h/2, h/2, n, f);
 }
 
-Matrix Matrix::perspective(const Angle &h, double a, double n, double f)
+Matrix Matrix::perspective(const Angle &h, float a, float n, float f)
 {
 {
-       double hh = tan(h/2.0)*n;
+       float hh = tan(h/2.0f)*n;
        return frustum(-hh*a, hh*a, -hh, hh, n, f);
 }
 
        return frustum(-hh*a, hh*a, -hh, hh, n, f);
 }
 
@@ -205,7 +201,7 @@ void MatrixStack::update()
                current_mode = mode;
        }
 
                current_mode = mode;
        }
 
-       glLoadMatrixd(matrices.back().data());
+       glLoadMatrixf(matrices.back().data());
 }
 
 MatrixStack &MatrixStack::operator=(const Matrix &m)
 }
 
 MatrixStack &MatrixStack::operator=(const Matrix &m)
index ce21b46f1192aab039ddbdd53b391a3e4e21b43c..2dc3b972721ef1eeb417f4d3b2be6499bb72ca45 100644 (file)
 namespace Msp {
 namespace GL {
 
 namespace Msp {
 namespace GL {
 
-class Matrix: public LinAl::SquareMatrix<double, 4>
+class Matrix: public LinAl::SquareMatrix<float, 4>
 {
 private:
 {
 private:
-       typedef LinAl::SquareMatrix<double, 4> Base;
-       typedef Geometry::Angle<double> Angle;
+       typedef LinAl::SquareMatrix<float, 4> Base;
+       typedef Geometry::Angle<float> Angle;
 
 public:
        Matrix();
        Matrix(const float *);
 
 public:
        Matrix();
        Matrix(const float *);
-       Matrix(const double *);
-       Matrix(const LinAl::Matrix<double, 4, 4> &);
+       Matrix(const LinAl::Matrix<float, 4, 4> &);
 
 
-       const double *data() const { return &Base::operator()(0, 0); }
+       const float *data() const { return &Base::operator()(0, 0); }
 
        void multiply(const Matrix &);
 
        void multiply(const Matrix &);
-       void translate(double x, double y, double z) { translate(Vector3(x, y, z)); }
+       void translate(float x, float y, float z) { translate(Vector3(x, y, z)); }
        void translate(const Vector3 &);
        void translate(const Vector3 &);
-       void rotate(const Angle &a, double x, double y, double z) { rotate(a, Vector3(x, y, z)); }
+       void rotate(const Angle &a, float x, float y, float z) { rotate(a, Vector3(x, y, z)); }
        void rotate(const Angle &, const Vector3 &);
        void rotate(const Angle &, const Vector3 &);
-       void rotate(double a, double x, double y, double z) { rotate(Angle::from_radians(a), Vector3(x, y, z)); }
-       void rotate(double a, const Vector3 &x) { rotate(Angle::from_radians(a), x); }
-       void rotate_deg(double a, double x, double y, double z) { rotate(Angle::from_degrees(a), Vector3(x, y, z)); }
-       void rotate_deg(double a, const Vector3 & x) { rotate(Angle::from_degrees(a), x); }
-       void scale(double s) { scale(Vector3(s, s, s)); }
-       void scale(double x, double y, double z) { scale(Vector3(x, y, z)); }
+       void rotate(float a, float x, float y, float z) { rotate(Angle::from_radians(a), Vector3(x, y, z)); }
+       void rotate(float a, const Vector3 &x) { rotate(Angle::from_radians(a), x); }
+       void rotate_deg(float a, float x, float y, float z) { rotate(Angle::from_degrees(a), Vector3(x, y, z)); }
+       void rotate_deg(float a, const Vector3 & x) { rotate(Angle::from_degrees(a), x); }
+       void scale(float s) { scale(Vector3(s, s, s)); }
+       void scale(float x, float y, float z) { scale(Vector3(x, y, z)); }
        void scale(const Vector3 &);
 
        Matrix operator*(const Matrix &) const;
        Matrix &operator*=(const Matrix &);
        Vector4 operator*(const Vector4 &) const;
        Vector3 operator*(const Vector3 &) const;
        void scale(const Vector3 &);
 
        Matrix operator*(const Matrix &) const;
        Matrix &operator*=(const Matrix &);
        Vector4 operator*(const Vector4 &) const;
        Vector3 operator*(const Vector3 &) const;
-       double operator[](unsigned) const;
+       float operator[](unsigned) const;
 
 
-       static Matrix translation(double x, double y, double z) { return translation(Vector3(x, y, z)); }
+       static Matrix translation(float x, float y, float z) { return translation(Vector3(x, y, z)); }
        static Matrix translation(const Vector3 &);
        static Matrix translation(const Vector3 &);
-       static Matrix rotation(const Angle &a, double x, double y, double z) { return rotation(a, Vector3(x, y, z)); }
+       static Matrix rotation(const Angle &a, float x, float y, float z) { return rotation(a, Vector3(x, y, z)); }
        static Matrix rotation(const Angle &, const Vector3 &);
        static Matrix rotation(const Angle &, const Vector3 &);
-       static Matrix rotation(double a, double x, double y, double z) { return rotation(Angle::from_radians(a), Vector3(x, y, z)); }
-       static Matrix rotation(double a, const Vector3 &x) { return rotation(Angle::from_radians(a), x); }
-       static Matrix rotation_deg(double a, double x, double y, double z) { return rotation(Angle::from_degrees(a), Vector3(x, y, z)); }
-       static Matrix rotation_deg(double a, const Vector3 &x) { return rotation(Angle::from_degrees(a), x); }
-       static Matrix scaling(double s) { return scaling(Vector3(s, s, s)); }
-       static Matrix scaling(double x, double y, double z) { return scaling(Vector3(x, y, z)); }
+       static Matrix rotation(float a, float x, float y, float z) { return rotation(Angle::from_radians(a), Vector3(x, y, z)); }
+       static Matrix rotation(float a, const Vector3 &x) { return rotation(Angle::from_radians(a), x); }
+       static Matrix rotation_deg(float a, float x, float y, float z) { return rotation(Angle::from_degrees(a), Vector3(x, y, z)); }
+       static Matrix rotation_deg(float a, const Vector3 &x) { return rotation(Angle::from_degrees(a), x); }
+       static Matrix scaling(float s) { return scaling(Vector3(s, s, s)); }
+       static Matrix scaling(float x, float y, float z) { return scaling(Vector3(x, y, z)); }
        static Matrix scaling(const Vector3 &);
 
        static Matrix scaling(const Vector3 &);
 
-       static Matrix ortho(double, double, double, double, double, double);
-       static Matrix ortho_centered(double, double);
-       static Matrix ortho_bottomleft(double, double);
-       static Matrix ortho_topleft(double, double);
-       static Matrix frustum(double, double, double, double, double, double);
-       static Matrix frustum_centered(double, double, double, double);
-       static Matrix perspective(const Angle &, double, double, double);
+       static Matrix ortho(float, float, float, float, float, float);
+       static Matrix ortho_centered(float, float);
+       static Matrix ortho_bottomleft(float, float);
+       static Matrix ortho_topleft(float, float);
+       static Matrix frustum(float, float, float, float, float, float);
+       static Matrix frustum_centered(float, float, float, float);
+       static Matrix perspective(const Angle &, float, float, float);
 };
 
 class MatrixStack
 };
 
 class MatrixStack
index 943dcfaf66a3f12381b56b5c087dde1f967f0941..1287914005055ff91bb2102260daf4b28d50e710 100644 (file)
@@ -149,9 +149,7 @@ void ProgramData::uniform_matrix3(const string &name, const float *v)
 
 void ProgramData::uniform(const string &name, const Matrix &m)
 {
 
 void ProgramData::uniform(const string &name, const Matrix &m)
 {
-       float v[16];
-       copy(m.data(), m.data()+16, v);
-       uniform_matrix4(name, v);
+       uniform_matrix4(name, m.data());
 }
 
 void ProgramData::uniform_matrix4(const string &name, const float *v)
 }
 
 void ProgramData::uniform_matrix4(const string &name, const float *v)
index d380257d83fc6910b4bab5a181b765ebfd41ebc4..91c056fe147cc4c994100b5a1aa1e2ec8534c730 100644 (file)
@@ -144,7 +144,7 @@ void ShadowMap::render(Renderer &renderer, const Tag &tag) const
        if(!enabled_passes.count(tag))
                return renderer.render(renderable, tag);
 
        if(!enabled_passes.count(tag))
                return renderer.render(renderable, tag);
 
-       const double *matrix = light_matrix.data();
+       const float *matrix = light_matrix.data();
 
        // Has side effect of changing the current unit
        depth_buf.bind_to(unit);
 
        // Has side effect of changing the current unit
        depth_buf.bind_to(unit);