1
0
Fork 0

Initial commit

This commit is contained in:
Henrik Hautakoski 2015-12-22 17:43:51 +01:00
commit edfc5298e1
252 changed files with 93965 additions and 0 deletions

128
source/Math/Color.cpp Normal file
View file

@ -0,0 +1,128 @@
#include <Spectre/Math/Color.h>
const Color Color::Black (0 , 0 , 0 , 255);
const Color Color::White (255, 255, 255, 255);
const Color Color::Red (255, 0 , 0 , 255);
const Color Color::Green (0 , 255, 0 , 255);
const Color Color::Blue (0 , 0 , 255, 255);
const Color Color::Transparant (0 , 0 , 0 , 0 );
Color::Color(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha)
{
r = red; g = green; b = blue; a = alpha;
}
Color::Color(const Color& color)
{
r = color.r; g = color.g; b = color.b; a = color.a;
}
Color& Color::operator=(const Color& color)
{
if (*this != color) {
r = color.r;
g = color.g;
b = color.b;
a = color.a;
}
return *this;
}
Vector3f Color::toRGBf() const
{
Vector3f v(r, g, b);
return v / 255.0f;
}
Vector4f Color::toRGBAf() const
{
Vector4f v(r, g, b, a);
return v / 255.0f;
}
// ---------
// Compare
// ---------
bool operator ==(const Color& c1, const Color& c2)
{
return c1.r == c2.r && c1.g == c2.g && c1.b == c2.b && c1.a == c2.b;
}
bool operator !=(const Color& c1, const Color& c2)
{
return !(c1.r == c2.r && c1.g == c2.g && c1.b == c2.b && c1.a == c2.b);
}
// ------------
// Arithmetic
// ------------
Color operator +(const Color& c1, const Color& c2)
{
return Color(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b, c1.a + c2.a);
}
Color operator -(const Color& c1, const Color& c2)
{
return Color(c1.r - c2.r, c1.g - c2.g, c1.b - c2.b, c1.a - c2.a);
}
Color operator /(const Color& c1, const Color& c2)
{
return Color(c1.r / c2.r, c1.g / c2.g, c1.b / c2.b, c1.a / c2.a);
}
Color operator *(const Color& c1, const Color& c2)
{
return Color(c1.r * c2.r, c1.g * c2.g, c1.b * c2.b, c1.a * c2.a);
}
Color& operator +=(Color& c1, const Color& c2)
{
c1.r += c2.r; c1.g += c2.g; c1.b += c2.b; c1.a += c2.a;
return c1;
}
Color& operator -=(Color& c1, const Color& c2)
{
c1.r -= c2.r; c1.g -= c2.g; c1.b -= c2.b; c1.a -= c2.a;
return c1;
}
Color& operator /=(Color& c1, const Color& c2)
{
c1.r /= c2.r; c1.g /= c2.g; c1.b /= c2.b; c1.a /= c2.a;
return c1;
}
Color& operator *=(Color& c1, const Color& c2)
{
c1.r *= c2.r; c1.g *= c2.g; c1.b *= c2.b; c1.a *= c2.a;
return c1;
}
// -------------------
// Scalar arithmetic
// -------------------
Color operator +(const Color& c, unsigned int s)
{
return Color(c.r + s, c.g + s, c.b + s, c.a + s);
}
Color operator -(const Color& c, unsigned int s)
{
return Color(c.r - s, c.g - s, c.b - s, c.a - s);
}
Color operator /(const Color& c, unsigned int s)
{
return Color(c.r / s, c.g / s, c.b / s, c.a / s);
}
Color operator *(const Color& c, unsigned int s)
{
return Color(c.r * s, c.g * s, c.b * s, c.a * s);
}

20
source/Math/Logarithm.cpp Normal file
View file

@ -0,0 +1,20 @@
// Logarithmic functions.
#include <cstdlib>
#include <Spectre/Math/Math.h>
namespace math {
#define LOG2INBASE10 0.30102999566f
double log(double base, double value) {
return log10(value) / log10(base);
}
double log2(double value) {
return log10(value) / LOG2INBASE10;
}
}; // namespace math

99
source/Math/Math.cpp Normal file
View file

