-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
#ifndef MSP_GL_MATRIX_H_
#define MSP_GL_MATRIX_H_
+#include <vector>
+#include <msp/geometry/angle.h>
+#include <msp/linal/squarematrix.h>
#include "gl.h"
+#include "vector.h"
namespace Msp {
namespace GL {
-enum MatrixMode
+class Matrix: public LinAl::SquareMatrix<float, 4>
{
- MODELVIEW=GL_MODELVIEW,
- PROJECTION=GL_PROJECTION,
- TEXTURE=GL_TEXTURE
+private:
+ typedef LinAl::SquareMatrix<float, 4> Base;
+ typedef Geometry::Angle<float> Angle;
+
+public:
+ Matrix();
+ Matrix(const float *);
+ Matrix(const LinAl::Matrix<float, 4, 4> &);
+
+ const float *data() const { return &Base::operator()(0, 0); }
+
+ Matrix &multiply(const Matrix &m) { return operator*=(m); }
+ Matrix &translate(float x, float y, float z) { return translate(Vector3(x, y, z)); }
+ Matrix &translate(const Vector3 &);
+ Matrix &rotate(const Angle &a, float x, float y, float z) { return rotate(a, Vector3(x, y, z)); }
+ Matrix &rotate(const Angle &, const Vector3 &);
+ Matrix &rotate(float a, float x, float y, float z) { return rotate(Angle::from_radians(a), Vector3(x, y, z)); }
+ Matrix &rotate(float a, const Vector3 &x) { return rotate(Angle::from_radians(a), x); }
+ Matrix &rotate_deg(float a, float x, float y, float z) { return rotate(Angle::from_degrees(a), Vector3(x, y, z)); }
+ Matrix &rotate_deg(float a, const Vector3 & x) { return rotate(Angle::from_degrees(a), x); }
+ Matrix &scale(float s) { return scale(Vector3(s, s, s)); }
+ Matrix &scale(float x, float y, float z) { return scale(Vector3(x, y, z)); }
+ Matrix &scale(const Vector3 &);
+
+ Matrix operator*(const Matrix &m) const { return static_cast<const Base &>(*this)*static_cast<const Base &>(m); }
+ Matrix &operator*=(const Matrix &m) { Base::operator*=(m); return *this; }
+ Vector4 operator*(const Vector4 &v) const { return static_cast<const Base &>(*this)*v; }
+ Vector3 operator*(const Vector3 &v) const { return ((*this)*compose(v, 1.0f)).slice<3>(0); }
+ float operator[](unsigned) const;
+
+ static Matrix translation(float x, float y, float z) { return translation(Vector3(x, y, z)); }
+ static Matrix translation(const Vector3 &);
+ 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(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 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);
};
-void matrix_mode(MatrixMode);
-void load_identity();
-void load_matrix(const float *);
-void load_matrix(const double *);
-void mult_matrix(const float *);
-void mult_matrix(const double *);
-void push_matrix();
-void pop_matrix();
-
-/// RAII object - pushes matrix when constructed and pops when destroyed
-struct PushMatrix
+class MatrixStack
{
- PushMatrix() { push_matrix(); }
- ~PushMatrix() { pop_matrix(); }
-};
+public:
+ class Push
+ {
+ private:
+ MatrixStack &stack;
+
+ public:
+ Push(MatrixStack &s): stack(s) { stack.push(); }
+ ~Push() { stack.pop(); }
+ };
+
+private:
+ GLenum mode;
+ std::vector<Matrix> matrices;
+
+ static GLenum current_mode;
-void translate(float, float, float);
-void rotate(float, float, float, float);
-void scale(float, float, float);
-void scale_uniform(float);
+ MatrixStack(const MatrixStack &);
+ MatrixStack &operator=(const MatrixStack &);
+ MatrixStack(GLenum);
+public:
+ MatrixStack();
+
+ const Matrix &top() const;
+ void load(const Matrix &);
+ void multiply(const Matrix &);
+ void push();
+ void pop();
+private:
+ virtual void update();
+
+public:
+ MatrixStack &operator=(const Matrix &);
+ MatrixStack &operator*=(const Matrix &);
+
+ static MatrixStack &modelview();
+ static MatrixStack &projection();
+};
} // namespace GL
} // namespace Msp