From d88f1ab5fd98fa71621a29ea9bfe0420be0f83e7 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 23 Jan 2009 23:56:39 +0000 Subject: [PATCH] fixed bug in QuadraticSolver, cheap lighting working git-svn-id: svn://anubis/fart/trunk@45 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- Makefile | 2 +- main/Scene.cc | 19 +++++++++---------- shapes/Sphere.cc | 6 ++---- test/tests.cc | 14 ++++++++++---- util/Solver.cc | 8 +++++++- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 5b48816..06ce9ff 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ export CPPFLAGS += -I"$(shell cd)" else export CPPFLAGS += -I"$(shell pwd)" endif -export CXXFLAGS := -O3 +export CXXFLAGS := -Wall -O3 all: $(TARGET) diff --git a/main/Scene.cc b/main/Scene.cc index 80c9e94..02feaf9 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -125,23 +125,22 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel) { /* transform the ray by the inverse of the shape's transform */ Transform inv = (*it)->getTransform().getInverse(); - Ray transformed_ray = inv.transform_ray(ray); + Ray local_ray = inv.transform_ray(ray); /* then intersect this inversely transformed ray with the shape */ - Solver::Result intersections = (*it)->intersect(transformed_ray); + Solver::Result intersections = (*it)->intersect(local_ray); if (intersections.numResults > 0) { - Vector local_intersection_point = - transformed_ray[intersections.results[0]]; - Vector normal = (*it)->getNormalAt(local_intersection_point); - double dot = -(normal % transformed_ray.getDirection()); - cout << "got dot " << dot << endl; + Vector local_normal = + (*it)->getNormalAt(local_ray[intersections.results[0]]); + double dot = local_ray.getDirection() % local_normal; + dot = -dot; if (dot < 0.0) dot = 0.0; - pixel[BMP_RED] = (unsigned char) (0xFF * dot); - pixel[BMP_GREEN] = (unsigned char) (0xFF * dot); - pixel[BMP_BLUE] = (unsigned char) (0xFF * dot); + pixel[BMP_RED] = 0xFF * dot; + pixel[BMP_GREEN] = 0xFF * dot; + pixel[BMP_BLUE] = 0xFF * dot; } else { diff --git a/shapes/Sphere.cc b/shapes/Sphere.cc index 091c97a..810d560 100644 --- a/shapes/Sphere.cc +++ b/shapes/Sphere.cc @@ -1,6 +1,7 @@ #include "Sphere.h" #include "util/Solver.h" +#include #include using namespace std; @@ -22,14 +23,11 @@ Solver::Result Sphere::intersect(const Ray & ray) + ray.getOrigin()[2] * ray.getOrigin()[2] - m_radius2); Solver::Result quadSolutions = solver.solve(); - int resIdx = 0; for (int i = 0; i < quadSolutions.numResults; i++) { if (quadSolutions.results[i] >= 0.0) { - res.results[resIdx] = quadSolutions.results[i]; - resIdx++; - res.numResults = resIdx; + res.results[res.numResults++] = quadSolutions.results[i]; } } return res; diff --git a/test/tests.cc b/test/tests.cc index 282c67a..294c7eb 100644 --- a/test/tests.cc +++ b/test/tests.cc @@ -2,14 +2,20 @@ #include #include #include "util/Transform.h" +#include "shapes/Sphere.h" using namespace std; int main() { - Transform t; - t.scale(3, 2, 1); - t.translate(-1, 2, 3); - cout << "t:" << endl << t.getMatrix(); + Sphere s(1.0); + Ray ray(Vector(0, -2.5, 0), Vector(0, 1, 0)); + + Solver::Result res = s.intersect(ray); + + for (int i = 0; i < res.numResults; i++) + { + cout << "t: " << res.results[i] << endl; + } return 0; } diff --git a/util/Solver.cc b/util/Solver.cc index f1c6151..125760d 100644 --- a/util/Solver.cc +++ b/util/Solver.cc @@ -34,7 +34,7 @@ Solver::Result QuadraticSolver::solve() { double sqrt_discriminant = sqrt(discriminant); double two_a = 2.0 * a; - if (sqrt_discriminant = 0.0) + if (sqrt_discriminant == 0.0) { res.numResults = 1; res.results[0] = (-b) / two_a; @@ -61,6 +61,9 @@ CubicSolver::CubicSolver(double a, double b, double c, double d) Solver::Result CubicSolver::solve() { + Result res; + /* TODO: fill in */ + return res; } @@ -74,6 +77,9 @@ QuarticSolver::QuarticSolver(double a, double b, double c, double d, double e) Solver::Result QuarticSolver::solve() { + Result res; + /* TODO: fill in */ + return res; } Solver::Result::Result()