-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector3.h
153 lines (123 loc) · 2.7 KB
/
vector3.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
/******************************************************
g-Matrix3D Neo Engine
Copyright (c)2003 Kim Seong Wan (kaswan, Â𻧱ͽÅ)
E-mail: kaswan@hitel.net
http://www.g-matrix.pe.kr
*******************************************************/
#ifndef VECTOR3_H
#define VECTOR3_H
#include "matrix.h"
#include "vector2.h"
struct Vector3 {
union {
float x;
float u;
float r;
};
union {
float y;
float v;
float g;
};
union {
float z;
float q;
float b;
};
Vector3(){ x = y = z = 0;} // for vertex normal initialization
Vector3(float a, float b, float c)
{
x = a;
y = b;
z = c;
}
Vector3(Vector2 & vec)
{
x = vec.x;
y = vec.y;
z = 1;
}
~Vector3(){}
Vector3 operator-(void) const
{
return Vector3(-x, -y, -z);
}
Vector3 operator+(const Vector3 &rhs) const
{
return Vector3(x + rhs.x, y + rhs.y, z + rhs.z);
}
Vector3 operator-(const Vector3 &rhs) const
{
return Vector3(x - rhs.x, y - rhs.y, z - rhs.z);
}
Vector3 operator*(const float scalar) const
{
return Vector3(x * scalar, y * scalar, z * scalar);
}
Vector3 operator/(const float scalar) const
{
return Vector3(x / scalar, y / scalar, z / scalar);
}
//friend Vector3 operator-(const Vector3 &lhs, const Vector3 &rhs);
friend Vector3 operator*(const float scalar, const Vector3 &rhs);
float operator*(const Vector3 &rhs) const
{
return (x * rhs.x + y * rhs.y + z * rhs.z);
}
Vector3 operator*(const Matrix3 &m) const
{
Vector3 temp;
temp.x = x * m._11 + y * m._21 + z * m._31;
temp.y = x * m._12 + y * m._22 + z * m._32;
temp.z = x * m._13 + y * m._23 + z * m._33;
return temp;
}
Vector3 operator*(const Matrix4 &m) const
{
Vector3 temp;
temp.x = x * m._11 + y * m._21 + z * m._31;// + m._41;
temp.y = x * m._12 + y * m._22 + z * m._32;// + m._42;
temp.z = x * m._13 + y * m._23 + z * m._33;// + m._43;
return temp;
}
// Cross product
Vector3 operator^(const Vector3 &rhs) const
{
Vector3 rvalue;
rvalue.x = y * rhs.z - z * rhs.y;
rvalue.y = z * rhs.x - x * rhs.z;
rvalue.z = x * rhs.y - y * rhs.x;
return rvalue;
}
Vector3 & operator+=(const Vector3 &rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
Vector3 & operator-=(const Vector3 &rhs)
{
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}
Vector3 & operator*=(const float scalar)
{
x *= scalar;
y *= scalar;
z *= scalar;
return *this;
}
Vector3 & operator/=(const float scalar)
{
x /= scalar;
y /= scalar;
z /= scalar;
return *this;
}
void Normalize(void);
float Length(void) const;
};
#endif