From f087488b391b9431cc9e6ca3e2e588962da12dbe Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 30 Jan 2009 01:25:57 +0000 Subject: [PATCH] using Color instead of Vector where appropriate, updated main/Light to use diffuse and specular lights git-svn-id: svn://anubis/fart/trunk@67 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- main/Light.cc | 5 +++-- main/Light.h | 18 +++++++++++++++--- main/PointLight.cc | 4 ++-- main/PointLight.h | 2 +- main/Scene.cc | 29 ++++++++++++++++------------- main/Scene.h | 3 ++- util/Color.cc | 14 ++++++++++++++ util/Color.h | 2 ++ 8 files changed, 55 insertions(+), 22 deletions(-) diff --git a/main/Light.cc b/main/Light.cc index c1b645c..95543b5 100644 --- a/main/Light.cc +++ b/main/Light.cc @@ -2,9 +2,10 @@ #include "Light.h" #include "util/Vector.h" -Light::Light(const Vector & position, const Color & color) +Light::Light(const Vector & position) + : m_diffuse_color(Color::white), + m_specular_color(Color::white) { m_position = position; - m_color = color; } diff --git a/main/Light.h b/main/Light.h index 6096e8b..9a37068 100644 --- a/main/Light.h +++ b/main/Light.h @@ -8,13 +8,25 @@ class Light { public: - Light(const Vector & position, const Color & color = Color::white); + Light(const Vector & position); const Vector & getPosition() const { return m_position; } - const Color & getColor() const { return m_color; } + + void setDiffuseColor(const Color & diffuse) + { + m_diffuse_color = diffuse; + } + const Color & getDiffuseColor() const { return m_diffuse_color; } + + void setSpecularColor(const Color & specular) + { + m_specular_color = specular; + } + const Color & getSpecularColor() const { return m_specular_color; } protected: Vector m_position; - Color m_color; + Color m_diffuse_color; + Color m_specular_color; }; #endif diff --git a/main/PointLight.cc b/main/PointLight.cc index 6df2b64..448b277 100755 --- a/main/PointLight.cc +++ b/main/PointLight.cc @@ -1,7 +1,7 @@ #include "PointLight.h" -PointLight::PointLight(const Vector & position, const Color & color) - : Light(position, color) +PointLight::PointLight(const Vector & position) + : Light(position) { } diff --git a/main/PointLight.h b/main/PointLight.h index cce4e8b..04825a5 100644 --- a/main/PointLight.h +++ b/main/PointLight.h @@ -8,7 +8,7 @@ class PointLight : public Light { public: - PointLight(const Vector & position, const Color & color = Color::white); + PointLight(const Vector & position); protected: }; diff --git a/main/Scene.cc b/main/Scene.cc index 6fba3b6..3458002 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -9,6 +9,7 @@ #include /* binary_function */ #include #include "BMP.h" +#include "util/Color.h" #include "shapes/Shape.h" #include "PointLight.h" using namespace std; @@ -132,7 +133,7 @@ void Scene::render() void Scene::renderPixel(int x, int y, unsigned char * pixel) { /* calculate the ray going from the camera through this pixel */ - double red = 0.0, green = 0.0, blue = 0.0; + Color finalColor; for (int i = 0; i < m_multisample_level; i++) { for (int j = 0; j < m_multisample_level; j++) @@ -144,28 +145,30 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel) Ray ray(Vector(0, 0, 0), Vector(rx, m_view_plane_dist, rz)); - Vector color = traceRay(ray); - - red += color[0]; - green += color[1]; - blue += color[2]; + finalColor += traceRay(ray); } } /* take the average of all the samples as the final pixel value */ - pixel[BMP_RED] = (unsigned char) (0xFF * red / m_multisample_level_squared); - pixel[BMP_GREEN] = (unsigned char) (0xFF * green / m_multisample_level_squared); - pixel[BMP_BLUE] = (unsigned char) (0xFF * blue / m_multisample_level_squared); + pixel[BMP_RED] = (unsigned char) + (0xFF * finalColor.r / m_multisample_level_squared); + pixel[BMP_GREEN] = (unsigned char) + (0xFF * finalColor.g / m_multisample_level_squared); + pixel[BMP_BLUE] = (unsigned char) + (0xFF * finalColor.b / m_multisample_level_squared); } -Vector Scene::traceRay(const Ray & ray) +Color Scene::traceRay(const Ray & ray) { - Vector color; + Color color; vector hits = getRayHits(ray); - if (hits.size() > 0) + + for (vector::const_iterator it = hits.begin(); + it != hits.end(); + it++) { - color[0] = color[1] = color[2] = 1.0; + /* compute the Phong lighting for each hit */ } return color; diff --git a/main/Scene.h b/main/Scene.h index 52dd719..228d7f9 100644 --- a/main/Scene.h +++ b/main/Scene.h @@ -7,6 +7,7 @@ #include #include #include "util/Ray.h" +#include "util/Color.h" #include "shapes/Shape.h" #include "Light.h" @@ -35,7 +36,7 @@ class Scene /* private methods */ void load(const char * filename); void renderPixel(int x, int y, unsigned char * pixel); - Vector traceRay(const Ray & ray); + Color traceRay(const Ray & ray); std::vector getRayHits(const Ray & ray); /* rendering parameters */ diff --git a/util/Color.cc b/util/Color.cc index e208979..ba7488d 100644 --- a/util/Color.cc +++ b/util/Color.cc @@ -29,6 +29,20 @@ Color Color::operator/(double scale) return Color(r / scale, g / scale, b / scale); } +Color & Color::operator+=(const Color & other) +{ + r += other.r; + g += other.g; + b += other.b; +} + +Color & Color::operator-=(const Color & other) +{ + r += other.r; + g += other.g; + b += other.b; +} + Color operator+(const Color & c1, const Color & c2) { return Color(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b); diff --git a/util/Color.h b/util/Color.h index 9359836..62eddc2 100644 --- a/util/Color.h +++ b/util/Color.h @@ -12,6 +12,8 @@ class Color Color operator*(double scale); Color operator/(double scale); + Color & operator+=(const Color & other); + Color & operator-=(const Color & other); static const Color black; static const Color white;