rayzor

view src/vmath.h @ 22:5380ff64e83f

minor changes from dos, and line endings cleanup
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 02 May 2014 14:32:58 +0300
parents 79609d482762
children
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 &v)
54 {
55 return Vector3(-v.x, -v.y, -v.z);
56 }
58 inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
59 {
60 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
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 Vector3 operator /(const Vector3 &v, float s)
69 {
70 return Vector3(v.x / s, v.y / s, v.z / s);
71 }
73 inline float dot(const Vector3 &a, const Vector3 &b)
74 {
75 return a.x * b.x + a.y * b.y + a.z * b.z;
76 }
78 inline Vector3 cross(const Vector3 &a, const Vector3 &b)
79 {
80 return Vector3(a.y * b.z - a.z * b.y,
81 a.z * b.x - a.x * b.z,
82 a.x * b.y - a.y * b.x);
83 }
85 inline Vector3 transform(const Matrix4x4 &m, const Vector3 &v)
86 {
87 float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3];
88 float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3];
89 float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3];
90 return Vector3(x, y, z);
91 }
93 inline Vector3 lerp(const Vector3 &a, const Vector3 &b, float t)
94 {
95 return Vector3(a.x + (b.x - a.x) * t,
96 a.y + (b.y - a.y) * t,
97 a.z + (b.z - a.z) * t);
98 }
100 inline Vector3 reflect(const Vector3 &v, const Vector3 &n)
101 {
102 float vdotn = dot(v, n);
103 return n * vdotn * 2.0 - v;
104 }
106 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior)
107 {
108 float cos_inc = dot(v, -n);
109 float radical = 1.0 + ior * ior * (cos_inc * cos_inc - 1.0);
111 if(radical < 0.0) { // total internal reflection
112 return -reflect(v, n);
113 }
115 float beta = ior * cos_inc - sqrt(radical);
116 return v * ior + n * beta;
117 }
119 // ---- Vector4 ----
121 class Vector4 {
122 public:
123 float x, y, z, w;
125 Vector4() : x(0), y(0), z(0), w(1.0) {}
126 Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0) {}
127 Vector4(float xx, float yy, float zz, float ww) : x(xx), y(yy), z(zz), w(ww) {}
129 float length_sq() const { return x * x + y * y + z * z + w * w; }
130 float length() const { return sqrt(x * x + y * y + z * z + w * w); }
132 void normalize()
133 {
134 float len = length();
135 if(len != 0.0) {
136 x /= len;
137 y /= len;
138 z /= len;
139 w /= len;
140 }
141 }
143 float &operator [](int idx)
144 {
145 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
146 }
147 const float &operator [](int idx) const
148 {
149 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
150 }
151 };
153 inline Vector4 operator +(const Vector4 &a, const Vector4 &b)
154 {
155 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
156 }
158 inline Vector4 operator -(const Vector4 &a, const Vector4 &b)
159 {
160 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
161 }
163 inline Vector4 operator -(const Vector4 &v)
164 {
165 return Vector4(-v.x, -v.y, -v.z, -v.w);
166 }
168 inline Vector4 operator *(const Vector4 &v, float s)
169 {
170 return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
171 }
173 inline Vector4 operator /(const Vector4 &v, float s)
174 {
175 return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
176 }
178 inline float dot(const Vector4 &a, const Vector4 &b)
179 {
180 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
181 }
183 inline Vector4 transform(const Matrix4x4 &m, const Vector4 &v)
184 {
185 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;
186 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;
187 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;
188 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;
189 return Vector4(x, y, z, w);
190 }
192 inline Vector4 lerp(const Vector4 &a, const Vector4 &b, float t)
193 {
194 return Vector4(a.x + (b.x - a.x) * t,
195 a.y + (b.y - a.y) * t,
196 a.z + (b.z - a.z) * t,
197 a.w + (b.w - a.w) * t);
198 }
201 #endif // VMATH_H_