diff options
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/color.cc | 115 | ||||
-rw-r--r-- | src/math/color.h | 46 | ||||
-rw-r--r-- | src/math/vector3f.cc | 35 | ||||
-rw-r--r-- | src/math/vector3f.h | 14 |
4 files changed, 131 insertions, 79 deletions
diff --git a/src/math/color.cc b/src/math/color.cc index ecdfd2f..7784338 100644 --- a/src/math/color.cc +++ b/src/math/color.cc @@ -11,77 +11,106 @@ namespace math { Color::Color() : - r(_r), g(_g), b(_b), a(_a) + r(rgba_data[0]), + g(rgba_data[1]), + b(rgba_data[2]), + a(rgba_data[3]) { - _r = _g = _b = 0.0f; - _a = 1.0f; + assign(1.0f, 1.0f); } -Color::Color(const float red, const float green , const float blue , const float alpha) : - r(_r), g(_g), b(_b), a(_a) +Color::Color(float red, float green, float blue, float alpha) : + r(rgba_data[0]), + g(rgba_data[1]), + b(rgba_data[2]), + a(rgba_data[3]) { - _r = red; - _g = green; - _b = blue; - _a = alpha; + assign(red, green, blue, alpha); } Color::Color(const float grey, const float alpha) : - r(_r), g(_g), b(_b), a(_a) + r(rgba_data[0]), + g(rgba_data[1]), + b(rgba_data[2]), + a(rgba_data[3]) { - _r = _g = _b = grey; - _a = alpha; + assign(grey, alpha); } -Color::Color(const Color &other) : - r(_r), g(_g), b(_b), a(_a) +Color::Color(Color const &other) : + r(rgba_data[0]), + g(rgba_data[1]), + b(rgba_data[2]), + a(rgba_data[3]) { - this->operator=(other); + assign(other); } -const Color & Color::operator=(const Color &other) + +void Color::assign(float red, float green, float blue, float alpha) { - this->_r = other._r; - this->_g = other._g; - this->_b = other._b; - this->_a = other._a; - return (*this); + rgba_data[0] = red; + rgba_data[1] = green; + rgba_data[2] = blue; + rgba_data[3] = alpha; } -void Color::normalize() +void Color::assign(Color const & other) { - float tmp = _r; + memcpy(rgba_data, other.rgba_data, sizeof(rgba_data)); +} - if (_g > tmp) - tmp = _g; - if (_b > tmp) - tmp = _b; +void Color::assign(float grey, float alpha) +{ + rgba_data[0] = rgba_data[1] = rgba_data[2] = grey; + rgba_data[3] = alpha; +} + +const Color & Color::operator=(Color const & other) +{ + assign(other); + return *this; +} + +void Color::clamp() +{ + for (int i =0; i < 4; i++) + if (rgba_data[i] < 0) + rgba_data[i] = 0; + + float tmp = rgba_data[0]; + if (rgba_data[1] > tmp) + tmp = rgba_data[1]; + if (rgba_data[2] > tmp) + tmp = rgba_data[2]; if (tmp > 1) { - _r /= tmp; - _g /= tmp; - _b /= tmp; + for (int i =0; i < 3; i++) + rgba_data[i] /= tmp; } + + if (rgba_data[3] > 1) + rgba_data[3] = 1; } float Color::red() const { - return _r; + return rgba_data[0]; } float Color::green() const { - return _g; + return rgba_data[1]; } float Color::blue() const { - return _b; + return rgba_data[2]; } float Color::alpha() const { - return _a; + return rgba_data[3]; } Color Color::operator*(float scalar) const @@ -89,25 +118,23 @@ Color Color::operator*(float scalar) const return Color(red()*scalar, green()*scalar, blue()*scalar, alpha()); } -Color operator*(float scalar, const Color& color) +Color const operator*(float scalar, Color const & color) { return color * scalar; } -std::ostream &operator<<(std::ostream &os, const Color &c) +std::ostream &operator<<(std::ostream &os, Color const & color) { - os << c.red() << " " << c.green() << " " << c.blue(); // << " " << c.alpha(); + os << color.red() << " " << color.green() << " " << color.blue(); // << " " << c.alpha(); return os; } std::istream &operator>>(std::istream & is, Color & color) { - float r, g, b, a; - is >> r; - is >> g; - is >> b; - //is >> a; - a = 1.0; - color = Color(r,g,b,a); + is >> color.r; + is >> color.g; + is >> color.b; + //is >> color.a; + color.a = 1.0; return (is); } diff --git a/src/math/color.h b/src/math/color.h index 51b415f..5684770 100644 --- a/src/math/color.h +++ b/src/math/color.h @@ -16,43 +16,55 @@ namespace math class Color { public: + /// create the default color, white Color(); - Color(const float, float const, const float, const float=1.0f); - Color(const float, const float=1.0f); - Color(const Color &); + /// create a color from float RGBA value + Color(float red, float green, float blue, float alpha=1.0f); + /// create a greyscale color + Color(const float grey, const float=1.0f); + /// create a copy from an existing color + Color(Color const & other); + /// red channel value float red() const; + /// green channel value float green() const; + /// blue channel value float blue() const; + /// alpha channel value float alpha() const; - const Color &operator=(const Color &); + /// assignment + void assign(Color const & other); + /// assignment + void assign(float red, float green, float blue, float alpha=1.0f); + /// assignment + void assign(float grey, float alpha=1.0f); + /// assignment operator + Color const &operator=(Color const & other); + /// multiply rgb values with scalar value. Color operator*(const float scalar) const; - // Some default colors - static const Color Black() { return Color(0.0f); }; - static const Color White() { return Color(1.0f); }; - static const Color Red() { return Color(1.0f,0.0f,0.0f); }; - static const Color Green() { return Color(0.0f,1.0f,0.0f); }; - static const Color Blue() { return Color(0.0f, 0.0f, 1.0f); }; - static const Color Yellow() { return Color(1.0f, 1.0f, 0.0f); }; - + /// clam color values to the 0-1 range + void clamp(); + float &r; float &g; float &b; float &a; -private: - void normalize(); - float _r, _g, _b, _a; + float rgba_data[4]; }; -std::ostream &operator<<(std::ostream &os, const Color &c); +/// write color RGB values to stream +std::ostream &operator<<(std::ostream &os, Color const & color); +/// read color RGB values from stream, alpha is set to 1.0 std::istream &operator>>(std::istream & is, Color & color); -Color operator*(const float scalar, const Color& color); +/// scalar-with-color multiplication +Color const operator*(float scalar, Color const & color); } // namespace math diff --git a/src/math/vector3f.cc b/src/math/vector3f.cc index a9e7e00..3d8e5ac 100644 --- a/src/math/vector3f.cc +++ b/src/math/vector3f.cc @@ -16,33 +16,40 @@ namespace math Vector3f::Vector3f() : x(coord[0]), y(coord[1]), z(coord[2]) { - for (int i=0; i < 3; i++) - coord[i] = 0; + assign(0,0,0); } Vector3f::Vector3f(const Vector3f &other) : x(coord[0]), y(coord[1]), z(coord[2]) { - for (int i=0; i < 3; i++) - coord[i] = other.coord[i]; + assign(other); } -Vector3f::Vector3f(const float xv, const float yv, const float zv) : +Vector3f::Vector3f(const float vx, const float vy, const float vz) : x(coord[0]), y(coord[1]), z(coord[2]) { - coord[0] = xv; - coord[1] = yv; - coord[2] = zv; + assign(vx, vy, vz); } Vector3f::~Vector3f() { } +void Vector3f::assign(const float vx, const float vy, const float vz) +{ + coord[0] = vx; + coord[1] = vy; + coord[2] = vz; +} + +void Vector3f::assign(Vector3f const & other) +{ + memcpy(coord, other.coord, sizeof(coord)); +} + Vector3f & Vector3f::operator=(const Vector3f & other) { - for (int i=0; i < 3; i++) - coord[i] = other.coord[i]; + assign(other); return (*this); } @@ -156,11 +163,11 @@ std::istream &operator>>(std::istream & is, Vector3f & vector) const Vector3f crossproduct(Vector3f const & first, Vector3f const& second) { - float x = first[1]*second[2] - first[2]*second[1]; - float y = first[2]*second[0] - first[0]*second[2]; - float z = first[0]*second[1] - first[1]*second[0]; + float vx = first[1]*second[2] - first[2]*second[1]; + float vy = first[2]*second[0] - first[0]*second[2]; + float vz = first[0]*second[1] - first[1]*second[0]; - return(Vector3f(x,y,z)); + return(Vector3f(vx,vy,vz)); } float dotproduct(const Vector3f& first, const Vector3f& second) diff --git a/src/math/vector3f.h b/src/math/vector3f.h index 20e47f2..e94f4c7 100644 --- a/src/math/vector3f.h +++ b/src/math/vector3f.h @@ -34,16 +34,22 @@ public: Vector3f(const Vector3f &other); /// Create a Vector3f with given coordinates - /** @param xv the x-coordinate of the location - * @param yv the y-coordinate of the location - * @param zv the z-coordinate of the location + /** @param x the x-coordinate of the location + * @param y the y-coordinate of the location + * @param z the z-coordinate of the location */ - Vector3f(const float xv, const float yv, const float zv); + Vector3f(const float x, const float y, const float z); /// Destructor ~Vector3f(); /* -- Assignment operators -- */ + /// assignment + void assign(const float x, const float y, const float z); + + /// assignment + void assign(Vector3f const & other); + /// assignment operator Vector3f& operator=(const Vector3f &other); |