Skip to content

Commit 7ec9bd7

Browse files
committed
Math additions
1 parent 8c8d6a5 commit 7ec9bd7

File tree

6 files changed

+256
-63
lines changed

6 files changed

+256
-63
lines changed

Runtime/Math/Math.sp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ extern
99
float64 sin(val: float64);
1010
float64 cos(val: float64);
1111
float64 tan(val: float64);
12+
float64 acos(val: float64);
13+
14+
int32 abs(val: int32);
15+
int64 llabs(val: int64);
16+
float64 fabs(val: float64);
1217
}
1318

1419
E: float = 2.718281828459045;
@@ -23,6 +28,8 @@ float Cos(val: float) => cos(val);
2328

2429
float Tan(val: float) => tan(val);
2530

31+
float Acos(val: float) => acos(val);
32+
2633
float Deg2Rad(deg: float) => deg * pi / 180.0;
2734

2835
int Min(l: int, r: int) =>
@@ -65,8 +72,34 @@ uint UMax(l: uint, r: uint) =>
6572
return r;
6673
}
6774

75+
float FMin(l: float, r: float) =>
76+
{
77+
if (l < r)
78+
{
79+
return l;
80+
}
81+
82+
return r;
83+
}
84+
85+
float FMax(l: float, r: float) =>
86+
{
87+
if (l > r)
88+
{
89+
return l;
90+
}
91+
92+
return r;
93+
}
94+
6895
int Clamp(value: int, min: int, max: int) => Min(Max(value, min), max);
6996

97+
float FClamp(value: float, min: float, max: float) => FMin(FMax(value, min), max);
98+
99+
int Abs(val: int) => llabs(val);
100+
101+
float FAbs(val: float) => fabs(val);
102+
70103
uint NextPowerOfTwo(value: uint)
71104
{
72105
if (!value) return 1;

Runtime/Math/Matrix/Matrix3.sp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ Matrix3::(m: [3][3]float32)
2424
this.m = m;
2525
}
2626

27+
Matrix3::(mat4: Matrix4)
28+
{
29+
m := mat4.m;
30+
m0 := m[0];
31+
m1 := m[1];
32+
m2 := m[2];
33+
34+
this.m[0] = float32:[m0[0], m0[1], m0[2]];
35+
this.m[1] = float32:[m1[0], m1[1], m1[2]];
36+
this.m[2] = float32:[m2[0], m2[1], m2[2]];
37+
}
38+
2739
Matrix3::SetIdentity() =>
2840
{
2941
this.m = [
@@ -71,5 +83,20 @@ Matrix3 Matrix3::operator::*(r: Matrix3)
7183
result.m[2][1] = x * r.m[0][1] + y * r.m[1][1] + z * r.m[2][1];
7284
result.m[2][2] = x * r.m[0][2] + y * r.m[1][2] + z * r.m[2][2];
7385

86+
return result;
87+
}
88+
89+
Vec3 Matrix3::operator::*(v: Vec3)
90+
{
91+
result := Vec3();
92+
93+
x := this.m[0];
94+
y := this.m[1];
95+
z := this.m[2];
96+
97+
result.x = v.x * x[0] + v.y * y[0] + v.z * z[0];
98+
result.y = v.x * x[1] + v.y * y[1] + v.z * z[1];
99+
result.z = v.x * x[2] + v.y * y[2] + v.z * z[2];
100+
74101
return result;
75102
}

Runtime/Math/Matrix/Matrix4.sp

Lines changed: 91 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ Matrix4::(val: float32)
2626
this.m[3] = float32:[val, val, val, val];
2727
}
2828

