gg.m4: fix a bug on translate, remove abs fn (#9224)

pull/9176/head^2
penguindark 2021-03-10 19:20:17 +01:00 committed by GitHub
parent f280a5c230
commit c554e0b33d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 64 deletions

View File

@ -192,10 +192,10 @@ fn test_vec4() {
assert v + m4.Vec4{ e: [f32(5), 6, 7, 8]! } == m4.Vec4{ e: [f32(6), 8, 10, 12]! }
assert v - m4.Vec4{ e: [f32(1), 2, 3, 4]! } == m4.Vec4{ e: [f32(0), 0, 0, 0]! }
assert v.mul_vec4(m4.Vec4{ e: [f32(2), 2, 2, 2]! }) == m4.Vec4{ e: [f32(2), 4, 6, 8]! }
assert m4.abs(v.normalize().mod() - 1) < m4.precision
assert f32_abs(v.normalize().mod() - 1) < m4.precision
v = m4.Vec4{[f32(1), 2, 3, 0]!}
assert m4.abs(v.normalize3().mod3() - 1) < m4.precision
assert m4.abs(v.normalize3().mod() - 1) < m4.precision
assert f32_abs(v.normalize3().mod3() - 1) < m4.precision
assert f32_abs(v.normalize3().mod() - 1) < m4.precision
// cross product
// x y z
// 1 2 3 ==> -3 6 -3 0

View File

@ -27,14 +27,6 @@ pub const precision = f32(10e-7)
* Utility
*
*********************************************************************/
pub fn abs(a f32) f32 {
if a >= f32(0.0) {
return a
} else {
return -a
}
}
// String representation of the matrix
pub fn (x Mat4) str() string {
unsafe {
@ -51,7 +43,7 @@ pub fn (a Mat4) clean() Mat4 {
unsafe {
x := Mat4{}
for c, value in a.e {
if abs(value) < m4.precision {
if f32_abs(value) < m4.precision {
x.e[c] = 0
} else {
x.e[c] = value
@ -75,7 +67,7 @@ pub fn (x Mat4) sum_all() f32 {
pub fn (x Mat4) is_equal(y Mat4) bool {
unsafe {
for c, value in x.e {
if abs(value - y.e[c]) > m4.precision {
if f32_abs(value - y.e[c]) > m4.precision {
return false
}
}
@ -163,7 +155,7 @@ pub fn (mut x Mat4) set_f32(value f32) {
[unsafe]
pub fn (mut x Mat4) set_row(row int, v3 Vec4) {
unsafe {
x.e[row * 4] = v3.e[0]
x.e[row * 4 + 0] = v3.e[0]
x.e[row * 4 + 1] = v3.e[1]
x.e[row * 4 + 2] = v3.e[2]
x.e[row * 4 + 3] = v3.e[3]
@ -177,7 +169,7 @@ pub fn (x Mat4) get_row(row int) Vec4 {
unsafe {
return Vec4{
e: [
x.e[row * 4],
x.e[row * 4 + 0],
x.e[row * 4 + 1],
x.e[row * 4 + 2],
x.e[row * 4 + 3],
@ -241,17 +233,17 @@ pub fn (mut x Mat4) swap_col(col1 int, col2 int) {
[unsafe]
pub fn (mut x Mat4) swap_row(row1 int, row2 int) {
unsafe {
v0 := x.e[row1 * 4]
v0 := x.e[row1 * 4 + 0]
v1 := x.e[row1 * 4 + 1]
v2 := x.e[row1 * 4 + 2]
v3 := x.e[row1 * 4 + 3]
x.e[row1 * 4] = x.e[row2 * 4]
x.e[row1 * 4 + 0] = x.e[row2 * 4 + 0]
x.e[row1 * 4 + 1] = x.e[row2 * 4 + 1]
x.e[row1 * 4 + 2] = x.e[row2 * 4 + 2]
x.e[row1 * 4 + 3] = x.e[row2 * 4 + 3]
x.e[row2 * 4] = v0
x.e[row2 * 4 + 0] = v0
x.e[row2 * 4 + 1] = v1
x.e[row2 * 4 + 2] = v2
x.e[row2 * 4 + 3] = v3
@ -410,15 +402,6 @@ pub fn mul(a Mat4, b Mat4) Mat4 {
// Multiply a Matrix by a vector
pub fn mul_vec(a Mat4, v Vec4) Vec4 {
unsafe {
/*
return Vec4{ e: [
a.e[0] * v.e[0] + a.e[4] * v.e[1] + a.e[8] * v.e[2] + a.e[12] * v.e[3],
a.e[1] * v.e[0] + a.e[5] * v.e[1] + a.e[9] * v.e[2] + a.e[13] * v.e[3],
a.e[2] * v.e[0] + a.e[6] * v.e[1] + a.e[10] * v.e[2] + a.e[14] * v.e[3],
a.e[3] * v.e[0] + a.e[7] * v.e[1] + a.e[11] * v.e[2] + a.e[15] * v.e[3],
]!
}
*/
return Vec4{ e: [
a.e[0 ] * v.e[0] + a.e[1 ] * v.e[1] + a.e[2 ] * v.e[2] + a.e[3 ] * v.e[3],
a.e[4 ] * v.e[0] + a.e[5 ] * v.e[1] + a.e[6 ] * v.e[2] + a.e[7 ] * v.e[3],
@ -589,10 +572,10 @@ pub fn rotate(angle f32, w Vec4) Mat4 {
pub fn (x Mat4) translate(w Vec4) Mat4 {
unsafe {
return Mat4{ e: [
x.e[0], x.e[1], x.e[2], x.e[3] + w.e[0],
x.e[4], x.e[5], x.e[6], x.e[7] + w.e[1],
x.e[8], x.e[9], x.e[10], x.e[11] + w.e[2],
x.e[12], x.e[13], x.e[14], x.e[15],
x.e[0], x.e[1], x.e[2 ], x.e[3 ] ,
x.e[4], x.e[5], x.e[6 ], x.e[7 ] ,
x.e[8], x.e[9], x.e[10], x.e[11] ,
x.e[12] + w.e[0], x.e[13] + w.e[1], x.e[14] + w.e[2], x.e[15],
]!
}
}

View File

@ -36,7 +36,7 @@ pub fn vec3(x f32, y f32, z f32) Vec4 {
pub fn (a Vec4) clean() Vec4 {
mut x := Vec4{}
for c, value in a.e {
if abs(value) < precision {
if f32_abs(value) < precision {
x.e[c] = 0
} else {
x.e[c] = value