From 5035c80d942c0ce6fe2c93e9b8910f7f10ff9815 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 28 Feb 2009 00:06:12 +0000 Subject: [PATCH] Subtract working, but reflected normals being returned... and where did my plane and cyl go? git-svn-id: svn://anubis/fart/trunk@159 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- main/Lighting.cc | 2 +- main/Scene-load.cc | 23 +++++++++++++++++++++++ main/Scene.cc | 8 ++++---- shapes/Subtract.cc | 36 +++++++++++++----------------------- 4 files changed, 41 insertions(+), 28 deletions(-) diff --git a/main/Lighting.cc b/main/Lighting.cc index 73cee51..280b540 100644 --- a/main/Lighting.cc +++ b/main/Lighting.cc @@ -28,7 +28,7 @@ Color Lighting::computePhong(const refptr material, directionToLight.reflect(surfaceNormal); /* calculate the diffuse term */ - double diffuse_coef = directionToLight % surfaceNormal; + double diffuse_coef = fabs(directionToLight % surfaceNormal); if (diffuse_coef > 0.0) { result += diffuseColor diff --git a/main/Scene-load.cc b/main/Scene-load.cc index c62fd80..050e649 100644 --- a/main/Scene-load.cc +++ b/main/Scene-load.cc @@ -45,6 +45,29 @@ void Scene::load(const char * filename) m_transform.rotate(-20, 0, 1, 0); m_transform.translate(2, 2, 1); + m = new Material(); + m->setDiffuseColor(Color::magenta); + m->setAmbientColor(Color::magenta); + + m_transform.translate(2.5, 1, -1.5); + shape1 = new Box(new Vector(1, 1, 1)); + shape1->setMaterial(m); + shape1->setTransform(m_transform); + + m = new Material(); + m->setDiffuseColor(Color::yellow); + m->setAmbientColor(Color::yellow); + + shape2 = new Sphere(0.6); + m_transform.translate(-0.5, -0.5, 0.5); + shape2->setTransform(m_transform); + shape2->setMaterial(m); + + shape = new Subtract(shape1, shape2); + m_shapes.push_back(shape); + + m_transform.translate(-2.0, 0.5, 1.0); + m = new Material(); m->setDiffuseColor(Color::blue); m->setAmbientColor(Color::blue); diff --git a/main/Scene.cc b/main/Scene.cc index e296fd6..f50dd5a 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -180,10 +180,10 @@ vector Scene::getRayHits(const Ray & ray) refptr shape = intersections[i].shape; const Vector & isect_point = intersections[i].vector; Vector normal = shape->getNormalAt(isect_point); - double dot = normal % ray.getDirection(); +// double dot = normal % ray.getDirection(); double intersect_dist = (isect_point - ray.getOrigin()).mag(); - if (dot < 0.0) /* cull back faces */ - { +// if (dot < 0.0) /* cull back faces */ +// { double transparency = (*it)->getTransparency(); if (transparency == 0.0 && (minSolidDist == 0.0 || minSolidDist > intersect_dist)) @@ -194,7 +194,7 @@ vector Scene::getRayHits(const Ray & ray) { hits.push_back(ShapeDistance(shape, intersect_dist)); } - } +// } } } diff --git a/shapes/Subtract.cc b/shapes/Subtract.cc index 9d9d0e5..e44ba44 100644 --- a/shapes/Subtract.cc +++ b/shapes/Subtract.cc @@ -26,31 +26,21 @@ Shape::IntersectionList Subtract::intersect(refptr _this, const Ray & ray double dot = - (ray.getDirection() % normal); bool front = dot > 0.0; bool left = merged[i].left; - if (front) - { - if (left) - in1 = true; - else - in2 = true; - if (!in_bool && in1 && !in2) - { - /* we found an intersection point with the boolean object */ - in_bool = true; - res.add( merged[i].intersection ); - } - } + if (left) + in1 = front; else + in2 = front; + if (!in_bool && in1 && !in2) { - if (left) - in1 = false; - else - in2 = false; - if (in_bool && !(in1 && !in2)) - { - /* we found an intersection point with the boolean object */ - res.add( merged[i].intersection ); - in_bool = false; - } + /* we found an intersection point with the boolean object */ + in_bool = true; + res.add( merged[i].intersection ); + } + else if (in_bool && !(in1 && !in2)) + { + /* we found an intersection point with the boolean object */ + res.add( merged[i].intersection ); + in_bool = false; } }