29+
Matrix4::(mat3: Matrix3)
30+
{
31+
m := mat3.m;
32+
m0 := m[0];
33+
m1 := m[1];
34+
m2 := m[2];
35+
36+
this.m[0] = float32:[m0[0], m0[1], m0[2], 0.0];
37+
this.m[1] = float32:[m1[0], m1[1], m1[2], 0.0];
38+
this.m[2] = float32:[m2[0], m2[1], m2[2], 0.0];
39+
}
40+
2941
Matrix4::SetIdentity() =>
3042
{
3143
this.m = [
@@ -71,13 +83,12 @@ float32 Matrix4::Determinant()
7183
return det;
7284
}
7385

74-
ref Matrix4 Matrix4::Compose(pos: Vec3, rot: Norm<Quaternion>, scale: Vec3)
86+
ref Matrix4 Matrix4::Compose(pos: Vec3, rot: Quaternion, scale: Vec3)
7587
{
76-
rotQuat := rot.vec;
77-
x := rotQuat.x;
78-
y := rotQuat.y;
79-
z := rotQuat.z;
80-
w := rotQuat.w;
88+
x := rot.x;
89+
y := rot.y;
90+
z := rot.z;
91+
w := rot.w;
8192

8293
x2 := x + x; y2 := y + y; z2 := z + z;
8394
xx := x * x2; xy := x * y2; xz := x * z2;
@@ -96,9 +107,6 @@ ref Matrix4 Matrix4::Compose(pos: Vec3, rot: Norm<Quaternion>, scale: Vec3)
96107

97108
Matrix4::Decompose(outPos: *Vec3, outRot: *Quaternion, outScale: *Vec3)
98109
{
99-
// Position
100-
outPos~ = Vec3(this.m[3][0], this.m[3][1], this.m[3][2]);
101-
102110
// Scale
103111
sx := Vec3(this.m[0][0], this.m[0][1], this.m[0][2]).Length();
104112
sy := Vec3(this.m[1][0], this.m[1][1], this.m[1][2]).Length();
@@ -123,54 +131,9 @@ Matrix4::Decompose(outPos: *Vec3, outRot: *Quaternion, outScale: *Vec3)
123131
);
124132

125133
outRot.FromRotationMatrix(mat3);
126-
}
127-
128-
ref [4]float32 Matrix4::operator::[](index: uint32) => this.m[index];
129-
130-
Matrix4 Matrix4::operator::*(r: Matrix4)
131-
{
132-
result := Matrix4();
133-
x := this.m[0][0];
134-
y := this.m[0][1];
135-
z := this.m[0][2];
136-
w := this.m[0][3];
137-
138-
result.m[0][0] = x * r.m[0][0] + y * r.m[1][0] + z * r.m[2][0] + w * r.m[3][0];
139-
result.m[0][1] = x * r.m[0][1] + y * r.m[1][1] + z * r.m[2][1] + w * r.m[3][1];
140-
result.m[0][2] = x * r.m[0][2] + y * r.m[1][2] + z * r.m[2][2] + w * r.m[3][2];
141-
result.m[0][3] = x * r.m[0][3] + y * r.m[1][3] + z * r.m[2][3] + w * r.m[3][3];
142-
143-
x = this.m[1][0];
144-
y = this.m[1][1];
145-
z = this.m[1][2];
146-
w = this.m[1][3];
147-
148-
result.m[1][0] = x * r.m[0][0] + y * r.m[1][0] + z * r.m[2][0] + w * r.m[3][0];
149-
result.m[1][1] = x * r.m[0][1] + y * r.m[1][1] + z * r.m[2][1] + w * r.m[3][1];
150-
result.m[1][2] = x * r.m[0][2] + y * r.m[1][2] + z * r.m[2][2] + w * r.m[3][2];
151-
result.m[1][3] = x * r.m[0][3] + y * r.m[1][3] + z * r.m[2][3] + w * r.m[3][3];
152-
153-
x = this.m[2][0];
154-
y = this.m[2][1];
155-
z = this.m[2][2];
156-
w = this.m[2][3];
157134

158-
result.m[2][0] = x * r.m[0][0] + y * r.m[1][0] + z * r.m[2][0] + w * r.m[3][0];
159-
result.m[2][1] = x * r.m[0][1] + y * r.m[1][1] + z * r.m[2][1] + w * r.m[3][1];
160-
result.m[2][2] = x * r.m[0][2] + y * r.m[1][2] + z * r.m[2][2] + w * r.m[3][2];
161-
result.m[2][3] = x * r.m[0][3] + y * r.m[1][3] + z * r.m[2][3] + w * r.m[3][3];
162-
163-
x = this.m[3][0];
164-
y = this.m[3][1];
165-
z = this.m[3][2];
166-
w = this.m[3][3];
167-
168-
result.m[3][0] = x * r.m[0][0] + y * r.m[1][0] + z * r.m[2][0] + w * r.m[3][0];
169-
result.m[3][1] = x * r.m[0][1] + y * r.m[1][1] + z * r.m[2][1] + w * r.m[3][1];
170-
result.m[3][2] = x * r.m[0][2] + y * r.m[1][2] + z * r.m[2][2] + w * r.m[3][2];
171-
result.m[3][3] = x * r.m[0][3] + y * r.m[1][3] + z * r.m[2][3] + w * r.m[3][3];
172-
173-
return result;
135+
// Position
136+
outPos~ = Vec3(this.m[3][0], this.m[3][1], this.m[3][2]);
174137
}
175138

176139
ref Matrix4 Matrix4::MakeRotation(angle: float32, axis: Norm<Vec3>)
@@ -207,6 +170,13 @@ ref Matrix4 Matrix4::Rotate(angle: float32, axis: Norm<Vec3>)
207170
return this;
208171
}
209172

