From 97b9fa9eabecbfa26b3d4bedf8647fea673c9d5e Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 23 Jan 2009 15:28:53 +0000 Subject: [PATCH] added getInverse() to util/Transform, fixed transform_*() functions git-svn-id: svn://anubis/fart/trunk@39 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- main/Scene.cc | 16 ++++++++++++---- util/Transform.cc | 25 ++++++++++++++++--------- util/Transform.h | 9 +++++---- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/main/Scene.cc b/main/Scene.cc index 79994e2..08d84c8 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -117,8 +117,16 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel) Ray ray(Vector(0, 0, 0), Vector(rx, ry, rz)); - // TODO: um... real work - pixel[BMP_RED] = 0; - pixel[BMP_GREEN] = 0; - pixel[BMP_BLUE] = 0; + /* loop through all shapes in the scene */ + for (vector::iterator it = m_shapes.begin(); + it != m_shapes.end(); + 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); + } } diff --git a/util/Transform.cc b/util/Transform.cc index 1ca90b3..7d0978a 100644 --- a/util/Transform.cc +++ b/util/Transform.cc @@ -7,6 +7,13 @@ Transform::Transform() 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) { Matrix t = Matrix::identity(); @@ -54,25 +61,25 @@ void Transform::scale(double xs, double ys, double zs) 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 newDirection = t.getMatrix() % r.getDirection(); + Vector newPosition = m_matrix * r.getOrigin(); + Vector newDirection = m_matrix % r.getDirection(); Ray res(newPosition, newDirection); return res; } diff --git a/util/Transform.h b/util/Transform.h index d03e7d2..fb1babc 100644 --- a/util/Transform.h +++ b/util/Transform.h @@ -11,14 +11,15 @@ class Transform { public: Transform(); + Transform getInverse(); void translate(double x, double y, double z); void rotate(double angle, double xv, double yv, double zv); void scale(double xs, double ys, double zs); Matrix & getMatrix() { return m_matrix; } - Vector transform_point(Transform & t, const Vector & v); - Vector transform_direction(Transform & t, const Vector & v); - Vector transform_normal(Transform & t, const Vector & v); - Ray transform_ray(Transform & t, const Ray & r); + Vector transform_point(const Vector & v); + Vector transform_direction(const Vector & v); + Vector transform_normal(const Vector & v); + Ray transform_ray(const Ray & r); protected: Matrix m_matrix;