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:
parent
8677680577
commit
41b4354d52
@ -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
|
||||
|
@ -1,5 +1,6 @@
|
||||
|
||||
#include "Lighting.h"
|
||||
#include <math.h> /* pow() */
|
||||
|
||||
Color Lighting::computePhong(const Material & material,
|
||||
const std::vector<Light *> & 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<Light *>::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;
|
||||
}
|
||||
|
@ -7,4 +7,5 @@ Material::Material()
|
||||
{
|
||||
m_diffuse_color = Color::white;
|
||||
m_specular_color = Color::white;
|
||||
m_shininess = 50;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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<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 */
|
||||
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;
|
||||
|
@ -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<Shape *> m_shapes;
|
||||
|
@ -4,4 +4,5 @@
|
||||
Shape::Shape()
|
||||
{
|
||||
m_transparency = 0.0;
|
||||
m_material = NULL;
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include "util/Ray.h"
|
||||
#include "util/Vector.h"
|
||||
#include "util/Transform.h"
|
||||
#include "main/Material.h"
|
||||
#include <vector>
|
||||
|
||||
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"
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
#ifndef COLOR_H
|
||||
#define COLOR_H COLOR_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user