diff --git a/main/Scene.cc b/main/Scene.cc index 1c7462d..80c9e94 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -132,9 +132,16 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel) if (intersections.numResults > 0) { - pixel[BMP_RED] = 0xFF; - pixel[BMP_GREEN] = 0xFF; - pixel[BMP_BLUE] = 0xFF; + 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; + 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); } else { diff --git a/shapes/Shape.h b/shapes/Shape.h index 5163503..748703e 100644 --- a/shapes/Shape.h +++ b/shapes/Shape.h @@ -4,6 +4,7 @@ #include "util/Solver.h" #include "util/Ray.h" +#include "util/Vector.h" #include "util/Transform.h" class Shape @@ -12,6 +13,7 @@ class Shape virtual Solver::Result intersect(const Ray & ray) = 0; void setTransform(const Transform & t) { m_transform = t; } Transform & getTransform() { return m_transform; } + virtual Vector getNormalAt(const Vector & pt) = 0; protected: Transform m_transform; diff --git a/shapes/Sphere.cc b/shapes/Sphere.cc index f23e1f6..091c97a 100644 --- a/shapes/Sphere.cc +++ b/shapes/Sphere.cc @@ -34,3 +34,9 @@ Solver::Result Sphere::intersect(const Ray & ray) } return res; } + +Vector Sphere::getNormalAt(const Vector & pt) +{ + Vector normal = pt; + return normal.normalize(); +} diff --git a/shapes/Sphere.h b/shapes/Sphere.h index 9268b3f..4824705 100644 --- a/shapes/Sphere.h +++ b/shapes/Sphere.h @@ -9,6 +9,7 @@ class Sphere : public Shape public: Sphere(double radius); Solver::Result intersect(const Ray & ray); + Vector getNormalAt(const Vector & pt); private: double m_radius; diff --git a/util/Vector.cc b/util/Vector.cc index b373843..871881c 100644 --- a/util/Vector.cc +++ b/util/Vector.cc @@ -26,14 +26,14 @@ Vector & Vector::normalize() return *this; } -double Vector::mag() +double Vector::mag() const { return sqrt(m_array[0] * m_array[0] + m_array[1] * m_array[1] + m_array[2] * m_array[2]); } -double Vector::mag2() +double Vector::mag2() const { return m_array[0] * m_array[0] + m_array[1] * m_array[1] diff --git a/util/Vector.h b/util/Vector.h index 3857780..a7e4c61 100644 --- a/util/Vector.h +++ b/util/Vector.h @@ -12,8 +12,8 @@ class Vector double & operator[](int idx) { return m_array[idx]; } double operator[](int idx) const { return m_array[idx]; } Vector & normalize(); - double mag(); - double mag2(); + double mag() const; + double mag2() const; private: double m_array[3];