1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #ifndef FLOAT4_H
4 #define FLOAT4_H
5 
6 #include "System/float3.h"
7 #include "System/creg/creg_cond.h"
8 
9 /** Float3 with a fourth data member, which is basically unused but required
10     to be able to pass data into e.g. OpenGL functions that expect
11     an array of 4 floats. */
12 struct float4 : public float3
13 {
14 	CR_DECLARE_STRUCT(float4)
15 
16 	union {
17 		struct { float w; };
18 		struct { float a; };
19 		struct { float y2; };
20 		struct { float q; };
21 		struct { float yend; };
22 	};
23 
24 	float4();
float3float425 	float4(const float3& f, const float w = 0.0f): float3(f), w(w) {}
float4float426 	float4(const float* f): float3(f[0], f[1], f[2]), w(f[3]) {}
float3float427 	float4(const float x, const float y, const float z, const float w = 0.0f): float3(x, y, z), w(w) {}
28 
29 	inline float4& operator= (const float f[4]) {
30 		x = f[0]; y = f[1];
31 		z = f[2]; w = f[3];
32 		return *this;
33 	}
34 
35 	inline float4& operator= (const float3& f) {
36 		x = f.x;
37 		y = f.y;
38 		z = f.z;
39 		return *this;
40 	}
41 
42 	inline float4& operator= (const float4& f) {
43 		x = f.x; y = f.y;
44 		z = f.z; w = f.w;
45 		return *this;
46 	}
47 
48 
49 	inline float4& operator += (const float4& f) {
50 		x += f.x; y += f.y;
51 		z += f.z; w += f.w;
52 		return *this;
53 	}
54 	inline float4& operator -= (const float4& f) {
55 		x -= f.x; y -= f.y;
56 		z -= f.z; w -= f.w;
57 		return *this;
58 	}
59 	inline float4& operator *= (const float4& f) {
60 		x *= f.x; y *= f.y;
61 		z *= f.z; w *= f.w;
62 		return *this;
63 	}
64 #if 0
65 	inline float4 operator + (const float3& f) const { return float4(x + f.x, y + f.y, z + f.z, w); }
66 	inline float4 operator - (const float3& f) const { return float4(x - f.x, y - f.y, z - f.z, w); }
67 
68 	inline float4& operator += (const float3& f) {
69 		x += f.x;
70 		y += f.y;
71 		z += f.z;
72 		return *this;
73 	}
74 	inline float4& operator -= (const float3& f) {
75 		x -= f.x;
76 		y -= f.y;
77 		z -= f.z;
78 		return *this;
79 	}
80 #endif
81 
82 	// (in)equality tests between float4 and float3 ignore the w-component
83 	inline bool operator == (const float3& f) const { return (this->float3::operator == (f)); }
84 	inline bool operator != (const float3& f) const { return (this->float3::operator != (f)); }
85 
86 	inline bool operator == (const float4& f) const {
87 		return math::fabs(x - f.x) <= math::fabs(float3::CMP_EPS * x)
88 			&& math::fabs(y - f.y) <= math::fabs(float3::CMP_EPS * y)
89 			&& math::fabs(z - f.z) <= math::fabs(float3::CMP_EPS * z)
90 			&& math::fabs(w - f.w) <= math::fabs(float3::CMP_EPS * w);
91 	}
92 
93 	inline bool operator != (const float4& f) const {
94 		return !(*this == f);
95 	}
96 
97 
dot4float498 	inline float dot4(const float4& f) const {
99 		return (x * f.x) + (y * f.y) + (z * f.z) + (w * f.w);
100 	}
101 
102 	/// Allows implicit conversion to float* (for passing to gl functions)
103 	operator const float* () const { return reinterpret_cast<const float*>(&x); }
104 	operator float* () { return reinterpret_cast<float*>(&x); }
105 };
106 
107 #endif /* FLOAT4_H */
108