eqemu

view 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 source
1 #ifndef VMATH_H_
2 #define VMATH_H_
4 #include <math.h>
6 class Vector2 {
7 public:
8 float x, y;
10 Vector2() : x(0), y(0) {}
11 Vector2(float xa, float ya) : x(xa), y(ya) {}
13 float &operator [](int idx) { return (&x)[idx]; }
14 const float &operator [](int idx) const { return (&x)[idx]; }
15 };
17 class Vector3 {
18 public:
19 float x, y, z;
21 Vector3() : x(0), y(0), z(0) {}
22 Vector3(float xa, float ya, float za) : x(xa), y(ya), z(za) {}
24 float &operator [](int idx) { return (&x)[idx]; }
25 const float &operator [](int idx) const { return (&x)[idx]; }
26 };
28 inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
29 {
30 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
31 }
33 inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
34 {
35 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
36 }
38 inline Vector3 operator *(const Vector3 &a, float s)
39 {
40 return Vector3(a.x * s, a.y * s, a.z * s);
41 }
43 inline float dot(const Vector3 &a, const Vector3 &b)
44 {
45 return a.x * b.x + a.y * b.y + a.z * b.z;
46 }
48 inline float length(const Vector3 &v)
49 {
50 return sqrt(dot(v, v));
51 }
53 inline Vector3 normalize(const Vector3 &v)
54 {
55 float len = length(v);
56 if(len == 0.0) {
57 return v;
58 }
59 return Vector3(v.x / len, v.y / len, v.z / len);
60 }
62 class Vector4 {
63 public:
64 float x, y, z, w;
66 Vector4() : x(0), y(0), z(0), w(0) {}
67 Vector4(float xa, float ya, float za, float wa) : x(xa), y(ya), z(za), w(wa) {}
69 float &operator [](int idx) { return (&x)[idx]; }
70 const float &operator [](int idx) const { return (&x)[idx]; }
71 };
73 class Ray {
74 public:
75 Vector3 origin, dir;
77 Ray() : origin(0, 0, 0), dir(0, 0, 1) {}
78 Ray(const Vector3 &o, const Vector3 &d) : origin(o), dir(d) {}
79 };
81 #endif // VMATH_H_