Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/Makefile.am8
-rw-r--r--src/common/color.cc84
-rw-r--r--src/common/color.h50
-rw-r--r--src/common/functions.cc48
-rw-r--r--src/common/functions.h42
-rw-r--r--src/common/osirion.h11
-rw-r--r--src/common/vector3f.cc151
-rw-r--r--src/common/vector3f.h138
8 files changed, 532 insertions, 0 deletions
diff --git a/src/common/Makefile.am b/src/common/Makefile.am
new file mode 100644
index 0000000..4f556bf
--- /dev/null
+++ b/src/common/Makefile.am
@@ -0,0 +1,8 @@
+
+METASOURCES = AUTO
+libcommon_la_LDFLAGS = -avoid-version -no-undefined
+noinst_LTLIBRARIES = libcommon.la
+libcommon_la_SOURCES = color.cc color.h functions.cc functions.h osirion.h\
+ vector3f.cc vector3f.h
+
+
diff --git a/src/common/color.cc b/src/common/color.cc
new file mode 100644
index 0000000..85892c8
--- /dev/null
+++ b/src/common/color.cc
@@ -0,0 +1,84 @@
+/*
+ ***************************************************************************
+ * Copyright (C) 2002-2004 by Stijn Buys *
+ * stijn.buys@pandora.be *
+ * *
+ * This software is redistributed under the terms of the *
+ * GNU General Public License. Please read LICENSE.txt. *
+ ***************************************************************************
+*/
+
+#include "color.h"
+
+Color::Color() {
+ _r = _g = _b = 0.0f;
+ _a = 1.0f;
+}
+
+Color::Color(const float red, const float green , const float blue , const float alpha) {
+ _r = red;
+ _g = green;
+ _b = blue;
+ _a = alpha;
+}
+
+Color::Color(const float grey, const float alpha) {
+ _r = _g = _b = grey;
+ _a = alpha;
+}
+
+Color::Color(const Color &other) {
+ this->operator=(other);
+}
+
+const Color & Color::operator=(const Color &other) {
+ this->_r = other._r;
+ this->_g = other._g;
+ this->_b = other._b;
+ this->_a = other._a;
+ return (*this);
+}
+
+void Color::normalize() {
+ float tmp = _r;
+
+ if (_g > tmp)
+ tmp = _g;
+ if ( _b > tmp)
+ tmp = _b;
+
+ if (tmp > 1) {
+ _r /= tmp;
+ _g /= tmp;
+ _b /= tmp;
+ }
+}
+
+float Color::red() const {
+ return _r;
+}
+
+float Color::green() const {
+ return _g;
+}
+
+float Color::blue() const {
+ return _b;
+}
+
+float Color::alpha() const {
+ return _a;
+}
+
+Color Color::operator*(float scalar) const {
+ return Color(red()*scalar, green()*scalar, blue()*scalar, alpha());
+}
+
+Color operator*(float scalar, const Color& color) {
+ return color * scalar;
+}
+std::ostream &operator<<(std::ostream &os, const Color &c) {
+ os << c.red() << " " << c.green() << " " << c.blue() << " " << c.alpha();
+ return os;
+}
+
diff --git a/src/common/color.h b/src/common/color.h
new file mode 100644
index 0000000..d84d1a3
--- /dev/null
+++ b/src/common/color.h
@@ -0,0 +1,50 @@
+/*
+ ***************************************************************************
+ * Copyright (C) 2004 by Stijn Buys *
+ * stijn.buys@pandora.be *
+ * *
+ * This software is redistributed under the terms of the *
+ * GNU General Public License. Please read LICENSE.txt. *
+ ***************************************************************************
+*/
+
+#ifndef __COLOR_HEADER__
+#define __COLOR_HEADER__
+
+#include <iostream>
+
+/// a class representing an RGBA color value
+class Color {
+ public:
+ Color();
+ Color(const float, float const, const float, const float=1.0f);
+ Color(const float, const float=1.0f);
+ Color(const Color &);
+
+ float red() const;
+ float green() const;
+ float blue() const;
+ float alpha() const;
+
+ const Color &operator=(const Color &);
+
+ 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); };
+
+ private:
+ void normalize();
+ float _r, _g, _b, _a;
+};
+
+std::ostream &operator<<(std::ostream &os, const Color &c);
+
+Color operator*(const float scalar, const Color& color);
+
+#endif // ___HEADER__
diff --git a/src/common/functions.cc b/src/common/functions.cc
new file mode 100644
index 0000000..7152c91
--- /dev/null
+++ b/src/common/functions.cc
@@ -0,0 +1,48 @@
+/* functions.cc
+ * This file is part of the Osirion project
+ */
+
+#include "functions.h"
+
+float min(float a, float b) {
+ return (a < b ? a : b);
+}
+
+float max(float a, float b) {
+ return (a > b ? a : b);
+}
+
+int min(int a, int b) {
+ return (a < b ? a : b);
+}
+
+int max(int a, int b) {
+ return (a > b ? a : b);
+}
+
+float randomf(const float max) {
+ return ((float) rand() / (float) RAND_MAX) * max;
+}
+
+unsigned randomi(const unsigned int max) {
+ return ((unsigned int)(rand() % max));
+}
+
+float degreesf(float angle) {
+ float r = angle;
+ while (r <= -180.0f)
+ r += 360.0f;
+ while (r >= 180.0f)
+ r -= 360.0f;
+ return r;
+}
+
+float sgnf(float value)
+{
+ if (value < 0 )
+ return -1;
+ else if (value == 0 )
+ return 0;
+
+ return 1;
+}
diff --git a/src/common/functions.h b/src/common/functions.h
new file mode 100644
index 0000000..02f25da
--- /dev/null
+++ b/src/common/functions.h
@@ -0,0 +1,42 @@
+/* functions.h
+ This file is part of the Osirion project
+*/
+
+#ifndef __INCLUDED_FUNCTIONS_H__
+#define __INCLUDED_FUNCTIONS_H__
+
+#include <cstdlib>
+#include <cmath>
+
+/// return the smallest of two float values
+float min(float a, float b);
+
+/// return the smallest of two integers
+int min(int a, int b);
+
+/// return the largest of two float values
+float max(float a, float b);
+
+/// return the largest of two integers
+int max(int a, int b);
+
+/// returns a random float
+/*! The value returned will be in the interval [0-max]
+ * @param max the maximum value returned
+**/
+float randomf(const float max = 1);
+
+/// returns a random integer
+/*! The value returned will be in the interval [0-(max-1)]
+ * @param max the maximum value returned
+**/
+unsigned int randomi(const unsigned int max);
+
+/// return the sign of a float value
+float sgnf(float value);
+
+/// return an angle in the ]-180,180] range
+float degreesf(float angle);
+
+#endif // __INCLUDED_FUNCTIONS_H__
+
diff --git a/src/common/osirion.h b/src/common/osirion.h
new file mode 100644
index 0000000..56d8b4e
--- /dev/null
+++ b/src/common/osirion.h
@@ -0,0 +1,11 @@
+/* osirion.h
+ This file is part of the Osirion project
+*/
+
+#ifndef __INCLUDED_OSIRION_H__
+#define __INCLUDED_OSIRION_H__
+
+#define OSIRION_VERSION "0.0.1"
+#define GAMESCALE 0.2f
+
+#endif // __INCLUDED_OSIRION_H__
diff --git a/src/common/vector3f.cc b/src/common/vector3f.cc
new file mode 100644
index 0000000..3e469b6
--- /dev/null
+++ b/src/common/vector3f.cc
@@ -0,0 +1,151 @@
+/* vector3f.h
+ This file is part of the Osirion project
+*/
+#include "vector3f.h"
+#include <cmath>
+
+Vector3f::Vector3f() :
+ x(coord[0]), y(coord[1]), z(coord[2])
+{
+ for (int i=0; i < 3; i++)
+ coord[i] = 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];
+}
+
+Vector3f::Vector3f(const float xv, const float yv, const float zv) :
+ x(coord[0]), y(coord[1]), z(coord[2])
+{
+ coord[0] = xv;
+ coord[1] = yv;
+ coord[2] = zv;
+}
+
+Vector3f::~Vector3f()
+{
+}
+
+
+Vector3f & Vector3f::operator=(const Vector3f & other)
+{
+ for (int i=0; i < 3; i++)
+ coord[i] = other.coord[i];
+ return (*this);
+}
+
+
+Vector3f & Vector3f::operator*=(const float scalar)
+{
+ for (int i=0; i < 3; i++)
+ coord[i] *= scalar;
+ return (*this);
+}
+
+
+Vector3f & Vector3f::operator/=(const float scalar) {
+ for (int i=0; i < 3; i++)
+ coord[i] /= scalar;
+ return (*this);
+}
+
+
+Vector3f &Vector3f::operator-=(const Vector3f & other) {
+ for (int i=0; i < 3; i++)
+ coord[i] -= other[i];
+ return (*this);
+}
+
+
+Vector3f &Vector3f::operator+=(const Vector3f &other) {
+ for (int i=0; i < 3; i++)
+ coord[i] += other[i];
+ return (*this);
+}
+
+
+Vector3f Vector3f::operator*(const float scalar) const {
+ Vector3f r(*this);
+ for (int i=0; i < 3; i++)
+ r.coord[i] *= scalar;
+ return (r);
+ }
+
+
+Vector3f Vector3f::operator/(const float scalar) const {
+ Vector3f r(*this);
+ for (int i=0; i < 3; i++)
+ r.coord[i] /= scalar;
+ return (r);
+}
+
+
+Vector3f Vector3f::operator-(const Vector3f& other) const {
+ Vector3f r(*this);
+ for (int i=0; i < 3; i++)
+ r.coord[i] -= other.coord[i];
+ return (r);
+}
+
+
+Vector3f Vector3f::operator+(const Vector3f& other) const {
+ Vector3f r(*this);
+ for (int i=0; i < 3; i++)
+ r.coord[i] += other.coord[i];
+ return (r);
+}
+
+
+float Vector3f::operator*(const Vector3f& other) const {
+ float r = 0;
+ for (int i=0; i < 3; i++)
+ r += coord[i] * other.coord[i];
+ return (r);
+}
+
+
+bool Vector3f::operator==(const Vector3f& other) const {
+ for (int i=0; i < 3; i++)
+ if (coord[i] != other.coord[i])
+ return (false);
+ return (true);
+}
+
+float Vector3f::lengthsquared() const {
+ double r = 0;
+ for (int i=0; i < 3; i++)
+ r += coord[i]*coord[i];
+ return ((float) r);
+}
+
+float Vector3f::length() const {
+ double r = 0;
+ for (int i=0; i < 3; i++)
+ r += coord[i]*coord[i];
+
+ return ((float) sqrt(r));
+}
+
+void Vector3f::normalize() {
+ (*this) /= this->length();
+}
+
+std::ostream &operator<<(std::ostream & os, const Vector3f & vector) {
+ os << vector[0] << " " << vector[1] << " " << vector[2];
+ return os;
+}
+
+std::istream &operator>>(std::istream & is, Vector3f & vector) {
+ for (int i=0; i < 3; i++)
+ is >> vector[i];
+ return is;
+}
+
+Vector3f operator*(float scalar, const Vector3f& vector) {
+ return vector * scalar;
+}
diff --git a/src/common/vector3f.h b/src/common/vector3f.h
new file mode 100644
index 0000000..53b2ce7
--- /dev/null
+++ b/src/common/vector3f.h
@@ -0,0 +1,138 @@
+/* vector3f.h
+ This file is part of the Osirion project
+*/
+
+ #ifndef __INCLUDED_VECTOR3F_H__
+ #define __INCLUDED_VECTOR3F_H__
+
+ #include <iostream>
+/// A point or vector in 3D-space
+/*!
+ An instance of this class represents a point in 3D-space or a 3D-vector
+ and forms the basic building block for all spatial calculations.
+ */
+class Vector3f {
+
+public:
+ /// Default constructor, creates a Vector3f (0,0,0)
+ Vector3f();
+
+ /// Copy constructor
+ /*! Create a new Vector3f that is a copy from an other
+ * @param other the vector to copy values from
+ */
+ Vector3f(const Vector3f &other);
+
+ /// Create a Vector3f with given coordinates
+ /*! Create a new vector at a given position in a 3D-space
+ * @param xv the x-coordinate of the location
+ * @param yv the y-coordinate of the location
+ * @param zv the z-coordinate of the location
+ */
+ Vector3f(const float xv, const float yv, const float zv);
+
+ /// Destructor
+ ~Vector3f();
+
+ /* -- Assignment operators -- */
+ /// assignment operator
+ Vector3f& operator=(const Vector3f &other);
+
+ /// multiplicate each element of the vector with a given value
+ /// @param scalar multiplication factor
+ Vector3f& operator*=(const float scalar);
+
+ /// divide each element of the vector by a given value
+ /// @param scalar divider
+ Vector3f& operator/=(const float scalar);
+
+ /// perform an element-wise subtraction
+ Vector3f& operator-=(const Vector3f &other);
+
+ /// perform ann element-wise addition
+ Vector3f& operator+=(const Vector3f &other);
+
+ /* -- Mathematical operators -- */
+
+ /// return this Vector multiplied with scalar
+ Vector3f operator*(const float scalar) const;
+
+ /// return this vector divided by a scalar
+ Vector3f operator/(const float scalar) const;
+
+ /// return the element-wise difference between two vectors
+ Vector3f operator-(const Vector3f &other) const;
+
+ /// return the element-wise sumn of two vectors
+ Vector3f operator+(const Vector3f &other) const;
+
+ /// return the vector cross-product
+ float operator*(const Vector3f &other) const;
+
+ /// comparison operator
+ bool operator==(const Vector3f &other) const;
+
+ /// assign a value to an element of this vector
+ /*! WARNING: range is not checked
+ * @param index the index of the element to assign to ( 0 <= index < 3 )
+ */
+ inline float& operator[](const unsigned int index) {
+ return coord[index];
+ }
+
+ /// returns the value of an element of this vector
+ /*! WARNING: range is not checked
+ * @param index the index of the element to return ( 0 <= index < 3 )
+ */
+ inline float operator[](const unsigned int index) const {
+ return coord[index];
+ }
+
+
+ float &x;
+ float &y;
+ float &z;
+
+ /// Return the cartesian length of this vector
+ float length() const;
+
+ /// Return the cartesian length squared (to speed up calculations)
+ float lengthsquared() const;
+
+ /// Divide this Vector by it's length
+ /// @see normalized()
+ /// WARNING: vector must not be (0, 0, 0)
+ void normalize();
+
+ /* static functions */
+
+ /// Returns the unity vector on the X-axis
+ static inline Vector3f Xaxis() { return Vector3f(1.0f, 0.0f, 0.0f); }
+
+ /// Returns the unity vector on the Y-axis
+ static inline Vector3f Yaxis() { return Vector3f(0.0f, 1.0f, 0.0f); }
+
+ /// Returns the unity vector on the Z-axis
+ static inline Vector3f Zaxis() { return Vector3f(0.0f, 0.0f, 1.0f); }
+
+ /// Return the cartesian length of a vector
+ static inline float length(const Vector3f& vector) { return vector.length(); }
+
+ /// Return a vector divided by it's length
+ /// @see normalize()
+ /// WARNING: vector must not be (0, 0, 0)
+ static inline Vector3f normalized(const Vector3f& vector) { return (vector / vector.length()); }
+
+ float coord[3];
+};
+
+/// Write a Vector3f to a std::ostream
+std::ostream &operator<<(std::ostream & os, const Vector3f & vector);
+
+/// Read a Vector3d from a std::istream
+std::istream &operator>>(std::istream & is, Vector3f& vector);
+
+/// scalar*Vector3f operators
+Vector3f operator*(float scalar, const Vector3f& vector);
+
+#endif // __INCLUDED_VECTOR3F_H__