1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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
}
}
@ -118,9 +110,9 @@ pub fn (mut x Mat4) set_f(index_col int, index_row int, value f32) {
pub fn (mut x Mat4) copy(y Mat4) {
unsafe {
x.e = [
y.e[0], y.e[1], y.e[2], y.e[3],
y.e[4], y.e[5], y.e[6], y.e[7],
y.e[8], y.e[9], y.e[10], y.e[11],
y.e[0 ], y.e[1 ], y.e[2 ], y.e[3 ],
y.e[4 ], y.e[5 ], y.e[6 ], y.e[7 ],
y.e[8 ], y.e[9 ], y.e[10], y.e[11],
y.e[12], y.e[13], y.e[14], y.e[15],
]!
}
@ -129,8 +121,8 @@ pub fn (mut x Mat4) copy(y Mat4) {
// Set the trace of the matrix using a vec4
pub fn (mut x Mat4) set_trace(v3 Vec4) {
unsafe {
x.e[0] = v3.e[0]
x.e[5] = v3.e[1]
x.e[0 ] = v3.e[0]
x.e[5 ] = v3.e[1]
x.e[10] = v3.e[2]
x.e[15] = v3.e[3]
}
@ -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],
@ -192,8 +184,8 @@ pub fn (x Mat4) get_row(row int) Vec4 {
pub fn (mut x Mat4) set_col(col int, v3 Vec4) {
unsafe {
x.e[col] = v3.e[0]
x.e[col + 4] = v3.e[1]
x.e[col + 8] = v3.e[2]
x.e[col + 4 ] = v3.e[1]
x.e[col + 8 ] = v3.e[2]
x.e[col + 12] = v3.e[3]
}
}
@ -206,8 +198,8 @@ pub fn (x Mat4) get_col(col int) Vec4 {
return Vec4{
e: [
x.e[col],
x.e[col + 4],
x.e[col + 8],
x.e[col + 4 ],
x.e[col + 8 ],
x.e[col + 12],
]!
}
@ -220,18 +212,18 @@ pub fn (x Mat4) get_col(col int) Vec4 {
pub fn (mut x Mat4) swap_col(col1 int, col2 int) {
unsafe {
v0 := x.e[col1]
v1 := x.e[col1 + 4]
v2 := x.e[col1 + 8]
v1 := x.e[col1 + 4 ]
v2 := x.e[col1 + 8 ]
v3 := x.e[col1 + 12]
x.e[col1] = x.e[col2]
x.e[col1 + 4] = x.e[col2 + 4]
x.e[col1 + 8] = x.e[col2 + 8]
x.e[col1 + 4 ] = x.e[col2 + 4 ]
x.e[col1 + 8 ] = x.e[col2 + 8 ]
x.e[col1 + 12] = x.e[col2 + 12]
x.e[col2] = v0
x.e[col2 + 4] = v1
x.e[col2 + 8] = v2
x.e[col2 + 4 ] = v1
x.e[col2 + 8 ] = v2
x.e[col2 + 12] = v3
}
}
@ -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
@ -265,10 +257,10 @@ pub fn (mut x Mat4) swap_row(row1 int, row2 int) {
pub fn (x Mat4) transpose() Mat4 {
unsafe {
return Mat4{ e: [
x.e[0], x.e[4], x.e[8], x.e[12],
x.e[1], x.e[5], x.e[9], x.e[13],
x.e[2], x.e[6], x.e[10], x.e[14],
x.e[3], x.e[7], x.e[11], x.e[15],
x.e[0 ], x.e[4 ], x.e[8 ], x.e[12],
x.e[1 ], x.e[5 ], x.e[9 ], x.e[13],
x.e[2 ], x.e[6 ], x.e[10], x.e[14],
x.e[3 ], x.e[7 ], x.e[11], x.e[15],
]!
}
}
@ -278,9 +270,9 @@ pub fn (x Mat4) transpose() Mat4 {
pub fn (x Mat4) mul_scalar(s f32) Mat4 {
unsafe {
return Mat4{ e: [
x.e[0] * s, x.e[1] * s, x.e[2] * s, x.e[3] * s,
x.e[4] * s, x.e[5] * s, x.e[6] * s, x.e[7] * s,
x.e[8] * s, x.e[9] * s, x.e[10] * s, x.e[11] * s,
x.e[0 ] * s, x.e[1 ] * s, x.e[2 ] * s, x.e[3 ] * s,
x.e[4 ] * s, x.e[5 ] * s, x.e[6 ] * s, x.e[7 ] * s,
x.e[8 ] * s, x.e[9 ] * s, x.e[10] * s, x.e[11] * s,
x.e[12] * s, x.e[13] * s, x.e[14] * s, x.e[15] * s,
]!
}
@ -335,9 +327,9 @@ pub fn set_m4(value f32) Mat4 {
pub fn (a Mat4) + (b Mat4) Mat4 {
unsafe {
return Mat4{ e: [
a.e[0] + b.e[0], a.e[1] + b.e[1], a.e[2] + b.e[2], a.e[3] + b.e[3],
a.e[4] + b.e[4], a.e[5] + b.e[5], a.e[6] + b.e[6], a.e[7] + b.e[7],
a.e[8] + b.e[8], a.e[9] + b.e[9], a.e[10] + b.e[10], a.e[11] + b.e[11],
a.e[0 ] + b.e[0 ], a.e[1 ] + b.e[1 ], a.e[2 ] + b.e[2 ], a.e[3 ] + b.e[3 ],
a.e[4 ] + b.e[4 ], a.e[5 ] + b.e[5 ], a.e[6 ] + b.e[6 ], a.e[7 ] + b.e[7 ],
a.e[8 ] + b.e[8 ], a.e[9 ] + b.e[9 ], a.e[10] + b.e[10], a.e[11] + b.e[11],
a.e[12] + b.e[12], a.e[13] + b.e[13], a.e[14] + b.e[14], a.e[15] + b.e[15],
]!
}
@ -348,9 +340,9 @@ pub fn (a Mat4) + (b Mat4) Mat4 {
pub fn (a Mat4) - (b Mat4) Mat4 {
unsafe {
return Mat4{ e: [
a.e[0] - b.e[0], a.e[1] - b.e[1], a.e[2] - b.e[2], a.e[3] - b.e[3],
a.e[4] - b.e[4], a.e[5] - b.e[5], a.e[6] - b.e[6], a.e[7] - b.e[7],
a.e[8] - b.e[8], a.e[9] - b.e[9], a.e[10] - b.e[10], a.e[11] - b.e[11],
a.e[0 ] - b.e[0 ], a.e[1 ] - b.e[1 ], a.e[2 ] - b.e[2 ], a.e[3 ] - b.e[3 ],
a.e[4 ] - b.e[4 ], a.e[5 ] - b.e[5 ], a.e[6 ] - b.e[6 ], a.e[7 ] - b.e[7 ],
a.e[8 ] - b.e[8 ], a.e[9 ] - b.e[9 ], a.e[10] - b.e[10], a.e[11] - b.e[11],
a.e[12] - b.e[12], a.e[13] - b.e[13], a.e[14] - b.e[14], a.e[15] - b.e[15],
]!
}
@ -410,19 +402,10 @@ 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],
a.e[8] * v.e[0] + a.e[9] * v.e[1] + a.e[10] * v.e[2] + a.e[11] * v.e[3],
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],
a.e[8 ] * v.e[0] + a.e[9 ] * v.e[1] + a.e[10] * v.e[2] + a.e[11] * v.e[3],
a.e[12] * v.e[0] + a.e[13] * v.e[1] + a.e[14] * v.e[2] + a.e[15] * 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