eqemu

diff src/vmath.h @ 4:3d3656360a82

rendering properly, added picking, almost done...
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Jul 2014 08:51:17 +0300
parents f9274bebe55e
children 2656099aff12
line diff
     1.1 --- a/src/vmath.h	Thu Jul 17 02:35:19 2014 +0300
     1.2 +++ b/src/vmath.h	Thu Jul 17 08:51:17 2014 +0300
     1.3 @@ -1,6 +1,8 @@
     1.4  #ifndef VMATH_H_
     1.5  #define VMATH_H_
     1.6  
     1.7 +#include <math.h>
     1.8 +
     1.9  class Vector2 {
    1.10  public:
    1.11  	float x, y;
    1.12 @@ -23,6 +25,40 @@
    1.13  	const float &operator [](int idx) const { return (&x)[idx]; }
    1.14  };
    1.15  
    1.16 +inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
    1.17 +{
    1.18 +	return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
    1.19 +}
    1.20 +
    1.21 +inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
    1.22 +{
    1.23 +	return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
    1.24 +}
    1.25 +
    1.26 +inline Vector3 operator *(const Vector3 &a, float s)
    1.27 +{
    1.28 +	return Vector3(a.x * s, a.y * s, a.z * s);
    1.29 +}
    1.30 +
    1.31 +inline float dot(const Vector3 &a, const Vector3 &b)
    1.32 +{
    1.33 +	return a.x * b.x + a.y * b.y + a.z * b.z;
    1.34 +}
    1.35 +
    1.36 +inline float length(const Vector3 &v)
    1.37 +{
    1.38 +	return sqrt(dot(v, v));
    1.39 +}
    1.40 +
    1.41 +inline Vector3 normalize(const Vector3 &v)
    1.42 +{
    1.43 +	float len = length(v);
    1.44 +	if(len == 0.0) {
    1.45 +		return v;
    1.46 +	}
    1.47 +	return Vector3(v.x / len, v.y / len, v.z / len);
    1.48 +}
    1.49 +
    1.50  class Vector4 {
    1.51  public:
    1.52  	float x, y, z, w;
    1.53 @@ -34,4 +70,12 @@
    1.54  	const float &operator [](int idx) const {  return (&x)[idx]; }
    1.55  };
    1.56  
    1.57 +class Ray {
    1.58 +public:
    1.59 +	Vector3 origin, dir;
    1.60 +
    1.61 +	Ray() : origin(0, 0, 0), dir(0, 0, 1) {}
    1.62 +	Ray(const Vector3 &o, const Vector3 &d) : origin(o), dir(d) {}
    1.63 +};
    1.64 +
    1.65  #endif	// VMATH_H_