From 705986ebcdd24573791aa58c7a8f2b7549c918a3 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 5 Dec 2010 18:56:03 +0000 Subject: [PATCH] Add conversion from Vector4 to Vector3 Add overloads of Matrix transformations with Vector3 parameters Add multiplication operator between Matrix and Vector4 --- source/matrix.cpp | 19 +++++++++++++++++++ source/matrix.h | 8 ++++++++ source/vector.h | 7 +++++++ 3 files changed, 34 insertions(+) diff --git a/source/matrix.cpp b/source/matrix.cpp index a4b60dc6..4c3a7ee6 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -134,6 +134,25 @@ Matrix &Matrix::operator*=(const Matrix &other) return *this; } +Vector4 Matrix::operator*(const Vector4 &vec) const +{ + if(flags==IDENTITY) + return vec; + else if(flags==TRANSLATE) + return Vector4(vec.x+vec.w*matrix[12], vec.y+vec.w*matrix[13], vec.z+vec.w*matrix[14], vec.w); + else if(flags==SCALE) + return Vector4(vec.x*matrix[0], vec.y*matrix[5], vec.z*matrix[10], vec.w); + else + { + Vector4 result; + result.x = vec.x*matrix[0]+vec.y*matrix[4]+vec.z*matrix[8]+vec.w*matrix[12]; + result.y = vec.x*matrix[1]+vec.y*matrix[5]+vec.z*matrix[9]+vec.w*matrix[13]; + result.z = vec.x*matrix[2]+vec.y*matrix[6]+vec.z*matrix[10]+vec.w*matrix[14]; + result.w = vec.x*matrix[3]+vec.y*matrix[7]+vec.z*matrix[11]+vec.w*matrix[15]; + return result; + } +} + double Matrix::operator[](unsigned i) const { if(i>=16) diff --git a/source/matrix.h b/source/matrix.h index a2194916..42e70c98 100644 --- a/source/matrix.h +++ b/source/matrix.h @@ -10,6 +10,7 @@ Distributed under the LGPL #include #include "gl.h" +#include "vector.h" namespace Msp { namespace GL { @@ -41,18 +42,25 @@ public: void multiply(const Matrix &); void translate(double, double, double); + void translate(const Vector3 &t) { translate(t.x, t.y, t.z); } void rotate(double, double, double, double); + void rotate(double a, const Vector3 &x) { rotate(a, x.x, x.y, x.z); } void rotate_deg(double, double, double, double); + void rotate_deg(double a, const Vector3 & x) { rotate_deg(a, x.x, x.y, x.z); } void scale(double); void scale(double, double, double); Matrix operator*(const Matrix &) const; Matrix &operator*=(const Matrix &); + Vector4 operator*(const Vector4 &) const; double operator[](unsigned) const; static Matrix translation(double, double, double); + static Matrix translation(const Vector3 &t) { return translation(t.x, t.y, t.z); } static Matrix rotation(double, double, double, double); + static Matrix rotation(double a, const Vector3 &x) { return rotation(a, x.x, x.y, x.z); } static Matrix rotation_deg(double, double, double, double); + static Matrix rotation_deg(double a, const Vector3 &x) { return rotation_deg(a, x.x, x.y, x.z); } static Matrix scaling(double); static Matrix scaling(double, double, double); diff --git a/source/vector.h b/source/vector.h index d1693af1..25ebac11 100644 --- a/source/vector.h +++ b/source/vector.h @@ -11,12 +11,15 @@ Distributed under the LGPL namespace Msp { namespace GL { +struct Vector4; + struct Vector3 { float x, y, z; Vector3(): x(0), y(0), z(0) { } Vector3(float x_, float y_, float z_): x(x_), y(y_), z(z_) { } + Vector3(const Vector4 &); }; struct Vector4 @@ -29,6 +32,10 @@ struct Vector4 Vector4(const Vector3 &v): x(v.x), y(v.y), z(v.z), w(1) { } }; +inline Vector3::Vector3(const Vector4 &v): + x(v.x/v.w), y(v.y/v.w), z(v.z/v.w) +{ } + } // namespace GL } // namespace Msp -- 2.45.2