Added streflop (standalone reproducible floating point library) layer to mega-glest (initial checkin only) and changed a few areas to use the library in linux

This commit is contained in:
Mark Vejvoda
2010-04-24 03:57:38 +00:00
parent a50b89e9cc
commit 2bfaa4d1d7
129 changed files with 15486 additions and 79 deletions

View File

@@ -0,0 +1,218 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 Martio Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef _SHARED_GRAPHICS_MATHUTIL_H_
#define _SHARED_GRAPHICS_MATHUTIL_H_
//#include <cmath>
#include "vec.h"
namespace Shared{ namespace Graphics{
const float pi= 3.1415926f;
const float sqrt2= 1.41421356f;
const float zero= 1e-6f;
const float infinity= 1e6f;
// =====================================================
// class Rect
// =====================================================
// 0 +-+
// | |
// +-+ 1
template<typename T>
class Rect2{
public:
Vec2<T> p[2];
public:
Rect2(){
};
Rect2(const Vec2<T> &p0, const Vec2<T> &p1){
this->p[0]= p0;
this->p[1]= p1;
}
Rect2(T p0x, T p0y, T p1x, T p1y){
p[0].x= p0x;
p[0].y= p0y;
p[1].x= p1x;
p[1].y= p1y;
}
Rect2<T> operator*(T scalar){
return Rect2<T>(
p[0]*scalar,
p[1]*scalar);
}
Rect2<T> operator/(T scalar){
return Rect2<T>(
p[0]/scalar,
p[1]/scalar);
}
bool isInside(const Vec2<T> &p) const{
return
p.x>=this->p[0].x &&
p.y>=this->p[0].y &&
p.x<this->p[1].x &&
p.y<this->p[1].y;
}
void clamp(T minX, T minY,T maxX, T maxY){
for(int i=0; i<2; ++i){
if(p[i].x<minX){
p[i].x= minX;
}
if(p[i].y<minY){
p[i].y= minY;
}
if(p[i].x>maxX){
p[i].x= maxX;
}
if(p[i].y>maxY){
p[i].y= maxY;
}
}
}
};
typedef Rect2<int> Rect2i;
typedef Rect2<char> Rect2c;
typedef Rect2<float> Rect2f;
typedef Rect2<double> Rect2d;
// =====================================================
// class Quad
// =====================================================
// 0 +-+ 2
// | |
// 1 +-+ 3
template<typename T>
class Quad2{
public:
Vec2<T> p[4];
public:
Quad2(){
};
Quad2(const Vec2<T> &p0, const Vec2<T> &p1, const Vec2<T> &p2, const Vec2<T> &p3){
this->p[0]= p0;
this->p[1]= p1;
this->p[2]= p2;
this->p[3]= p3;
}
explicit Quad2(const Rect2<T> &rect){
this->p[0]= rect.p[0];
this->p[1]= Vec2<T>(rect.p[0].x, rect.p[1].y);
this->p[2]= rect.p[1];
this->p[3]= Vec2<T>(rect.p[1].x, rect.p[0].y);
}
Quad2<T> operator*(T scalar){
return Quad2<T>(
p[0]*scalar,
p[1]*scalar,
p[2]*scalar,
p[3]*scalar);
}
Quad2<T> operator/(T scalar){
return Quad2<T>(
p[0]/scalar,
p[1]/scalar,
p[2]/scalar,
p[3]/scalar);
}
Rect2<T> computeBoundingRect() const{
return Rect2i(
min(p[0].x, p[1].x),
min(p[0].y, p[2].y),
max(p[2].x, p[3].x),
max(p[1].y, p[3].y));
}
bool isInside(const Vec2<T> &pt) const{
if(!computeBoundingRect().isInside(pt))
return false;
bool left[4];
left[0]= (pt.y - p[0].y)*(p[1].x - p[0].x) - (pt.x - p[0].x)*(p[1].y - p[0].y) < 0;
left[1]= (pt.y - p[1].y)*(p[3].x - p[1].x) - (pt.x - p[1].x)*(p[3].y - p[1].y) < 0;
left[2]= (pt.y - p[3].y)*(p[2].x - p[3].x) - (pt.x - p[3].x)*(p[2].y - p[3].y) < 0;
left[3]= (pt.y - p[2].y)*(p[0].x - p[2].x) - (pt.x - p[2].x)*(p[0].y - p[2].y) < 0;
return left[0] && left[1] && left[2] && left[3];
}
void clamp(T minX, T minY, T maxX, T maxY){
for(int i=0; i<4; ++i){
if(p[i].x<minX){
p[i].x= minX;
}
if(p[i].y<minY){
p[i].y= minY;
}
if(p[i].x>maxX){
p[i].x= maxX;
}
if(p[i].y>maxY){
p[i].y= maxY;
}
}
}
float area(){
Vec2i v0= p[3]-p[0];
Vec2i v1= p[1]-p[2];
return 0.5f * ((v0.x * v1.y) - (v0.y * v1.x));
}
};
typedef Quad2<int> Quad2i;
typedef Quad2<char> Quad2c;
typedef Quad2<float> Quad2f;
typedef Quad2<double> Quad2d;
// =====================================================
// Misc
// =====================================================
inline int next2Power(int n){
int i;
for (i=1; i<n; i*=2);
return i;
}
template<typename T>
inline T degToRad(T deg){
return (deg*2*pi)/360;
}
template<typename T>
inline T radToDeg(T rad){
return (rad*360)/(2*pi);
}
}}//end namespace
#endif

