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

vlib: run vfmt over vlib files, so that v doc -m vlib/ can run without warnings

This commit is contained in:
Delyan Angelov
2020-10-21 12:23:03 +03:00
parent 5b1ab3b0bb
commit dab66593fc
27 changed files with 343 additions and 334 deletions

View File

@@ -1,7 +1,6 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module glm
import math
@@ -33,17 +32,17 @@ struct Vec3 {
z f32
}
pub fn vec3(x, y, z f32) Vec3 {
res := Vec3 {
x: x,
y: y,
z: z,
pub fn vec3(x f32, y f32, z f32) Vec3 {
res := Vec3{
x: x
y: y
z: z
}
return res
}
fn mat4(f &f32) Mat4 {
res := Mat4 {
res := Mat4{
data: f
}
return res
@@ -59,11 +58,11 @@ pub fn (v Vec2) str() string {
pub fn (m Mat4) str() string {
mut s := '[ '
for i in 0..4 {
for i in 0 .. 4 {
if i != 0 {
s += ' '
}
for j in 0..4 {
for j in 0 .. 4 {
val := unsafe {m.data[i * 4 + j]}
s += '${val:5.2f} '
}
@@ -75,28 +74,28 @@ pub fn (m Mat4) str() string {
return s
}
fn vec2(x, y int) Vec2 {
res := Vec2 {
x: f32(x),
y: f32(y),
fn vec2(x int, y int) Vec2 {
res := Vec2{
x: f32(x)
y: f32(y)
}
return res
}
fn (a Vec3) add(b Vec3) Vec3 {
res := Vec3 {
x: a.x + b.x,
y: a.y + b.y,
z: a.z + b.z,
res := Vec3{
x: a.x + b.x
y: a.y + b.y
z: a.z + b.z
}
return res
}
fn (a Vec3) sub(b Vec3) Vec3 {
res := Vec3 {
x: a.x - b.x,
y: a.y - b.y,
z: a.z - b.z,
res := Vec3{
x: a.x - b.x
y: a.y - b.y
z: a.z - b.z
}
return res
}
@@ -105,10 +104,10 @@ fn (a Vec3) sub(b Vec3) Vec3 {
// # return myglm_mult(a,b);
// }
fn (a Vec3) mult_scalar(b f32) Vec3 {
res := Vec3 {
x: a.x * b,
y: a.y * b,
z: a.z * b,
res := Vec3{
x: a.x * b
y: a.y * b
z: a.z * b
}
return res
}
@@ -117,7 +116,7 @@ fn (a Vec3) print() {
x := a.x
y := a.y
z := a.z
C.printf('vec3{%f,%f,%f}\n',x,y,z)
C.printf('vec3{%f,%f,%f}\n', x, y, z)
// println('vec3{$x,$y,$z}')
}
@@ -129,10 +128,10 @@ fn rotate(m Mat4, angle f32, vec Vec3) Mat4 {
return Mat4{}
}
*/
fn f32_calloc(n int) &f32 {
return voidptr(vcalloc(n * int(sizeof(f32))))
}
// fn translate(vec Vec3) *f32 {
pub fn translate(m Mat4, v Vec3) Mat4 {
// # return glm__mat4(myglm_translate(vec.x,vec.y,vec.z) );
@@ -142,12 +141,30 @@ pub fn translate(m Mat4, v Vec3) Mat4 {
y := v.y
z := v.z
unsafe {
a00 := a[0] a01 := a[1] a02 := a[2] a03 := a[3]
a10 := a[4] a11 := a[5] a12 := a[6] a13 := a[7]
a20 := a[8] a21 := a[9] a22 := a[10] a23 := a[11]
out[0] = a00 out[1] = a01 out[2] = a02 out[3] = a03
out[4] = a10 out[5] = a11 out[6] = a12 out[7] = a13
out[8] = a20 out[9] = a21 out[10] = a22 out[11] = a23
a00 := a[0]
a01 := a[1]
a02 := a[2]
a03 := a[3]
a10 := a[4]
a11 := a[5]
a12 := a[6]
a13 := a[7]
a20 := a[8]
a21 := a[9]
a22 := a[10]
a23 := a[11]
out[0] = a00
out[1] = a01
out[2] = a02
out[3] = a03
out[4] = a10
out[5] = a11
out[6] = a12
out[7] = a13
out[8] = a20
out[9] = a21
out[10] = a22
out[11] = a23
out[12] = a00 * x + a10 * y + a20 * z + a[12]
out[13] = a01 * x + a11 * y + a21 * z + a[13]
out[14] = a02 * x + a12 * y + a22 * z + a[14]
@@ -163,8 +180,8 @@ fn normalize(vec Vec3) Vec3 {
}
*/
// https://github.com/g-truc/glm/blob/0ceb2b755fb155d593854aefe3e45d416ce153a4/glm/ext/matrix_clip_space.inl
pub fn ortho(left, right, bottom, top f32) Mat4 {
//println('glm ortho($left, $right, $bottom, $top)')
pub fn ortho(left f32, right f32, bottom f32, top f32) Mat4 {
// println('glm ortho($left, $right, $bottom, $top)')
// mat<4, 4, T, defaultp> Result(static_cast<T>(1));
n := 16
mut res := f32_calloc(n)
@@ -172,16 +189,16 @@ pub fn ortho(left, right, bottom, top f32) Mat4 {
res[0] = 2.0 / (right - left)
res[5] = 2.0 / (top - bottom)
res[10] = 1.0
res[12] = - (right + left) / (right - left)
res[13] = - (top + bottom) / (top - bottom)
res[12] = -(right + left) / (right - left)
res[13] = -(top + bottom) / (top - bottom)
res[15] = 1.0
}
return mat4(res)
}
// https://github.com/g-truc/glm/blob/0ceb2b755fb155d593854aefe3e45d416ce153a4/glm/ext/matrix_clip_space.inl
pub fn ortho_zo(left, right, bottom, top, zNear, zFar f32) Mat4 {
//println('glm ortho($left, $right, $bottom, $top)')
pub fn ortho_zo(left f32, right f32, bottom f32, top f32, zNear f32, zFar f32) Mat4 {
// println('glm ortho($left, $right, $bottom, $top)')
// mat<4, 4, T, defaultp> Result(static_cast<T>(1));
n := 16
mut res := f32_calloc(n)
@@ -189,9 +206,9 @@ pub fn ortho_zo(left, right, bottom, top, zNear, zFar f32) Mat4 {
res[0] = 2.0 / (right - left)
res[5] = 2.0 / (top - bottom)
res[10] = 1.0
res[12] = - (right + left) / (right - left)
res[13] = - (top + bottom) / (top - bottom)
res[14] = - zNear / (zFar - zNear)
res[12] = -(right + left) / (right - left)
res[13] = -(top + bottom) / (top - bottom)
res[14] = -zNear / (zFar - zNear)
res[15] = 1.0
}
return mat4(res)
@@ -226,16 +243,16 @@ pub fn scale(m Mat4, v Vec3) Mat4 {
}
// multiplicates two matrices
pub fn mult(a, b Mat4) Mat4 {
pub fn mult(a Mat4, b Mat4) Mat4 {
mut out := f32_calloc(16)
for i in 0..4 {
for r in 0..4 {
for i in 0 .. 4 {
for r in 0 .. 4 {
mut prod := f32(0)
for c in 0..4 {
prod += unsafe {a.data[c*4+r] * b.data[i*4+c]}
for c in 0 .. 4 {
prod += unsafe {a.data[c * 4 + r] * b.data[i * 4 + c]}
}
unsafe {
out[i*4+r] = prod
out[i * 4 + r] = prod
}
}
}
@@ -246,48 +263,44 @@ pub fn rotate(angle f32, axis Vec3, src Mat4) Mat4 {
c := f32(math.cos(angle))
s := f32(math.sin(angle))
oneminusc := f32(1.0) - c
xy := axis.x * axis.y
yz := axis.y * axis.z
xz := axis.x * axis.z
xs := axis.x * s
ys := axis.y * s
zs := axis.z * s
f00 := axis.x * axis.x * oneminusc + c
f01 := xy * oneminusc + zs
f02 := xz * oneminusc - ys
f10 := xy * oneminusc-zs
f10 := xy * oneminusc - zs
f11 := axis.y * axis.y * oneminusc + c
f12 := yz * oneminusc + xs
f20 := xz * oneminusc + ys
f21 := yz * oneminusc - xs
f22 := axis.z *axis.z * oneminusc + c
f22 := axis.z * axis.z * oneminusc + c
data := src.data
unsafe {
t00 := data[0] * f00 + data[4] * f01 + data[8] * f02
t01 := data[1] * f00 + data[5] * f01 + data[9] * f02
t02 := data[2] * f00 + data[6] * f01 + data[10] * f02
t03 := data[3] * f00 + data[7] * f01 + data[11] * f02
t10 := data[0] * f10 + data[4] * f11 + data[8] * f12
t11 := data[1] * f10 + data[5] * f11 + data[9] * f12
t12 := data[2] * f10 + data[6] * f11 + data[10] * f12
t13 := data[3] * f10 + data[7] * f11 + data[11] * f12
mut dest := src.data
dest[8] = data[0] * f20 + data[4] * f21 + data[8] * f22
dest[9] = data[1] * f20 + data[5] * f21 + data[9] * f22
dest[10] = data[2] * f20 + data[6] * f21 + data[10] * f22
dest[11] = data[3] * f20 + data[7] * f21 + data[11] * f22
dest[0] = t00 dest[1] = t01 dest[2] = t02 dest[3] = t03
dest[4] = t10 dest[5] = t11 dest[6] = t12 dest[7] = t13
dest[0] = t00
dest[1] = t01
dest[2] = t02
dest[3] = t03
dest[4] = t10
dest[5] = t11
dest[6] = t12
dest[7] = t13
return mat4(dest)
}
}
@@ -357,23 +370,19 @@ pub fn identity2(mut res &f32) {
}
pub fn identity3() []f32 {
res := [f32(1.0), 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
]
res := [f32(1.0), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
return res
}
// https://github.com/toji/gl-matrix/blob/1549cf21dfa14a2bc845993485343d519cf064fe/src/gl-matrix/mat4.js
fn ortho_js(left, right, bottom, top f32) &f32 {
// mynear := 1
// myfar := 1
fn ortho_js(left f32, right f32, bottom f32, top f32) &f32 {
// mynear := 1
// myfar := 1
lr := 1.0 / (left - right)
bt := 1.0 / (bottom - top)
nf := f32(1.0) / 1.0// (mynear -myfar)
nf := f32(1.0) / 1.0 // (mynear -myfar)
unsafe {
mut out := &f32( malloc (int(sizeof(f32) * 16)))
mut out := &f32(malloc(int(sizeof(f32) * 16)))
out[0] = -2.0 * lr
out[1] = 0
out[2] = 0
@@ -388,18 +397,18 @@ fn ortho_js(left, right, bottom, top f32) &f32 {
out[11] = 0
out[12] = (left + right) * lr
out[13] = (top + bottom) * bt
out[14] = 1.0 * nf//(far + near) * nf;
out[14] = 1.0 * nf // (far + near) * nf;
out[15] = 1
return out
}
//f := 0.0
//return &f
// f := 0.0
// return &f
}
// fn ortho_old(a, b, c, d f32) *f32 {
// # return myglm_ortho(a,b,c,d);
// }
fn cross(a, b Vec3) Vec3 {
fn cross(a Vec3, b Vec3) Vec3 {
// # return myglm_cross(a,b);
return Vec3{}
}

View File

@@ -1,40 +1,40 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// might need special case for this
// import gl
import glm
fn cmp(a, b f32) bool {
fn cmp(a f32, b f32) bool {
return int(a * 1000) == int(b * 1000)
}
fn test_ortho() {
projection := glm.ortho(0, 200, 400, 0)
$if debug {
println(projection.data[0])
println(unsafe {projection.data[0]})
}
unsafe {
assert cmp(projection.data[0], 0.01)
assert cmp(projection.data[1], 0.000000)
assert cmp(projection.data[2], 0.000000)
assert cmp(projection.data[3], 0.000000)
assert cmp(projection.data[4], 0.000000)
assert cmp(projection.data[5], -0.005000)
assert cmp(projection.data[6], 0.000000)
assert cmp(projection.data[7], 0.000000)
assert cmp(projection.data[8], 0.000000)
assert cmp(projection.data[9], 0.000000)
assert cmp(projection.data[10], 1.000000)
assert cmp(projection.data[11], 0.000000)
assert cmp(projection.data[12], -1.000000)
assert cmp(projection.data[13], 1.000000)
assert cmp(projection.data[14], 0.000000)
assert cmp(projection.data[15], 1.000000)
}
assert cmp(projection.data[0], 0.01)
assert cmp(projection.data[1], 0.000000)
assert cmp(projection.data[2], 0.000000)
assert cmp(projection.data[3], 0.000000)
assert cmp(projection.data[4], 0.000000)
assert cmp(projection.data[5], - 0.005000)
assert cmp(projection.data[6], 0.000000)
assert cmp(projection.data[7], 0.000000)
assert cmp(projection.data[8], 0.000000)
assert cmp(projection.data[9], 0.000000)
assert cmp(projection.data[10], 1.000000)
assert cmp(projection.data[11], 0.000000)
assert cmp(projection.data[12], - 1.000000)
assert cmp(projection.data[13], 1.000000)
assert cmp(projection.data[14], 0.000000)
assert cmp(projection.data[15], 1.000000)
// f := gg.ortho(1,2,3,4)
/*
// for debugging broken tetris in gg.o
/*
// for debugging broken tetris in gg.o
# projection.data[0]=0.010000;
# projection.data[1]=0.000000;
# projection.data[2]=0.000000;
@@ -51,7 +51,7 @@ fn test_ortho() {
# projection.data[13]=1.000000;
# projection.data[14]=0.000000;
# projection.data[15]=1.000000;
*/
*/
}
fn test_rotate() {
@@ -67,16 +67,13 @@ fn test_rotate() {
$if debug {
println(m)
}
mut m1 := glm.identity()
mut m2 := glm.identity()
m1 = glm.rotate(1, glm.vec3(1, 0, 0), m1)
m2 = glm.rotate(1, glm.vec3(0, 1, 0), m2)
mut same := true
for i in 0..15 {
if m1.data[i] != m2.data[i] {
for i in 0 .. 15 {
if unsafe {m1.data[i]} != unsafe {m2.data[i]} {
same = false
}
}
@@ -85,29 +82,30 @@ fn test_rotate() {
fn test_translate() {
mut m := glm.identity()
m = glm.translate(m, glm.vec3(0, 0, - 0.5))
m = glm.translate(m, glm.vec3(0, 0, -0.5))
$if debug {
println(m)
}
assert m.data[0] == 1.0
assert m.data[1] == 0.0
assert m.data[2] == 0.0
assert m.data[3] == 0.0
//
assert m.data[4] == 0.0
assert m.data[5] == 1.0
assert m.data[6] == 0.0
assert m.data[7] == 0.0
assert m.data[8] == 0.0
assert m.data[9] == 0.0
assert m.data[10] == 1.0
assert m.data[11] == 0.0
//
assert m.data[12] == 0.0
assert m.data[13] == 0.0
assert m.data[14] == -0.5
assert m.data[15] == 1.0
unsafe {
assert m.data[0] == 1.0
assert m.data[1] == 0.0
assert m.data[2] == 0.0
assert m.data[3] == 0.0
//
assert m.data[4] == 0.0
assert m.data[5] == 1.0
assert m.data[6] == 0.0
assert m.data[7] == 0.0
assert m.data[8] == 0.0
assert m.data[9] == 0.0
assert m.data[10] == 1.0
assert m.data[11] == 0.0
//
assert m.data[12] == 0.0
assert m.data[13] == 0.0
assert m.data[14] == -0.5
assert m.data[15] == 1.0
}
}
fn f32_calloc(n int) &f32 {
@@ -116,42 +114,42 @@ fn f32_calloc(n int) &f32 {
fn test_mult1() {
mut adata := f32_calloc(16)
adata[1*4+1] = 6
adata[2*4+3] = 2
adata[0*4+2] = 3
adata[2*4+1] = 1
unsafe {
adata[1 * 4 + 1] = 6
adata[2 * 4 + 3] = 2
adata[0 * 4 + 2] = 3
adata[2 * 4 + 1] = 1
}
mut bdata := f32_calloc(16)
bdata[1*4+1] = -2
bdata[2*4+3] = 1
bdata[0*4+2] = 6
bdata[2*4+1] = -3
unsafe {
bdata[1 * 4 + 1] = -2
bdata[2 * 4 + 3] = 1
bdata[0 * 4 + 2] = 6
bdata[2 * 4 + 1] = -3
}
mut expected := f32_calloc(16)
expected[0*4+0] = 0 /* 0*0+0*0+0*6+0*0 */
expected[0*4+1] = 6 /* 0*0+0*6+1*6+0*0 */
expected[0*4+2] = 0 /* 3*0+0*0+0*6+0*0 */
expected[0*4+3] = 12 /* 0*0+0*0+2*6+0*0 */
expected[1*4+0] = 0 /* 0*0+0*-2+0*0+0*0 */
expected[1*4+1] = -12 /* 0*0­+6*-2+1*0­+0*0 */
expected[1*4+2] = 0 /* 3*0­+0*-2­+0*0­+0*0 */
expected[1*4+3] = 0 /* 0*0­+0*-2­+2*0­+0*0 */
expected[2*4+0] = 0 /* 0*0­+0*-3­+0*0­+0*1 */
expected[2*4+1] = -18 /* 0*0­+6*-3­+1*0­+0*1 */
expected[2*4+2] = 0 /* 3*0­+0*-3+0*0­+0*1 */
expected[2*4+3] = 0 /* 0*0­+0*-3­+2*0­+0*1 */
expected[3*4+0] = 0 /* 0*0­+0*0­+0*0­+0*0 */
expected[3*4+1] = 0 /* 0*0­+6*0­+1*0­+0*0 */
expected[3*4+2] = 0 /* 3*0­+0*0­+0*0­+0*0 */
expected[3*4+3] = 0 /* 0*0­+0*0­+2*0­+0*0 */
unsafe {
expected[0 * 4 + 0] = 0 // 0*0+0*0+0*6+0*0
expected[0 * 4 + 1] = 6 // 0*0+0*6+1*6+0*0
expected[0 * 4 + 2] = 0 // 3*0+0*0+0*6+0*0
expected[0 * 4 + 3] = 12 // 0*0+0*0+2*6+0*0
expected[1 * 4 + 0] = 0 // 0*0+0*-2+0*0+0*0
expected[1 * 4 + 1] = -12 // 0*0­+6*-2+1*0­+0*0
expected[1 * 4 + 2] = 0 // 3*0­+0*-2­+0*0­+0*0
expected[1 * 4 + 3] = 0 // 0*0­+0*-2­+2*0­+0*0
expected[2 * 4 + 0] = 0 // 0*0­+0*-3­+0*0­+0*1
expected[2 * 4 + 1] = -18 // 0*0­+6*-3­+1*0­+0*1
expected[2 * 4 + 2] = 0 // 3*0­+0*-3+0*0­+0*1
expected[2 * 4 + 3] = 0 // 0*0­+0*-3­+2*0­+0*1
expected[3 * 4 + 0] = 0 // 0*0­+0*0­+0*0­+0*0
expected[3 * 4 + 1] = 0 // 0*0­+6*0­+1*0­+0*0
expected[3 * 4 + 2] = 0 // 3*0­+0*0­+0*0­+0*0
expected[3 * 4 + 3] = 0 // 0*0­+0*0­+2*0­+0*0
}
mut a := glm.Mat4{adata}
b := glm.Mat4{bdata}
a = glm.mult(a, b)
for i in 0..15 {
assert a.data[i] == expected[i]
for i in 0 .. 15 {
assert unsafe {a.data[i]} == unsafe {expected[i]}
}
}
}