-
Notifications
You must be signed in to change notification settings - Fork 5
/
vector4.h
executable file
·156 lines (129 loc) · 2.79 KB
/
vector4.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/******************************************************
g-Matrix3D Neo Engine
Copyright (c)2003 Kim Seong Wan (kaswan, Â𻧱ͽÅ)
E-mail: kaswan@hitel.net
http://www.g-matrix.pe.kr
*******************************************************/
#ifndef VECTOR4_H
#define VECTOR4_H
#include "matrix.h"
#include "vector3.h"
struct Vector4 {
union {
float x;
float u;
float r;
};
union {
float y;
float v;
float g;
};
union {
float z;
//float r;
float b;
};
union {
float w;
float rhw;
float q;
float a;
};
Vector4(){}
Vector4(float a, float b, float c, float d = 1)
{
x = a;
y = b;
z = c;
w = d;
}
Vector4(const Vector3 & vec)
{
x = vec.x;
y = vec.y;
z = vec.z;
w = 1;
}
~Vector4(){}
Vector4 operator-(void) const
{
return Vector4(-x, -y, -z, -w);
}
Vector4 operator+(const Vector4 &rhs) const
{
return Vector4(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w);
}
Vector4 operator-(const Vector4 &rhs) const
{
return Vector4(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w);
}
Vector4 operator*(const float scalar) const
{
return Vector4(x * scalar, y * scalar, z * scalar, w * scalar);
}
Vector4 operator/(const float scalar) const
{
return Vector4(x / scalar, y / scalar, z / scalar, w / scalar);
}
friend Vector4 operator*(const float scalar, const Vector4 &rhs);
float operator*(const Vector4 &rhs) const
{
//return (x * rhs.x + y * rhs.y + z * rhs.z + w * rhs.w);
return (x * rhs.x + y * rhs.y + z * rhs.z);
}
Vector4 operator*(const Matrix4 &m) const
{
Vector4 temp;
temp.x = x * m._11 + y * m._21 + z * m._31 + w * m._41;
temp.y = x * m._12 + y * m._22 + z * m._32 + w * m._42;
temp.z = x * m._13 + y * m._23 + z * m._33 + w * m._43;
temp.w = x * m._14 + y * m._24 + z * m._34 + w * m._44;
return temp;
}
Vector4 & operator*=(const Matrix4 &m)
{
Vector4 temp;
temp.x = x * m._11 + y * m._21 + z * m._31 + w * m._41;
temp.y = x * m._12 + y * m._22 + z * m._32 + w * m._42;
temp.z = x * m._13 + y * m._23 + z * m._33 + w * m._43;
temp.w = x * m._14 + y * m._24 + z * m._34 + w * m._44;
*this = temp;
return *this;
}
Vector4 & operator+=(const Vector4 &rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
w += rhs.w;
return *this;
}
Vector4 & operator-=(const Vector4 &rhs)
{
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
w -= rhs.w;
return *this;
}
Vector4 & operator*=(const float scalar)
{
x *= scalar;
y *= scalar;
z *= scalar;
w *= scalar;
return *this;
}
Vector4 & operator/=(const float scalar)
{
x /= scalar;
y /= scalar;
z /= scalar;
w /= scalar;
return *this;
}
public:
void PerspectiveDivide(void);
};
#endif