X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fmatrix.cpp;h=549fe8b61578ecf94958e2650519aea7579a8e00;hb=196093790e242dec24bfbbf7ad8c28dcc442824c;hp=a4b60dc604a17fb5b95bbd7a5262628b26259408;hpb=955e7cada42e099016879332e71863e46075d72b;p=libs%2Fgl.git diff --git a/source/matrix.cpp b/source/matrix.cpp index a4b60dc6..549fe8b6 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -1,13 +1,6 @@ -/* $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; @@ -134,10 +127,29 @@ 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) - throw InvalidParameterValue("Matrix element index out of range"); + throw out_of_range("Matrix::operator[]"); return matrix[i]; } @@ -203,7 +215,7 @@ Matrix Matrix::scaling(double x, double y, double z) Matrix Matrix::ortho(double l, double r, double b, double t, double n, double f) { if(l==r || b==t || n==f) - throw InvalidParameterValue("Orthogonal projection can't have zero dimension"); + throw invalid_argument("Matrix::ortho"); Matrix result; result.matrix[0] = 2/(r-l); @@ -234,7 +246,7 @@ Matrix Matrix::ortho_topleft(double w, double h) Matrix Matrix::frustum(double l, double r, double b, double t, double n, double f) { if(l==r || b==t || n<=0 || f<=n) - throw InvalidParameterValue("Invalid frustum parameters"); + throw invalid_argument("Matrix::frustum"); Matrix result; result.matrix[0] = 2*n/(r-l); @@ -275,7 +287,7 @@ MatrixStack::MatrixStack(): matrices.push_back(Matrix()); } -const Matrix &MatrixStack::top() +const Matrix &MatrixStack::top() const { return matrices.back(); } @@ -300,7 +312,7 @@ void MatrixStack::push() void MatrixStack::pop() { if(matrices.size()==1) - throw InvalidState("Can't pop the last matrix"); + throw stack_underflow("MatrixStack::pop()"); matrices.pop_back(); update(); @@ -356,7 +368,7 @@ void matrix_mode(MatrixMode m) else if(m==PROJECTION) active_stack = &MatrixStack::projection(); else - throw InvalidParameterValue("Texture matrices are not supported"); + throw invalid_argument("matrix_mode"); } void load_identity()