moved some Matrix and Transform functionality into header files
git-svn-id: svn://anubis/fart/trunk@310 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
parent
9fd9c5774e
commit
ad6ef8f315
@ -206,49 +206,6 @@ Matrix & Matrix::operator*=(const Matrix & other)
|
|||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* transform a point */
|
|
||||||
Vector operator*(const Matrix & m, const Vector & v)
|
|
||||||
{
|
|
||||||
Vector res;
|
|
||||||
for (int i = 0; i < 3; 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* transform a direction */
|
|
||||||
Vector operator%(const Matrix & m, const Vector & v)
|
|
||||||
{
|
|
||||||
Vector res;
|
|
||||||
for (int i = 0; i < 3; i++)
|
|
||||||
{
|
|
||||||
res[i] = m[i][0] * v[0]
|
|
||||||
+ m[i][1] * v[1]
|
|
||||||
+ m[i][2] * v[2];
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const Matrix & m1, const Matrix & m2)
|
bool operator==(const Matrix & m1, const Matrix & m2)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
|
@ -17,6 +17,49 @@ class Matrix
|
|||||||
Matrix getInverse();
|
Matrix getInverse();
|
||||||
Matrix & operator*=(const Matrix & other);
|
Matrix & operator*=(const Matrix & other);
|
||||||
|
|
||||||
|
Matrix operator*(const Matrix & m2) const
|
||||||
|
{
|
||||||
|
Matrix res;
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < 4; j++)
|
||||||
|
{
|
||||||
|
res[i][j] = m_matrix[i][0] * m2.m_matrix[0][j]
|
||||||
|
+ m_matrix[i][1] * m2.m_matrix[1][j]
|
||||||
|
+ m_matrix[i][2] * m2.m_matrix[2][j]
|
||||||
|
+ m_matrix[i][3] * m2.m_matrix[3][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* transform a point */
|
||||||
|
Vector operator*(const Vector & v)
|
||||||
|
{
|
||||||
|
Vector res;
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
res[i] = m_matrix[i][0] * v[0]
|
||||||
|
+ m_matrix[i][1] * v[1]
|
||||||
|
+ m_matrix[i][2] * v[2]
|
||||||
|
+ m_matrix[i][3]; /* v[3] is implicitly 1.0 */
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* transform a direction */
|
||||||
|
Vector operator%(const Vector & v)
|
||||||
|
{
|
||||||
|
Vector res;
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
res[i] = m_matrix[i][0] * v[0]
|
||||||
|
+ m_matrix[i][1] * v[1]
|
||||||
|
+ m_matrix[i][2] * v[2];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
double m_matrix[4][4];
|
double m_matrix[4][4];
|
||||||
double m_inverse[4][4];
|
double m_inverse[4][4];
|
||||||
@ -25,9 +68,6 @@ class Matrix
|
|||||||
void calculateInverse();
|
void calculateInverse();
|
||||||
};
|
};
|
||||||
|
|
||||||
Matrix operator*(const Matrix & m1, const Matrix & m2);
|
|
||||||
Vector operator*(const Matrix & m, const Vector & v); /* transform point */
|
|
||||||
Vector operator%(const Matrix & m, const Vector & v); /* transform direction */
|
|
||||||
bool operator==(const Matrix & m1, const Matrix & m2);
|
bool operator==(const Matrix & m1, const Matrix & m2);
|
||||||
std::ostream & operator<<(std::ostream & out, const Matrix & m);
|
std::ostream & operator<<(std::ostream & out, const Matrix & m);
|
||||||
|
|
||||||
|
@ -2,17 +2,6 @@
|
|||||||
#include "Transform.h"
|
#include "Transform.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
Transform::Transform()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
Transform Transform::getInverse()
|
|
||||||
{
|
|
||||||
Transform inv;
|
|
||||||
inv.m_matrix = m_matrix.getInverse();
|
|
||||||
return inv;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Transform::lookAt(const Vector & eye,
|
void Transform::lookAt(const Vector & eye,
|
||||||
const Vector & focus,
|
const Vector & focus,
|
||||||
const Vector & up)
|
const Vector & up)
|
||||||
@ -81,33 +70,3 @@ void Transform::scale(double xs, double ys, double zs)
|
|||||||
t[2][2] = zs;
|
t[2][2] = zs;
|
||||||
m_matrix *= t;
|
m_matrix *= t;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector Transform::transform_point(const Vector & v)
|
|
||||||
{
|
|
||||||
return m_matrix * v;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector Transform::transform_direction(const Vector & v)
|
|
||||||
{
|
|
||||||
return m_matrix % v;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector Transform::transform_normal(const Vector & v)
|
|
||||||
{
|
|
||||||
return (m_matrix % v).normalize();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ray Transform::transform_ray(const Ray & r)
|
|
||||||
{
|
|
||||||
Vector newPosition = m_matrix * r.getOrigin();
|
|
||||||
Vector newDirection = m_matrix % r.getDirection();
|
|
||||||
Ray res(newPosition, newDirection);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
Transform Transform::operator*(const Transform & other) const
|
|
||||||
{
|
|
||||||
Transform t;
|
|
||||||
t.m_matrix = m_matrix * other.m_matrix;
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
@ -11,8 +11,13 @@
|
|||||||
class Transform
|
class Transform
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Transform();
|
Transform getInverse()
|
||||||
Transform getInverse();
|
{
|
||||||
|
Transform inv;
|
||||||
|
inv.m_matrix = m_matrix.getInverse();
|
||||||
|
return inv;
|
||||||
|
}
|
||||||
|
|
||||||
void lookAt(const Vector & eye,
|
void lookAt(const Vector & eye,
|
||||||
const Vector & focus,
|
const Vector & focus,
|
||||||
const Vector & up);
|
const Vector & up);
|
||||||
@ -32,16 +37,36 @@ class Transform
|
|||||||
scale((*vec)[0], (*vec)[1], (*vec)[2]);
|
scale((*vec)[0], (*vec)[1], (*vec)[2]);
|
||||||
}
|
}
|
||||||
Matrix & getMatrix() { return m_matrix; }
|
Matrix & getMatrix() { return m_matrix; }
|
||||||
Vector transform_point(const Vector & v);
|
Vector transform_point(const Vector & v)
|
||||||
Vector transform_direction(const Vector & v);
|
{
|
||||||
Vector transform_normal(const Vector & v);
|
return m_matrix * v;
|
||||||
Ray transform_ray(const Ray & r);
|
}
|
||||||
Transform operator*(const Transform & other) const;
|
|
||||||
|
Vector transform_direction(const Vector & v)
|
||||||
|
{
|
||||||
|
return m_matrix % v;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector transform_normal(const Vector & v)
|
||||||
|
{
|
||||||
|
return (m_matrix % v).normalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ray transform_ray(const Ray & r)
|
||||||
|
{
|
||||||
|
return Ray(m_matrix * r.getOrigin(), m_matrix % r.getDirection());
|
||||||
|
}
|
||||||
|
|
||||||
|
Transform operator*(const Transform & other) const
|
||||||
|
{
|
||||||
|
Transform t;
|
||||||
|
t.m_matrix = m_matrix * other.m_matrix;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Matrix m_matrix;
|
Matrix m_matrix;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user