updated Matrix, Transform, main Makefile

git-svn-id: svn://anubis/fart/trunk@7 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2009-01-18 16:51:51 +00:00
parent 838c1b644a
commit 997a8f12d1
5 changed files with 99 additions and 0 deletions

View File

@ -8,6 +8,7 @@ endif
all: $(TARGET) all: $(TARGET)
.PHONY: $(TARGET)
$(TARGET): $(TARGET):
make -C util make -C util
make -C shapes make -C shapes

44
util/Matrix.cc Normal file
View File

@ -0,0 +1,44 @@
#include "Matrix.h"
Matrix Matrix::identity()
{
Matrix res;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
res[i][j] = i == j ? 1.0 : 0.0;
}
}
return res;
}
Matrix operator*(const Matrix & m1, const Matrix & m2)
{
Matrix res;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
res[i][j] = m1[i][0] * m2[0][j]
+ m1[i][1] * m2[1][j]
+ m1[i][2] * m2[2][j]
+ m1[i][3] * m2[3][j];
}
}
return res;
}
Vector operator*(const Matrix & m, const Vector & v)
{
Vector res;
for (int i = 0; i < 4; i++)
{
res[i] = m[i][0] * v[0]
+ m[i][1] * v[1]
+ m[i][2] * v[2]
+ m[i][3]; /* v[3] is implicitly 1.0 */
}
return res;
}

23
util/Matrix.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef MATRIX_H
#define MATRIX_H MATRIX_H
#include "Vector.h"
class Matrix
{
public:
typedef double Matrix_row_t[4];
Matrix_row_t & operator[](int idx) { return m_matrix[idx]; }
const Matrix_row_t & operator[](int idx) const { return m_matrix[idx]; }
static Matrix identity();
protected:
double m_matrix[4][4];
};
Matrix operator*(const Matrix & m1, const Matrix & m2);
Vector operator*(const Matrix & m, const Vector & v);
#endif

12
util/Transform.cc Normal file
View File

@ -0,0 +1,12 @@
#include "Transform.h"
void Transform::push()
{
m_matrices.push(m_matrices.top());
}
void Transform::pop()
{
m_matrices.pop();
}

19
util/Transform.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef TRANSFORM_H
#define TRANSFORM_H TRANSFORM_H
#include "Matrix.h"
#include <stack>
class Transform
{
public:
void push();
void pop();
protected:
std::stack<Matrix> m_matrices;
};
#endif