Simulant  21.12-1292
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
vec4.h
1 #pragma once
2 
3 #include "utils.h"
4 #include <cmath>
5 #include <ostream>
6 #include <vector>
7 
8 namespace smlt {
9 
10 typedef std::vector<float> FloatArray;
11 
12 struct Vec2;
13 struct Vec3;
14 
15 struct Vec4 {
16  friend struct Vec3;
17  friend struct Mat4;
18 
19  float x;
20  float y;
21  float z;
22  float w;
23 
24  static Vec4 zero() {
25  return Vec4();
26  }
27 
28  static Vec4 one() {
29  return Vec4(1, 1, 1, 1);
30  }
31 
32  Vec4():
33  x(0), y(0), z(0), w(0) {
34  }
35 
36  Vec4(const FloatArray& arr) :
37  x(arr[0]), y(arr[1]), z(arr[2]), w(arr[3]) {}
38 
39  Vec4(float x, float y, float z, float w):
40  x(x), y(y), z(z), w(w) {
41 
42  }
43 
44  Vec4(const Vec3& v, float w);
45 
46  operator FloatArray() const {
47  return {x, y, z, w};
48  }
49 
50  bool equals(const Vec4& rhs) const {
51  return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w;
52  }
53 
54  bool operator==(const Vec4& rhs) const {
55  return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w;
56  }
57 
58  bool operator!=(const Vec4& rhs) const {
59  return (x != rhs.x) || (y != rhs.y) || (z != rhs.z) || (w != rhs.w);
60  }
61 
62  Vec4 operator+(const Vec4& rhs) const {
63  return Vec4(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w);
64  }
65 
66  Vec4 operator-(const Vec4& rhs) const {
67  return Vec4(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w);
68  }
69 
70  Vec4 operator*(const float& rhs) const {
71  return Vec4(
72  x * rhs,
73  y * rhs,
74  z * rhs,
75  w * rhs
76  );
77  }
78 
79  float length() const {
80  return fast_sqrt(length_squared());
81  }
82 
83  float length_squared() const {
84 #ifdef __DREAMCAST__
85  return MATH_Sum_of_Squares(x, y, z, w);
86 #else
87  return dot(*this);
88 #endif
89  }
90 
91  void normalize() {
92  float l = fast_inverse_sqrt(length_squared());
93  x *= l;
94  y *= l;
95  z *= l;
96  w *= l;
97  }
98 
99  float dot(const Vec4& rhs) const {
100 #ifdef __DREAMCAST__
101  return MATH_fipr(x, y, z, w, rhs.x, rhs.y, rhs.z, rhs.w);
102 #else
103  return x * rhs.x + y * rhs.y + z * rhs.z + w * rhs.w;
104 #endif
105  }
106 
107  const smlt::Vec4 normalized() const {
108  smlt::Vec4 result = *this;
109  result.normalize();
110  return result;
111  }
112 
113  Vec3 xyz() const;
114  Vec2 xy() const;
115 };
116 
117 std::ostream& operator<<(std::ostream& stream, const Vec4& vec);
118 
119 
120 }
smlt::Mat4
Definition: mat4.h:35
smlt::Vec3
Definition: vec3.h:25
smlt
Definition: animation.cpp:25
smlt::Vec4
Definition: vec4.h:15
smlt::Vec2
Definition: vec2.h:16