rayzor

view src/vmath.h @ 15:be616b58df99

continued the renderer slightly
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 13 Apr 2014 09:54:36 +0300
parents d94a69933a71
children 79609d482762
line source
1 #ifndef VMATH_H_
2 #define VMATH_H_
4 #include <math.h>
5 #include "vmathmat.h"
7 #define DEG2RAD(x) (M_PI * (x) / 180.0)
8 #define RAD2DEG(x) (180.0 * (x) / M_PI)
10 class Vector3 {
11 public:
12 float x, y, z;
14 Vector3() : x(0), y(0), z(0) {}
15 Vector3(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {}
17 float length_sq() const { return x * x + y * y + z * z; }
18 float length() const { return sqrt(x * x + y * y + z * z); }
20 void normalize()
21 {
22 float len = length();
23 if(len != 0.0) {
24 x /= len;
25 y /= len;
26 z /= len;
27 }
28 }
30 float &operator [](int idx) { return idx == 2 ? z : (idx == 1 ? y : x); }
31 const float &operator [](int idx) const { return idx == 2 ? z : (idx == 1 ? y : x); }
32 };
34 inline Vector3 normalize(const Vector3 &v)
35 {
36 float len = v.length();
37 if(len != 0.0) {
38 return Vector3(v.x / len, v.y / len, v.z / len);
39 }
40 return v;
41 }
43 inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
44 {
45 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
46 }
48 inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
49 {
50 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
51 }
53 inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
54 {
55 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
56 }
58 inline Vector3 operator *(const Vector3 &v, float s)
59 {
60 return Vector3(v.x * s, v.y * s, v.z * s);
61 }
63 inline Vector3 operator /(const Vector3 &v, float s)
64 {
65 return Vector3(v.x / s, v.y / s, v.z / s);
66 }
68 inline float dot(const Vector3 &a, const Vector3 &b)
69 {
70 return a.x * b.x + a.y * b.y + a.z * b.z;
71 }
73 inline Vector3 cross(const Vector3 &a, const Vector3 &b)
74 {
75 return Vector3(a.y * b.z - a.z * b.y,
76 a.z * b.x - a.x * b.z,
77 a.x * b.y - a.y * b.x);
78 }
80 inline Vector3 transform(const Matrix4x4 &m, const Vector3 &v)
81 {
82 float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3];
83 float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3];
84 float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3];
85 return Vector3(x, y, z);
86 }
88 // ---- Vector4 ----
90 class Vector4 {
91 public:
92 float x, y, z, w;
94 Vector4() : x(0), y(0), z(0), w(1.0) {}
95 Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0) {}
96 Vector4(float xx, float yy, float zz, float ww) : x(xx), y(yy), z(zz), w(ww) {}
98 float length_sq() const { return x * x + y * y + z * z + w * w; }
99 float length() const { return sqrt(x * x + y * y + z * z + w * w); }
101 void normalize()
102 {
103 float len = length();
104 if(len != 0.0) {
105 x /= len;
106 y /= len;
107 z /= len;
108 w /= len;
109 }
110 }
112 float &operator [](int idx)
113 {
114 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
115 }
116 const float &operator [](int idx) const
117 {
118 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
119 }
120 };
122 inline Vector4 operator +(const Vector4 &a, const Vector4 &b)
123 {
124 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
125 }
127 inline Vector4 operator -(const Vector4 &a, const Vector4 &b)
128 {
129 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
130 }
132 inline Vector4 operator *(const Vector4 &v, float s)
133 {
134 return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
135 }
137 inline Vector4 operator /(const Vector4 &v, float s)
138 {
139 return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
140 }
142 inline float dot(const Vector4 &a, const Vector4 &b)
143 {
144 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
145 }
147 inline Vector4 transform(const Matrix4x4 &m, const Vector4 &v)
148 {
149 float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3] * v.w;
150 float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3] * v.w;
151 float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3] * v.w;
152 float w = m.m[3][0] * v.x + m.m[3][1] * v.y + m.m[3][2] * v.z + m.m[3][3] * v.w;
153 return Vector4(x, y, z, w);
154 }
156 #endif // VMATH_H_