-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_3d.cpp
More file actions
181 lines (141 loc) · 5.89 KB
/
math_3d.cpp
File metadata and controls
181 lines (141 loc) · 5.89 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Copyright 2010 Etay Meiri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
#include "math_3d.h"
Vector3f Vector3f::Cross(const Vector3f& v) const
{
const float _x = y * v.z - z * v.y;
const float _y = z * v.x - x * v.z;
const float _z = x * v.y - y * v.x;
return Vector3f(_x, _y, _z);
}
Vector3f& Vector3f::Normalize()
{
const float Length = sqrtf(x * x + y * y + z * z);
x /= Length;
y /= Length;
z /= Length;
return *this;
}
void Vector3f::Rotate(float Angle, const Vector3f& Axe)
{
const float SinHalfAngle = sinf(ToRadian(Angle/2));
const float CosHalfAngle = cosf(ToRadian(Angle/2));
const float Rx = Axe.x * SinHalfAngle;
const float Ry = Axe.y * SinHalfAngle;
const float Rz = Axe.z * SinHalfAngle;
const float Rw = CosHalfAngle;
Quaternion RotationQ(Rx, Ry, Rz, Rw);
Quaternion ConjugateQ = RotationQ.Conjugate();
// ConjugateQ.Normalize();
Quaternion W = RotationQ * (*this) * ConjugateQ;
x = W.x;
y = W.y;
z = W.z;
}
void Matrix4f::InitScaleTransform(float ScaleX, float ScaleY, float ScaleZ)
{
m[0][0] = ScaleX; m[0][1] = 0.0f; m[0][2] = 0.0f; m[0][3] = 0.0f;
m[1][0] = 0.0f; m[1][1] = ScaleY; m[1][2] = 0.0f; m[1][3] = 0.0f;
m[2][0] = 0.0f; m[2][1] = 0.0f; m[2][2] = ScaleZ; m[2][3] = 0.0f;
m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = 0.0f; m[3][3] = 1.0f;
}
void Matrix4f::InitRotateTransform(float RotateX, float RotateY, float RotateZ)
{
Matrix4f rx, ry, rz;
const float x = ToRadian(RotateX);
const float y = ToRadian(RotateY);
const float z = ToRadian(RotateZ);
rx.m[0][0] = 1.0f; rx.m[0][1] = 0.0f ; rx.m[0][2] = 0.0f ; rx.m[0][3] = 0.0f;
rx.m[1][0] = 0.0f; rx.m[1][1] = cosf(x); rx.m[1][2] = -sinf(x); rx.m[1][3] = 0.0f;
rx.m[2][0] = 0.0f; rx.m[2][1] = sinf(x); rx.m[2][2] = cosf(x) ; rx.m[2][3] = 0.0f;
rx.m[3][0] = 0.0f; rx.m[3][1] = 0.0f ; rx.m[3][2] = 0.0f ; rx.m[3][3] = 1.0f;
ry.m[0][0] = cosf(y); ry.m[0][1] = 0.0f; ry.m[0][2] = -sinf(y); ry.m[0][3] = 0.0f;
ry.m[1][0] = 0.0f ; ry.m[1][1] = 1.0f; ry.m[1][2] = 0.0f ; ry.m[1][3] = 0.0f;
ry.m[2][0] = sinf(y); ry.m[2][1] = 0.0f; ry.m[2][2] = cosf(y) ; ry.m[2][3] = 0.0f;
ry.m[3][0] = 0.0f ; ry.m[3][1] = 0.0f; ry.m[3][2] = 0.0f ; ry.m[3][3] = 1.0f;
rz.m[0][0] = cosf(z); rz.m[0][1] = -sinf(z); rz.m[0][2] = 0.0f; rz.m[0][3] = 0.0f;
rz.m[1][0] = sinf(z); rz.m[1][1] = cosf(z) ; rz.m[1][2] = 0.0f; rz.m[1][3] = 0.0f;
rz.m[2][0] = 0.0f ; rz.m[2][1] = 0.0f ; rz.m[2][2] = 1.0f; rz.m[2][3] = 0.0f;
rz.m[3][0] = 0.0f ; rz.m[3][1] = 0.0f ; rz.m[3][2] = 0.0f; rz.m[3][3] = 1.0f;
*this = rz * ry * rx;
}
void Matrix4f::InitTranslationTransform(float x, float y, float z)
{
m[0][0] = 1.0f; m[0][1] = 0.0f; m[0][2] = 0.0f; m[0][3] = x;
m[1][0] = 0.0f; m[1][1] = 1.0f; m[1][2] = 0.0f; m[1][3] = y;
m[2][0] = 0.0f; m[2][1] = 0.0f; m[2][2] = 1.0f; m[2][3] = z;
m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = 0.0f; m[3][3] = 1.0f;
}
void Matrix4f::InitCameraTransform(const Vector3f& Target, const Vector3f& Up)
{
Vector3f N = Target;
N.Normalize();
Vector3f U = Up;
U.Normalize();
U = U.Cross(N);
Vector3f V = N.Cross(U);
m[0][0] = U.x; m[0][1] = U.y; m[0][2] = U.z; m[0][3] = 0.0f;
m[1][0] = V.x; m[1][1] = V.y; m[1][2] = V.z; m[1][3] = 0.0f;
m[2][0] = N.x; m[2][1] = N.y; m[2][2] = N.z; m[2][3] = 0.0f;
m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = 0.0f; m[3][3] = 1.0f;
}
void Matrix4f::InitPersProjTransform(float FOV, float Width, float Height, float zNear, float zFar)
{
const float ar = Width / Height;
const float zRange = zNear - zFar;
const float tanHalfFOV = tanf(ToRadian(FOV / 2.0f));
m[0][0] = 1.0f/(tanHalfFOV * ar); m[0][1] = 0.0f; m[0][2] = 0.0f; m[0][3] = 0.0;
m[1][0] = 0.0f; m[1][1] = 1.0f/tanHalfFOV; m[1][2] = 0.0f; m[1][3] = 0.0;
m[2][0] = 0.0f; m[2][1] = 0.0f; m[2][2] = (-zNear -zFar)/zRange ; m[2][3] = 2.0f * zFar*zNear/zRange;
m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = 1.0f; m[3][3] = 0.0;
}
Quaternion::Quaternion(float _x, float _y, float _z, float _w)
{
x = _x;
y = _y;
z = _z;
w = _w;
}
void Quaternion::Normalize()
{
float Length = sqrtf(x * x + y * y + z * z + w * w);
x /= Length;
y /= Length;
z /= Length;
w /= Length;
}
Quaternion Quaternion::Conjugate()
{
Quaternion ret(-x, -y, -z, w);
return ret;
}
Quaternion operator*(const Quaternion& l, const Quaternion& r)
{
const float w = (l.w * r.w) - (l.x * r.x) - (l.y * r.y) - (l.z * r.z);
const float x = (l.x * r.w) + (l.w * r.x) + (l.y * r.z) - (l.z * r.y);
const float y = (l.y * r.w) + (l.w * r.y) + (l.z * r.x) - (l.x * r.z);
const float z = (l.z * r.w) + (l.w * r.z) + (l.x * r.y) - (l.y * r.x);
Quaternion ret(x, y, z, w);
return ret;
}
Quaternion operator*(const Quaternion& q, const Vector3f& v)
{
const float w = - (q.x * v.x) - (q.y * v.y) - (q.z * v.z);
const float x = (q.w * v.x) + (q.y * v.z) - (q.z * v.y);
const float y = (q.w * v.y) + (q.z * v.x) - (q.x * v.z);
const float z = (q.w * v.z) + (q.x * v.y) - (q.y * v.x);
Quaternion ret(x, y, z, w);
return ret;
}