From 0c7f85db9641b7c9d238b58b2a323c4fa41d9465 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 23 Feb 2009 20:24:30 +0000 Subject: [PATCH 2/3] updating IntersectList to new Shape::IntersectionList format git-svn-id: svn://anubis/fart/branches/2009-02-23_Shape_IntersectList_Update@144 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- main/Scene.cc | 10 ++++++---- shapes/Box.cc | 10 +++++++--- shapes/Box.h | 2 +- shapes/Cyl.cc | 19 ++++++++++++++----- shapes/Cyl.h | 2 +- shapes/Intersect.cc | 9 +++++---- shapes/Intersect.h | 2 +- shapes/Plane.cc | 9 ++++++--- shapes/Plane.h | 2 +- shapes/Shape.h | 7 +++++-- shapes/Sphere.cc | 10 ++++++---- shapes/Sphere.h | 2 +- 12 files changed, 54 insertions(+), 30 deletions(-) diff --git a/main/Scene.cc b/main/Scene.cc index f635bc0..425f60a 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -171,15 +171,17 @@ vector Scene::getRayHits(const Ray & ray) it != m_shapes.end(); it++) { - Shape::IntersectList intersections = (*it)->intersect(ray); + Shape::IntersectionList intersections = (*it)->intersect(ray); for (int i = 0, num_results = intersections.size(); i < num_results; i++) { - Vector normal = (*it)->getNormalAt(intersections[i]); + refptr shape = intersections[i].first; + refptr isect_point = intersections[i].second; + Vector normal = shape->getNormalAt(*isect_point); double dot = normal % ray.getDirection(); - double intersect_dist = (intersections[i] - ray.getOrigin()).mag(); + double intersect_dist = ((*isect_point) - ray.getOrigin()).mag(); if (dot < 0.0) /* cull back faces */ { double transparency = (*it)->getTransparency(); @@ -190,7 +192,7 @@ vector Scene::getRayHits(const Ray & ray) } if (minSolidDist == 0.0 || minSolidDist >= intersect_dist) { - hits.push_back(ShapeDistance(*it, intersect_dist)); + hits.push_back(ShapeDistance(shape, intersect_dist)); } } } diff --git a/shapes/Box.cc b/shapes/Box.cc index 9cf2b58..ae07812 100644 --- a/shapes/Box.cc +++ b/shapes/Box.cc @@ -15,11 +15,11 @@ Box::Box(refptr size) m_size[2] = fabs(m_size[2]) / 2.0; } -Shape::IntersectList Box::intersect(const Ray & ray) +Shape::IntersectionList Box::intersect(refptr _this, const Ray & ray) { Ray ray_inv = m_inverse.transform_ray(ray); - IntersectList res; + IntersectionList res; /* * Ray equation: R = R0 + tRd * x = R0x + tRdx @@ -45,7 +45,11 @@ Shape::IntersectList Box::intersect(const Ray & ray) && (dim == 1 || fabs(isect_point[1]) <= m_size[1]) && (dim == 2 || fabs(isect_point[2]) <= m_size[2]) ) { - res.push_back(m_transform.transform_point(isect_point)); + res.push_back( + Intersection(_this, + m_transform.transform_point( + isect_point)) + ); } } } diff --git a/shapes/Box.h b/shapes/Box.h index caf809d..556c385 100644 --- a/shapes/Box.h +++ b/shapes/Box.h @@ -8,7 +8,7 @@ class Box : public Shape { public: Box(refptr size); - IntersectList intersect(const Ray & ray); + IntersectionList intersect(refptr _this, const Ray & ray); Vector getNormalAt(const Vector & pt); protected: diff --git a/shapes/Cyl.cc b/shapes/Cyl.cc index aafe022..833755e 100644 --- a/shapes/Cyl.cc +++ b/shapes/Cyl.cc @@ -19,10 +19,10 @@ Cyl::Cyl(double bottom_radius, double top_radius, double height) m_slope = (m_top_radius - m_bottom_radius) / m_height; /* rise over run */ } -Shape::IntersectList Cyl::intersect(const Ray & ray) +Shape::IntersectionList Cyl::intersect(refptr _this, const Ray & ray) { Ray ray_inv = m_inverse.transform_ray(ray); - IntersectList res; + IntersectionList res; /* First intersect with the bottom plane, if it has positive area */ if (m_bottom_radius > 0.0) @@ -36,7 +36,10 @@ Shape::IntersectList Cyl::intersect(const Ray & ray) if (isect_point[0]*isect_point[0] + isect_point[1]*isect_point[1] < m_bottom_radius_2) { - res.push_back(m_transform.transform_point(isect_point)); + res.push_back( + Intersection(_this, + m_transform.transform_point(isect_point)) + ); } } } @@ -53,7 +56,10 @@ Shape::IntersectList Cyl::intersect(const Ray & ray) if (isect_point[0]*isect_point[0] + isect_point[1]*isect_point[1] < m_top_radius_2) { - res.push_back(m_transform.transform_point(isect_point)); + res.push_back( + Intersection(_this, + m_transform.transform_point(isect_point)) + ); } } } @@ -97,7 +103,10 @@ Shape::IntersectList Cyl::intersect(const Ray & ray) Vector isect_point = ray_inv[solutions.results[i]]; if (isect_point[2] >= 0.0 && isect_point[2] <= m_height) { - res.push_back(m_transform.transform_point(isect_point)); + res.push_back( + Intersection(_this, + m_transform.transform_point(isect_point)) + ); } } } diff --git a/shapes/Cyl.h b/shapes/Cyl.h index 89f5894..eeef786 100644 --- a/shapes/Cyl.h +++ b/shapes/Cyl.h @@ -8,7 +8,7 @@ class Cyl : public Shape { public: Cyl(double bottom_radius, double top_radius, double height); - IntersectList intersect(const Ray & ray); + IntersectionList intersect(refptr _this, const Ray & ray); Vector getNormalAt(const Vector & pt); protected: diff --git a/shapes/Intersect.cc b/shapes/Intersect.cc index b1e9393..c1a81a2 100644 --- a/shapes/Intersect.cc +++ b/shapes/Intersect.cc @@ -7,12 +7,13 @@ Intersect::Intersect(refptr shape1, refptr shape2) m_shape2 = shape2; } -Shape::IntersectList Intersect::intersect(const Ray & ray) +Shape::IntersectionList Intersect::intersect(refptr _this, const Ray & ray) { - IntersectList res; - IntersectList res1 = m_shape1->intersect(ray); - IntersectList res2 = m_shape2->intersect(ray); + IntersectionList res; + IntersectionList res1 = m_shape1->intersect(ray); + IntersectionList res2 = m_shape2->intersect(ray); + /* TODO: finish */ return res; } diff --git a/shapes/Intersect.h b/shapes/Intersect.h index d20581e..4a975a7 100644 --- a/shapes/Intersect.h +++ b/shapes/Intersect.h @@ -8,7 +8,7 @@ class Intersect : public Shape { public: Intersect(refptr shape1, refptr shape2); - IntersectList intersect(const Ray & ray); + IntersectionList intersect(refptr _this, const Ray & ray); Vector getNormalAt(const Vector & pt); protected: diff --git a/shapes/Plane.cc b/shapes/Plane.cc index b04bfce..e32cb8d 100644 --- a/shapes/Plane.cc +++ b/shapes/Plane.cc @@ -13,11 +13,11 @@ Plane::Plane(double a, double b, double c, double d) m_d = d; } -Shape::IntersectList Plane::intersect(const Ray & ray) +Shape::IntersectionList Plane::intersect(refptr _this, const Ray & ray) { Ray ray_inv = m_inverse.transform_ray(ray); - IntersectList res; + IntersectionList res; /* * Plane equation: ax + by + cz + d = 0 * Ray equation: R = R0 + tRd @@ -38,7 +38,10 @@ Shape::IntersectList Plane::intersect(const Ray & ray) Solver::Result solutions = solver.solve(); if (solutions.numResults > 0) { - res.push_back(m_transform.transform_point(ray_inv[solutions.results[0]])); + Vector isect_point = ray_inv[solutions.results[0]]; + res.push_back( + Intersection(_this, m_transform.transform_point(isect_point)) + ); } return res; } diff --git a/shapes/Plane.h b/shapes/Plane.h index dcf799c..46fecce 100644 --- a/shapes/Plane.h +++ b/shapes/Plane.h @@ -8,7 +8,7 @@ class Plane : public Shape { public: Plane(double a, double b, double c, double d); - IntersectList intersect(const Ray & ray); + IntersectionList intersect(refptr _this, const Ray & ray); Vector getNormalAt(const Vector & pt); protected: diff --git a/shapes/Shape.h b/shapes/Shape.h index 847bee0..044c7e5 100644 --- a/shapes/Shape.h +++ b/shapes/Shape.h @@ -9,15 +9,18 @@ #include "util/Material.h" #include "util/refptr.h" #include +#include class Shape { public: - typedef std::vector IntersectList; + typedef std::pair< refptr , Vector > Intersection; + typedef std::vector< Intersection > IntersectionList; Shape(); virtual ~Shape(); - virtual IntersectList intersect(const Ray & ray) = 0; + virtual IntersectionList intersect(refptr _this, + const Ray & ray) = 0; virtual Vector getNormalAt(const Vector & pt) = 0; void setTransform(Transform & t) diff --git a/shapes/Sphere.cc b/shapes/Sphere.cc index e3aaea6..ff2fb90 100644 --- a/shapes/Sphere.cc +++ b/shapes/Sphere.cc @@ -11,11 +11,11 @@ Sphere::Sphere(double radius) m_radius2 = radius * radius; } -Shape::IntersectList Sphere::intersect(const Ray & ray) +Shape::IntersectionList Sphere::intersect(refptr _this, const Ray & ray) { Ray ray_inv = m_inverse.transform_ray(ray); - IntersectList res; + IntersectionList res; QuadraticSolver solver(1.0, 2 * ( ray_inv.getOrigin()[0] * ray_inv.getDirection()[0] + ray_inv.getOrigin()[1] * ray_inv.getDirection()[1] @@ -29,8 +29,10 @@ Shape::IntersectList Sphere::intersect(const Ray & ray) { if (quadSolutions.results[i] >= 0.0) { - res.push_back(m_transform.transform_point( - ray_inv[quadSolutions.results[i]])); + Vector isect_point = ray_inv[quadSolutions.results[i]]; + res.push_back( + Intersection(_this, m_transform.transform_point(isect_point)) + ); } } return res; diff --git a/shapes/Sphere.h b/shapes/Sphere.h index cbdb132..165272e 100644 --- a/shapes/Sphere.h +++ b/shapes/Sphere.h @@ -8,7 +8,7 @@ class Sphere : public Shape { public: Sphere(double radius); - IntersectList intersect(const Ray & ray); + IntersectionList intersect(refptr _this, const Ray & ray); Vector getNormalAt(const Vector & pt); protected: From ab2832a6607e8d2421c0d6c1e4489fc423c8d322 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 23 Feb 2009 20:26:45 +0000 Subject: [PATCH 3/3] fixed compile issues with switch to new Shape::IntersectionList git-svn-id: svn://anubis/fart/branches/2009-02-23_Shape_IntersectList_Update@145 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- main/Scene.cc | 8 ++++---- shapes/Intersect.cc | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/main/Scene.cc b/main/Scene.cc index 425f60a..9270054 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -171,17 +171,17 @@ vector Scene::getRayHits(const Ray & ray) it != m_shapes.end(); it++) { - Shape::IntersectionList intersections = (*it)->intersect(ray); + Shape::IntersectionList intersections = (*it)->intersect(*it, ray); for (int i = 0, num_results = intersections.size(); i < num_results; i++) { refptr shape = intersections[i].first; - refptr isect_point = intersections[i].second; - Vector normal = shape->getNormalAt(*isect_point); + const Vector & isect_point = intersections[i].second; + Vector normal = shape->getNormalAt(isect_point); double dot = normal % ray.getDirection(); - double intersect_dist = ((*isect_point) - ray.getOrigin()).mag(); + double intersect_dist = (isect_point - ray.getOrigin()).mag(); if (dot < 0.0) /* cull back faces */ { double transparency = (*it)->getTransparency(); diff --git a/shapes/Intersect.cc b/shapes/Intersect.cc index c1a81a2..667e2cf 100644 --- a/shapes/Intersect.cc +++ b/shapes/Intersect.cc @@ -10,8 +10,8 @@ Intersect::Intersect(refptr shape1, refptr shape2) Shape::IntersectionList Intersect::intersect(refptr _this, const Ray & ray) { IntersectionList res; - IntersectionList res1 = m_shape1->intersect(ray); - IntersectionList res2 = m_shape2->intersect(ray); + IntersectionList res1 = m_shape1->intersect(m_shape1, ray); + IntersectionList res2 = m_shape2->intersect(m_shape2, ray); /* TODO: finish */