View File

@@ -0,0 +1,162 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 Martio Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef _SHARED_GRAPHICS_MATRIX_H_
#define _SHARED_GRAPHICS_MATRIX_H_
//#include <cmath>
#include "vec.h"
namespace Shared{ namespace Graphics{
// =====================================================
// class Matrix3
// =====================================================
template<typename T>
class Matrix3{
private:
T data[9];
public:
Matrix3(){};
Matrix3(T *p){
for(int i=0; i<9; ++i){
data[i]= p[i];
}
}
T *ptr(){
return data;
}
const T *ptr() const{
return data;
}
T &operator[](int i){
return data[i];
}
T &operator()(int i, int j){
return data[i*3+j];
}
Vec3<T> operator * (const Vec3<T> &v) const{
Vec3<T> rv;
return Vec3f(
data[0]*v.x + data[1]*v.y + data[2]*v.z,
data[3]*v.x + data[4]*v.y + data[5]*v.z,
data[6]*v.x + data[7]*v.y + data[8]*v.z);
}
Matrix3<T> operator * (const Matrix3<T> &m) const{
Matrix3<T> rm;
for(int i=0; i<3; ++i){
for(int j=0; j<3; ++j){
T acum= 0.0f;
for(int k=0; k<3; ++k){
acum+= data[i*3+k]*m[k*3+j];
}
rm[i*3+j]= acum;
}
}
return rm;
}
void traspose(){
for(int i=0; i<3; ++i){
for(int j=0; j<3; ++j){
T tmp= data[j*3+i];
data[j*3+i]= data[i*3+j];
data[i*3+j]= tmp;
}
}
}
};
typedef Matrix3<float> Matrix3f;
typedef Matrix3<double> Matrix3d;
// =====================================================
// class Matrix4
// =====================================================
template<typename T>
class Matrix4{
private:
T data[16];
public:
Matrix4(){};
Matrix4(T *p){
for(int i=0; i<16; ++i){
data[i]= p[i];
}
}
T *ptr(){
return data;
}
const T *ptr() const{
return data;
}
T &operator[](int i){
return data[i];
}
const T &operator[](int i) const{
return data[i];
}
T &operator()(int i, int j){
return data[i*4+j];
}
Vec4<T> operator * (const Vec4<T> &v) const{
Vec4<T> rv;
return Vec4f(
data[0]*v.x + data[1]*v.y + data[2]*v.z + data[3]*v.w,
data[4]*v.x + data[5]*v.y + data[6]*v.z + data[7]*v.w,
data[8]*v.x + data[9]*v.y + data[10]*v.z + data[11]*v.w,
data[12]*v.x + data[13]*v.y + data[14]*v.z + data[15]*v.w);
}
Matrix4<T> operator * (const Matrix4<T> &m) const{
Matrix4<T> rm;
for(int i=0; i<4; ++i){
for(int j=0; j<4; ++j){
T acum= 0.0f;
for(int k=0; k<4; ++k){
acum+= data[i*4+k]*m[k*4+j];
}
rm[i*4+j]= acum;
}
}
return rm;
}
};
typedef Matrix4<float> Matrix4f;
typedef Matrix4<double> Matrix4d;
}} //enmd namespace
#endif

View File