@ -0,0 +1,99 @@
#include <cstdlib>
#include <Spectre/Math/Math.h>
namespace math {
float rand(float min, float max) {
float r = ((float) ::rand()) / RAND_MAX;
return ((max - min) * r) + min;
}
float deg2rad(float deg) {
return deg * 3.141592654f / 180.0f;
}
unsigned int nextPowerOfTwo(unsigned int value) {
if (value == 0) {
return 1;
}
unsigned int exp = static_cast<unsigned int>(::ceil(log2(value)));
return ::pow(2.0f, exp);
}
// left, right, bottom, top, near, far.
Matrix4f orthoProjection(float left, float right,
float bottom, float top,
float zNear, float zFar) {
float rl = (right - left),
tb = (top - bottom),
fn = (zFar - zNear),
rl2 = 2.0f / rl,
tb2 = 2.0f / tb,
fn2 = 2.0f / fn,
x = (right + left) / rl,
y = (top + bottom) / tb,
z = (zFar + zNear) / fn;
return Matrix4f(
rl2 , 0.0f, 0.0f, -x,
0.0f, tb2 , 0.0f, -y,
0.0f, 0.0f, -fn2, -z,
0.0f, 0.0f, 0.0f, 1.0f
);
}
Matrix4f rotation(float theta) {
float r = deg2rad(theta);
float s = std::sin(r);
float c = std::cos(r);
return Matrix4f(
c, -s, 0.0f, 0.0f,
s, c, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
Matrix4f translate(const Vector2f& v) {
return Matrix4f(
1.0f, 0.0f, 0.0f, v.x,
0.0f, 1.0f, 0.0f, v.y,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
Matrix4f scale(const Vector2f& f) {
return Matrix4f(
f.x , 0.0f, 0.0f, 0.0f,
0.0f, f.y , 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
Vector3f getTranslate(const Matrix4f matrix) {
return Vector3f(matrix.e[12], matrix.e[13], matrix.e[14]);
}
Vector3f getUpVector(const Matrix4f matrix) {
return Vector3f(matrix.e[4], matrix.e[5], matrix.e[6]);
}
Vector3f getForwardVector(const Matrix4f matrix) {
return Vector3f(matrix.e[8], matrix.e[9], matrix.e[10]);
}
};

158
source/Math/Transform.cpp Normal file
View file

@ -0,0 +1,158 @@
#include <Spectre/Math/Math.h>
#include <Spectre/Math/Transform.h>
Transform::Transform() :
m_matrix (Matrix4f::Identity)
{
}
Transform::Transform( float a00, float a01, float a02,
float a10, float a11, float a12,
float a20, float a21, float a22) :
m_matrix (a00, a01, 0.0f, a02,
a10, a11, 0.0f, a12,
0.0f, 0.0f, 1.0f, 0.0f,
a20, a21, 0.0f, a22)
{
}
Transform::Transform(const Matrix4f& matrix) :
m_matrix (matrix)
{
}
void Transform::set(Vector2f _translate, float _rotate, Vector2f _scale)
{
reset();
translate(_translate).rotate(_rotate).scale(_scale);
}
void Transform::setMatrix(const Matrix4f& matrix)
{
m_matrix = matrix;
}
void Transform::reset()
{
m_matrix = Matrix4f::Identity;
}
Vector2f Transform::getUpVector() const
{
return Vector2f(m_matrix.e[4], m_matrix.e[5]).normalize();
}
Vector2f Transform::getRightVector() const
{
return Vector2f(m_matrix.e[0], m_matrix.e[1]).normalize();
}
Transform& Transform::translate(float x, float y)
{
Transform t( 1, 0, x,
0, 1, y,
0, 0, 1);
return multiply(t);
}
Transform& Transform::translate(Vector2f vec)
{
return translate(vec.x, vec.y);
}
Transform& Transform::rotate(float theta)
{
float r = math::deg2rad(theta);
float c = std::cos(r);
float s = std::sin(r);
// Always rotate along z-axis in 2D.
Transform rot( c, -s, 0,
s, c, 0,
0, 0, 1);
return multiply(rot);
}
Transform& Transform::scale(float x, float y)
{
Transform s( x, 0, 0,
0, y, 0,
0, 0, 1);
return multiply(s);
}
Transform& Transform::scale(Vector2f vec)
{
return scale(vec.x, vec.y);
}
Transform& Transform::scale(float s)
{
return scale(s, s);
}
Transform& Transform::multiply(const Transform& other)
{
m_matrix *= other.m_matrix;
return *this;
}
Vector2f Transform::transformPoint(float x, float y) const
{
return Vector2f(m_matrix.e[0] * x + m_matrix.e[4] * y + m_matrix.e[12],
m_matrix.e[1] * x + m_matrix.e[5] * y + m_matrix.e[13]);
}
Vector2f Transform::transformPoint(const Vector2f& vec) const
{
return transformPoint(vec.x, vec.y);
}
Matrix4f& Transform::getMatrix()
{
return m_matrix;
}
const Matrix4f& Transform::getMatrix() const
{
return m_matrix;
}
float* Transform::getRawMatrix()
{
return m_matrix.e;
}
const float* Transform::getRawMatrix() const
{
return m_matrix.e;
}
Matrix4f Transform::getRotationMatrix() const
{
return Matrix4f(
m_matrix.e[0], m_matrix.e[4], 0.0f, 0.0f,
m_matrix.e[1], m_matrix.e[5], 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
Transform operator*(const Transform& a, const Transform& b)
{
return Transform(a.getMatrix() * b.getMatrix());
}
Transform& operator*=(Transform& a, const Transform& other)
{
a.multiply(other);
return a;
}
Vector2f operator*(const Transform& transform, const Vector2f& vec)
{
return transform.transformPoint(vec);
}