moved a bunch of functionality from Ray.cc and Color.cc into Ray.h and Color.h

git-svn-id: svn://anubis/fart/trunk@308 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2010-09-28 17:16:39 +00:00
parent 3f0d6cd83e
commit 835a0c968f
4 changed files with 137 additions and 152 deletions

View File

@ -10,109 +10,6 @@ const Color Color::yellow = Color(1, 1, 0);
const Color Color::cyan = Color(0, 1, 1); const Color Color::cyan = Color(0, 1, 1);
const Color Color::magenta = Color(1, 0, 1); const Color Color::magenta = Color(1, 0, 1);
Color::Color()
{
r = g = b = 0.0;
}
Color::Color(double r, double g, double b)
{
this->r = r;
this->g = g;
this->b = b;
}
Color::Color(const Vector & v)
{
r = v[0];
g = v[1];
b = v[2];
}
Color::Color(refptr<Vector> v)
{
r = (*v)[0];
g = (*v)[1];
b = (*v)[2];
}
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) const
{
return Color(r / scale, g / scale, b / scale);
}
Color & Color::operator+=(const Color & other)
{
r += other.r;
g += other.g;
b += other.b;
return *this;
}
Color & Color::operator-=(const Color & other)
{
r += other.r;
g += other.g;
b += other.b;
return *this;
}
Color & Color::operator*=(double scale)
{
r *= scale;
g *= scale;
b *= scale;
return *this;
}
Color & Color::operator*=(const Color & other)
{
r *= other.r;
g *= other.g;
b *= other.b;
return *this;
}
Color & Color::operator/=(double scale)
{
r /= scale;
g /= scale;
b /= scale;
return *this;
}
Color & Color::operator/=(const Color & other)
{
r /= other.r;
g /= other.g;
b /= other.b;
return *this;
}
Color operator+(const Color & c1, const Color & c2)
{
return Color(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b);
}
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) std::ostream & operator<<(std::ostream & out, const Color & color)
{ {
out << "[" << color.r << ", " << color.g << ", " << color.b << "]"; out << "[" << color.r << ", " << color.g << ", " << color.b << "]";

View File

@ -11,20 +11,108 @@ class Color
public: public:
double r, g, b; double r, g, b;
Color(); Color()
Color(double r, double g, double b); {
Color(const Vector & v); r = g = b = 0.0;
Color(refptr<Vector> v); }
Color operator*(const Color & other) const; Color(double r, double g, double b)
Color operator*(double scale) const; {
Color operator/(double scale) const; this->r = r;
Color & operator+=(const Color & other); this->g = g;
Color & operator-=(const Color & other); this->b = b;
Color & operator*=(double scale); }
Color & operator*=(const Color & other);
Color & operator/=(double scale); Color(const Vector & v)
Color & operator/=(const Color & other); {
r = v[0];
g = v[1];
b = v[2];
}
Color(refptr<Vector> v)
{
r = (*v)[0];
g = (*v)[1];
b = (*v)[2];
}
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 operator*(double scale) const
{
return Color(r * scale, g * scale, b * scale);
}
Color operator/(double scale) const
{
return Color(r / scale, g / scale, b / scale);
}
Color & operator+=(const Color & other)
{
r += other.r;
g += other.g;
b += other.b;
return *this;
}
Color & operator-=(const Color & other)
{
r += other.r;
g += other.g;
b += other.b;
return *this;
}
Color & operator*=(double scale)
{
r *= scale;
g *= scale;
b *= scale;
return *this;
}
Color & operator*=(const Color & other)
{
r *= other.r;
g *= other.g;
b *= other.b;
return *this;
}
Color & operator/=(double scale)
{
r /= scale;
g /= scale;
b /= scale;
return *this;
}
Color & operator/=(const Color & other)
{
r /= other.r;
g /= other.g;
b /= other.b;
return *this;
}
Color operator+(const Color & c2)
{
return Color(r + c2.r, g + c2.g, b + c2.b);
}
Color operator-(const Color & c2)
{
return Color(r - c2.r, g - c2.g, b - c2.b);
}
static const Color black; static const Color black;
static const Color white; static const Color white;
@ -36,8 +124,6 @@ class Color
static const Color magenta; static const Color magenta;
}; };
Color operator+(const Color & c1, const Color & c2);
Color operator-(const Color & c1, const Color & c2);
static inline Color operator*(double d, const Color & c) { return c * d; } static inline Color operator*(double d, const Color & c) { return c * d; }
static inline Color operator/(double d, const Color & c) { return c / d; } static inline Color operator/(double d, const Color & c) { return c / d; }
std::ostream & operator<<(std::ostream & out, const Color & color); std::ostream & operator<<(std::ostream & out, const Color & color);

View File

@ -5,42 +5,13 @@
#include "Ray.h" #include "Ray.h"
Ray::Ray()
{
}
Ray::Ray(const Vector & origin, const Vector & direction)
{
m_origin = origin;
m_direction = direction;
m_direction.normalize();
}
Ray Ray::randomRay() Ray Ray::randomRay()
{ {
return Ray(Vector(0, 0, 0), Vector::randomVector()); return Ray(Vector(0, 0, 0), Vector::randomVector());
} }
/*
* return a vector for the point at distance dist
* from the ray's origin point, along its direction.
*/
Vector Ray::getPositionAt(double dist) const
{
Vector v;
v[0] = m_origin[0] + dist * m_direction[0];
v[1] = m_origin[1] + dist * m_direction[1];
v[2] = m_origin[2] + dist * m_direction[2];
return v;
}
std::ostream & operator<<(std::ostream & out, const Ray & r) std::ostream & operator<<(std::ostream & out, const Ray & r)
{ {
out << "(" << r.getOrigin() << " -> " << r.getDirection() << ")"; out << "(" << r.getOrigin() << " -> " << r.getDirection() << ")";
return out; return out;
} }
Ray Ray::shift(double amt)
{
return Ray(getPositionAt(amt), m_direction);
}

View File

@ -8,16 +8,47 @@
class Ray class Ray
{ {
public: public:
Ray(); Ray()
Ray(const Vector & origin, const Vector & direction); {
static Ray randomRay(); }
Ray(const Vector & origin, const Vector & direction)
: m_origin(origin), m_direction(direction)
{
m_direction.normalize();
}
/*
* return a vector for the point at distance dist
* from the ray's origin point, along its direction.
*/
Vector getPositionAt(double dist) const
{
Vector v;
v[0] = m_origin[0] + dist * m_direction[0];
v[1] = m_origin[1] + dist * m_direction[1];
v[2] = m_origin[2] + dist * m_direction[2];
return v;
}
Ray shift(double amt)
{
return Ray(getPositionAt(amt), m_direction, false);
}
const Vector & getOrigin() const { return m_origin; } const Vector & getOrigin() const { return m_origin; }
const Vector & getDirection() const { return m_direction; } const Vector & getDirection() const { return m_direction; }
Vector getPositionAt(double dist) const;
static Ray randomRay();
Vector operator[](double dist) const { return getPositionAt(dist); } Vector operator[](double dist) const { return getPositionAt(dist); }
Ray shift(double amt);
protected: protected:
Ray(const Vector & origin, const Vector & direction, bool no_norm)
: m_origin(origin), m_direction(direction)
{
/* no normalize version */
}
Vector m_origin; Vector m_origin;
Vector m_direction; Vector m_direction;
}; };