-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaths.cpp
173 lines (129 loc) · 3.21 KB
/
maths.cpp
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
#include "maths.h"
namespace Maths
{
XMFLOAT3 AddFloat3(XMFLOAT3 a, XMFLOAT3 b)
{
XMFLOAT3 result;
result.x = a.x + b.x;
result.y = a.y + b.y;
result.z = a.z + b.z;
return result;
}
XMFLOAT3 TakeFloat3(XMFLOAT3 a, XMFLOAT3 b)
{
XMFLOAT3 result;
result.x = a.x - b.x;
result.y = a.y - b.y;
result.z = a.z - b.z;
return result;
}
XMFLOAT3 MultFloat3(XMFLOAT3 a, XMFLOAT3 b)
{
XMFLOAT3 result;
result.x = a.x * b.x;
result.y = a.y * b.y;
result.z = a.z * b.z;
return result;
}
XMFLOAT3 ScalarFloat3(XMFLOAT3 a, float b)
{
XMFLOAT3 result = a;
result.x *= b;
result.y *= b;
result.z *= b;
return result;
}
XMFLOAT4 ScalarFloat4(XMFLOAT4 a, float b)
{
XMFLOAT4 result = a;
result.x *= b;
result.y *= b;
result.z *= b;
return result;
}
//https://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.geometric.xmvector3normalize(v=vs.85).aspx
XMVECTOR XMVector3NormalizeRobust(FXMVECTOR V)
{
// Compute the maximum absolute value component.
XMVECTOR vAbs = XMVectorAbs(V);
XMVECTOR max0 = XMVectorSplatX(vAbs);
XMVECTOR max1 = XMVectorSplatY(vAbs);
XMVECTOR max2 = XMVectorSplatZ(vAbs);
max0 = XMVectorMax(max0, max1);
max0 = XMVectorMax(max0, max2);
// Divide by the maximum absolute component.
XMVECTOR normalized = XMVectorDivide(V, max0);
// Set to zero when the original length is zero.
XMVECTOR mask = XMVectorNotEqual(g_XMZero, max0);
normalized = XMVectorAndInt(normalized, mask);
XMVECTOR t0 = XMVector3LengthSq(normalized);
XMVECTOR length = XMVectorSqrt(t0);
// Divide by the length to normalize.
normalized = XMVectorDivide(normalized, length);
// Set to zero when the original length is zero or infinity. In the
// latter case, this is considered to be an unexpected condition.
normalized = XMVectorAndInt(normalized, mask);
return normalized;
}
XMFLOAT3 ComputeNormal(XMFLOAT3 a, XMFLOAT3 b, XMFLOAT3 c)
{
//first, convert all of these to XMVECTORs
XMVECTOR aVec = XMLoadFloat3(&a);
XMVECTOR bVec = XMLoadFloat3(&b);
XMVECTOR cVec = XMLoadFloat3(&c);
XMVECTOR resultVec = XMVector3NormalizeRobust(XMVector3Cross(bVec - aVec, cVec - aVec));
XMFLOAT3 result;
XMStoreFloat3(&result, resultVec);
return result;
}
float RandFloat(float min, float max)
{
srand(time(NULL));
float result = min;
float dif = max - min;
float mult = (float)(rand() % 100) / 100.0f;
result += dif * mult;
return result;
}
int RandInt(int min, int max)
{
srand(time(NULL));
int result = min;
int dif = max - min;
result += (rand() % dif);
return result;
}
float RandFloatSeeded(float min, float max, int seed)
{
srand(seed);
float result = min;
float dif = max - min;
float mult = (float)(rand() % 10000) / 10000.0f;
result += dif * mult;
return result;
}
int RandIntSeeded(int min, int max, int seed)
{
srand(seed);
int result = min;
int dif = max - min;
result += (rand() % dif);
return result;
}
XMFLOAT3 Float4To3(XMFLOAT4 in)
{
XMFLOAT3 result;
result.x = in.x;
result.y = in.y;
result.z = in.z;
return result;
}
XMFLOAT4 Float3To4(XMFLOAT3 in)
{
XMFLOAT4 result;
result.x = in.x;
result.y = in.y;
result.z = in.z;
return result;
}
}