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
This commit is contained in:
Josh Holtrop 2009-01-30 20:53:13 +00:00
parent 8677680577
commit 41b4354d52
10 changed files with 73 additions and 10 deletions

View File

@ -23,14 +23,10 @@ class Light
} }
const Color & getSpecularColor() const { return m_specular_color; } const Color & getSpecularColor() const { return m_specular_color; }
void setShininess(double shininess) { m_shininess = shininess; }
double getShininess() const { return m_shininess; }
protected: protected:
Vector m_position; Vector m_position;
Color m_diffuse_color; Color m_diffuse_color;
Color m_specular_color; Color m_specular_color;
double m_shininess;
}; };
#endif #endif

View File

@ -1,5 +1,6 @@
#include "Lighting.h" #include "Lighting.h"
#include <math.h> /* pow() */
Color Lighting::computePhong(const Material & material, Color Lighting::computePhong(const Material & material,
const std::vector<Light *> & lights, const std::vector<Light *> & lights,
@ -9,11 +10,31 @@ Color Lighting::computePhong(const Material & material,
const Color & ambientLight) const Color & ambientLight)
{ {
Color result = 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<Light *>::const_iterator it = lights.begin(); for (std::vector<Light *>::const_iterator it = lights.begin();
it != lights.end(); it != lights.end();
it++) 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; return result;
} }

View File

@ -7,4 +7,5 @@ Material::Material()
{ {
m_diffuse_color = Color::white; m_diffuse_color = Color::white;
m_specular_color = Color::white; m_specular_color = Color::white;
m_shininess = 50;
} }

View File

@ -24,9 +24,13 @@ class Material
} }
const Color & getSpecularColor() const { return m_specular_color; } const Color & getSpecularColor() const { return m_specular_color; }
void setShininess(double shininess) { m_shininess = shininess; }
double getShininess() const { return m_shininess; }
protected: protected:
Color m_diffuse_color; Color m_diffuse_color;
Color m_specular_color; Color m_specular_color;
double m_shininess;
}; };
#endif #endif

View File

@ -12,6 +12,7 @@
#include "util/Color.h" #include "util/Color.h"
#include "shapes/Shape.h" #include "shapes/Shape.h"
#include "PointLight.h" #include "PointLight.h"
#include "Lighting.h"
using namespace std; using namespace std;
Scene::Scene(const map<string, const char *> & options, Scene::Scene(const map<string, const char *> & 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 */ /* 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) pixel[BMP_RED] = (unsigned char)
(0xFF * finalColor.r / m_multisample_level_squared); (0xFF * finalColor.r / m_multisample_level_squared);
pixel[BMP_GREEN] = (unsigned char) pixel[BMP_GREEN] = (unsigned char)
@ -169,7 +172,18 @@ Color Scene::traceRay(const Ray & ray)
it++) it++)
{ {
/* compute the Phong lighting for each hit */ /* 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; return color;

View File

@ -46,6 +46,7 @@ class Scene
std::string m_output_file_name; std::string m_output_file_name;
bool m_verbose; bool m_verbose;
double m_vfov; double m_vfov;
Color m_ambient_light;
/* private data */ /* private data */
std::vector<Shape *> m_shapes; std::vector<Shape *> m_shapes;

View File

@ -4,4 +4,5 @@
Shape::Shape() Shape::Shape()
{ {
m_transparency = 0.0; m_transparency = 0.0;
m_material = NULL;
} }

View File

@ -6,6 +6,7 @@
#include "util/Ray.h" #include "util/Ray.h"
#include "util/Vector.h" #include "util/Vector.h"
#include "util/Transform.h" #include "util/Transform.h"
#include "main/Material.h"
#include <vector> #include <vector>
class Shape class Shape
@ -23,13 +24,18 @@ class Shape
m_inverse = t.getInverse(); m_inverse = t.getInverse();
} }
Transform & getTransform() { return m_transform; } Transform & getTransform() { return m_transform; }
void setTransparency(double t) { m_transparency = t; } void setTransparency(double t) { m_transparency = t; }
double getTransparency() const { return m_transparency; } double getTransparency() const { return m_transparency; }
void setMaterial(Material * material) { m_material = material; }
Material * getMaterial() const { return m_material; }
protected: protected:
Transform m_transform; Transform m_transform;
Transform m_inverse; Transform m_inverse;
double m_transparency; double m_transparency;
Material * m_material;
}; };
#include "Sphere.h" #include "Sphere.h"

View File

@ -19,12 +19,21 @@ Color::Color(double r, double g, double b)
this->b = 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); 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); 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); 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;
}

View File

@ -2,6 +2,8 @@
#ifndef COLOR_H #ifndef COLOR_H
#define COLOR_H COLOR_H #define COLOR_H COLOR_H
#include <iostream>
class Color class Color
{ {
public: public:
@ -10,8 +12,9 @@ class Color
Color(); Color();
Color(double r, double g, double b); Color(double r, double g, double b);
Color operator*(double scale); Color operator*(const Color & other) const;
Color operator/(double scale); Color operator*(double scale) const;
Color operator/(double scale) const;
Color & operator+=(const Color & other); Color & operator+=(const Color & other);
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);
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 #endif