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 committed 2009-01-30 20:53:13 +00:00
1 parent 8677680577
commit 41b4354d52
10 files changed
+73 -10

No files matched your search

-4
View File
@@ -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
+22 -1
View File
@@ -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;
}
+1
View File
@@ -7,4 +7,5 @@ Material::Material()
{
m_diffuse_color = Color::white;
m_specular_color = Color::white;
m_shininess = 50;
}
+4
View File
@@ -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
+15 -1
View File
@@ -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;
+1
View File
@@ -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;