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:
parent
08a0047ba8
commit
f087488b39
@ -2,9 +2,10 @@
|
||||
#include "Light.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_color = color;
|
||||
}
|
||||
|
||||
|
18
main/Light.h
18
main/Light.h
@ -8,13 +8,25 @@
|
||||
class Light
|
||||
{
|
||||
public:
|
||||
Light(const Vector & position, const Color & color = Color::white);
|
||||
Light(const Vector & 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:
|
||||
Vector m_position;
|
||||
Color m_color;
|
||||
Color m_diffuse_color;
|
||||
Color m_specular_color;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -1,7 +1,7 @@
|
||||
|
||||
#include "PointLight.h"
|
||||
|
||||
PointLight::PointLight(const Vector & position, const Color & color)
|
||||
: Light(position, color)
|
||||
PointLight::PointLight(const Vector & position)
|
||||
: Light(position)
|
||||
{
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
class PointLight : public Light
|
||||
{
|
||||
public:
|
||||
PointLight(const Vector & position, const Color & color = Color::white);
|
||||
PointLight(const Vector & position);
|
||||
|
||||
protected:
|
||||
};
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <functional> /* binary_function */
|
||||
#include <math.h>
|
||||
#include "BMP.h"
|
||||
#include "util/Color.h"
|
||||
#include "shapes/Shape.h"
|
||||
#include "PointLight.h"
|
||||
using namespace std;
|
||||
@ -132,7 +133,7 @@ void Scene::render()
|
||||
void Scene::renderPixel(int x, int y, unsigned char * 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 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));
|
||||
|
||||
Vector color = traceRay(ray);
|
||||
|
||||
red += color[0];
|
||||
green += color[1];
|
||||
blue += color[2];
|
||||
finalColor += traceRay(ray);
|
||||
}
|
||||
}
|
||||
|
||||
/* 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_GREEN] = (unsigned char) (0xFF * green / m_multisample_level_squared);
|
||||
pixel[BMP_BLUE] = (unsigned char) (0xFF * blue / m_multisample_level_squared);
|
||||
pixel[BMP_RED] = (unsigned char)
|
||||
(0xFF * finalColor.r / 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);
|
||||
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;
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include "util/Ray.h"
|
||||
#include "util/Color.h"
|
||||
#include "shapes/Shape.h"
|
||||
#include "Light.h"
|
||||
|
||||
@ -35,7 +36,7 @@ class Scene
|
||||
/* private methods */
|
||||
void load(const char * filename);
|
||||
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);
|
||||
|
||||
/* rendering parameters */
|
||||
|
@ -29,6 +29,20 @@ Color Color::operator/(double 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)
|
||||
{
|
||||
return Color(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b);
|
||||
|
@ -12,6 +12,8 @@ class Color
|
||||
|
||||
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 white;
|
||||
|
Loading…
x
Reference in New Issue
Block a user