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
This commit is contained in:
Josh Holtrop 2009-01-30 01:25:57 +00:00
parent 08a0047ba8
commit f087488b39
8 changed files with 55 additions and 22 deletions

View File

@ -2,9 +2,10 @@
#include "Light.h" #include "Light.h"
#include "util/Vector.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_position = position;
m_color = color;
} }

View File

@ -8,13 +8,25 @@
class Light class Light
{ {
public: public:
Light(const Vector & position, const Color & color = Color::white); Light(const Vector & position);
const Vector & getPosition() const { return m_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: protected:
Vector m_position; Vector m_position;
Color m_color; Color m_diffuse_color;
Color m_specular_color;
}; };
#endif #endif

View File

@ -1,7 +1,7 @@
#include "PointLight.h" #include "PointLight.h"
PointLight::PointLight(const Vector & position, const Color & color) PointLight::PointLight(const Vector & position)
: Light(position, color) : Light(position)
{ {
} }

View File

@ -8,7 +8,7 @@
class PointLight : public Light class PointLight : public Light
{ {
public: public:
PointLight(const Vector & position, const Color & color = Color::white); PointLight(const Vector & position);
protected: protected:
}; };

View File

@ -9,6 +9,7 @@
#include <functional> /* binary_function */ #include <functional> /* binary_function */
#include <math.h> #include <math.h>
#include "BMP.h" #include "BMP.h"
#include "util/Color.h"
#include "shapes/Shape.h" #include "shapes/Shape.h"
#include "PointLight.h" #include "PointLight.h"
using namespace std; using namespace std;
@ -132,7 +133,7 @@ void Scene::render()
void Scene::renderPixel(int x, int y, unsigned char * pixel) void Scene::renderPixel(int x, int y, unsigned char * pixel)
{ {
/* calculate the ray going from the camera through this 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 i = 0; i < m_multisample_level; i++)
{ {
for (int j = 0; j < m_multisample_level; j++) 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)); Ray ray(Vector(0, 0, 0), Vector(rx, m_view_plane_dist, rz));
Vector color = traceRay(ray); finalColor += traceRay(ray);
red += color[0];
green += color[1];
blue += color[2];
} }
} }
/* take the average of all the samples as the final pixel value */ /* 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_RED] = (unsigned char)
pixel[BMP_GREEN] = (unsigned char) (0xFF * green / m_multisample_level_squared); (0xFF * finalColor.r / m_multisample_level_squared);
pixel[BMP_BLUE] = (unsigned char) (0xFF * blue / 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<ShapeDistance> hits = getRayHits(ray); vector<ShapeDistance> hits = getRayHits(ray);
if (hits.size() > 0)
for (vector<ShapeDistance>::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; return color;

View File

@ -7,6 +7,7 @@
#include <vector> #include <vector>
#include <utility> #include <utility>
#include "util/Ray.h" #include "util/Ray.h"
#include "util/Color.h"
#include "shapes/Shape.h" #include "shapes/Shape.h"
#include "Light.h" #include "Light.h"
@ -35,7 +36,7 @@ class Scene
/* private methods */ /* private methods */
void load(const char * filename); void load(const char * filename);
void renderPixel(int x, int y, unsigned char * pixel); void renderPixel(int x, int y, unsigned char * pixel);
Vector traceRay(const Ray & ray); Color traceRay(const Ray & ray);
std::vector<ShapeDistance> getRayHits(const Ray & ray); std::vector<ShapeDistance> getRayHits(const Ray & ray);
/* rendering parameters */ /* rendering parameters */

View File

@ -29,6 +29,20 @@ Color Color::operator/(double scale)
return Color(r / scale, g / scale, b / 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) 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);

View File

@ -12,6 +12,8 @@ class Color
Color operator*(double scale); Color operator*(double scale);
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 black;
static const Color white; static const Color white;