graphene

changeset 3:d71b4e899e08

minimal matrix and vec4
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Jul 2015 05:52:39 +0300
parents fb032d88839f
children d30e24132b6e
files src/gmath/gmath.h src/gmath/matrix.h src/gmath/vec.h src/gmath/vector.cc src/gmath/vector.h src/graphene3d.h
diffstat 6 files changed, 275 insertions(+), 150 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/gmath/gmath.h	Sat Jul 25 05:52:39 2015 +0300
     1.3 @@ -0,0 +1,7 @@
     1.4 +#ifndef GMATH_H_
     1.5 +#define GMATH_H_
     1.6 +
     1.7 +#include "vec.h"
     1.8 +#include "matrix.h"
     1.9 +
    1.10 +#endif	// GMATH_H_
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/gmath/matrix.h	Sat Jul 25 05:52:39 2015 +0300
     2.3 @@ -0,0 +1,58 @@
     2.4 +#ifndef GMATH_MATRIX_H_
     2.5 +#define GMATH_MATRIX_H_
     2.6 +
     2.7 +#include <string.h>
     2.8 +#include "vec.h"
     2.9 +
    2.10 +namespace gph {
    2.11 +
    2.12 +class Matrix4x4 {
    2.13 +private:
    2.14 +	float m[4][4];
    2.15 +
    2.16 +public:
    2.17 +	static Matrix4x4 identity;
    2.18 +
    2.19 +	Matrix4x4()
    2.20 +	{
    2.21 +		memcpy((float*)m, (const float*)identity.m, 16 * sizeof(float));
    2.22 +	}
    2.23 +
    2.24 +	Matrix4x4(const float *m)
    2.25 +	{
    2.26 +		memcpy((float*)this->m, (const float*)m, 16 * sizeof(float));
    2.27 +	}
    2.28 +
    2.29 +	Matrix4x4(float m00, float m01, float m02, float m03,
    2.30 +			float m10, float m11, float m12, float m13,
    2.31 +			float m20, float m21, float m22, float m23,
    2.32 +			float m30, float m31, float m32, float m33)
    2.33 +	{
    2.34 +		m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03;
    2.35 +		m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13;
    2.36 +		m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23;
    2.37 +		m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33;
    2.38 +	}
    2.39 +
    2.40 +	Matrix4x4(const Vector4 &v0, const Vector4 &v1, const Vector4 &v2, const Vector4 &v3)
    2.41 +	{
    2.42 +		m[0][0] = v0.x; m[0][1] = v0.y; m[0][2] = v0.z; m[0][3] = v0.w;
    2.43 +		m[1][0] = v1.x; m[1][1] = v1.y; m[1][2] = v1.z; m[1][3] = v1.w;
    2.44 +		m[2][0] = v2.x; m[2][1] = v2.y; m[2][2] = v2.z; m[2][3] = v2.w;
    2.45 +		m[3][0] = v3.x; m[3][1] = v3.y; m[3][2] = v3.z; m[3][3] = v3.w;
    2.46 +	}
    2.47 +
    2.48 +	float *operator [](int idx)
    2.49 +	{
    2.50 +		return m[idx];
    2.51 +	}
    2.52 +
    2.53 +	const float *operator [](int idx) const
    2.54 +	{
    2.55 +		return m[idx];
    2.56 +	}
    2.57 +};
    2.58 +
    2.59 +}	// namespace gph
    2.60 +
    2.61 +#endif	// GMATH_MATRIX_H_
     3.1 --- a/src/gmath/vec.h	Sat Jul 25 05:08:22 2015 +0300
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,149 +0,0 @@
     3.4 -#ifndef GMATH_VEC_H_
     3.5 -#define GMATH_VEC_H_
     3.6 -
     3.7 -#include <math.h>
     3.8 -
     3.9 -namespace gph {
    3.10 -
    3.11 -class Vector3 {
    3.12 -public:
    3.13 -	float x, y, z;
    3.14 -
    3.15 -	Vector3() : x(0), y(0), z(0) {}
    3.16 -	Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}
    3.17 -
    3.18 -	inline void normalize();
    3.19 -};
    3.20 -
    3.21 -inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
    3.22 -{
    3.23 -	return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
    3.24 -}
    3.25 -
    3.26 -inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
    3.27 -{
    3.28 -	return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
    3.29 -}
    3.30 -
    3.31 -inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
    3.32 -{
    3.33 -	return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
    3.34 -}
    3.35 -
    3.36 -inline Vector3 operator /(const Vector3 &a, const Vector3 &b)
    3.37 -{
    3.38 -	return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
    3.39 -}
    3.40 -
    3.41 -inline Vector3 operator *(const Vector3 &v, float s)
    3.42 -{
    3.43 -	return Vector3(v.x * s, v.y * s, v.z * s);
    3.44 -}
    3.45 -
    3.46 -inline Vector3 operator *(float s, const Vector3 &v)
    3.47 -{
    3.48 -	return Vector3(s * v.x, s * v.y, s * v.z);
    3.49 -}
    3.50 -
    3.51 -inline Vector3 operator /(const Vector3 &v, float s)
    3.52 -{
    3.53 -	return Vector3(v.x / s, v.y / s, v.z / s);
    3.54 -}
    3.55 -
    3.56 -inline Vector3 operator /(float s, const Vector3 &v)
    3.57 -{
    3.58 -	return Vector3(s / v.x, s / v.y, s / v.z);
    3.59 -}
    3.60 -
    3.61 -inline Vector3 &operator +=(Vector3 &a, const Vector3 &b)
    3.62 -{
    3.63 -	a.x += b.x;
    3.64 -	a.y += b.y;
    3.65 -	a.z += b.z;
    3.66 -	return a;
    3.67 -}
    3.68 -
    3.69 -inline Vector3 &operator -=(Vector3 &a, const Vector3 &b)
    3.70 -{
    3.71 -	a.x -= b.x;
    3.72 -	a.y -= b.y;
    3.73 -	a.z -= b.z;
    3.74 -	return a;
    3.75 -}
    3.76 -
    3.77 -inline Vector3 &operator *=(Vector3 &a, const Vector3 &b)
    3.78 -{
    3.79 -	a.x *= b.x;
    3.80 -	a.y *= b.y;
    3.81 -	a.z *= b.z;
    3.82 -	return a;
    3.83 -}
    3.84 -
    3.85 -inline Vector3 &operator /=(Vector3 &a, const Vector3 &b)
    3.86 -{
    3.87 -	a.x /= b.x;
    3.88 -	a.y /= b.y;
    3.89 -	a.z /= b.z;
    3.90 -	return a;
    3.91 -}
    3.92 -
    3.93 -inline Vector3 &operator *=(Vector3 &v, float s)
    3.94 -{
    3.95 -	v.x *= s;
    3.96 -	v.y *= s;
    3.97 -	v.z *= s;
    3.98 -	return v;
    3.99 -}
   3.100 -
   3.101 -inline Vector3 &operator /=(Vector3 &v, float s)
   3.102 -{
   3.103 -	v.x /= s;
   3.104 -	v.y /= s;
   3.105 -	v.z /= s;
   3.106 -	return v;
   3.107 -}
   3.108 -
   3.109 -inline float dot(const Vector3 &a, const Vector3 &b)
   3.110 -{
   3.111 -	return a.x * b.x + a.y * b.y + a.z * b.z;
   3.112 -}
   3.113 -
   3.114 -inline Vector3 cross(const Vector3 &a, const Vector3 &b)
   3.115 -{
   3.116 -	return Vector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
   3.117 -}
   3.118 -
   3.119 -inline float length(const Vector3 &v)
   3.120 -{
   3.121 -	return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
   3.122 -}
   3.123 -
   3.124 -inline float length_sq(const Vector3 &v)
   3.125 -{
   3.126 -	return v.x * v.x + v.y * v.y + v.z * v.z;
   3.127 -}
   3.128 -
   3.129 -inline Vector3 normalize(const Vector3 &v)
   3.130 -{
   3.131 -	float len = length(v);
   3.132 -	if(len == 0.0f) {
   3.133 -		return v;
   3.134 -	}
   3.135 -
   3.136 -	return Vector3(v.x / len, v.y / len, v.z / len);
   3.137 -}
   3.138 -
   3.139 -inline void Vector3::normalize()
   3.140 -{
   3.141 -	float len = length(*this);
   3.142 -	if(len != 0.0f) {
   3.143 -		x /= len;
   3.144 -		y /= len;
   3.145 -		z /= len;
   3.146 -	}
   3.147 -}
   3.148 -
   3.149 -
   3.150 -}
   3.151 -
   3.152 -#endif	/* GMATH_VEC_H_ */
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/gmath/vector.cc	Sat Jul 25 05:52:39 2015 +0300
     4.3 @@ -0,0 +1,15 @@
     4.4 +#include "vec.h"
     4.5 +
     4.6 +namespace gph {
     4.7 +
     4.8 +Vector3::Vector3(const Vector4 &v)
     4.9 +	: x(v.x), y(v.y), z(v.z)
    4.10 +{
    4.11 +}
    4.12 +
    4.13 +Vector4::Vector4(const Vector3 &v)
    4.14 +	: x(v.x), y(v.y), z(v.z), w(1.0f)
    4.15 +{
    4.16 +}
    4.17 +
    4.18 +}	// namespace gph
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/gmath/vector.h	Sat Jul 25 05:52:39 2015 +0300
     5.3 @@ -0,0 +1,194 @@
     5.4 +#ifndef GMATH_VEC_H_
     5.5 +#define GMATH_VEC_H_
     5.6 +
     5.7 +#include <math.h>
     5.8 +
     5.9 +namespace gph {
    5.10 +
    5.11 +class Vector4;
    5.12 +
    5.13 +class Vector3 {
    5.14 +public:
    5.15 +	float x, y, z;
    5.16 +
    5.17 +	Vector3() : x(0), y(0), z(0) {}
    5.18 +	Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}
    5.19 +	Vector3(const Vector4 &v);
    5.20 +
    5.21 +	inline void normalize()
    5.22 +	{
    5.23 +		float len = (float)sqrt(x * x + y * y + z * z);
    5.24 +		if(len != 0.0f) {
    5.25 +			x /= len;
    5.26 +			y /= len;
    5.27 +			z /= len;
    5.28 +		}
    5.29 +	}
    5.30 +
    5.31 +	inline float &operator[] (int idx)
    5.32 +	{
    5.33 +		return idx == 0 ? x : (idx == 1 ? y : z);
    5.34 +	}
    5.35 +
    5.36 +	inline const float &operator[] (int idx) const
    5.37 +	{
    5.38 +		return idx == 0 ? x : (idx == 1 ? y : z);
    5.39 +	}
    5.40 +};
    5.41 +
    5.42 +
    5.43 +class Vector4 {
    5.44 +public:
    5.45 +	float x, y, z, w;
    5.46 +
    5.47 +	Vector4() : x(0), y(0), z(0), w(0) {}
    5.48 +	Vector4(float x_, float y_, float z_, float w_) : x(x_), y(y_), z(z_), w(w_) {}
    5.49 +	Vector4(const Vector3 &v);
    5.50 +
    5.51 +	inline void normalize()
    5.52 +	{
    5.53 +		float len = (float)sqrt(x * x + y * y + z * z + w * w);
    5.54 +		if(len != 0.0f) {
    5.55 +			x /= len;
    5.56 +			y /= len;
    5.57 +			z /= len;
    5.58 +			w /= len;
    5.59 +		}
    5.60 +	}
    5.61 +
    5.62 +	inline float &operator[] (int idx)
    5.63 +	{
    5.64 +		return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
    5.65 +	}
    5.66 +
    5.67 +	inline const float &operator[] (int idx) const
    5.68 +	{
    5.69 +		return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
    5.70 +	}
    5.71 +};
    5.72 +
    5.73 +// ---- Vector3 functions ----
    5.74 +
    5.75 +inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
    5.76 +{
    5.77 +	return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
    5.78 +}
    5.79 +
    5.80 +inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
    5.81 +{
    5.82 +	return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
    5.83 +}
    5.84 +
    5.85 +inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
    5.86 +{
    5.87 +	return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
    5.88 +}
    5.89 +
    5.90 +inline Vector3 operator /(const Vector3 &a, const Vector3 &b)
    5.91 +{
    5.92 +	return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
    5.93 +}
    5.94 +
    5.95 +inline Vector3 operator *(const Vector3 &v, float s)
    5.96 +{
    5.97 +	return Vector3(v.x * s, v.y * s, v.z * s);
    5.98 +}
    5.99 +
   5.100 +inline Vector3 operator *(float s, const Vector3 &v)
   5.101 +{
   5.102 +	return Vector3(s * v.x, s * v.y, s * v.z);
   5.103 +}
   5.104 +
   5.105 +inline Vector3 operator /(const Vector3 &v, float s)
   5.106 +{
   5.107 +	return Vector3(v.x / s, v.y / s, v.z / s);
   5.108 +}
   5.109 +
   5.110 +inline Vector3 operator /(float s, const Vector3 &v)
   5.111 +{
   5.112 +	return Vector3(s / v.x, s / v.y, s / v.z);
   5.113 +}
   5.114 +
   5.115 +inline Vector3 &operator +=(Vector3 &a, const Vector3 &b)
   5.116 +{
   5.117 +	a.x += b.x;
   5.118 +	a.y += b.y;
   5.119 +	a.z += b.z;
   5.120 +	return a;
   5.121 +}
   5.122 +
   5.123 +inline Vector3 &operator -=(Vector3 &a, const Vector3 &b)
   5.124 +{
   5.125 +	a.x -= b.x;
   5.126 +	a.y -= b.y;
   5.127 +	a.z -= b.z;
   5.128 +	return a;
   5.129 +}
   5.130 +
   5.131 +inline Vector3 &operator *=(Vector3 &a, const Vector3 &b)
   5.132 +{
   5.133 +	a.x *= b.x;
   5.134 +	a.y *= b.y;
   5.135 +	a.z *= b.z;
   5.136 +	return a;
   5.137 +}
   5.138 +
   5.139 +inline Vector3 &operator /=(Vector3 &a, const Vector3 &b)
   5.140 +{
   5.141 +	a.x /= b.x;
   5.142 +	a.y /= b.y;
   5.143 +	a.z /= b.z;
   5.144 +	return a;
   5.145 +}
   5.146 +
   5.147 +inline Vector3 &operator *=(Vector3 &v, float s)
   5.148 +{
   5.149 +	v.x *= s;
   5.150 +	v.y *= s;
   5.151 +	v.z *= s;
   5.152 +	return v;
   5.153 +}
   5.154 +
   5.155 +inline Vector3 &operator /=(Vector3 &v, float s)
   5.156 +{
   5.157 +	v.x /= s;
   5.158 +	v.y /= s;
   5.159 +	v.z /= s;
   5.160 +	return v;
   5.161 +}
   5.162 +
   5.163 +inline float dot(const Vector3 &a, const Vector3 &b)
   5.164 +{
   5.165 +	return a.x * b.x + a.y * b.y + a.z * b.z;
   5.166 +}
   5.167 +
   5.168 +inline Vector3 cross(const Vector3 &a, const Vector3 &b)
   5.169 +{
   5.170 +	return Vector3(a.y * b.z - a.z * b.y,
   5.171 +			a.z * b.x - a.x * b.z,
   5.172 +			a.x * b.y - a.y * b.x);
   5.173 +}
   5.174 +
   5.175 +inline float length(const Vector3 &v)
   5.176 +{
   5.177 +	return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
   5.178 +}
   5.179 +
   5.180 +inline float length_sq(const Vector3 &v)
   5.181 +{
   5.182 +	return v.x * v.x + v.y * v.y + v.z * v.z;
   5.183 +}
   5.184 +
   5.185 +inline Vector3 normalize(const Vector3 &v)
   5.186 +{
   5.187 +	float len = length(v);
   5.188 +	if(len == 0.0f) {
   5.189 +		return v;
   5.190 +	}
   5.191 +
   5.192 +	return Vector3(v.x / len, v.y / len, v.z / len);
   5.193 +}
   5.194 +
   5.195 +}
   5.196 +
   5.197 +#endif	/* GMATH_VEC_H_ */
     6.1 --- a/src/graphene3d.h	Sat Jul 25 05:08:22 2015 +0300
     6.2 +++ b/src/graphene3d.h	Sat Jul 25 05:52:39 2015 +0300
     6.3 @@ -2,6 +2,6 @@
     6.4  #define GRAPHENE3D_H_
     6.5  
     6.6  #include "opengl.h"
     6.7 -#include "gmath/vec.h"
     6.8 +#include "gmath/gmath.h"
     6.9  
    6.10  #endif	// GRAPHENE3D_H_