X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fmatrix.cpp;h=311958ab99542574edfe3cda35059d0eade1b24b;hp=9af850d2dfa68956037be0e4854716813fdc79a6;hb=bec07999d95b76f4b47cffcc564d0cd0afc0435e;hpb=41339bc44d076569c680b2c24c75b30ef1254c1b diff --git a/source/matrix.cpp b/source/matrix.cpp index 9af850d2..311958ab 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -1,73 +1,119 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - +#include +#include +#include +#include "error.h" #include "matrix.h" +using namespace std; + namespace Msp { namespace GL { -void matrix_mode(MatrixMode m) +Matrix::Matrix(): + Base(Base::identity()) +{ } + +Matrix::Matrix(const float *m): + Base(m) +{ } + +Matrix::Matrix(const LinAl::Matrix &other): + Base(other) +{ } + +Matrix &Matrix::translate(const Vector3 &t) +{ + return multiply(translation(t)); +} + +Matrix &Matrix::rotate(const Angle &a, const Vector3 &x) +{ + return multiply(rotation(a, x)); +} + +Matrix &Matrix::scale(const Vector3 &s) { - glMatrixMode(m); + return multiply(scaling(s)); } -void load_identity() +float Matrix::operator[](unsigned i) const { - glLoadIdentity(); + if(i>=16) + throw out_of_range("Matrix::operator[]"); + return operator()(i%4, i/4); } -void load_matrix(const float *matrix) +Matrix Matrix::translation(const Vector3 &t) { - glLoadMatrixf(matrix); + return Geometry::AffineTransformation::translation(t).get_matrix(); } -void load_matrix(const double *matrix) +Matrix Matrix::rotation(const Angle &a, const Vector3 &x) { - glLoadMatrixd(matrix); + return Geometry::AffineTransformation::rotation(a, x).get_matrix(); } -void mult_matrix(const float *matrix) +Matrix Matrix::scaling(const Vector3 &s) { - glMultMatrixf(matrix); + return Geometry::AffineTransformation::scaling(s).get_matrix(); } -void mult_matrix(const double *matrix) +Matrix Matrix::ortho(float l, float r, float b, float t, float n, float f) { - glMultMatrixd(matrix); + if(l==r || b==t || n==f) + throw invalid_argument("Matrix::ortho"); + + Matrix result; + result(0, 0) = 2/(r-l); + result(1, 1) = 2/(t-b); + result(2, 2) = -2/(f-n); + result(0, 3) = -(r+l)/(r-l); + result(1, 3) = -(t+b)/(t-b); + result(2, 3) = -(f+n)/(f-n); + return result; } -void push_matrix() +Matrix Matrix::ortho_centered(float w, float h) { - glPushMatrix(); + return ortho(-w/2, w/2, -h/2, h/2, -1, 1); } -void pop_matrix() +Matrix Matrix::ortho_bottomleft(float w, float h) { - glPopMatrix(); + return ortho(0, w, 0, h, -1, 1); } -void translate(float x, float y, float z) +Matrix Matrix::ortho_topleft(float w, float h) { - glTranslatef(x, y, z); + return ortho(0, w, h, 0, -1, 1); } -void rotate(float a, float x, float y, float z) +Matrix Matrix::frustum(float l, float r, float b, float t, float n, float f) { - glRotatef(a, x, y, z); + if(l==r || b==t || n<=0 || f<=n) + throw invalid_argument("Matrix::frustum"); + + Matrix result; + result(0, 0) = 2*n/(r-l); + result(1, 1) = 2*n/(t-b); + result(0, 2) = (r+l)/(r-l); + result(1, 2) = (t+b)/(t-b); + result(2, 2) = -(f+n)/(f-n); + result(3, 2) = -1; + result(2, 3) = -2*f*n/(f-n); + result(3, 3) = 0; + return result; } -void scale(float x, float y, float z) +Matrix Matrix::frustum_centered(float w, float h, float n, float f) { - glScalef(x, y, z); + return frustum(-w/2, w/2, -h/2, h/2, n, f); } -void scale_uniform(float s) +Matrix Matrix::perspective(const Angle &h, float a, float n, float f) { - scale(s, s, s); + float hh = tan(h/2.0f)*n; + return frustum(-hh*a, hh*a, -hh, hh, n, f); } } // namespace GL