From 41b4354d526bd2588dfe3a9c0492751f27fe2ff5 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 30 Jan 2009 20:53:13 +0000 Subject: [PATCH] trying to get Phong shading model working, having very large color values come back though git-svn-id: svn://anubis/fart/trunk@72 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- main/Light.h | 4 ---- main/Lighting.cc | 23 ++++++++++++++++++++++- main/Material.cc | 1 + main/Material.h | 4 ++++ main/Scene.cc | 16 +++++++++++++++- main/Scene.h | 1 + shapes/Shape.cc | 1 + shapes/Shape.h | 6 ++++++ util/Color.cc | 19 +++++++++++++++++-- util/Color.h | 8 ++++++-- 10 files changed, 73 insertions(+), 10 deletions(-) diff --git a/main/Light.h b/main/Light.h index 5e4dc96..9a37068 100644 --- a/main/Light.h +++ b/main/Light.h @@ -23,14 +23,10 @@ class Light } const Color & getSpecularColor() const { return m_specular_color; } - void setShininess(double shininess) { m_shininess = shininess; } - double getShininess() const { return m_shininess; } - protected: Vector m_position; Color m_diffuse_color; Color m_specular_color; - double m_shininess; }; #endif diff --git a/main/Lighting.cc b/main/Lighting.cc index e15841c..24b5f88 100644 --- a/main/Lighting.cc +++ b/main/Lighting.cc @@ -1,5 +1,6 @@ #include "Lighting.h" +#include /* pow() */ Color Lighting::computePhong(const Material & material, const std::vector & lights, @@ -9,11 +10,31 @@ Color Lighting::computePhong(const Material & material, const Color & ambientLight) { Color result = ambientLight; - Vector V = -viewRay.getDirection(); + + Vector viewDirection = -viewRay.getDirection(); + double shininess = material.getShininess(); + const Color & diffuseColor = material.getDiffuseColor(); + const Color & specularColor = material.getSpecularColor(); + for (std::vector::const_iterator it = lights.begin(); it != lights.end(); it++) { + Vector directionToLight = (*it)->getPosition() - surfacePoint; + directionToLight.normalize(); + Vector reflectedLightDirection = + directionToLight.reflect(surfaceNormal); + + /* calculate the diffuse term */ + result += diffuseColor + * (*it)->getDiffuseColor() + * (directionToLight % surfaceNormal); + + /* calculate the specular term */ + result += specularColor + * (*it)->getSpecularColor() + * pow(reflectedLightDirection % viewDirection, shininess); } + return result; } diff --git a/main/Material.cc b/main/Material.cc index 68f90bc..3f608ff 100644 --- a/main/Material.cc +++ b/main/Material.cc @@ -7,4 +7,5 @@ Material::Material() { m_diffuse_color = Color::white; m_specular_color = Color::white; + m_shininess = 50; } diff --git a/main/Material.h b/main/Material.h index 14dfec8..9bae30d 100644 --- a/main/Material.h +++ b/main/Material.h @@ -24,9 +24,13 @@ class Material } const Color & getSpecularColor() const { return m_specular_color; } + void setShininess(double shininess) { m_shininess = shininess; } + double getShininess() const { return m_shininess; } + protected: Color m_diffuse_color; Color m_specular_color; + double m_shininess; }; #endif diff --git a/main/Scene.cc b/main/Scene.cc index afc9c42..417cb10 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -12,6 +12,7 @@ #include "util/Color.h" #include "shapes/Shape.h" #include "PointLight.h" +#include "Lighting.h" using namespace std; Scene::Scene(const map & options, @@ -150,6 +151,8 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel) } /* take the average of all the samples as the final pixel value */ + if (finalColor.r > 1.0 || finalColor.g > 1.0 || finalColor.b > 1.0) + cerr << "Error: " << finalColor << endl; pixel[BMP_RED] = (unsigned char) (0xFF * finalColor.r / m_multisample_level_squared); pixel[BMP_GREEN] = (unsigned char) @@ -169,7 +172,18 @@ Color Scene::traceRay(const Ray & ray) it++) { /* compute the Phong lighting for each hit */ - color = Color::white; + const Material * material = it->first->getMaterial(); + if (material == NULL) + material = &Material::white; + + Vector surfacePoint = ray[it->second]; + + color = Lighting::computePhong(*material, + m_lights, + ray, + surfacePoint, + it->first->getNormalAt(surfacePoint), + m_ambient_light); } return color; diff --git a/main/Scene.h b/main/Scene.h index a85fd95..39503a6 100644 --- a/main/Scene.h +++ b/main/Scene.h @@ -46,6 +46,7 @@ class Scene std::string m_output_file_name; bool m_verbose; double m_vfov; + Color m_ambient_light; /* private data */ std::vector m_shapes; diff --git a/shapes/Shape.cc b/shapes/Shape.cc index b00e1e3..7acce71 100755 --- a/shapes/Shape.cc +++ b/shapes/Shape.cc @@ -4,4 +4,5 @@ Shape::Shape() { m_transparency = 0.0; + m_material = NULL; } diff --git a/shapes/Shape.h b/shapes/Shape.h index ccc2b58..dfb78d0 100644 --- a/shapes/Shape.h +++ b/shapes/Shape.h @@ -6,6 +6,7 @@ #include "util/Ray.h" #include "util/Vector.h" #include "util/Transform.h" +#include "main/Material.h" #include class Shape @@ -23,13 +24,18 @@ class Shape m_inverse = t.getInverse(); } Transform & getTransform() { return m_transform; } + void setTransparency(double t) { m_transparency = t; } double getTransparency() const { return m_transparency; } + void setMaterial(Material * material) { m_material = material; } + Material * getMaterial() const { return m_material; } + protected: Transform m_transform; Transform m_inverse; double m_transparency; + Material * m_material; }; #include "Sphere.h" diff --git a/util/Color.cc b/util/Color.cc index ba7488d..b58a6fe 100644 --- a/util/Color.cc +++ b/util/Color.cc @@ -19,12 +19,21 @@ Color::Color(double r, double g, double b) this->b = b; } -Color Color::operator*(double scale) +Color Color::operator*(const Color & other) const +{ + Color result; + result.r = r * other.r; + result.g = g * other.g; + result.b = b * other.b; + return result; +} + +Color Color::operator*(double scale) const { return Color(r * scale, g * scale, b * scale); } -Color Color::operator/(double scale) +Color Color::operator/(double scale) const { return Color(r / scale, g / scale, b / scale); } @@ -52,3 +61,9 @@ Color operator-(const Color & c1, const Color & c2) { return Color(c1.r - c2.r, c1.g - c2.g, c1.b - c2.b); } + +std::ostream & operator<<(std::ostream & out, const Color & color) +{ + out << "[" << color.r << ", " << color.g << ", " << color.b << "]"; + return out; +} diff --git a/util/Color.h b/util/Color.h index 62eddc2..1dacbf7 100644 --- a/util/Color.h +++ b/util/Color.h @@ -2,6 +2,8 @@ #ifndef COLOR_H #define COLOR_H COLOR_H +#include + class Color { public: @@ -10,8 +12,9 @@ class Color Color(); Color(double r, double g, double b); - Color operator*(double scale); - Color operator/(double scale); + Color operator*(const Color & other) const; + Color operator*(double scale) const; + Color operator/(double scale) const; Color & operator+=(const Color & other); Color & operator-=(const Color & other); @@ -24,6 +27,7 @@ class Color Color operator+(const Color & c1, const Color & c2); Color operator-(const Color & c1, const Color & c2); +std::ostream & operator<<(std::ostream & out, const Color & color); #endif