173+
ref Matrix4 Matrix4::MakeTranslation(vec: Vec3)
174+
{
175+
this.m[3] := float32:[vec.x, vec.y, vec.z, 1.0];
176+
177+
return this;
178+
}
179+
210180
ref Matrix4 Matrix4::LookAt(camera: Vec3, center: Vec3, up: Vec3)
211181
{
212182
forward := (center - camera).Normalize().vec;
@@ -236,4 +206,69 @@ ref Matrix4 Matrix4::Perspective(fov: float32, aspect: float32, near: float32, f
236206
this.m[3] = float32:[0.0, 0.0, -(far * near) / (far - near), 0.0];
237207

238208
return this;
209+
}
210+
211+
ref [4]float32 Matrix4::operator::[](index: uint32) => this.m[index];
212+
213+
Matrix4 Matrix4::operator::*(r: Matrix4)
214+
{
215+
result := Matrix4();
216+
x := this.m[0][0];
217+
y := this.m[0][1];
218+
z := this.m[0][2];
219+
w := this.m[0][3];
220+
221+
result.m[0][0] = x * r.m[0][0] + y * r.m[1][0] + z * r.m[2][0] + w * r.m[3][0];
222+
result.m[0][1] = x * r.m[0][1] + y * r.m[1][1] + z * r.m[2][1] + w * r.m[3][1];
223+
result.m[0][2] = x * r.m[0][2] + y * r.m[1][2] + z * r.m[2][2] + w * r.m[3][2];
224+
result.m[0][3] = x * r.m[0][3] + y * r.m[1][3] + z * r.m[2][3] + w * r.m[3][3];
225+
226+
x = this.m[1][0];
227+
y = this.m[1][1];
228+
z = this.m[1][2];
229+
w = this.m[1][3];
230+
231+
result.m[1][0] = x * r.m[0][0] + y * r.m[1][0] + z * r.m[2][0] + w * r.m[3][0];
232+
result.m[1][1] = x * r.m[0][1] + y * r.m[1][1] + z * r.m[2][1] + w * r.m[3][1];
233+
result.m[1][2] = x * r.m[0][2] + y * r.m[1][2] + z * r.m[2][2] + w * r.m[3][2];
234+
result.m[1][3] = x * r.m[0][3] + y * r.m[1][3] + z * r.m[2][3] + w * r.m[3][3];
235+
236+
x = this.m[2][0];
237+
y = this.m[2][1];
238+
z = this.m[2][2];
239+
w = this.m[2][3];
240+
241+
result.m[2][0] = x * r.m[0][0] + y * r.m[1][0] + z * r.m[2][0] + w * r.m[3][0];
242+
result.m[2][1] = x * r.m[0][1] + y * r.m[1][1] + z * r.m[2][1] + w * r.m[3][1];
243+
result.m[2][2] = x * r.m[0][2] + y * r.m[1][2] + z * r.m[2][2] + w * r.m[3][2];
244+
result.m[2][3] = x * r.m[0][3] + y * r.m[1][3] + z * r.m[2][3] + w * r.m[3][3];
245+
246+
x = this.m[3][0];
247+
y = this.m[3][1];
248+
z = this.m[3][2];
249+
w = this.m[3][3];
250+
251+
result.m[3][0] = x * r.m[0][0] + y * r.m[1][0] + z * r.m[2][0] + w * r.m[3][0];
252+
result.m[3][1] = x * r.m[0][1] + y * r.m[1][1] + z * r.m[2][1] + w * r.m[3][1];
253+
result.m[3][2] = x * r.m[0][2] + y * r.m[1][2] + z * r.m[2][2] + w * r.m[3][2];
254+
result.m[3][3] = x * r.m[0][3] + y * r.m[1][3] + z * r.m[2][3] + w * r.m[3][3];
255+
256+
return result;
257+
}
258+
259+
Vec4 Matrix3::operator::*(v: Vec4)
260+
{
261+
result := Vec4();
262+
263+
x := this.m[0];
264+
y := this.m[1];
265+
z := this.m[2];
266+
w := this.m[3];
267+
268+
result.x = v.x * x[0] + v.y * y[0] + v.z * z[0] + v.w * w[0];
269+
result.y = v.x * x[1] + v.y * y[1] + v.z * z[1] + v.w * w[1];
270+
result.z = v.x * x[2] + v.y * y[2] + v.z * z[2] + v.w * w[2];
271+
result.w = v.x * x[3] + v.y * y[3] + v.z * z[3] + v.w * w[3];
272+
273+
return result;
239274
}

0 commit comments

Comments
 (0)