@@ -21,7 +21,7 @@
#include "random.h"
using std::list;
using Shared::Util::Random;
//using Shared::Util::Random;
namespace Shared{ namespace Graphics{
@@ -95,7 +95,7 @@ protected:
protected:
Particle *particles;
Random random;
Shared::Util::Random random;
BlendMode blendMode;
State state;

View File

@@ -0,0 +1,444 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 Martio Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef _SHARED_GRAPHICS_VEC_H_
#define _SHARED_GRAPHICS_VEC_H_
#include "streflop_cond.h"
//#include <cmath>
namespace Shared{ namespace Graphics{
template<typename T> class Vec2;
template<typename T> class Vec3;
template<typename T> class Vec4;
// =====================================================
// class Vec2
// =====================================================
template<typename T>
class Vec2{
public:
T x;
T y;
public:
Vec2(){
};
explicit Vec2(T *p){
this->x= p[0];
this->y= p[1];
}
explicit Vec2(T xy){
this->x= xy;
this->y= xy;
}
template<typename S>
explicit Vec2(const Vec2<S> &v){
this->x= v.x;
this->y= v.y;
}
Vec2(T x, T y){
this->x= x;
this->y= y;
}
T *ptr(){
return reinterpret_cast<T*>(this);
}
const T *ptr() const{
return reinterpret_cast<const T*>(this);
}
bool operator ==(const Vec2<T> &v) const{
return x==v.x && y==v.y;
}
bool operator !=(const Vec2<T> &v) const{
return x!=v.x || y!=v.y;
}
Vec2<T> operator +(const Vec2<T> &v) const{
return Vec2(x+v.x, y+v.y);
}
Vec2<T> operator -(const Vec2<T> &v) const{
return Vec2(x-v.x, y-v.y);
}
Vec2<T> operator -() const{
return Vec2(-x, -y);
}
Vec2<T> operator *(const Vec2<T> &v) const{
return Vec2(x*v.x, y*v.y);
}
Vec2<T> operator *(T s) const{
return Vec2(x*s, y*s);
}
Vec2<T> operator /(const Vec2<T> &v) const{
return Vec2(x/v.x, y/v.y);
}
Vec2<T> operator /(T s) const{
return Vec2(x/s, y/s);
}
Vec2<T> operator +=(const Vec2<T> &v){
x+=v.x;
y+=v.y;
return *this;
}
Vec2<T> operator -=(const Vec2<T> &v){
x-=v.x;
y-=v.y;
return *this;
}
Vec2<T> lerp(T t, const Vec2<T> &v) const{
return *this + (v - *this)*t;
}
T dot(const Vec2<T> &v) const{
return x*v.x+y*v.y;
}
float dist(const Vec2<T> &v) const{
return Vec2<T>(v-*this).length();
}
float length() const{
return static_cast<float>(streflop::sqrt(static_cast<float>(x*x + y*y)));
}
void normalize(){
T m= length();
x/= m;
y/= m;
}
};
typedef Vec2<int> Vec2i;
typedef Vec2<bool> Vec2b;
typedef Vec2<char> Vec2c;
typedef Vec2<float> Vec2f;
typedef Vec2<double> Vec2d;
// =====================================================
// class Vec3
// =====================================================
template<typename T>
class Vec3{
public:
T x;
T y;
T z;
public:
Vec3(){
};
explicit Vec3(T *p){
this->x= p[0];
this->y= p[1];
this->z= p[2];
}
explicit Vec3(T xyz){
this->x= xyz;
this->y= xyz;
this->z= xyz;
}
template<typename S>
explicit Vec3(const Vec3<S> &v){
this->x= v.x;
this->y= v.y;
this->z= v.z;
}
Vec3(T x, T y, T z){
this->x= x;
this->y= y;
this->z= z;
}
explicit Vec3(Vec4<T> v){
this->x= v.x;
this->y= v.y;
this->z= v.z;
}
T *ptr(){
return reinterpret_cast<T*>(this);
}
const T *ptr() const{
return reinterpret_cast<const T*>(this);
}
bool operator ==(const Vec3<T> &v) const{
return x==v.x && y==v.y && z==v.z;
}
bool operator !=(const Vec3<T> &v) const{
return x!=v.x || y!=v.y || z!=v.z;
}
Vec3<T> operator +(const Vec3<T> &v) const{
return Vec3(x+v.x, y+v.y, z+v.z);
}
Vec3<T> operator -(const Vec3<T> &v) const{
return Vec3(x-v.x, y-v.y, z-v.z);
}
Vec3<T> operator -() const{
return Vec3(-x, -y, -z);
}
Vec3<T> operator *(const Vec3<T> &v) const{
return Vec3(x*v.x, y*v.y, z*v.z);
}
Vec3<T> operator *(T s) const{
return Vec3(x*s, y*s, z*s);
}
Vec3<T> operator /(const Vec3<T> &v) const{
return Vec3(x/v.x, y/v.y, z/v.z);
}
Vec3<T> operator /(T s) const{
return Vec3(x/s, y/s, z/s);
}
Vec3<T> operator +=(const Vec3<T> &v){
x+=v.x;
y+=v.y;
z+=v.z;
return *this;
}
Vec3<T> operator -=(const Vec3<T> &v){
x-=v.x;
y-=v.y;
z-=v.z;
return *this;
}
Vec3<T> lerp(T t, const Vec3<T> &v) const{
return *this + (v - *this) * t;
}
T dot(const Vec3<T> &v) const{
return x*v.x + y*v.y + z*v.z;
}
float dist(const Vec3<T> &v) const{
return Vec3<T>(v-*this).length();
}
float length() const{
return static_cast<float>(streflop::sqrt(x*x + y*y + z*z));
}
void normalize(){
T m= length();
x/= m;
y/= m;
z/= m;
}
Vec3<T> getNormalized() const{
T m= length();
return Vec3<T>(x/m, y/m, z/m);
}
Vec3<T> cross(const Vec3<T> &v) const{
return Vec3<T>(
this->y*v.z-this->z*v.y,
this->z*v.x-this->x*v.z,
this->x*v.y-this->y*v.x);
}
Vec3<T> normal(const Vec3<T> &p1, const Vec3<T> &p2) const{
Vec3<T> rv;
rv= (p2-*this).cross(p1-*this);
rv.normalize();
return rv;
}
Vec3<T> normal(const Vec3<T> &p1, const Vec3<T> &p2, const Vec3<T> &p3, const Vec3<T> &p4) const{
Vec3<T> rv;
rv= this->normal(p1, p2);
rv= rv + this->normal(p2, p3);
rv= rv + this->normal(p3, p4);
rv= rv + this->normal(p4, p1);
rv.normalize();
return rv;
}
};
typedef Vec3<int> Vec3i;
typedef Vec3<bool> Vec3b;
typedef Vec3<char> Vec3c;
typedef Vec3<float> Vec3f;
typedef Vec3<double> Vec3d;
// =====================================================
// class Vec4
// =====================================================
template<typename T>
class Vec4{
public:
T x;
T y;
T z;
T w;
public:
Vec4(){
};
explicit Vec4(T *p){
this->x= p[0];
this->y= p[1];
this->z= p[2];
this->w= p[3];
}
explicit Vec4(T xyzw){
this->x= xyzw;
this->y= xyzw;
this->z= xyzw;
this->w= xyzw;
}
template<typename S>
explicit Vec4(const Vec4<S> &v){
this->x= v.x;
this->y= v.y;
this->z= v.z;
this->w= v.w;
}
Vec4(T x, T y, T z, T w){
this->x= x;
this->y= y;
this->z= z;
this->w= w;
}
Vec4(Vec3<T> v, T w){
this->x= v.x;
this->y= v.y;
this->z= v.z;
this->w= w;
}
explicit Vec4(Vec3<T> v){
this->x= v.x;
this->y= v.y;
this->z= v.z;
this->w= 1;
}
T *ptr(){
return reinterpret_cast<T*>(this);
}
const T *ptr() const{
return reinterpret_cast<const T*>(this);
}
bool operator ==(const Vec4<T> &v) const{
return x==v.x && y==v.y && z==v.z && w==v.w;
}
bool operator !=(const Vec4<T> &v) const{
return x!=v.x || y!=v.y || z!=v.z || w!=v.w;
}
Vec4<T> operator +(const Vec4<T> &v) const{
return Vec4(x+v.x, y+v.y, z+v.z, w+v.w);
}
Vec4<T> operator -(const Vec4<T> &v) const{
return Vec4(x-v.x, y-v.y, z-v.z, w-v.w);
}
Vec4<T> operator -() const{
return Vec4(-x, -y, -z, -w);
}
Vec4<T> operator *(const Vec4<T> &v) const{
return Vec4(x*v.x, y*v.y, z*v.z, w*v.w);
}
Vec4<T> operator *(T s) const{
return Vec4(x*s, y*s, z*s, w*s);
}
Vec4<T> operator /(const Vec4<T> &v) const{
return Vec4(x/v.x, y/v.y, z/v.z, w/v.w);
}
Vec4<T> operator /(T s) const{
return Vec4(x/s, y/s, z/s, w/s);
}
Vec4<T> operator +=(const Vec4<T> &v){
x+=v.x;
y+=v.y;
z+=v.z;
w+=w.z;
return *this;
}
Vec4<T> operator -=(const Vec4<T> &v){
x-=v.x;
y-=v.y;
z-=v.z;
w-=w.z;
return *this;
}
Vec4<T> lerp(T t, const Vec4<T> &v) const{
return *this + (v - *this) *t;
}
T dot(const Vec4<T> &v) const{
return x*v.x + y*v.y + z*v.z + w*v.w;
}
};
typedef Vec4<int> Vec4i;
typedef Vec4<bool> Vec4b;
typedef Vec4<char> Vec4c;
typedef Vec4<float> Vec4f;
typedef Vec4<double> Vec4d;
}} //enmd namespace
#endif