added getInverse() to util/Transform, fixed transform_*() functions

git-svn-id: svn://anubis/fart/trunk@39 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2009-01-23 15:28:53 +00:00
parent 7d461477b3
commit 97b9fa9eab
3 changed files with 33 additions and 17 deletions

View File

@ -117,8 +117,16 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel)
Ray ray(Vector(0, 0, 0), Vector(rx, ry, rz)); Ray ray(Vector(0, 0, 0), Vector(rx, ry, rz));
// TODO: um... real work /* loop through all shapes in the scene */
pixel[BMP_RED] = 0; for (vector<Shape *>::iterator it = m_shapes.begin();
pixel[BMP_GREEN] = 0; it != m_shapes.end();
pixel[BMP_BLUE] = 0; it++)
{
/* transform the ray by the inverse of the shape's transform */
Transform inv = (*it)->getTransform().getInverse();
Ray transformed_ray = inv.transform_ray(ray);
/* then intersect this inversely transformed ray with the shape */
Solver::Result intersections = (*it)->intersect(transformed_ray);
}
} }

View File

@ -7,6 +7,13 @@ Transform::Transform()
m_matrix = Matrix::identity(); m_matrix = Matrix::identity();
} }
Transform Transform::getInverse()
{
Transform inv;
inv.m_matrix = m_matrix.getInverse();
return inv;
}
void Transform::translate(double x, double y, double z) void Transform::translate(double x, double y, double z)
{ {
Matrix t = Matrix::identity(); Matrix t = Matrix::identity();
@ -54,25 +61,25 @@ void Transform::scale(double xs, double ys, double zs)
m_matrix *= t; m_matrix *= t;
} }
Vector Transform::transform_point(Transform & t, const Vector & v) Vector Transform::transform_point(const Vector & v)
{ {
return t.getMatrix() * v; return m_matrix * v;
} }
Vector Transform::transform_direction(Transform & t, const Vector & v) Vector Transform::transform_direction(const Vector & v)
{ {
return t.getMatrix() % v; return m_matrix % v;
} }
Vector Transform::transform_normal(Transform & t, const Vector & v) Vector Transform::transform_normal(const Vector & v)
{ {
return (t.getMatrix() % v).normalize(); return (m_matrix % v).normalize();
} }
Ray Transform::transform_ray(Transform & t, const Ray & r) Ray Transform::transform_ray(const Ray & r)
{ {
Vector newPosition = t.getMatrix() * r.getOrigin(); Vector newPosition = m_matrix * r.getOrigin();
Vector newDirection = t.getMatrix() % r.getDirection(); Vector newDirection = m_matrix % r.getDirection();
Ray res(newPosition, newDirection); Ray res(newPosition, newDirection);
return res; return res;
} }

View File

@ -11,14 +11,15 @@ class Transform
{ {
public: public:
Transform(); Transform();
Transform getInverse();
void translate(double x, double y, double z); void translate(double x, double y, double z);
void rotate(double angle, double xv, double yv, double zv); void rotate(double angle, double xv, double yv, double zv);
void scale(double xs, double ys, double zs); void scale(double xs, double ys, double zs);
Matrix & getMatrix() { return m_matrix; } Matrix & getMatrix() { return m_matrix; }
Vector transform_point(Transform & t, const Vector & v); Vector transform_point(const Vector & v);
Vector transform_direction(Transform & t, const Vector & v); Vector transform_direction(const Vector & v);
Vector transform_normal(Transform & t, const Vector & v); Vector transform_normal(const Vector & v);
Ray transform_ray(Transform & t, const Ray & r); Ray transform_ray(const Ray & r);
protected: protected:
Matrix m_matrix; Matrix m_matrix;