mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vfmt: implement support for // vfmt off and // vfmt on, with it, v fmt -w .
now works. (#16335)
This commit is contained in:
parent
131d07aede
commit
b52b8429d4
@ -1,3 +1,6 @@
|
||||
## V 0.3.3
|
||||
-*Not yet released*
|
||||
- vfmt now supports `// vfmt off` and `// vfmt on` for turning off the formatting locally for *short* snippets of code. Useful for keeping your carefully arranged matrices in tact.
|
||||
## V 0.3.2
|
||||
*31 Oct 2022*
|
||||
- New simplified string interpolation: `println("Hello, {name}!")`. It will be the only way, old syntax (`${name}` and `$name`)
|
||||
|
@ -151,13 +151,11 @@ pub fn (mut ts TestSession) print_messages() {
|
||||
|
||||
pub fn new_test_session(_vargs string, will_compile bool) TestSession {
|
||||
mut skip_files := []string{}
|
||||
|
||||
// Skip the call_v_from_c files. They need special instructions for compilation.
|
||||
// Check the README.md for detailed information.
|
||||
skip_files << 'examples/call_v_from_c/v_test_print.v'
|
||||
skip_files << 'examples/call_v_from_c/v_test_math.v'
|
||||
|
||||
if will_compile {
|
||||
// Skip the call_v_from_c files. They need special instructions for compilation.
|
||||
// Check the README.md for detailed information.
|
||||
skip_files << 'examples/call_v_from_c/v_test_print.v'
|
||||
skip_files << 'examples/call_v_from_c/v_test_math.v'
|
||||
$if msvc {
|
||||
skip_files << 'vlib/v/tests/const_comptime_eval_before_vinit_test.v' // _constructor used
|
||||
}
|
||||
|
@ -181,7 +181,7 @@ fn (foptions &FormatOptions) format_pipe() {
|
||||
input_text := os.get_raw_lines_joined()
|
||||
file_ast := parser.parse_text(input_text, '', table, .parse_comments, prefs)
|
||||
// checker.new_checker(table, prefs).check(file_ast)
|
||||
formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug)
|
||||
formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug, source_text: input_text)
|
||||
print(formatted_content)
|
||||
flush_stdout()
|
||||
foptions.vlog('fmt.fmt worked and $formatted_content.len bytes were written to stdout.')
|
||||
|
@ -33,20 +33,7 @@ const vet_folders = [
|
||||
'examples/term.ui',
|
||||
]
|
||||
|
||||
const verify_known_failing_exceptions = [
|
||||
// Handcrafted meaningful formatting of code parts (mostly arrays)
|
||||
'examples/sokol/02_cubes_glsl/cube_glsl.v',
|
||||
'examples/sokol/03_march_tracing_glsl/rt_glsl.v',
|
||||
'examples/sokol/04_multi_shader_glsl/rt_glsl.v',
|
||||
'examples/sokol/05_instancing_glsl/rt_glsl.v',
|
||||
'vlib/v/checker/tests/modules/deprecated_module/main.v' /* adds deprecated_module. module prefix to imports, even though the folder has v.mod */,
|
||||
'vlib/gg/m4/graphic.v',
|
||||
'vlib/gg/m4/m4_test.v',
|
||||
'vlib/gg/m4/matrix.v'
|
||||
// TODOs and unfixed vfmt bugs
|
||||
'vlib/v/tests/inout/string_interpolation_inner_expr_cbr.vv', /* for new string interpolation, prevent resolving to nested interpolation */
|
||||
'vlib/v/tests/string_new_interpolation_test.v', /* new string interpolation */
|
||||
]
|
||||
const verify_known_failing_exceptions = []string{}
|
||||
|
||||
const vfmt_verify_list = [
|
||||
'cmd/',
|
||||
@ -55,11 +42,7 @@ const vfmt_verify_list = [
|
||||
'vlib/',
|
||||
]
|
||||
|
||||
const vfmt_known_failing_exceptions = arrays.merge(verify_known_failing_exceptions, [
|
||||
'vlib/regex/regex_test.v' /* contains meaningfull formatting of the test case data */,
|
||||
'vlib/crypto/sha512/sha512block_generic.v' /* formatting of large constant arrays wraps to too many lines */,
|
||||
'vlib/crypto/aes/const.v' /* formatting of large constant arrays wraps to too many lines */,
|
||||
])
|
||||
const vfmt_known_failing_exceptions = arrays.merge(verify_known_failing_exceptions, []string{})
|
||||
|
||||
const vexe = os.getenv('VEXE')
|
||||
|
||||
|
@ -103,9 +103,11 @@ fn draw_triangle() {
|
||||
sgl.defaults()
|
||||
sgl.begin_triangles()
|
||||
{
|
||||
// vfmt off
|
||||
sgl.v2f_c3b( 0.0, 0.5, 255, 0 , 0 )
|
||||
sgl.v2f_c3b(-0.5, -0.5, 0, 0 , 255)
|
||||
sgl.v2f_c3b( 0.5, -0.5, 0, 255, 0 )
|
||||
// vfmt on
|
||||
}
|
||||
sgl.end()
|
||||
}
|
||||
@ -114,9 +116,9 @@ fn draw_triangle() {
|
||||
fn cube() {
|
||||
sgl.begin_quads()
|
||||
{
|
||||
// edge color
|
||||
sgl.c3f(1.0, 0.0, 0.0)
|
||||
// edge coord
|
||||
// vfmt off
|
||||
sgl.c3f(1.0, 0.0, 0.0) // edge color
|
||||
// edge coordinates
|
||||
// x,y,z, texture cord: u,v
|
||||
sgl.v3f_t2f(-1.0, 1.0, -1.0, -1.0, 1.0)
|
||||
sgl.v3f_t2f( 1.0, 1.0, -1.0, 1.0, 1.0)
|
||||
@ -147,6 +149,7 @@ fn cube() {
|
||||
sgl.v3f_t2f(-1.0, 1.0, 1.0, 1.0, 1.0)
|
||||
sgl.v3f_t2f( 1.0, 1.0, 1.0, 1.0, -1.0)
|
||||
sgl.v3f_t2f( 1.0, 1.0, -1.0, -1.0, -1.0)
|
||||
// vfmt on
|
||||
}
|
||||
sgl.end()
|
||||
}
|
||||
@ -177,8 +180,10 @@ fn draw_cubes(app App) {
|
||||
{
|
||||
sgl.translate(0.0, 0.0, 3.0)
|
||||
sgl.scale(0.5, 0.5, 0.5)
|
||||
// vfmt off
|
||||
sgl.rotate(-3.0 * sgl.rad(2 * rot[0]), 1.0, 0.0, 0.0)
|
||||
sgl.rotate(3.0 * sgl.rad(2 * rot[1]), 0.0, 0.0, 1.0)
|
||||
sgl.rotate( 3.0 * sgl.rad(2 * rot[1]), 0.0, 0.0, 1.0)
|
||||
// vfmt on
|
||||
cube()
|
||||
}
|
||||
sgl.pop_matrix()
|
||||
@ -189,10 +194,10 @@ fn draw_cubes(app App) {
|
||||
fn cube_texture(r f32, g f32, b f32) {
|
||||
sgl.begin_quads()
|
||||
{
|
||||
// edge color
|
||||
sgl.c3f(r, g, b)
|
||||
sgl.c3f(r, g, b) // edge color
|
||||
// edge coord
|
||||
// x,y,z, texture cord: u,v
|
||||
// vfmt off
|
||||
sgl.v3f_t2f(-1.0, 1.0, -1.0, 0.0 , 0.25)
|
||||
sgl.v3f_t2f( 1.0, 1.0, -1.0, 0.25, 0.25)
|
||||
sgl.v3f_t2f( 1.0, -1.0, -1.0, 0.25, 0.0 )
|
||||
@ -222,6 +227,7 @@ fn cube_texture(r f32, g f32, b f32) {
|
||||
sgl.v3f_t2f(-1.0, 1.0, 1.0, 0.25, 0.25)
|
||||
sgl.v3f_t2f( 1.0, 1.0, 1.0, 0.25, 0.0 )
|
||||
sgl.v3f_t2f( 1.0, 1.0, -1.0, 0.0 , 0.0 )
|
||||
// vfmt on
|
||||
}
|
||||
sgl.end()
|
||||
}
|
||||
@ -253,6 +259,7 @@ fn init_cube_glsl(mut app App) {
|
||||
// d := u16(32767/8) // for compatibility with D3D11, 32767 stand for 1
|
||||
d := f32(1.0) // 0.05)
|
||||
c := u32(0xFFFFFF_FF) // color RGBA8
|
||||
// vfmt off
|
||||
vertices := [
|
||||
// Face 0
|
||||
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
|
||||
@ -285,8 +292,11 @@ fn init_cube_glsl(mut app App) {
|
||||
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
|
||||
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
|
||||
]
|
||||
// vfmt on
|
||||
|
||||
mut vert_buffer_desc := gfx.BufferDesc{label: c'cube-vertices'}
|
||||
mut vert_buffer_desc := gfx.BufferDesc{
|
||||
label: c'cube-vertices'
|
||||
}
|
||||
unsafe { vmemset(&vert_buffer_desc, 0, int(sizeof(vert_buffer_desc))) }
|
||||
|
||||
vert_buffer_desc.size = usize(vertices.len * int(sizeof(Vertex_t)))
|
||||
@ -300,14 +310,16 @@ fn init_cube_glsl(mut app App) {
|
||||
vbuf := gfx.make_buffer(&vert_buffer_desc)
|
||||
|
||||
// create an index buffer for the cube
|
||||
// vfmt off
|
||||
indices := [
|
||||
u16(0), 1, 2, 0, 2, 3,
|
||||
6, 5, 4, 7, 6, 4,
|
||||
8, 9, 10, 8, 10, 11,
|
||||
14, 13, 12, 15, 14, 12,
|
||||
16, 17, 18, 16, 18, 19,
|
||||
22, 21, 20, 23, 22, 20
|
||||
u16(0), 1, 2, 0, 2, 3,
|
||||
6, 5, 4, 7, 6, 4,
|
||||
8, 9, 10, 8, 10, 11,
|
||||
14, 13, 12, 15, 14, 12,
|
||||
16, 17, 18, 16, 18, 19,
|
||||
22, 21, 20, 23, 22, 20,
|
||||
]
|
||||
// vfmt on
|
||||
|
||||
mut index_buffer_desc := gfx.BufferDesc{
|
||||
label: c'cube-indices'
|
||||
@ -331,9 +343,9 @@ fn init_cube_glsl(mut app App) {
|
||||
|
||||
pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
|
||||
// the constants [C.ATTR_vs_pos, C.ATTR_vs_color0, C.ATTR_vs_texcoord0] are generated bysokol-shdc
|
||||
pipdesc.layout.attrs[C.ATTR_vs_pos ].format = .float3 // x,y,z as f32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_color0 ].format = .ubyte4n // color as u32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .float2 // u,v as f32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_pos].format = .float3 // x,y,z as f32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_color0].format = .ubyte4n // color as u32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .float2 // u,v as f32
|
||||
// pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .short2n // u,v as u16
|
||||
|
||||
pipdesc.shader = shader
|
||||
@ -443,8 +455,10 @@ fn draw_texture_cubes(app App) {
|
||||
{
|
||||
sgl.translate(0.0, 0.0, 3.0)
|
||||
sgl.scale(0.5, 0.5, 0.5)
|
||||
// vfmt off
|
||||
sgl.rotate(-3.0 * sgl.rad(2 * rot[0]), 1.0, 0.0, 0.0)
|
||||
sgl.rotate(3.0 * sgl.rad(2 * rot[1]), 0.0, 0.0, 1.0)
|
||||
sgl.rotate( 3.0 * sgl.rad(2 * rot[1]), 0.0, 0.0, 1.0)
|
||||
// vfmt on
|
||||
cube_texture(1, 1, 1)
|
||||
}
|
||||
sgl.pop_matrix()
|
||||
|
@ -122,6 +122,7 @@ fn init_cube_glsl(mut app App) {
|
||||
// d := u16(32767) // for compatibility with D3D11, 32767 stand for 1
|
||||
d := f32(1.0)
|
||||
c := u32(0xFFFFFF_FF) // color RGBA8
|
||||
// vfmt off
|
||||
vertices := [
|
||||
// Face 0
|
||||
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
|
||||
@ -154,8 +155,11 @@ fn init_cube_glsl(mut app App) {
|
||||
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
|
||||
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
|
||||
]
|
||||
// vfmt on
|
||||
|
||||
mut vert_buffer_desc := gfx.BufferDesc{label: c'cube-vertices'}
|
||||
mut vert_buffer_desc := gfx.BufferDesc{
|
||||
label: c'cube-vertices'
|
||||
}
|
||||
unsafe { vmemset(&vert_buffer_desc, 0, int(sizeof(vert_buffer_desc))) }
|
||||
|
||||
vert_buffer_desc.size = usize(vertices.len * int(sizeof(Vertex_t)))
|
||||
@ -168,17 +172,21 @@ fn init_cube_glsl(mut app App) {
|
||||
vbuf := gfx.make_buffer(&vert_buffer_desc)
|
||||
|
||||
// create an index buffer for the cube
|
||||
// vfmt off
|
||||
indices := [
|
||||
u16(0), 1, 2, 0, 2, 3,
|
||||
6, 5, 4, 7, 6, 4,
|
||||
8, 9, 10, 8, 10, 11,
|
||||
14, 13, 12, 15, 14, 12,
|
||||
16, 17, 18, 16, 18, 19,
|
||||
22, 21, 20, 23, 22, 20,
|
||||
u16(0), 1, 2, 0, 2, 3,
|
||||
6, 5, 4, 7, 6, 4,
|
||||
8, 9, 10, 8, 10, 11,
|
||||
14, 13, 12, 15, 14, 12,
|
||||
16, 17, 18, 16, 18, 19,
|
||||
22, 21, 20, 23, 22, 20,
|
||||
]
|
||||
// vfmt on
|
||||
|
||||
mut index_buffer_desc := gfx.BufferDesc{label: c'cube-indices'}
|
||||
unsafe {vmemset(&index_buffer_desc, 0, int(sizeof(index_buffer_desc)))}
|
||||
mut index_buffer_desc := gfx.BufferDesc{
|
||||
label: c'cube-indices'
|
||||
}
|
||||
unsafe { vmemset(&index_buffer_desc, 0, int(sizeof(index_buffer_desc))) }
|
||||
|
||||
index_buffer_desc.size = usize(indices.len * int(sizeof(u16)))
|
||||
index_buffer_desc.data = gfx.Range{
|
||||
@ -186,7 +194,7 @@ fn init_cube_glsl(mut app App) {
|
||||
size: usize(indices.len * int(sizeof(u16)))
|
||||
}
|
||||
|
||||
index_buffer_desc.@type = .indexbuffer
|
||||
index_buffer_desc.@type = .indexbuffer
|
||||
ibuf := gfx.make_buffer(&index_buffer_desc)
|
||||
|
||||
// create shader
|
||||
@ -197,9 +205,9 @@ fn init_cube_glsl(mut app App) {
|
||||
pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
|
||||
|
||||
// the constants [C.ATTR_vs_pos, C.ATTR_vs_color0, C.ATTR_vs_texcoord0] are generated by sokol-shdc
|
||||
pipdesc.layout.attrs[C.ATTR_vs_pos ].format = .float3 // x,y,z as f32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_color0 ].format = .ubyte4n // color as u32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .float2 // u,v as f32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_pos].format = .float3 // x,y,z as f32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_color0].format = .ubyte4n // color as u32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .float2 // u,v as f32
|
||||
// pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .short2n // u,v as u16
|
||||
|
||||
pipdesc.shader = shader
|
||||
@ -222,21 +230,24 @@ fn init_cube_glsl(mut app App) {
|
||||
|
||||
[inline]
|
||||
fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 {
|
||||
return m4.Vec4{e:[x, y, z, w]!}
|
||||
return m4.Vec4{
|
||||
e: [x, y, z, w]!
|
||||
}
|
||||
}
|
||||
|
||||
fn calc_tr_matrices(w f32, h f32, rx f32, ry f32, in_scale f32) m4.Mat4 {
|
||||
proj := m4.perspective(60, w/h, 0.01, 10.0)
|
||||
view := m4.look_at(vec4(f32(0.0) ,0 , 6, 0), vec4(f32(0), 0, 0, 0), vec4(f32(0), 1, 0, 0))
|
||||
proj := m4.perspective(60, w / h, 0.01, 10.0)
|
||||
view := m4.look_at(vec4(f32(0.0), 0, 6, 0), vec4(f32(0), 0, 0, 0), vec4(f32(0), 1,
|
||||
0, 0))
|
||||
view_proj := view * proj
|
||||
|
||||
rxm := m4.rotate(m4.rad(rx), vec4(f32(1), 0, 0, 0))
|
||||
rym := m4.rotate(m4.rad(ry), vec4(f32(0), 1, 0, 0))
|
||||
|
||||
model := rym * rxm
|
||||
model := rym * rxm
|
||||
scale_m := m4.scale(vec4(in_scale, in_scale, in_scale, 1))
|
||||
|
||||
res := (scale_m * model) * view_proj
|
||||
res := (scale_m * model) * view_proj
|
||||
return res
|
||||
}
|
||||
|
||||
@ -270,6 +281,7 @@ fn draw_cube_glsl(app App) {
|
||||
}
|
||||
gfx.apply_uniforms(.vs, C.SLOT_vs_params, &vs_uniforms_range)
|
||||
|
||||
// vfmt off
|
||||
// *** fragment shader uniforms ***
|
||||
time_ticks := f32(time.ticks() - app.ticks) / 1000
|
||||
mut tmp_fs_params := [
|
||||
@ -282,6 +294,7 @@ fn draw_cube_glsl(app App) {
|
||||
0,
|
||||
0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h
|
||||
]!
|
||||
// vfmt on
|
||||
fs_uniforms_range := gfx.Range{
|
||||
ptr: unsafe { &tmp_fs_params }
|
||||
size: usize(sizeof(tmp_fs_params))
|
||||
@ -355,9 +368,9 @@ fn my_init(mut app App) {
|
||||
tmp_txt[i + 3] = u8(0xFF)
|
||||
} else {
|
||||
col := if ((x + y) & 1) == 1 { 0xFF } else { 128 }
|
||||
tmp_txt[i + 0] = u8(col) // red
|
||||
tmp_txt[i + 1] = u8(col) // green
|
||||
tmp_txt[i + 2] = u8(col) // blue
|
||||
tmp_txt[i + 0] = u8(col) // red
|
||||
tmp_txt[i + 1] = u8(col) // green
|
||||
tmp_txt[i + 2] = u8(col) // blue
|
||||
tmp_txt[i + 3] = u8(0xFF) // alpha
|
||||
}
|
||||
i += 4
|
||||
|
@ -26,6 +26,7 @@ import time
|
||||
#flag -I @VMODROOT/.
|
||||
#include "rt_glsl_march.h" # Should be generated with `v shader .` (see the instructions at the top of this file)
|
||||
#include "rt_glsl_puppy.h" # Should be generated with `v shader .` (see the instructions at the top of this file)
|
||||
|
||||
fn C.rt_march_shader_desc(gfx.Backend) &gfx.ShaderDesc
|
||||
fn C.rt_puppy_shader_desc(gfx.Backend) &gfx.ShaderDesc
|
||||
|
||||
@ -45,10 +46,10 @@ mut:
|
||||
mouse_y int = -1
|
||||
mouse_down bool
|
||||
// glsl
|
||||
cube_pip_glsl gfx.Pipeline
|
||||
cube_bind gfx.Bindings
|
||||
pipe map[string]gfx.Pipeline
|
||||
bind map[string]gfx.Bindings
|
||||
cube_pip_glsl gfx.Pipeline
|
||||
cube_bind gfx.Bindings
|
||||
pipe map[string]gfx.Pipeline
|
||||
bind map[string]gfx.Bindings
|
||||
// time
|
||||
ticks i64
|
||||
}
|
||||
@ -124,6 +125,7 @@ fn init_cube_glsl_m(mut app App) {
|
||||
// d := u16(32767) // for compatibility with D3D11, 32767 stand for 1
|
||||
d := f32(1.0)
|
||||
c := u32(0xFFFFFF_FF) // color RGBA8
|
||||
// vfmt off
|
||||
vertices := [
|
||||
// Face 0
|
||||
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
|
||||
@ -156,8 +158,11 @@ fn init_cube_glsl_m(mut app App) {
|
||||
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
|
||||
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
|
||||
]
|
||||
// vfmt on
|
||||
|
||||
mut vert_buffer_desc := gfx.BufferDesc{label: c'cube-vertices'}
|
||||
mut vert_buffer_desc := gfx.BufferDesc{
|
||||
label: c'cube-vertices'
|
||||
}
|
||||
unsafe { vmemset(&vert_buffer_desc, 0, int(sizeof(vert_buffer_desc))) }
|
||||
vert_buffer_desc.size = usize(vertices.len * int(sizeof(Vertex_t)))
|
||||
vert_buffer_desc.data = gfx.Range{
|
||||
@ -167,19 +172,23 @@ fn init_cube_glsl_m(mut app App) {
|
||||
vert_buffer_desc.@type = .vertexbuffer
|
||||
vbuf := gfx.make_buffer(&vert_buffer_desc)
|
||||
|
||||
/* create an index buffer for the cube */
|
||||
// create an index buffer for the cube
|
||||
// vfmt off
|
||||
indices := [
|
||||
u16(0), 1, 2, 0, 2, 3,
|
||||
6, 5, 4, 7, 6, 4,
|
||||
8, 9, 10, 8, 10, 11,
|
||||
u16(0), 1, 2, 0, 2, 3,
|
||||
6 , 5, 4, 7, 6, 4,
|
||||
8 , 9, 10, 8, 10, 11,
|
||||
/*
|
||||
u16(14), 13, 12, 15, 14, 12,
|
||||
16, 17, 18, 16, 18, 19,
|
||||
22, 21, 20, 23, 22, 20
|
||||
u16(14), 13, 12, 15, 14, 12,
|
||||
16, 17, 18, 16, 18, 19,
|
||||
22, 21, 20, 23, 22, 20,
|
||||
*/
|
||||
]
|
||||
// vfmt on
|
||||
|
||||
mut index_buffer_desc := gfx.BufferDesc{label: c'cube-indices'}
|
||||
mut index_buffer_desc := gfx.BufferDesc{
|
||||
label: c'cube-indices'
|
||||
}
|
||||
unsafe { vmemset(&index_buffer_desc, 0, int(sizeof(index_buffer_desc))) }
|
||||
index_buffer_desc.size = usize(indices.len * int(sizeof(u16)))
|
||||
index_buffer_desc.data = gfx.Range{
|
||||
@ -196,11 +205,13 @@ fn init_cube_glsl_m(mut app App) {
|
||||
unsafe { vmemset(&pipdesc, 0, int(sizeof(pipdesc))) }
|
||||
pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
|
||||
|
||||
// vfmt off
|
||||
// the constants [C.ATTR_vs_m_pos, C.ATTR_vs_m_color0, C.ATTR_vs_m_texcoord0] are generated by sokol-shdc
|
||||
pipdesc.layout.attrs[C.ATTR_vs_m_pos ].format = .float3 // x,y,z as f32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_m_color0 ].format = .ubyte4n // color as u32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_m_texcoord0].format = .float2 // u,v as f32
|
||||
// pipdesc.layout.attrs[C.ATTR_vs_m_texcoord0].format = .short2n // u,v as u16
|
||||
// vfmt on
|
||||
|
||||
pipdesc.shader = shader
|
||||
pipdesc.index_type = .uint16
|
||||
@ -230,6 +241,7 @@ fn init_cube_glsl_p(mut app App) {
|
||||
// d := u16(32767) // for compatibility with D3D11, 32767 stand for 1
|
||||
d := f32(1.0)
|
||||
c := u32(0xFFFFFF_FF) // color RGBA8
|
||||
// vfmt off
|
||||
vertices := [
|
||||
// Face 0
|
||||
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
|
||||
@ -262,6 +274,7 @@ fn init_cube_glsl_p(mut app App) {
|
||||
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
|
||||
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
|
||||
]
|
||||
// vfmt off
|
||||
|
||||
mut vert_buffer_desc := gfx.BufferDesc{label: c'cube-vertices'}
|
||||
unsafe { vmemset(&vert_buffer_desc, 0, int(sizeof(vert_buffer_desc))) }
|
||||
@ -274,19 +287,22 @@ fn init_cube_glsl_p(mut app App) {
|
||||
vbuf := gfx.make_buffer(&vert_buffer_desc)
|
||||
|
||||
/* create an index buffer for the cube */
|
||||
// vfmt off
|
||||
indices := [
|
||||
/*
|
||||
u16(0), 1, 2, 0, 2, 3,
|
||||
6, 5, 4, 7, 6, 4,
|
||||
8, 9, 10, 8, 10, 11,
|
||||
u16(0), 1, 2, 0, 2, 3,
|
||||
6, 5, 4, 7, 6, 4,
|
||||
8, 9, 10, 8, 10, 11,
|
||||
*/
|
||||
u16(14), 13, 12, 15, 14, 12,
|
||||
16, 17, 18, 16, 18, 19,
|
||||
22, 21, 20, 23, 22, 20
|
||||
|
||||
u16(14), 13, 12, 15, 14, 12,
|
||||
16 , 17, 18, 16, 18, 19,
|
||||
22 , 21, 20, 23, 22, 20,
|
||||
]
|
||||
// vfmt on
|
||||
|
||||
mut index_buffer_desc := gfx.BufferDesc{label: c'cube-indices'}
|
||||
mut index_buffer_desc := gfx.BufferDesc{
|
||||
label: c'cube-indices'
|
||||
}
|
||||
unsafe { vmemset(&index_buffer_desc, 0, int(sizeof(index_buffer_desc))) }
|
||||
index_buffer_desc.size = usize(indices.len * int(sizeof(u16)))
|
||||
index_buffer_desc.data = gfx.Range{
|
||||
@ -303,11 +319,13 @@ fn init_cube_glsl_p(mut app App) {
|
||||
unsafe { vmemset(&pipdesc, 0, int(sizeof(pipdesc))) }
|
||||
pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
|
||||
|
||||
// vfmt off
|
||||
// the constants [C.ATTR_vs_p_pos, C.ATTR_vs_p_color0, C.ATTR_vs_p_texcoord0] are generated by sokol-shdc
|
||||
pipdesc.layout.attrs[C.ATTR_vs_p_pos ].format = .float3 // x,y,z as f32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_p_color0 ].format = .ubyte4n // color as u32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_p_texcoord0].format = .float2 // u,v as f32
|
||||
// pipdesc.layout.attrs[C.ATTR_vs_p_texcoord0].format = .short2n // u,v as u16
|
||||
// vfmt on
|
||||
|
||||
pipdesc.shader = shader
|
||||
pipdesc.index_type = .uint16
|
||||
@ -334,21 +352,24 @@ fn init_cube_glsl_p(mut app App) {
|
||||
|
||||
[inline]
|
||||
fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 {
|
||||
return m4.Vec4{e:[x, y, z, w]!}
|
||||
return m4.Vec4{
|
||||
e: [x, y, z, w]!
|
||||
}
|
||||
}
|
||||
|
||||
fn calc_tr_matrices(w f32, h f32, rx f32, ry f32, in_scale f32) m4.Mat4 {
|
||||
proj := m4.perspective(60, w/h, 0.01, 10.0)
|
||||
view := m4.look_at(vec4(f32(0.0) ,0 , 6, 0), vec4(f32(0), 0, 0, 0), vec4(f32(0), 1, 0, 0))
|
||||
proj := m4.perspective(60, w / h, 0.01, 10.0)
|
||||
view := m4.look_at(vec4(f32(0.0), 0, 6, 0), vec4(f32(0), 0, 0, 0), vec4(f32(0), 1,
|
||||
0, 0))
|
||||
view_proj := view * proj
|
||||
|
||||
rxm := m4.rotate(m4.rad(rx), vec4(f32(1), 0, 0, 0))
|
||||
rym := m4.rotate(m4.rad(ry), vec4(f32(0), 1, 0, 0))
|
||||
|
||||
model := rym * rxm
|
||||
model := rym * rxm
|
||||
scale_m := m4.scale(vec4(in_scale, in_scale, in_scale, 1))
|
||||
|
||||
res := (scale_m * model) * view_proj
|
||||
res := (scale_m * model) * view_proj
|
||||
return res
|
||||
}
|
||||
|
||||
@ -381,6 +402,7 @@ fn draw_cube_glsl_m(app App) {
|
||||
|
||||
// *** fragment shader uniforms ***
|
||||
time_ticks := f32(time.ticks() - app.ticks) / 1000
|
||||
// vfmt off
|
||||
mut tmp_fs_params := [
|
||||
f32(ws.width),
|
||||
ws.height * ratio, // x,y resolution to pass to FS
|
||||
@ -393,6 +415,7 @@ fn draw_cube_glsl_m(app App) {
|
||||
0,
|
||||
0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h
|
||||
]!
|
||||
// vfmt on
|
||||
fs_uniforms_range := gfx.Range{
|
||||
ptr: unsafe { &tmp_fs_params }
|
||||
size: usize(sizeof(tmp_fs_params))
|
||||
@ -438,12 +461,12 @@ fn draw_cube_glsl_p(app App) {
|
||||
ws.height * ratio, // x,y resolution to pass to FS
|
||||
0,
|
||||
0, // dont send mouse position
|
||||
/* app.mouse_x, // mouse x */
|
||||
/* ws.height - app.mouse_y*2, // mouse y scaled */
|
||||
time_ticks, // time as f32
|
||||
// app.mouse_x, // mouse x
|
||||
// ws.height - app.mouse_y*2, // mouse y scaled
|
||||
time_ticks, // time as f32
|
||||
app.frame_count, // frame count
|
||||
0,
|
||||
0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h
|
||||
0, // padding bytes , see "fs_params" struct paddings in rt_glsl.h
|
||||
]!
|
||||
fs_uniforms_range := gfx.Range{
|
||||
ptr: unsafe { &tmp_fs_params }
|
||||
@ -543,9 +566,9 @@ fn my_init(mut app App) {
|
||||
tmp_txt[i + 3] = u8(0xFF)
|
||||
} else {
|
||||
col := if ((x + y) & 1) == 1 { 0xFF } else { 128 }
|
||||
tmp_txt[i + 0] = u8(col) // red
|
||||
tmp_txt[i + 1] = u8(col) // green
|
||||
tmp_txt[i + 2] = u8(col) // blue
|
||||
tmp_txt[i + 0] = u8(col) // red
|
||||
tmp_txt[i + 1] = u8(col) // green
|
||||
tmp_txt[i + 2] = u8(col) // blue
|
||||
tmp_txt[i + 3] = u8(0xFF) // alpha
|
||||
}
|
||||
i += 4
|
||||
|
@ -17,10 +17,8 @@ import gg
|
||||
import gg.m4
|
||||
import gx
|
||||
import math
|
||||
|
||||
import sokol.gfx
|
||||
//import sokol.sgl
|
||||
|
||||
// import sokol.sgl
|
||||
import time
|
||||
|
||||
const (
|
||||
@ -32,31 +30,27 @@ const (
|
||||
|
||||
struct App {
|
||||
mut:
|
||||
gg &gg.Context = unsafe { nil }
|
||||
texture gfx.Image
|
||||
init_flag bool
|
||||
frame_count int
|
||||
|
||||
mouse_x int = -1
|
||||
mouse_y int = -1
|
||||
mouse_down bool
|
||||
gg &gg.Context = unsafe { nil }
|
||||
texture gfx.Image
|
||||
init_flag bool
|
||||
frame_count int
|
||||
|
||||
mouse_x int = -1
|
||||
mouse_y int = -1
|
||||
mouse_down bool
|
||||
// glsl
|
||||
cube_pip_glsl gfx.Pipeline
|
||||
cube_bind gfx.Bindings
|
||||
|
||||
pipe map[string]gfx.Pipeline
|
||||
bind map[string]gfx.Bindings
|
||||
|
||||
pipe map[string]gfx.Pipeline
|
||||
bind map[string]gfx.Bindings
|
||||
// time
|
||||
ticks i64
|
||||
|
||||
ticks i64
|
||||
// instances
|
||||
inst_pos [num_inst]m4.Vec4
|
||||
|
||||
inst_pos [num_inst]m4.Vec4
|
||||
// camera
|
||||
camera_x f32
|
||||
camera_z f32
|
||||
camera_x f32
|
||||
camera_z f32
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
@ -64,13 +58,15 @@ mut:
|
||||
******************************************************************************/
|
||||
#flag -I @VMODROOT/.
|
||||
#include "rt_glsl_instancing.h" # Should be generated with `v shader .` (see the instructions at the top of this file)
|
||||
|
||||
fn C.instancing_shader_desc(gfx.Backend) &gfx.ShaderDesc
|
||||
|
||||
/******************************************************************************
|
||||
* Texture functions
|
||||
******************************************************************************/
|
||||
fn create_texture(w int, h int, buf byteptr) gfx.Image{
|
||||
fn create_texture(w int, h int, buf byteptr) gfx.Image {
|
||||
sz := w * h * 4
|
||||
// vfmt off
|
||||
mut img_desc := gfx.ImageDesc{
|
||||
width: w
|
||||
height: h
|
||||
@ -83,9 +79,10 @@ fn create_texture(w int, h int, buf byteptr) gfx.Image{
|
||||
label: &u8(0)
|
||||
d3d11_texture: 0
|
||||
}
|
||||
// vfmt on
|
||||
// comment if .dynamic is enabled
|
||||
img_desc.data.subimage[0][0] = gfx.Range{
|
||||
ptr: buf
|
||||
ptr: buf
|
||||
size: usize(sz)
|
||||
}
|
||||
|
||||
@ -93,12 +90,12 @@ fn create_texture(w int, h int, buf byteptr) gfx.Image{
|
||||
return sg_img
|
||||
}
|
||||
|
||||
fn destroy_texture(sg_img gfx.Image){
|
||||
fn destroy_texture(sg_img gfx.Image) {
|
||||
gfx.destroy_image(sg_img)
|
||||
}
|
||||
|
||||
// Use only if usage: .dynamic is enabled
|
||||
fn update_text_texture(sg_img gfx.Image, w int, h int, buf byteptr){
|
||||
fn update_text_texture(sg_img gfx.Image, w int, h int, buf byteptr) {
|
||||
sz := w * h * 4
|
||||
mut tmp_sbc := gfx.ImageData{}
|
||||
tmp_sbc.subimage[0][0] = gfx.Range{
|
||||
@ -122,23 +119,23 @@ fn update_text_texture(sg_img gfx.Image, w int, h int, buf byteptr){
|
||||
*/
|
||||
|
||||
struct Vertex_t {
|
||||
x f32
|
||||
y f32
|
||||
z f32
|
||||
color u32
|
||||
|
||||
//u u16 // for compatibility with D3D11
|
||||
//v u16 // for compatibility with D3D11
|
||||
u f32
|
||||
v f32
|
||||
x f32
|
||||
y f32
|
||||
z f32
|
||||
color u32
|
||||
// u u16 // for compatibility with D3D11
|
||||
// v u16 // for compatibility with D3D11
|
||||
u f32
|
||||
v f32
|
||||
}
|
||||
|
||||
// march shader init
|
||||
fn init_cube_glsl_i(mut app App) {
|
||||
/* cube vertex buffer */
|
||||
//d := u16(32767) // for compatibility with D3D11, 32767 stand for 1
|
||||
// cube vertex buffer
|
||||
// d := u16(32767) // for compatibility with D3D11, 32767 stand for 1
|
||||
d := f32(1.0)
|
||||
c := u32(0xFFFFFF_FF) // color RGBA8
|
||||
// vfmt off
|
||||
vertices := [
|
||||
// Face 0
|
||||
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
|
||||
@ -171,54 +168,63 @@ fn init_cube_glsl_i(mut app App) {
|
||||
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
|
||||
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
|
||||
]
|
||||
// vfmt on
|
||||
|
||||
mut vert_buffer_desc := gfx.BufferDesc{label: c'cube-vertices'}
|
||||
unsafe {vmemset(&vert_buffer_desc, 0, int(sizeof(vert_buffer_desc)))}
|
||||
mut vert_buffer_desc := gfx.BufferDesc{
|
||||
label: c'cube-vertices'
|
||||
}
|
||||
unsafe { vmemset(&vert_buffer_desc, 0, int(sizeof(vert_buffer_desc))) }
|
||||
vert_buffer_desc.size = usize(vertices.len * int(sizeof(Vertex_t)))
|
||||
vert_buffer_desc.data = gfx.Range{
|
||||
ptr: vertices.data
|
||||
size: usize(vertices.len * int(sizeof(Vertex_t)))
|
||||
}
|
||||
vert_buffer_desc.@type = .vertexbuffer
|
||||
vert_buffer_desc.@type = .vertexbuffer
|
||||
vbuf := gfx.make_buffer(&vert_buffer_desc)
|
||||
|
||||
/* create an instance buffer for the cube */
|
||||
mut inst_buffer_desc := gfx.BufferDesc{label: c'instance-data'}
|
||||
unsafe {vmemset(&inst_buffer_desc, 0, int(sizeof(inst_buffer_desc)))}
|
||||
// create an instance buffer for the cube
|
||||
mut inst_buffer_desc := gfx.BufferDesc{
|
||||
label: c'instance-data'
|
||||
}
|
||||
unsafe { vmemset(&inst_buffer_desc, 0, int(sizeof(inst_buffer_desc))) }
|
||||
|
||||
inst_buffer_desc.size = usize(num_inst * int(sizeof(m4.Vec4)))
|
||||
inst_buffer_desc.@type = .vertexbuffer
|
||||
inst_buffer_desc.usage = .stream
|
||||
inst_buffer_desc.@type = .vertexbuffer
|
||||
inst_buffer_desc.usage = .stream
|
||||
inst_buf := gfx.make_buffer(&inst_buffer_desc)
|
||||
|
||||
|
||||
/* create an index buffer for the cube */
|
||||
// create an index buffer for the cube
|
||||
// vfmt off
|
||||
indices := [
|
||||
u16(0), 1, 2, 0, 2, 3,
|
||||
6, 5, 4, 7, 6, 4,
|
||||
8, 9, 10, 8, 10, 11,
|
||||
14, 13, 12, 15, 14, 12,
|
||||
16, 17, 18, 16, 18, 19,
|
||||
22, 21, 20, 23, 22, 20
|
||||
u16(0), 1, 2, 0, 2, 3,
|
||||
6, 5, 4, 7, 6, 4,
|
||||
8, 9, 10, 8, 10, 11,
|
||||
14, 13, 12, 15, 14, 12,
|
||||
16, 17, 18, 16, 18, 19,
|
||||
22, 21, 20, 23, 22, 20,
|
||||
]
|
||||
// vfmt on
|
||||
|
||||
mut index_buffer_desc := gfx.BufferDesc{label: c'cube-indices'}
|
||||
unsafe {vmemset(&index_buffer_desc, 0, int(sizeof(index_buffer_desc)))}
|
||||
index_buffer_desc.size = usize(indices.len * int(sizeof(u16)))
|
||||
mut index_buffer_desc := gfx.BufferDesc{
|
||||
label: c'cube-indices'
|
||||
}
|
||||
unsafe { vmemset(&index_buffer_desc, 0, int(sizeof(index_buffer_desc))) }
|
||||
index_buffer_desc.size = usize(indices.len * int(sizeof(u16)))
|
||||
index_buffer_desc.data = gfx.Range{
|
||||
ptr: indices.data
|
||||
size: usize(indices.len * int(sizeof(u16)))
|
||||
}
|
||||
index_buffer_desc.@type = .indexbuffer
|
||||
index_buffer_desc.@type = .indexbuffer
|
||||
ibuf := gfx.make_buffer(&index_buffer_desc)
|
||||
|
||||
/* create shader */
|
||||
// create shader
|
||||
shader := gfx.make_shader(C.instancing_shader_desc(C.sg_query_backend()))
|
||||
|
||||
mut pipdesc := gfx.PipelineDesc{}
|
||||
unsafe {vmemset(&pipdesc, 0, int(sizeof(pipdesc)))}
|
||||
unsafe { vmemset(&pipdesc, 0, int(sizeof(pipdesc))) }
|
||||
pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
|
||||
|
||||
// vfmt off
|
||||
// the constants [C.ATTR_vs_m_pos, C.ATTR_vs_m_color0, C.ATTR_vs_m_texcoord0] are generated by sokol-shdc
|
||||
pipdesc.layout.attrs[C.ATTR_vs_i_pos ].format = .float3 // x,y,z as f32
|
||||
pipdesc.layout.attrs[C.ATTR_vs_i_pos ].buffer_index = 0
|
||||
@ -233,6 +239,7 @@ fn init_cube_glsl_i(mut app App) {
|
||||
pipdesc.layout.buffers[1].step_func = .per_instance // we will pass a single parameter for each instance!!
|
||||
pipdesc.layout.attrs[C.ATTR_vs_i_inst_pos ].format = .float4
|
||||
pipdesc.layout.attrs[C.ATTR_vs_i_inst_pos ].buffer_index = 1
|
||||
// vfmt on
|
||||
|
||||
pipdesc.shader = shader
|
||||
pipdesc.index_type = .uint16
|
||||
@ -243,44 +250,46 @@ fn init_cube_glsl_i(mut app App) {
|
||||
}
|
||||
pipdesc.cull_mode = .back
|
||||
|
||||
pipdesc.label = "glsl_shader pipeline".str
|
||||
pipdesc.label = 'glsl_shader pipeline'.str
|
||||
|
||||
mut bind := gfx.Bindings{}
|
||||
unsafe {vmemset(&bind, 0, int(sizeof(bind)))}
|
||||
bind.vertex_buffers[0] = vbuf // vertex buffer
|
||||
bind.vertex_buffers[1] = inst_buf // instance buffer
|
||||
bind.index_buffer = ibuf
|
||||
unsafe { vmemset(&bind, 0, int(sizeof(bind))) }
|
||||
bind.vertex_buffers[0] = vbuf // vertex buffer
|
||||
bind.vertex_buffers[1] = inst_buf // instance buffer
|
||||
bind.index_buffer = ibuf
|
||||
bind.fs_images[C.SLOT_tex] = app.texture
|
||||
app.bind['inst'] = bind
|
||||
app.pipe['inst'] = gfx.make_pipeline(&pipdesc)
|
||||
|
||||
println("GLSL March init DONE!")
|
||||
println('GLSL March init DONE!')
|
||||
}
|
||||
|
||||
fn calc_tr_matrices(w f32, h f32, rx f32, ry f32, in_scale f32) m4.Mat4{
|
||||
fn calc_tr_matrices(w f32, h f32, rx f32, ry f32, in_scale f32) m4.Mat4 {
|
||||
// vfmt off
|
||||
proj := m4.perspective(60, w/h, 0.01, 4000.0)
|
||||
view := m4.look_at(m4.Vec4{e:[f32(0.0),100,6,0]!}, m4.Vec4{e:[f32(0),0,0,0]!}, m4.Vec4{e:[f32(0),1.0,0,0]!})
|
||||
view_proj := view * proj
|
||||
|
||||
rxm := m4.rotate(m4.rad(rx), m4.Vec4{e:[f32(1),0,0,0]!})
|
||||
rym := m4.rotate(m4.rad(ry), m4.Vec4{e:[f32(0),1,0,0]!})
|
||||
// vfmt on
|
||||
|
||||
model := rym * rxm
|
||||
scale_m := m4.scale(m4.Vec4{e:[in_scale, in_scale, in_scale, 1]!})
|
||||
model := rym * rxm
|
||||
scale_m := m4.scale(m4.Vec4{ e: [in_scale, in_scale, in_scale, 1]! })
|
||||
|
||||
res := (scale_m * model)* view_proj
|
||||
res := (scale_m * model) * view_proj
|
||||
return res
|
||||
}
|
||||
|
||||
// triangles draw
|
||||
fn draw_cube_glsl_i(mut app App){
|
||||
fn draw_cube_glsl_i(mut app App) {
|
||||
if app.init_flag == false {
|
||||
return
|
||||
}
|
||||
|
||||
ws := gg.window_size_real_pixels()
|
||||
//ratio := f32(ws.width) / ws.height
|
||||
dw := f32(ws.width / 2)
|
||||
// ratio := f32(ws.width) / ws.height
|
||||
dw := f32(ws.width / 2)
|
||||
dh := f32(ws.height / 2)
|
||||
|
||||
rot := [f32(app.mouse_y), f32(app.mouse_x)]
|
||||
@ -296,25 +305,27 @@ fn draw_cube_glsl_i(mut app App){
|
||||
time_ticks := f32(time.ticks() - app.ticks) / 1000
|
||||
cube_size := 2
|
||||
sz := 128 // field size dimension
|
||||
cx := 64 // x center for the cubes
|
||||
cz := 64 // z center for the cubes
|
||||
//frame := (app.frame_count/4) % 100
|
||||
for index in 0..num_inst {
|
||||
cx := 64 // x center for the cubes
|
||||
cz := 64 // z center for the cubes
|
||||
// frame := (app.frame_count/4) % 100
|
||||
for index in 0 .. num_inst {
|
||||
x := f32(index % sz)
|
||||
z := f32(index / sz)
|
||||
// simply waves
|
||||
y := f32(math.cos((x+time_ticks)/2.0)*math.sin(z/2.0))*2
|
||||
y := f32(math.cos((x + time_ticks) / 2.0) * math.sin(z / 2.0)) * 2
|
||||
// sombrero function
|
||||
//r := ((x-cx)*(x-cx)+(z-cz)*(z-cz))/(sz/2)
|
||||
//y := f32(math.sin(r+time_ticks)*4.0)
|
||||
// r := ((x-cx)*(x-cx)+(z-cz)*(z-cz))/(sz/2)
|
||||
// y := f32(math.sin(r+time_ticks)*4.0)
|
||||
spare_param := f32(index % 10)
|
||||
// vfmt off
|
||||
app.inst_pos[index] = m4.Vec4{e:[f32((x - cx - app.camera_x) * cube_size),y ,f32( (z - cz - app.camera_z) * cube_size),spare_param]!}
|
||||
// vfmt on
|
||||
}
|
||||
range := gfx.Range{
|
||||
ptr: unsafe { &app.inst_pos }
|
||||
size: usize(num_inst * int(sizeof(m4.Vec4)))
|
||||
}
|
||||
gfx.update_buffer(app.bind['inst'].vertex_buffers[1], &range )
|
||||
gfx.update_buffer(app.bind['inst'].vertex_buffers[1], &range)
|
||||
|
||||
// Uniforms
|
||||
// *** vertex shadeer uniforms ***
|
||||
@ -326,43 +337,44 @@ fn draw_cube_glsl_i(mut app App){
|
||||
}
|
||||
gfx.apply_uniforms(.vs, C.SLOT_vs_params_i, &vs_uniforms_range)
|
||||
|
||||
/*
|
||||
/*
|
||||
// *** fragment shader uniforms ***
|
||||
time_ticks := f32(time.ticks() - app.ticks) / 1000
|
||||
// vfmt off
|
||||
mut tmp_fs_params := [
|
||||
f32(ws.width), ws.height * ratio, // x,y resolution to pass to FS
|
||||
0,0, // dont send mouse position
|
||||
0,0, // dont send mouse position
|
||||
//app.mouse_x, // mouse x
|
||||
//ws.height - app.mouse_y*2, // mouse y scaled
|
||||
time_ticks, // time as f32
|
||||
app.frame_count, // frame count
|
||||
0,0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h
|
||||
time_ticks, // time as f32
|
||||
app.frame_count, // frame count
|
||||
0,0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h
|
||||
]!
|
||||
// vfmt on
|
||||
fs_uniforms_range := gfx.Range{
|
||||
ptr: unsafe { &tmp_fs_params }
|
||||
size: usize(sizeof(tmp_fs_params))
|
||||
}
|
||||
gfx.apply_uniforms(.fs, C.SLOT_fs_params, &fs_uniforms_range)
|
||||
*/
|
||||
*/
|
||||
// 3 vertices for triangle * 2 triangles per face * 6 faces = 36 vertices to draw for num_inst times
|
||||
gfx.draw(0, (3 * 2) * 6, num_inst)
|
||||
}
|
||||
|
||||
|
||||
fn draw_start_glsl(app App){
|
||||
fn draw_start_glsl(app App) {
|
||||
if app.init_flag == false {
|
||||
return
|
||||
}
|
||||
|
||||
ws := gg.window_size_real_pixels()
|
||||
//ratio := f32(ws.width) / ws.height
|
||||
//dw := f32(ws.width / 2)
|
||||
//dh := f32(ws.height / 2)
|
||||
// ratio := f32(ws.width) / ws.height
|
||||
// dw := f32(ws.width / 2)
|
||||
// dh := f32(ws.height / 2)
|
||||
|
||||
gfx.apply_viewport(0, 0, ws.width, ws.height, true)
|
||||
}
|
||||
|
||||
fn draw_end_glsl(app App){
|
||||
fn draw_end_glsl(app App) {
|
||||
gfx.end_pass()
|
||||
gfx.commit()
|
||||
}
|
||||
@ -385,7 +397,7 @@ fn frame(mut app App) {
|
||||
gfx.begin_default_pass(&pass_action, ws.width, ws.height)
|
||||
|
||||
draw_start_glsl(app)
|
||||
draw_cube_glsl_i(mut app)
|
||||
draw_cube_glsl_i(mut app)
|
||||
draw_end_glsl(app)
|
||||
app.frame_count++
|
||||
}
|
||||
@ -419,9 +431,9 @@ fn my_init(mut app App) {
|
||||
tmp_txt[i + 3] = u8(0xFF)
|
||||
} else {
|
||||
col := if ((x + y) & 1) == 1 { 0xFF } else { 128 }
|
||||
tmp_txt[i + 0] = u8(col) // red
|
||||
tmp_txt[i + 1] = u8(col) // green
|
||||
tmp_txt[i + 2] = u8(col) // blue
|
||||
tmp_txt[i + 0] = u8(col) // red
|
||||
tmp_txt[i + 1] = u8(col) // green
|
||||
tmp_txt[i + 2] = u8(col) // blue
|
||||
tmp_txt[i + 3] = u8(0xFF) // alpha
|
||||
}
|
||||
i += 4
|
||||
@ -431,7 +443,6 @@ fn my_init(mut app App) {
|
||||
app.texture = create_texture(w, h, tmp_txt)
|
||||
free(tmp_txt)
|
||||
}
|
||||
|
||||
// glsl
|
||||
init_cube_glsl_i(mut app)
|
||||
app.init_flag = true
|
||||
@ -441,10 +452,10 @@ fn my_init(mut app App) {
|
||||
* events handling
|
||||
******************************************************************************/
|
||||
fn my_event_manager(mut ev gg.Event, mut app App) {
|
||||
if ev.typ == .mouse_down{
|
||||
if ev.typ == .mouse_down {
|
||||
app.mouse_down = true
|
||||
}
|
||||
if ev.typ == .mouse_up{
|
||||
if ev.typ == .mouse_up {
|
||||
app.mouse_down = false
|
||||
}
|
||||
if app.mouse_down == true && ev.typ == .mouse_move {
|
||||
@ -467,7 +478,7 @@ fn my_event_manager(mut ev gg.Event, mut app App) {
|
||||
.s { app.camera_z -= step }
|
||||
.a { app.camera_x -= step }
|
||||
.d { app.camera_x += step }
|
||||
else{}
|
||||
else {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -475,12 +486,13 @@ fn my_event_manager(mut ev gg.Event, mut app App) {
|
||||
/******************************************************************************
|
||||
* Main
|
||||
******************************************************************************/
|
||||
fn main(){
|
||||
fn main() {
|
||||
// App init
|
||||
mut app := &App{
|
||||
gg: 0
|
||||
}
|
||||
|
||||
// vfmt off
|
||||
app.gg = gg.new_context(
|
||||
width: win_width
|
||||
height: win_height
|
||||
@ -492,6 +504,7 @@ fn main(){
|
||||
init_fn: my_init
|
||||
event_fn: my_event_manager
|
||||
)
|
||||
// vfmt on
|
||||
|
||||
app.ticks = time.ticks()
|
||||
app.gg.run()
|
||||
|
@ -23,7 +23,7 @@ module aes
|
||||
// Reducing mod poly corresponds to binary xor with poly every
|
||||
// time a 0x100 bit appears.
|
||||
const (
|
||||
poly = (1<<8) | (1<<4) | (1<<3) | (1<<1) | (1<<0) // x⁸ + x⁴ + x³ + x + 1
|
||||
poly = (1 << 8) | (1 << 4) | (1 << 3) | (1 << 1) | (1 << 0) // x⁸ + x⁴ + x³ + x + 1
|
||||
)
|
||||
|
||||
// Powers of x mod poly in GF(2).
|
||||
@ -48,10 +48,12 @@ const (
|
||||
]
|
||||
)
|
||||
|
||||
// vfmt off
|
||||
|
||||
// FIPS-197 Figure 7. S-box substitution values in hexadecimal format.
|
||||
const (
|
||||
s_box0 = [
|
||||
u8(0x63), 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
|
||||
u8(0x63), 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
|
||||
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
|
||||
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
|
||||
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
|
||||
@ -73,7 +75,7 @@ const (
|
||||
// FIPS-197 Figure 14. Inverse S-box substitution values in hexadecimal format.
|
||||
const (
|
||||
s_box1 = [
|
||||
u8(0x52), 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
|
||||
u8(0x52), 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
|
||||
0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
|
||||
0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
|
||||
0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
|
||||
@ -96,7 +98,7 @@ const (
|
||||
|
||||
const (
|
||||
te0 = [
|
||||
u32(0xc66363a5), 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554,
|
||||
u32(0xc66363a5), 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554,
|
||||
0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a,
|
||||
0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87, 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b,
|
||||
0x41adadec, 0xb3d4d467, 0x5fa2a2fd, 0x45afafea, 0x239c9cbf, 0x53a4a4f7, 0xe4727296, 0x9bc0c05b,
|
||||
@ -130,7 +132,7 @@ const (
|
||||
0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a,
|
||||
]
|
||||
te1 = [
|
||||
u32(0xa5c66363), 0x84f87c7c, 0x99ee7777, 0x8df67b7b, 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5,
|
||||
u32(0xa5c66363), 0x84f87c7c, 0x99ee7777, 0x8df67b7b, 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5,
|
||||
0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b, 0x19e7fefe, 0x62b5d7d7, 0xe64dabab, 0x9aec7676,
|
||||
0x458fcaca, 0x9d1f8282, 0x4089c9c9, 0x87fa7d7d, 0x15effafa, 0xebb25959, 0xc98e4747, 0x0bfbf0f0,
|
||||
0xec41adad, 0x67b3d4d4, 0xfd5fa2a2, 0xea45afaf, 0xbf239c9c, 0xf753a4a4, 0x96e47272, 0x5b9bc0c0,
|
||||
@ -164,7 +166,7 @@ const (
|
||||
0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f, 0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616,
|
||||
]
|
||||
te2 = [
|
||||
u32(0x63a5c663), 0x7c84f87c, 0x7799ee77, 0x7b8df67b, 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5,
|
||||
u32(0x63a5c663), 0x7c84f87c, 0x7799ee77, 0x7b8df67b, 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5,
|
||||
0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b, 0xfe19e7fe, 0xd762b5d7, 0xabe64dab, 0x769aec76,
|
||||
0xca458fca, 0x829d1f82, 0xc94089c9, 0x7d87fa7d, 0xfa15effa, 0x59ebb259, 0x47c98e47, 0xf00bfbf0,
|
||||
0xadec41ad, 0xd467b3d4, 0xa2fd5fa2, 0xafea45af, 0x9cbf239c, 0xa4f753a4, 0x7296e472, 0xc05b9bc0,
|
||||
@ -198,7 +200,7 @@ const (
|
||||
0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f, 0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16,
|
||||
]
|
||||
te3 = [
|
||||
u32(0x6363a5c6), 0x7c7c84f8, 0x777799ee, 0x7b7b8df6, 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491,
|
||||
u32(0x6363a5c6), 0x7c7c84f8, 0x777799ee, 0x7b7b8df6, 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491,
|
||||
0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56, 0xfefe19e7, 0xd7d762b5, 0xababe64d, 0x76769aec,
|
||||
0xcaca458f, 0x82829d1f, 0xc9c94089, 0x7d7d87fa, 0xfafa15ef, 0x5959ebb2, 0x4747c98e, 0xf0f00bfb,
|
||||
0xadadec41, 0xd4d467b3, 0xa2a2fd5f, 0xafafea45, 0x9c9cbf23, 0xa4a4f753, 0x727296e4, 0xc0c05b9b,
|
||||
@ -236,7 +238,7 @@ const (
|
||||
// Lookup tables for decryption.
|
||||
const (
|
||||
td0 = [
|
||||
u32(0x51f4a750), 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393,
|
||||
u32(0x51f4a750), 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393,
|
||||
0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f,
|
||||
0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1, 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6,
|
||||
0x038f5fe7, 0x15929c95, 0xbf6d7aeb, 0x955259da, 0xd4be832d, 0x587421d3, 0x49e06929, 0x8ec9c844,
|
||||
@ -270,7 +272,7 @@ const (
|
||||
0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742,
|
||||
]
|
||||
td1 = [
|
||||
u32(0x5051f4a7), 0x537e4165, 0xc31a17a4, 0x963a275e, 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303,
|
||||
u32(0x5051f4a7), 0x537e4165, 0xc31a17a4, 0x963a275e, 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303,
|
||||
0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c, 0xfc4fe5d7, 0xd7c52acb, 0x80263544, 0x8fb562a3,
|
||||
0x49deb15a, 0x6725ba1b, 0x9845ea0e, 0xe15dfec0, 0x02c32f75, 0x12814cf0, 0xa38d4697, 0xc66bd3f9,
|
||||
0xe7038f5f, 0x9515929c, 0xebbf6d7a, 0xda955259, 0x2dd4be83, 0xd3587421, 0x2949e069, 0x448ec9c8,
|
||||
@ -304,7 +306,7 @@ const (
|
||||
0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1, 0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857,
|
||||
]
|
||||
td2 = [
|
||||
u32(0xa75051f4), 0x65537e41, 0xa4c31a17, 0x5e963a27, 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3,
|
||||
u32(0xa75051f4), 0x65537e41, 0xa4c31a17, 0x5e963a27, 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3,
|
||||
0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502, 0xd7fc4fe5, 0xcbd7c52a, 0x44802635, 0xa38fb562,
|
||||
0x5a49deb1, 0x1b6725ba, 0x0e9845ea, 0xc0e15dfe, 0x7502c32f, 0xf012814c, 0x97a38d46, 0xf9c66bd3,
|
||||
0x5fe7038f, 0x9c951592, 0x7aebbf6d, 0x59da9552, 0x832dd4be, 0x21d35874, 0x692949e0, 0xc8448ec9,
|
||||
@ -338,7 +340,7 @@ const (
|
||||
0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456, 0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8,
|
||||
]
|
||||
td3 = [
|
||||
u32(0xf4a75051), 0x4165537e, 0x17a4c31a, 0x275e963a, 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b,
|
||||
u32(0xf4a75051), 0x4165537e, 0x17a4c31a, 0x275e963a, 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b,
|
||||
0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5, 0xe5d7fc4f, 0x2acbd7c5, 0x35448026, 0x62a38fb5,
|
||||
0xb15a49de, 0xba1b6725, 0xea0e9845, 0xfec0e15d, 0x2f7502c3, 0x4cf01281, 0x4697a38d, 0xd3f9c66b,
|
||||
0x8f5fe703, 0x929c9515, 0x6d7aebbf, 0x5259da95, 0xbe832dd4, 0x7421d358, 0xe0692949, 0xc9c8448e,
|
||||
@ -372,3 +374,5 @@ const (
|
||||
0xa8017139, 0x0cb3de08, 0xb4e49cd8, 0x56c19064, 0xcb84617b, 0x32b670d5, 0x6c5c7448, 0xb85742d0,
|
||||
]
|
||||
)
|
||||
|
||||
// vfmt on
|
||||
|
@ -9,8 +9,9 @@ module sha512
|
||||
|
||||
import math.bits
|
||||
|
||||
const (
|
||||
_k = [u64(0x428a2f98d728ae22), u64(0x7137449123ef65cd), u64(0xb5c0fbcfec4d3b2f), u64(0xe9b5dba58189dbbc),
|
||||
// vfmt off
|
||||
const _k = [
|
||||
u64(0x428a2f98d728ae22), u64(0x7137449123ef65cd), u64(0xb5c0fbcfec4d3b2f), u64(0xe9b5dba58189dbbc),
|
||||
u64(0x3956c25bf348b538), u64(0x59f111f1b605d019), u64(0x923f82a4af194f9b), u64(0xab1c5ed5da6d8118),
|
||||
u64(0xd807aa98a3030242), u64(0x12835b0145706fbe), u64(0x243185be4ee4b28c), u64(0x550c7dc3d5ffb4e2),
|
||||
u64(0x72be5d74f27b896f), u64(0x80deb1fe3b1696b1), u64(0x9bdc06a725c71235), u64(0xc19bf174cf692694),
|
||||
@ -29,8 +30,9 @@ const (
|
||||
u64(0xca273eceea26619c), u64(0xd186b8c721c0c207), u64(0xeada7dd6cde0eb1e), u64(0xf57d4f7fee6ed178),
|
||||
u64(0x06f067aa72176fba), u64(0x0a637dc5a2c898a6), u64(0x113f9804bef90dae), u64(0x1b710b35131c471b),
|
||||
u64(0x28db77f523047d84), u64(0x32caab7b40c72493), u64(0x3c9ebe0a15c9bebc), u64(0x431d67c49c100d4c),
|
||||
u64(0x4cc5d4becb3e42b6), u64(0x597f299cfc657e2a), u64(0x5fcb6fab3ad6faec), u64(0x6c44198c4a475817)]
|
||||
)
|
||||
u64(0x4cc5d4becb3e42b6), u64(0x597f299cfc657e2a), u64(0x5fcb6fab3ad6faec), u64(0x6c44198c4a475817),
|
||||
]
|
||||
// vfmt on
|
||||
|
||||
fn block_generic(mut dig Digest, p_ []u8) {
|
||||
unsafe {
|
||||
@ -47,16 +49,26 @@ fn block_generic(mut dig Digest, p_ []u8) {
|
||||
for p.len >= chunk {
|
||||
for i in 0 .. 16 {
|
||||
j := i * 8
|
||||
w[i] = (u64(p[j]) << 56) |
|
||||
(u64(p[j + 1]) << 48) | (u64(p[j + 2]) << 40) |
|
||||
(u64(p[j + 3]) << 32) | (u64(p[j + 4]) << 24) |
|
||||
(u64(p[j + 5]) << 16) | (u64(p[j + 6]) << 8) | u64(p[j + 7])
|
||||
// vfmt off
|
||||
w[i] = (
|
||||
u64(p[j ]) << 56) |
|
||||
(u64(p[j + 1]) << 48) |
|
||||
(u64(p[j + 2]) << 40) |
|
||||
(u64(p[j + 3]) << 32) |
|
||||
(u64(p[j + 4]) << 24) |
|
||||
(u64(p[j + 5]) << 16) |
|
||||
(u64(p[j + 6]) << 8) |
|
||||
u64(p[j + 7]
|
||||
)
|
||||
// vfmt on
|
||||
}
|
||||
for i := 16; i < 80; i++ {
|
||||
v1 := w[i - 2]
|
||||
// vfmt off
|
||||
t1 := bits.rotate_left_64(v1, -19) ^ bits.rotate_left_64(v1, -61) ^ (v1 >> 6)
|
||||
v2 := w[i - 15]
|
||||
t2 := bits.rotate_left_64(v2, -1) ^ bits.rotate_left_64(v2, -8) ^ (v2 >> 7)
|
||||
t2 := bits.rotate_left_64(v2, -1) ^ bits.rotate_left_64(v2, -8) ^ (v2 >> 7)
|
||||
// vfmt on
|
||||
w[i] = t1 + w[i - 7] + t2 + w[i - 16]
|
||||
}
|
||||
mut a := h0
|
||||
@ -68,11 +80,10 @@ fn block_generic(mut dig Digest, p_ []u8) {
|
||||
mut g := h6
|
||||
mut h := h7
|
||||
for i in 0 .. 80 {
|
||||
t1 := h +
|
||||
(bits.rotate_left_64(e, -14) ^ bits.rotate_left_64(e, -18) ^ bits.rotate_left_64(e, -41)) +
|
||||
((e & f) ^ (~e & g)) + _k[i] + w[i]
|
||||
t2 := (bits.rotate_left_64(a, -28) ^ bits.rotate_left_64(a, -34) ^ bits.rotate_left_64(a, -39)) +
|
||||
((a & b) ^ (a & c) ^ (b & c))
|
||||
// vfmt off
|
||||
t1 := h + (bits.rotate_left_64(e, -14) ^ bits.rotate_left_64(e, -18) ^ bits.rotate_left_64(e, -41)) + ((e & f) ^ (~e & g)) + _k[i] + w[i]
|
||||
t2 := (bits.rotate_left_64(a, -28) ^ bits.rotate_left_64(a, -34) ^ bits.rotate_left_64(a, -39)) + ((a & b) ^ (a & c) ^ (b & c))
|
||||
// vfmt on
|
||||
h = g
|
||||
g = f
|
||||
f = e
|
||||
|
@ -1,15 +1,11 @@
|
||||
module m4
|
||||
|
||||
/**********************************************************************
|
||||
*
|
||||
* Simply vector/matrix graphic utility
|
||||
*
|
||||
* Simple vector/matrix graphic utility
|
||||
* Copyright (c) 2021 Dario Deledda. All rights reserved.
|
||||
* Use of this source code is governed by an MIT license
|
||||
* that can be found in the LICENSE file.
|
||||
*
|
||||
* TODO:
|
||||
**********************************************************************/
|
||||
module m4
|
||||
|
||||
import math
|
||||
|
||||
// Translate degrees to radians
|
||||
@ -32,34 +28,41 @@ pub fn ortho(left f32, right f32, bottom f32, top f32, z_near f32, z_far f32) Ma
|
||||
tpb := top + bottom
|
||||
fmn := z_far - z_near
|
||||
fpn := z_far + z_near
|
||||
// vfmt off
|
||||
if fmn != 0 {
|
||||
return Mat4{ e: [
|
||||
2 / rml, 0 , 0, -(rpl / rml),
|
||||
0 , 2 / tmb, 0, -(tpb / tmb),
|
||||
0 , 0, 2 / fmn, -(fpn / fmn),
|
||||
0 , 0, 0, 1,
|
||||
return Mat4{
|
||||
e: [
|
||||
2 / rml, 0, 0, -(rpl / rml),
|
||||
0 , 2 / tmb, 0, -(tpb / tmb),
|
||||
0 , 0, 2 / fmn, -(fpn / fmn),
|
||||
0 , 0, 0, 1,
|
||||
]!
|
||||
}
|
||||
}
|
||||
return Mat4{ e: [
|
||||
2 / rml, 0 , 0, -(rpl / rml),
|
||||
0 , 2 / tmb, 0, -(tpb / tmb),
|
||||
0 , 0, 0, 0,
|
||||
0 , 0, 0, 1,
|
||||
return Mat4{
|
||||
e: [
|
||||
2 / rml, 0, 0, -(rpl / rml),
|
||||
0, 2 / tmb, 0, -(tpb / tmb),
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 1,
|
||||
]!
|
||||
}
|
||||
// vfmt on
|
||||
}
|
||||
|
||||
// Calculate the perspective matrix using (fov:fov, ar:aspect_ratio ,n:near_pane, f:far_plane) as parameters
|
||||
pub fn perspective(fov f32, ar f32, n f32, f f32) Mat4 {
|
||||
ctan := f32(1.0 / math.tan(fov * (f32(math.pi) / 360.0))) // for the FOV we use 360 instead 180
|
||||
return Mat4{ e: [
|
||||
ctan / ar, 0, 0, 0,
|
||||
0, ctan, 0, 0,
|
||||
0, 0, (n + f) / (n - f), -1.0,
|
||||
0, 0, (2.0 * n * f) / (n - f), 0,
|
||||
// vfmt off
|
||||
return Mat4{
|
||||
e: [
|
||||
ctan / ar, 0, 0, 0,
|
||||
0, ctan, 0, 0,
|
||||
0, 0, (n + f) / (n - f), -1.0,
|
||||
0, 0, (2.0 * n * f) / (n - f), 0,
|
||||
]!
|
||||
}
|
||||
// vfmt on
|
||||
}
|
||||
|
||||
// Calculate the look-at matrix
|
||||
@ -67,44 +70,33 @@ pub fn look_at(eye Vec4, center Vec4, up Vec4) Mat4 {
|
||||
f := (center - eye).normalize3()
|
||||
s := (f % up).normalize3()
|
||||
u := (s % f)
|
||||
|
||||
return Mat4{ e: [
|
||||
/* [0][0] */ s.e[0],
|
||||
/* [0][1] */ u.e[0],
|
||||
/* [0][2] */ - f.e[0],
|
||||
/* [0][3] */ 0,
|
||||
|
||||
/* [1][1] */ s.e[1],
|
||||
/* [1][1] */ u.e[1],
|
||||
/* [1][2] */ - f.e[1],
|
||||
/* [1][3] */ 0,
|
||||
|
||||
/* [2][0] */ s.e[2],
|
||||
/* [2][1] */ u.e[2],
|
||||
/* [2][2] */ - f.e[2],
|
||||
/* [2][3] */ 0,
|
||||
|
||||
/* [3][0] */ - (s * eye),
|
||||
/* [3][1] */ - (u * eye),
|
||||
/* [3][2] */ f * eye,
|
||||
/* [3][3] */ 1,
|
||||
]!
|
||||
}
|
||||
// vfmt off
|
||||
return Mat4{e: [
|
||||
/* [0][0] */ s.e[0], /* [0][1] */ u.e[0], /* [0][2] */ -f.e[0], /* [0][3] */ 0,
|
||||
/* [1][1] */ s.e[1], /* [1][1] */ u.e[1], /* [1][2] */ -f.e[1], /* [1][3] */ 0,
|
||||
/* [2][0] */ s.e[2], /* [2][1] */ u.e[2], /* [2][2] */ -f.e[2], /* [2][3] */ 0,
|
||||
/* [3][0] */ -(s * eye), /* [3][1] */ -(u * eye), /* [3][2] */ f * eye, /* [3][3] */ 1,
|
||||
]!}
|
||||
// vfmt on
|
||||
}
|
||||
|
||||
|
||||
// Get the complete transformation matrix for GLSL demos
|
||||
pub fn calc_tr_matrices(w f32, h f32, rx f32, ry f32, in_scale f32) Mat4 {
|
||||
proj := perspective(60, w / h, 0.01, 10.0)
|
||||
view := look_at(Vec4{ e: [f32(0.0), 1.5, 6, 0]! }, Vec4{ e: [f32(0), 0, 0, 0]! }, Vec4{ e: [f32(0), 1.0, 0, 0]! })
|
||||
// vfmt off
|
||||
view := look_at(
|
||||
Vec4{ e: [f32(0.0), 1.5, 6, 0]! },
|
||||
Vec4{ e: [f32(0), 0, 0, 0]!},
|
||||
Vec4{ e: [f32(0), 1.0, 0, 0]!}
|
||||
)
|
||||
view_proj := view * proj
|
||||
|
||||
rxm := rotate(rad(rx), Vec4{ e: [f32(1), 0, 0, 0]! })
|
||||
rym := rotate(rad(ry), Vec4{ e: [f32(0), 1, 0, 0]! })
|
||||
scale_m := scale(Vec4{ e: [in_scale, in_scale, in_scale, 1]! })
|
||||
// vfmt on
|
||||
|
||||
model := rym * rxm
|
||||
scale_m := scale(Vec4{ e: [in_scale, in_scale, in_scale, 1]! })
|
||||
|
||||
res := (scale_m * model) * view_proj
|
||||
return res
|
||||
}
|
||||
|
@ -1,25 +1,26 @@
|
||||
import gg.m4
|
||||
|
||||
// vfmt off
|
||||
pub fn test_m4() {
|
||||
unsafe {
|
||||
// Test Mat4
|
||||
mut a := m4.Mat4{ e: [
|
||||
f32(0), 1, 2, 3,
|
||||
4, 5, 6, 7,
|
||||
8, 9, 10, 11,
|
||||
f32(0), 1, 2, 3,
|
||||
4, 5, 6, 7,
|
||||
8, 9, 10, 11,
|
||||
12, 13, 14, 15,
|
||||
]!
|
||||
]!
|
||||
}
|
||||
mut b := m4.Mat4{}
|
||||
mut c := m4.Mat4{}
|
||||
|
||||
// equal test
|
||||
assert a.e == [
|
||||
f32(0), 1, 2, 3,
|
||||
4, 5, 6, 7,
|
||||
8, 9, 10, 11,
|
||||
f32(0), 1, 2, 3,
|
||||
4, 5, 6, 7,
|
||||
8, 9, 10, 11,
|
||||
12, 13, 14, 15,
|
||||
]!
|
||||
]!
|
||||
|
||||
// copy test
|
||||
b.copy(a)
|
||||
@ -53,33 +54,33 @@ fn test_swap_col_row() {
|
||||
unsafe {
|
||||
// swap_col / swap_row
|
||||
b := m4.Mat4{ e: [
|
||||
f32(1), 2, 3, 4,
|
||||
5, 6, 7, 8,
|
||||
9, 10, 11, 12,
|
||||
f32(1), 2, 3, 4,
|
||||
5, 6, 7, 8,
|
||||
9, 10, 11, 12,
|
||||
13, 14, 15, 16,
|
||||
]!
|
||||
]!
|
||||
}
|
||||
b.swap_col(0, 2)
|
||||
assert b.e == [
|
||||
f32(3), 2, 1, 4,
|
||||
7, 6, 5, 8,
|
||||
11, 10, 9, 12,
|
||||
f32(3), 2, 1, 4,
|
||||
7, 6, 5, 8,
|
||||
11, 10, 9, 12,
|
||||
15, 14, 13, 16,
|
||||
]!
|
||||
]!
|
||||
b = m4.Mat4{ e: [
|
||||
f32(1), 2, 3, 4,
|
||||
5, 6, 7, 8,
|
||||
9, 10, 11, 12,
|
||||
f32(1), 2, 3, 4,
|
||||
5, 6, 7, 8,
|
||||
9, 10, 11, 12,
|
||||
13, 14, 15, 16,
|
||||
]!
|
||||
]!
|
||||
}
|
||||
b.swap_row(0, 2)
|
||||
assert b.e == [
|
||||
f32(9), 10, 11, 12,
|
||||
5, 6, 7, 8,
|
||||
1, 2, 3, 4,
|
||||
5, 6, 7, 8,
|
||||
1, 2, 3, 4,
|
||||
13, 14, 15, 16,
|
||||
]!
|
||||
]!
|
||||
}
|
||||
}
|
||||
|
||||
@ -96,11 +97,11 @@ fn test_sum_sub() {
|
||||
fn test_transpose() {
|
||||
unsafe {
|
||||
b := m4.Mat4{ e: [
|
||||
f32(0), 1, 2, 3,
|
||||
4, 5, 6, 7,
|
||||
8, 9, 10, 11,
|
||||
f32(0), 1, 2, 3,
|
||||
4, 5, 6, 7,
|
||||
8, 9, 10, 11,
|
||||
12, 13, 14, 15,
|
||||
]!
|
||||
]!
|
||||
}
|
||||
assert b.transpose().transpose().e == b.e
|
||||
}
|
||||
@ -109,18 +110,18 @@ fn test_transpose() {
|
||||
fn test_multiplication() {
|
||||
unsafe {
|
||||
b := m4.Mat4{ e: [
|
||||
f32(1), 0, 0, 0,
|
||||
f32(1), 0, 0, 0,
|
||||
0, 2, 0, 0,
|
||||
0, 0, 3, 0,
|
||||
0, 0, 0, 4,
|
||||
]!
|
||||
]!
|
||||
}
|
||||
c := m4.Mat4{ e: [
|
||||
f32(1), 2, 3, 4,
|
||||
5, 6, 7, 8,
|
||||
9, 10, 11, 12,
|
||||
f32(1), 2, 3, 4,
|
||||
5, 6, 7, 8,
|
||||
9, 10, 11, 12,
|
||||
13, 14, 15, 16,
|
||||
]!
|
||||
]!
|
||||
}
|
||||
|
||||
assert (c * c).e == [
|
||||
@ -128,7 +129,7 @@ fn test_multiplication() {
|
||||
202,228,254,280,
|
||||
314,356,398,440,
|
||||
426,484,542,600,
|
||||
]!
|
||||
]!
|
||||
|
||||
assert m4.mul(c, c).e == [
|
||||
f32(90),100,110,120,
|
||||
@ -138,18 +139,18 @@ fn test_multiplication() {
|
||||
]!
|
||||
|
||||
assert m4.mul(b, c).e == [
|
||||
f32(1), 2, 3, 4,
|
||||
f32(1), 2, 3, 4,
|
||||
10, 12, 14, 16,
|
||||
27, 30, 33, 36,
|
||||
52, 56, 60, 64,
|
||||
]!
|
||||
]!
|
||||
|
||||
assert (b * c).e == [
|
||||
f32(1), 2, 3, 4,
|
||||
f32(1), 2, 3, 4,
|
||||
10, 12, 14, 16,
|
||||
27, 30, 33, 36,
|
||||
52, 56, 60, 64,
|
||||
]!
|
||||
]!
|
||||
|
||||
assert m4.det(b) == 24
|
||||
}
|
||||
@ -158,20 +159,20 @@ fn test_multiplication() {
|
||||
fn test_det() {
|
||||
unsafe {
|
||||
b := m4.Mat4{ e: [
|
||||
f32(5), 6, 6, 8,
|
||||
f32(5), 6, 6, 8,
|
||||
2, 2, 2, 8,
|
||||
6, 6, 2, 8,
|
||||
2, 3, 6, 7,
|
||||
]!
|
||||
]!
|
||||
}
|
||||
assert m4.det(b) == -8
|
||||
|
||||
c := m4.Mat4{ e: [
|
||||
f32(1), 8, 2, 3,
|
||||
f32(1), 8, 2, 3,
|
||||
8, 2, 3, 1,
|
||||
2, 3, 3, 2,
|
||||
3, 1, 2, 4,
|
||||
]!
|
||||
]!
|
||||
}
|
||||
// println("*** INVERSE ****")
|
||||
// println(m4.mul(b.inverse(),b))
|
||||
@ -190,8 +191,8 @@ fn test_vec4() {
|
||||
assert v * v.inv() == 4
|
||||
assert v.mul_scalar(1.0 / v.mod()).mod() == 1
|
||||
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 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 f32_abs(v.normalize().mod() - 1) < m4.precision
|
||||
v = m4.Vec4{[f32(1), 2, 3, 0]!}
|
||||
assert f32_abs(v.normalize3().mod3() - 1) < m4.precision
|
||||
@ -201,35 +202,36 @@ fn test_vec4() {
|
||||
// 1 2 3 ==> -3 6 -3 0
|
||||
// 4 5 6
|
||||
// println(m4.Vec4{[f32(1),2,3,2]!} % m4.Vec4{[f32(4),5,6,2]!})
|
||||
assert m4.Vec4{[f32(1), 2, 3, 0]!} % m4.Vec4{[f32(4), 5, 6, 0]!} == m4.Vec4{[ f32(-3), 6, -3, 0, ]!}
|
||||
assert m4.Vec4{[f32(1), 2, 3, 13]!} % m4.Vec4{[f32(4), 5, 6, 11]!} == m4.Vec4{[ f32(-3), 6, -3, 0, ]!}
|
||||
assert m4.Vec4{[f32(1), 2, 3, 0]!} % m4.Vec4{[f32(4), 5, 6, 0 ]!} == m4.Vec4{[ f32(-3), 6, -3, 0, ]!}
|
||||
assert m4.Vec4{[f32(1), 2, 3, 13]!} % m4.Vec4{[f32(4), 5, 6, 11]!} == m4.Vec4{[ f32(-3), 6, -3, 0, ]!}
|
||||
// matrix * vector
|
||||
a := m4.Mat4{ e: [
|
||||
f32(1),2,3,4
|
||||
5,6,7,8
|
||||
9,10,11,12
|
||||
13,14,15,16
|
||||
f32(1), 2, 3, 4
|
||||
5, 6, 7, 8
|
||||
9, 10, 11, 12
|
||||
13, 14, 15, 16
|
||||
]!
|
||||
}
|
||||
assert m4.mul_vec(a, m4.Vec4{[f32(1), 2, 3, 4]!}) == m4.Vec4{[ f32(30), 70, 110,150, ]!}
|
||||
// Rotation
|
||||
// println("*** Rotation ****")
|
||||
rotx := m4.rotate(m4.rad(-90), m4.Vec4{ e: [f32(1.0), 0, 0, 0]! }).clean()
|
||||
roty := m4.rotate(m4.rad(-90), m4.Vec4{ e: [f32(0), 1.0, 0, 0]! }).clean()
|
||||
rotz := m4.rotate(m4.rad(-90), m4.Vec4{ e: [f32(0), 0, 1, 0]! }).clean()
|
||||
rotx := m4.rotate(m4.rad(-90), m4.Vec4{ e: [f32(1.0), 0, 0, 0]! }).clean()
|
||||
roty := m4.rotate(m4.rad(-90), m4.Vec4{ e: [f32(0), 1.0, 0, 0]! }).clean()
|
||||
rotz := m4.rotate(m4.rad(-90), m4.Vec4{ e: [f32(0), 0, 1, 0]! }).clean()
|
||||
// println( rotx )
|
||||
// println( roty )
|
||||
// println( rotz )
|
||||
// println( m4.mul_vec(rotx, m4.Vec4{e:[f32(0),0,1,0]!}).clean())
|
||||
assert m4.mul_vec(roty, m4.Vec4{ e: [f32(1.0), 0.0, 0, 0]! }).clean() == m4.Vec4{ e: [f32(0), 0.0, -1, 0]! }
|
||||
assert m4.mul_vec(rotz, m4.Vec4{ e: [f32(1.0), 0.0, 0, 0]! }).clean() == m4.Vec4{ e: [f32(0), 1, 0, 0]! }
|
||||
assert m4.mul_vec(rotx, m4.Vec4{ e: [f32(0), 0, 1, 0]! }).clean() == m4.Vec4{ e: [f32(0), -1, 0, 0]! }
|
||||
assert m4.mul_vec(roty, m4.Vec4{ e: [f32(1.0), 0.0, 0, 0]! }).clean() == m4.Vec4{ e: [f32(0), 0.0, -1, 0]! }
|
||||
assert m4.mul_vec(rotz, m4.Vec4{ e: [f32(1.0), 0.0, 0, 0]! }).clean() == m4.Vec4{ e: [f32(0), 1, 0, 0]! }
|
||||
assert m4.mul_vec(rotx, m4.Vec4{ e: [f32(0), 0, 1, 0]! }).clean() == m4.Vec4{ e: [f32(0), -1, 0, 0]! }
|
||||
// println("****************")
|
||||
}
|
||||
|
||||
fn test_proj() {
|
||||
ort := m4.ortho(0,300,0,200,0,0)
|
||||
assert m4.mul_vec(ort, m4.Vec4{[ f32(150), 100, 0, 1]!}) == m4.Vec4{[ f32(0), 0, 0, 1]!}
|
||||
assert m4.mul_vec(ort, m4.Vec4{[ f32(0), 0, 0, 1]!}) == m4.Vec4{[ f32(-1), -1, 0, 1]!}
|
||||
assert m4.mul_vec(ort, m4.Vec4{[ f32(300), 200, 0, 1]!}) == m4.Vec4{[ f32(1), 1, 0, 1]!}
|
||||
assert m4.mul_vec(ort, m4.Vec4{[ f32(150), 100, 0, 1]!}) == m4.Vec4{[ f32(0), 0, 0, 1]!}
|
||||
assert m4.mul_vec(ort, m4.Vec4{[ f32(0), 0, 0, 1]!}) == m4.Vec4{[ f32(-1), -1, 0, 1]!}
|
||||
assert m4.mul_vec(ort, m4.Vec4{[ f32(300), 200, 0, 1]!}) == m4.Vec4{[ f32(1), 1, 0, 1]!}
|
||||
}
|
||||
// vfmt on
|
||||
|
@ -30,10 +30,13 @@ pub const precision = f32(10e-7)
|
||||
// String representation of the matrix
|
||||
pub fn (x Mat4) str() string {
|
||||
unsafe {
|
||||
return '|${x.e[0]:-6.3},${x.e[1]:-6.3},${x.e[2]:-6.3},${x.e[3]:-6.3}|\n' +
|
||||
'|${x.e[4]:-6.3},${x.e[5]:-6.3},${x.e[6]:-6.3},${x.e[7]:-6.3}|\n' +
|
||||
'|${x.e[8]:-6.3},${x.e[9]:-6.3},${x.e[10]:-6.3},${x.e[11]:-6.3}|\n' +
|
||||
// vfmt off
|
||||
return
|
||||
'|${x.e[0]: -6.3},${x.e[1]: -6.3},${x.e[2]: -6.3},${x.e[3]: -6.3}|\n' +
|
||||
'|${x.e[4]: -6.3},${x.e[5]: -6.3},${x.e[6]: -6.3},${x.e[7]: -6.3}|\n' +
|
||||
'|${x.e[8]: -6.3},${x.e[9]: -6.3},${x.e[10]:-6.3},${x.e[11]:-6.3}|\n' +
|
||||
'|${x.e[12]:-6.3},${x.e[13]:-6.3},${x.e[14]:-6.3},${x.e[15]:-6.3}|'
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
@ -110,10 +113,12 @@ 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 = [
|
||||
// vfmt off
|
||||
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],
|
||||
// vfmt on
|
||||
]!
|
||||
}
|
||||
}
|
||||
@ -121,29 +126,35 @@ 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 {
|
||||
// vfmt off
|
||||
x.e[0 ] = v3.e[0]
|
||||
x.e[5 ] = v3.e[1]
|
||||
x.e[10] = v3.e[2]
|
||||
x.e[15] = v3.e[3]
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
// Get the trace of the matrix
|
||||
pub fn (x Mat4) get_trace() Vec4 {
|
||||
unsafe {
|
||||
return Vec4{ e: [ x.e[0], x.e[5], x.e[10], x.e[15], ]! }
|
||||
// vfmt off
|
||||
return Vec4{ e: [ x.e[0], x.e[5], x.e[10], x.e[15], ]! }
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
// Set all the matrix elements to value
|
||||
pub fn (mut x Mat4) set_f32(value f32) {
|
||||
unsafe {
|
||||
// vfmt off
|
||||
x.e = [
|
||||
value, value, value, value,
|
||||
value, value, value, value,
|
||||
value, value, value, value,
|
||||
value, value, value, value,
|
||||
value, value, value, value,
|
||||
value, value, value, value,
|
||||
value, value, value, value,
|
||||
value, value, value, value,
|
||||
]!
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
@ -151,8 +162,7 @@ pub fn (mut x Mat4) set_f32(value f32) {
|
||||
// Rows/Column access
|
||||
//-------------------------------------
|
||||
// Set the row as the input vec4
|
||||
[direct_array_access]
|
||||
[unsafe]
|
||||
[direct_array_access; unsafe]
|
||||
pub fn (mut x Mat4) set_row(row int, v3 Vec4) {
|
||||
unsafe {
|
||||
x.e[row * 4 + 0] = v3.e[0]
|
||||
@ -163,74 +173,63 @@ pub fn (mut x Mat4) set_row(row int, v3 Vec4) {
|
||||
}
|
||||
|
||||
// Get a row from a matrix
|
||||
[direct_array_access]
|
||||
[unsafe]
|
||||
[direct_array_access; unsafe]
|
||||
pub fn (x Mat4) get_row(row int) Vec4 {
|
||||
unsafe {
|
||||
return Vec4{
|
||||
e: [
|
||||
x.e[row * 4 + 0],
|
||||
x.e[row * 4 + 1],
|
||||
x.e[row * 4 + 2],
|
||||
x.e[row * 4 + 3],
|
||||
]!
|
||||
}
|
||||
// vfmt off
|
||||
return Vec4{ e: [ x.e[row * 4], x.e[row * 4 + 1], x.e[row * 4 + 2], x.e[row * 4 + 3], ]! }
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
// Set the column as the input vec4
|
||||
[direct_array_access]
|
||||
[unsafe]
|
||||
[direct_array_access; unsafe]
|
||||
pub fn (mut x Mat4) set_col(col int, v3 Vec4) {
|
||||
unsafe {
|
||||
x.e[col] = v3.e[0]
|
||||
// vfmt off
|
||||
x.e[col ] = v3.e[0]
|
||||
x.e[col + 4 ] = v3.e[1]
|
||||
x.e[col + 8 ] = v3.e[2]
|
||||
x.e[col + 12] = v3.e[3]
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
// Get a column from a matrix
|
||||
[direct_array_access]
|
||||
[unsafe]
|
||||
[direct_array_access; unsafe]
|
||||
pub fn (x Mat4) get_col(col int) Vec4 {
|
||||
unsafe {
|
||||
return Vec4{
|
||||
e: [
|
||||
x.e[col],
|
||||
x.e[col + 4 ],
|
||||
x.e[col + 8 ],
|
||||
x.e[col + 12],
|
||||
]!
|
||||
}
|
||||
// vfmt off
|
||||
return Vec4{ e: [ x.e[col], x.e[col + 4 ], x.e[col + 8 ], x.e[col + 12], ]!}
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
// Swap two columns in the matrix
|
||||
[direct_array_access]
|
||||
[unsafe]
|
||||
[direct_array_access; unsafe]
|
||||
pub fn (mut x Mat4) swap_col(col1 int, col2 int) {
|
||||
unsafe {
|
||||
v0 := x.e[col1]
|
||||
// vfmt off
|
||||
v0 := x.e[col1 ]
|
||||
v1 := x.e[col1 + 4 ]
|
||||
v2 := x.e[col1 + 8 ]
|
||||
v3 := x.e[col1 + 12]
|
||||
|
||||
x.e[col1] = x.e[col2]
|
||||
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 + 12] = x.e[col2 + 12]
|
||||
|
||||
x.e[col2] = v0
|
||||
x.e[col2 ] = v0
|
||||
x.e[col2 + 4 ] = v1
|
||||
x.e[col2 + 8 ] = v2
|
||||
x.e[col2 + 12] = v3
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
// Swap two rows in the matrix
|
||||
[direct_array_access]
|
||||
[unsafe]
|
||||
[direct_array_access; unsafe]
|
||||
pub fn (mut x Mat4) swap_row(row1 int, row2 int) {
|
||||
unsafe {
|
||||
v0 := x.e[row1 * 4 + 0]
|
||||
@ -256,26 +255,28 @@ pub fn (mut x Mat4) swap_row(row1 int, row2 int) {
|
||||
// Transpose the matrix
|
||||
pub fn (x Mat4) transpose() Mat4 {
|
||||
unsafe {
|
||||
// vfmt off
|
||||
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],
|
||||
]!}
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
// Multiply the all the elements of the matrix by a scalar
|
||||
pub fn (x Mat4) mul_scalar(s f32) Mat4 {
|
||||
unsafe {
|
||||
// vfmt off
|
||||
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[12] * s, x.e[13] * s, x.e[14] * s, x.e[15] * 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,
|
||||
]!}
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
@ -286,35 +287,38 @@ pub fn (x Mat4) mul_scalar(s f32) Mat4 {
|
||||
*********************************************************************/
|
||||
// Return a zero matrix
|
||||
pub fn zero_m4() Mat4 {
|
||||
// vfmt off
|
||||
return Mat4{ e: [
|
||||
f32(0), 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
]!
|
||||
}
|
||||
f32(0), 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
]!}
|
||||
// vfmt on
|
||||
}
|
||||
|
||||
// Return a unity matrix
|
||||
pub fn unit_m4() Mat4 {
|
||||
// vfmt off
|
||||
return Mat4{ e: [
|
||||
f32(1), 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1,
|
||||
]!
|
||||
}
|
||||
f32(1), 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1,
|
||||
]!}
|
||||
// vfmt on
|
||||
}
|
||||
|
||||
// Return a matrix initialized with value
|
||||
pub fn set_m4(value f32) Mat4 {
|
||||
// vfmt off
|
||||
return Mat4{ e: [
|
||||
value, value, value, value,
|
||||
value, value, value, value,
|
||||
value, value, value, value,
|
||||
value, value, value, value,
|
||||
]!
|
||||
}
|
||||
value, value, value, value,
|
||||
value, value, value, value,
|
||||
value, value, value, value,
|
||||
value, value, value, value,
|
||||
]!}
|
||||
// vfmt on
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
@ -326,55 +330,57 @@ pub fn set_m4(value f32) Mat4 {
|
||||
// Sum of matrix, operator +
|
||||
pub fn (a Mat4) + (b Mat4) Mat4 {
|
||||
unsafe {
|
||||
// vfmt off
|
||||
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[12] + b.e[12], a.e[13] + b.e[13], a.e[14] + b.e[14], a.e[15] + b.e[15],
|
||||
]!
|
||||
}
|
||||
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],
|
||||
]!}
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
// Subtraction of matrix, operator -
|
||||
pub fn (a Mat4) - (b Mat4) Mat4 {
|
||||
unsafe {
|
||||
// vfmt off
|
||||
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[12] - b.e[12], a.e[13] - b.e[13], a.e[14] - b.e[14], a.e[15] - b.e[15],
|
||||
]!
|
||||
}
|
||||
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],
|
||||
]!}
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
// Multiplication of matrix, operator *
|
||||
pub fn (a Mat4) * (b Mat4) Mat4 {
|
||||
unsafe {
|
||||
return Mat4{
|
||||
e: [
|
||||
/* [0][0] */ a.f[0][0] * b.f[0][0] + a.f[0][1] * b.f[1][0] + a.f[0][2] * b.f[2][0] + a.f[0][3] * b.f[3][0]
|
||||
/* [0][1] */, a.f[0][0] * b.f[0][1] + a.f[0][1] * b.f[1][1] + a.f[0][2] * b.f[2][1] + a.f[0][3] * b.f[3][1]
|
||||
/* [0][2] */, a.f[0][0] * b.f[0][2] + a.f[0][1] * b.f[1][2] + a.f[0][2] * b.f[2][2] + a.f[0][3] * b.f[3][2]
|
||||
/* [0][3] */, a.f[0][0] * b.f[0][3] + a.f[0][1] * b.f[1][3] + a.f[0][2] * b.f[2][3] + a.f[0][3] * b.f[3][3]
|
||||
// vfmt off
|
||||
return Mat4{ e: [
|
||||
/* [0][0] */ a.f[0][0] * b.f[0][0] + a.f[0][1] * b.f[1][0] + a.f[0][2] * b.f[2][0] + a.f[0][3] * b.f[3][0],
|
||||
/* [0][1] */ a.f[0][0] * b.f[0][1] + a.f[0][1] * b.f[1][1] + a.f[0][2] * b.f[2][1] + a.f[0][3] * b.f[3][1],
|
||||
/* [0][2] */ a.f[0][0] * b.f[0][2] + a.f[0][1] * b.f[1][2] + a.f[0][2] * b.f[2][2] + a.f[0][3] * b.f[3][2],
|
||||
/* [0][3] */ a.f[0][0] * b.f[0][3] + a.f[0][1] * b.f[1][3] + a.f[0][2] * b.f[2][3] + a.f[0][3] * b.f[3][3],
|
||||
|
||||
/* [1][0] */, a.f[1][0] * b.f[0][0] + a.f[1][1] * b.f[1][0] + a.f[1][2] * b.f[2][0] + a.f[1][3] * b.f[3][0]
|
||||
/* [1][1] */, a.f[1][0] * b.f[0][1] + a.f[1][1] * b.f[1][1] + a.f[1][2] * b.f[2][1] + a.f[1][3] * b.f[3][1]
|
||||
/* [1][2] */, a.f[1][0] * b.f[0][2] + a.f[1][1] * b.f[1][2] + a.f[1][2] * b.f[2][2] + a.f[1][3] * b.f[3][2]
|
||||
/* [1][3] */, a.f[1][0] * b.f[0][3] + a.f[1][1] * b.f[1][3] + a.f[1][2] * b.f[2][3] + a.f[1][3] * b.f[3][3]
|
||||
/* [1][0] */ a.f[1][0] * b.f[0][0] + a.f[1][1] * b.f[1][0] + a.f[1][2] * b.f[2][0] + a.f[1][3] * b.f[3][0],
|
||||
/* [1][1] */ a.f[1][0] * b.f[0][1] + a.f[1][1] * b.f[1][1] + a.f[1][2] * b.f[2][1] + a.f[1][3] * b.f[3][1],
|
||||
/* [1][2] */ a.f[1][0] * b.f[0][2] + a.f[1][1] * b.f[1][2] + a.f[1][2] * b.f[2][2] + a.f[1][3] * b.f[3][2],
|
||||
/* [1][3] */ a.f[1][0] * b.f[0][3] + a.f[1][1] * b.f[1][3] + a.f[1][2] * b.f[2][3] + a.f[1][3] * b.f[3][3],
|
||||
|
||||
/* [2][0] */, a.f[2][0] * b.f[0][0] + a.f[2][1] * b.f[1][0] + a.f[2][2] * b.f[2][0] + a.f[2][3] * b.f[3][0]
|
||||
/* [2][1] */, a.f[2][0] * b.f[0][1] + a.f[2][1] * b.f[1][1] + a.f[2][2] * b.f[2][1] + a.f[2][3] * b.f[3][1]
|
||||
/* [2][2] */, a.f[2][0] * b.f[0][2] + a.f[2][1] * b.f[1][2] + a.f[2][2] * b.f[2][2] + a.f[2][3] * b.f[3][2]
|
||||
/* [2][3] */, a.f[2][0] * b.f[0][3] + a.f[2][1] * b.f[1][3] + a.f[2][2] * b.f[2][3] + a.f[2][3] * b.f[3][3]
|
||||
/* [2][0] */ a.f[2][0] * b.f[0][0] + a.f[2][1] * b.f[1][0] + a.f[2][2] * b.f[2][0] + a.f[2][3] * b.f[3][0],
|
||||
/* [2][1] */ a.f[2][0] * b.f[0][1] + a.f[2][1] * b.f[1][1] + a.f[2][2] * b.f[2][1] + a.f[2][3] * b.f[3][1],
|
||||
/* [2][2] */ a.f[2][0] * b.f[0][2] + a.f[2][1] * b.f[1][2] + a.f[2][2] * b.f[2][2] + a.f[2][3] * b.f[3][2],
|
||||
/* [2][3] */ a.f[2][0] * b.f[0][3] + a.f[2][1] * b.f[1][3] + a.f[2][2] * b.f[2][3] + a.f[2][3] * b.f[3][3],
|
||||
|
||||
/* [3][0] */, a.f[3][0] * b.f[0][0] + a.f[3][1] * b.f[1][0] + a.f[3][2] * b.f[2][0] + a.f[3][3] * b.f[3][0]
|
||||
/* [3][1] */, a.f[3][0] * b.f[0][1] + a.f[3][1] * b.f[1][1] + a.f[3][2] * b.f[2][1] + a.f[3][3] * b.f[3][1]
|
||||
/* [3][2] */, a.f[3][0] * b.f[0][2] + a.f[3][1] * b.f[1][2] + a.f[3][2] * b.f[2][2] + a.f[3][3] * b.f[3][2]
|
||||
/* [3][3] */, a.f[3][0] * b.f[0][3] + a.f[3][1] * b.f[1][3] + a.f[3][2] * b.f[2][3] + a.f[3][3] * b.f[3][3],
|
||||
]!
|
||||
}
|
||||
/* [3][0] */ a.f[3][0] * b.f[0][0] + a.f[3][1] * b.f[1][0] + a.f[3][2] * b.f[2][0] + a.f[3][3] * b.f[3][0],
|
||||
/* [3][1] */ a.f[3][0] * b.f[0][1] + a.f[3][1] * b.f[1][1] + a.f[3][2] * b.f[2][1] + a.f[3][3] * b.f[3][1],
|
||||
/* [3][2] */ a.f[3][0] * b.f[0][2] + a.f[3][1] * b.f[1][2] + a.f[3][2] * b.f[2][2] + a.f[3][3] * b.f[3][2],
|
||||
/* [3][3] */ a.f[3][0] * b.f[0][3] + a.f[3][1] * b.f[1][3] + a.f[3][2] * b.f[2][3] + a.f[3][3] * b.f[3][3],
|
||||
]!}
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
@ -402,13 +408,14 @@ pub fn mul(a Mat4, b Mat4) Mat4 {
|
||||
// Multiply a Matrix by a vector
|
||||
pub fn mul_vec(a Mat4, v Vec4) Vec4 {
|
||||
unsafe {
|
||||
// vfmt off
|
||||
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[12] * v.e[0] + a.e[13] * v.e[1] + a.e[14] * v.e[2] + a.e[15] * 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],
|
||||
]!}
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
@ -440,11 +447,13 @@ pub fn det(x Mat4) f32 {
|
||||
t[4] = x02 * x23 - x03 * x22
|
||||
t[5] = x02 * x13 - x03 * x12
|
||||
|
||||
return 0.0 +
|
||||
// vfmt off
|
||||
return
|
||||
x00 * (x11 * t[0] - x21 * t[1] + x31 * t[2]) -
|
||||
x10 * (x01 * t[0] - x21 * t[3] + x31 * t[4]) +
|
||||
x20 * (x01 * t[1] - x11 * t[3] + x31 * t[5]) -
|
||||
x30 * (x01 * t[2] - x11 * t[4] + x21 * t[5])
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
@ -454,6 +463,7 @@ pub fn (x Mat4) inverse() Mat4 {
|
||||
mut t := [6]f32{}
|
||||
mut det := f32(0)
|
||||
|
||||
// vfmt off
|
||||
a := x.f[0][0]
|
||||
b := x.f[1][0]
|
||||
c := x.f[2][0]
|
||||
@ -479,15 +489,15 @@ pub fn (x Mat4) inverse() Mat4 {
|
||||
t[5] = i * n - m * j
|
||||
|
||||
mut dest := Mat4{}
|
||||
dest.f[0][0] = f * t[0] - g * t[1] + h * t[2]
|
||||
dest.f[0][0] = f * t[0] - g * t[1] + h * t[2]
|
||||
dest.f[0][1] = -(e * t[0] - g * t[3] + h * t[4])
|
||||
dest.f[0][2] = e * t[1] - f * t[3] + h * t[5]
|
||||
dest.f[0][2] = e * t[1] - f * t[3] + h * t[5]
|
||||
dest.f[0][3] = -(e * t[2] - f * t[4] + g * t[5])
|
||||
|
||||
dest.f[1][0] = -(b * t[0] - c * t[1] + d * t[2])
|
||||
dest.f[1][1] = a * t[0] - c * t[3] + d * t[4]
|
||||
dest.f[1][1] = a * t[0] - c * t[3] + d * t[4]
|
||||
dest.f[1][2] = -(a * t[1] - b * t[3] + d * t[5])
|
||||
dest.f[1][3] = a * t[2] - b * t[4] + c * t[5]
|
||||
dest.f[1][3] = a * t[2] - b * t[4] + c * t[5]
|
||||
|
||||
t[0] = g * p - o * h
|
||||
t[1] = f * p - n * h
|
||||
@ -496,9 +506,9 @@ pub fn (x Mat4) inverse() Mat4 {
|
||||
t[4] = e * o - m * g
|
||||
t[5] = e * n - m * f
|
||||
|
||||
dest.f[2][0] = b * t[0] - c * t[1] + d * t[2]
|
||||
dest.f[2][0] = b * t[0] - c * t[1] + d * t[2]
|
||||
dest.f[2][1] = -(a * t[0] - c * t[3] + d * t[4])
|
||||
dest.f[2][2] = a * t[1] - b * t[3] + d * t[5]
|
||||
dest.f[2][2] = a * t[1] - b * t[3] + d * t[5]
|
||||
dest.f[2][3] = -(a * t[2] - b * t[4] + c * t[5])
|
||||
|
||||
t[0] = g * l - k * h
|
||||
@ -509,9 +519,10 @@ pub fn (x Mat4) inverse() Mat4 {
|
||||
t[5] = e * j - i * f
|
||||
|
||||
dest.f[3][0] = -(b * t[0] - c * t[1] + d * t[2])
|
||||
dest.f[3][1] = a * t[0] - c * t[3] + d * t[4]
|
||||
dest.f[3][1] = a * t[0] - c * t[3] + d * t[4]
|
||||
dest.f[3][2] = -(a * t[1] - b * t[3] + d * t[5])
|
||||
dest.f[3][3] = a * t[2] - b * t[4] + c * t[5]
|
||||
dest.f[3][3] = a * t[2] - b * t[4] + c * t[5]
|
||||
// vfmt on
|
||||
|
||||
tmp := (a * dest.f[0][0] + b * dest.f[0][1] + c * dest.f[0][2] + d * dest.f[0][3])
|
||||
if tmp != 0 {
|
||||
@ -538,28 +549,14 @@ pub fn rotate(angle f32, w Vec4) Mat4 {
|
||||
ay := axis.e[1]
|
||||
az := axis.e[2]
|
||||
|
||||
// vfmt off
|
||||
return Mat4{ e: [
|
||||
/* [0][0] */ (ax * ax * cv) + cs
|
||||
/* [0][1] */, (ax * ay * cv) + az * sn
|
||||
/* [0][2] */, (ax * az * cv) - ay * sn
|
||||
/* [0][3] */, 0
|
||||
|
||||
/* [1][0] */, (ay * ax * cv) - az * sn
|
||||
/* [1][1] */, (ay * ay * cv) + cs
|
||||
/* [1][2] */, (ay * az * cv) + ax * sn
|
||||
/* [1][3] */, 0
|
||||
|
||||
/* [2][0] */, (az * ax * cv) + ay * sn
|
||||
/* [2][1] */, (az * ay * cv) - ax * sn
|
||||
/* [2][2] */, (az * az * cv) + cs
|
||||
/* [2][3] */, 0
|
||||
|
||||
/* [3][0] */, 0
|
||||
/* [3][1] */, 0
|
||||
/* [3][2] */, 0
|
||||
/* [3][3] */, 1,
|
||||
]!
|
||||
}
|
||||
/* [0][0] */ (ax * ax * cv) + cs , /* [0][1] */ (ax * ay * cv) + az * sn , /* [0][2] */ (ax * az * cv) - ay * sn , /* [0][3] */ 0,
|
||||
/* [1][0] */ (ay * ax * cv) - az * sn , /* [1][1] */ (ay * ay * cv) + cs , /* [1][2] */ (ay * az * cv) + ax * sn , /* [1][3] */ 0,
|
||||
/* [2][0] */ (az * ax * cv) + ay * sn , /* [2][1] */ (az * ay * cv) - ax * sn , /* [2][2] */ (az * az * cv) + cs , /* [2][3] */ 0,
|
||||
/* [3][0] */ 0, /* [3][1] */ 0 , /* [3][2] */ 0 , /* [3][3] */ 1,
|
||||
]!}
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
@ -571,25 +568,27 @@ pub fn rotate(angle f32, w Vec4) Mat4 {
|
||||
// Get a matrix translated by a vector w
|
||||
pub fn (x Mat4) translate(w Vec4) Mat4 {
|
||||
unsafe {
|
||||
// vfmt off
|
||||
return Mat4{ e: [
|
||||
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],
|
||||
]!
|
||||
}
|
||||
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],
|
||||
]!}
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
||||
// Get a scale matrix, the scale vector is w, only xyz are evaluated.
|
||||
pub fn scale(w Vec4) Mat4 {
|
||||
unsafe {
|
||||
// vfmt off
|
||||
return Mat4{ e: [
|
||||
w.e[0], 0, 0, 0,
|
||||
0, w.e[1], 0, 0,
|
||||
0, 0, w.e[2], 0,
|
||||
0, 0, 0, 1,
|
||||
]!
|
||||
}
|
||||
w.e[0], 0, 0, 0,
|
||||
0, w.e[1], 0, 0,
|
||||
0, 0, w.e[2], 0,
|
||||
0, 0, 0, 1,
|
||||
]!}
|
||||
// vfmt on
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ import regex
|
||||
import rand
|
||||
import strings
|
||||
|
||||
const debug = true // true for debug println
|
||||
|
||||
/******************************************************************************
|
||||
*
|
||||
* Test section
|
||||
@ -14,6 +16,7 @@ struct TestItem {
|
||||
e int
|
||||
}
|
||||
|
||||
// vfmt off
|
||||
const(
|
||||
match_test_suite = [
|
||||
// minus in CC
|
||||
@ -422,10 +425,7 @@ const (
|
||||
Test_split{'1234', r'\d+', []},
|
||||
]
|
||||
)
|
||||
|
||||
const (
|
||||
debug = true // true for debug println
|
||||
)
|
||||
// vfmt on
|
||||
|
||||
fn test_regex() {
|
||||
// check capturing groups
|
||||
@ -590,7 +590,9 @@ fn test_regex() {
|
||||
// check replace simple
|
||||
for c, to in match_test_suite_replace_simple {
|
||||
// debug print
|
||||
if debug { println('#$c [$to.src] q[$to.q] $to.r') }
|
||||
if debug {
|
||||
println('#$c [$to.src] q[$to.q] $to.r')
|
||||
}
|
||||
|
||||
mut re := regex.regex_opt(to.q) or {
|
||||
eprintln('err: $err')
|
||||
@ -609,7 +611,9 @@ fn test_regex() {
|
||||
// check match and find
|
||||
for c, to in match_test_suite {
|
||||
// debug print
|
||||
if debug { println('#$c [$to.src] q[$to.q] $to.s $to.e') }
|
||||
if debug {
|
||||
println('#$c [$to.src] q[$to.q] $to.s $to.e')
|
||||
}
|
||||
|
||||
// test the find
|
||||
if to.s > 0 {
|
||||
@ -675,7 +679,9 @@ fn test_regex() {
|
||||
}
|
||||
}
|
||||
|
||||
if debug { println('DONE!') }
|
||||
if debug {
|
||||
println('DONE!')
|
||||
}
|
||||
}
|
||||
|
||||
// test regex_base function
|
||||
@ -790,6 +796,7 @@ struct Test_find_groups {
|
||||
res []int // groups indexes
|
||||
}
|
||||
|
||||
// vfmt off
|
||||
const (
|
||||
find_groups_test_suite = [
|
||||
Test_find_groups{
|
||||
@ -815,6 +822,7 @@ find_groups_test_suite = [
|
||||
},
|
||||
]
|
||||
)
|
||||
// vfmt on
|
||||
|
||||
fn test_groups_in_find() {
|
||||
for test_obj in find_groups_test_suite {
|
||||
|
@ -1,12 +1,13 @@
|
||||
vlib/v/checker/tests/modules/deprecated_module/main.v:2:1: notice: module `deprecated_module.www.ttt` will be deprecated after 2999-01-01, and will become an error after 2999-06-30; use xxx.yyy
|
||||
1 | import bbb.ccc
|
||||
2 | import www.ttt
|
||||
vlib/v/checker/tests/modules/deprecated_module/main.v:5:1: notice: module `deprecated_module.www.ttt` will be deprecated after 2999-01-01, and will become an error after 2999-06-30; use xxx.yyy
|
||||
3 | // That is unrelated to what this file tests, but should be investigated further and fixed when the module lookup disrepancy is fixed.
|
||||
4 | import bbb.ccc
|
||||
5 | import www.ttt
|
||||
| ~~~~~~~~~~~~~~
|
||||
3 | import xxx.yyy
|
||||
4 |
|
||||
vlib/v/checker/tests/modules/deprecated_module/main.v:12:11: error: undefined ident: `deprecated_module.www.ttt.non_existing`
|
||||
10 | dump(ttt.f())
|
||||
11 | dump(yyy.f())
|
||||
12 | dump(ttt.non_existing)
|
||||
6 | import xxx.yyy
|
||||
7 | // vfmt on
|
||||
vlib/v/checker/tests/modules/deprecated_module/main.v:16:11: error: undefined ident: `deprecated_module.www.ttt.non_existing`
|
||||
14 | dump(ttt.f())
|
||||
15 | dump(yyy.f())
|
||||
16 | dump(ttt.non_existing)
|
||||
| ~~~~~~~~~~~~
|
||||
13 | }
|
||||
17 | }
|
||||
|
@ -1,6 +1,10 @@
|
||||
// vfmt off
|
||||
// TODO: without vfmt off, vfmt is buggy, and keeps converting the imports below to `import deprecated_module.bbb.ccc` for some reason, even though the folder has `v.mod`.
|
||||
// That is unrelated to what this file tests, but should be investigated further and fixed when the module lookup disrepancy is fixed.
|
||||
import bbb.ccc
|
||||
import www.ttt
|
||||
import xxx.yyy
|
||||
// vfmt on
|
||||
|
||||
// Note: www.ttt has been deprecated.
|
||||
// => compiling this should produce an error,
|
||||
|
@ -26,6 +26,16 @@ pub struct CommentsOptions {
|
||||
}
|
||||
|
||||
pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
|
||||
if node.text.starts_with('\x01 vfmt on') {
|
||||
f.vfmt_on(node.pos.line_nr)
|
||||
}
|
||||
defer {
|
||||
// ensure that the `vfmt off` comment itself was sent to the output,
|
||||
// by defering the check for that state transition:
|
||||
if node.text.starts_with('\x01 vfmt off') {
|
||||
f.vfmt_off(node.pos.line_nr)
|
||||
}
|
||||
}
|
||||
// Shebang in .vsh files
|
||||
if node.text.starts_with('#!') {
|
||||
f.writeln(node.text)
|
||||
|
@ -50,9 +50,16 @@ pub mut:
|
||||
is_mbranch_expr bool // match a { x...y { } }
|
||||
fn_scope &ast.Scope = unsafe { nil }
|
||||
wsinfix_depth int
|
||||
format_state FormatState
|
||||
source_text string // can be set by `echo "println('hi')" | v fmt`, i.e. when processing source not from a file, but from stdin. In this case, it will contain the entire input text. You can use f.file.path otherwise, and read from that file.
|
||||
}
|
||||
|
||||
pub fn fmt(file ast.File, table &ast.Table, pref &pref.Preferences, is_debug bool) string {
|
||||
[params]
|
||||
pub struct FmtOptions {
|
||||
source_text string
|
||||
}
|
||||
|
||||
pub fn fmt(file ast.File, table &ast.Table, pref &pref.Preferences, is_debug bool, options FmtOptions) string {
|
||||
mut f := Fmt{
|
||||
file: file
|
||||
table: table
|
||||
@ -61,6 +68,7 @@ pub fn fmt(file ast.File, table &ast.Table, pref &pref.Preferences, is_debug boo
|
||||
out: strings.new_builder(1000)
|
||||
out_imports: strings.new_builder(200)
|
||||
}
|
||||
f.source_text = options.source_text
|
||||
f.process_file_imports(file)
|
||||
f.set_current_module_name('main')
|
||||
// As these are toplevel stmts, the indent increase done in f.stmts() has to be compensated
|
||||
@ -328,9 +336,18 @@ pub fn (mut f Fmt) imports(imports []ast.Import) {
|
||||
continue
|
||||
}
|
||||
already_imported[import_text] = true
|
||||
f.out_imports.writeln(import_text)
|
||||
f.import_comments(imp.comments, inline: true)
|
||||
f.import_comments(imp.next_comments)
|
||||
|
||||
if !f.format_state.is_vfmt_on {
|
||||
import_original_source_lines := f.get_source_lines()#[imp.pos.line_nr..
|
||||
imp.pos.last_line + 1].join('\n')
|
||||
f.out_imports.writeln(import_original_source_lines)
|
||||
// NOTE: imp.comments are on the *same line*, so they are already included in import_original_source_lines
|
||||
f.import_comments(imp.next_comments)
|
||||
} else {
|
||||
f.out_imports.writeln(import_text)
|
||||
f.import_comments(imp.comments, inline: true)
|
||||
f.import_comments(imp.next_comments)
|
||||
}
|
||||
num_imports++
|
||||
}
|
||||
if num_imports > 0 {
|
||||
@ -441,7 +458,7 @@ pub fn (mut f Fmt) stmts(stmts []ast.Stmt) {
|
||||
|
||||
pub fn (mut f Fmt) stmt(node ast.Stmt) {
|
||||
if f.is_debug {
|
||||
eprintln('stmt: ${node.pos:-42} | node: ${node.type_name():-20}')
|
||||
eprintln('stmt ${node.type_name():-20} | pos: $node.pos.line_str()')
|
||||
}
|
||||
match node {
|
||||
ast.EmptyStmt, ast.NodeError {}
|
||||
@ -544,7 +561,7 @@ fn stmt_is_single_line(stmt ast.Stmt) bool {
|
||||
pub fn (mut f Fmt) expr(node_ ast.Expr) {
|
||||
mut node := unsafe { node_ }
|
||||
if f.is_debug {
|
||||
eprintln('expr: ${node.pos():-42} | node: ${node.type_name():-20} | $node.str()')
|
||||
eprintln('expr ${node.type_name():-20} | pos: $node.pos().line_str() | $node.str()')
|
||||
}
|
||||
match mut node {
|
||||
ast.NodeError {}
|
||||
|
96
vlib/v/fmt/tests/vfmt_off_vfmt_on_keep.vv
Normal file
96
vlib/v/fmt/tests/vfmt_off_vfmt_on_keep.vv
Normal file
@ -0,0 +1,96 @@
|
||||
// This file checks that using a single comment line like this: `// vfmt off`
|
||||
// should turn off the formatting that vfmt does, *till the end of the file*
|
||||
// or till it encounters a single comment line like this: `// vfmt on` .
|
||||
//
|
||||
// NOTE: all lines after a `// vfmt off` line, should be copied 1:1 from the
|
||||
// input source code, without any changes to the spacing/formatting.
|
||||
import sokol.sgl
|
||||
|
||||
struct Abc {
|
||||
b string
|
||||
}
|
||||
|
||||
// vfmt off
|
||||
struct St { a int }
|
||||
// vfmt on
|
||||
|
||||
fn xyz() {
|
||||
println('hi')
|
||||
}
|
||||
|
||||
// vfmt off
|
||||
fn abc() {
|
||||
println('hello')
|
||||
println('another')
|
||||
}
|
||||
// vfmt on
|
||||
|
||||
fn main() {
|
||||
println('hi')
|
||||
// vfmt off
|
||||
println( 'unformatted' )
|
||||
|
||||
// some ASCII art here
|
||||
|
||||
code( )
|
||||
another( )
|
||||
// vfmt on
|
||||
println('end')
|
||||
}
|
||||
|
||||
// vertex specification for a cube with colored sides and texture coords
|
||||
fn cube() {
|
||||
sgl.begin_quads()
|
||||
{
|
||||
// edge color
|
||||
sgl.c3f(1.0, 0.0, 0.0)
|
||||
// edge coord
|
||||
// x,y,z, texture cord: u,v
|
||||
// vfmt off
|
||||
sgl.v3f_t2f(-1.0, 1.0, -1.0, -1.0, 1.0)
|
||||
sgl.v3f_t2f( 1.0, 1.0, -1.0, 1.0, 1.0)
|
||||
sgl.v3f_t2f( 1.0, -1.0, -1.0, 1.0, -1.0)
|
||||
sgl.v3f_t2f(-1.0, -1.0, -1.0, -1.0, -1.0)
|
||||
sgl.c3f(0.0, 1.0, 0.0)
|
||||
sgl.v3f_t2f(-1.0, -1.0, 1.0, -1.0, 1.0)
|
||||
sgl.v3f_t2f( 1.0, -1.0, 1.0, 1.0, 1.0)
|
||||
sgl.v3f_t2f( 1.0, 1.0, 1.0, 1.0, -1.0)
|
||||
sgl.v3f_t2f(-1.0, 1.0, 1.0, -1.0, -1.0)
|
||||
sgl.c3f(0.0, 0.0, 1.0)
|
||||
sgl.v3f_t2f(-1.0, -1.0, 1.0, -1.0, 1.0)
|
||||
sgl.v3f_t2f(-1.0, 1.0, 1.0, 1.0, 1.0)
|
||||
sgl.v3f_t2f(-1.0, 1.0, -1.0, 1.0, -1.0)
|
||||
sgl.v3f_t2f(-1.0, -1.0, -1.0, -1.0, -1.0)
|
||||
sgl.c3f(1.0, 0.5, 0.0)
|
||||
sgl.v3f_t2f(1.0, -1.0, 1.0, -1.0, 1.0)
|
||||
sgl.v3f_t2f(1.0, -1.0, -1.0, 1.0, 1.0)
|
||||
sgl.v3f_t2f(1.0, 1.0, -1.0, 1.0, -1.0)
|
||||
sgl.v3f_t2f(1.0, 1.0, 1.0, -1.0, -1.0)
|
||||
sgl.c3f(0.0, 0.5, 1.0)
|
||||
sgl.v3f_t2f( 1.0, -1.0, -1.0, -1.0, 1.0)
|
||||
sgl.v3f_t2f( 1.0, -1.0, 1.0, 1.0, 1.0)
|
||||
sgl.v3f_t2f(-1.0, -1.0, 1.0, 1.0, -1.0)
|
||||
sgl.v3f_t2f(-1.0, -1.0, -1.0, -1.0, -1.0)
|
||||
sgl.c3f(1.0, 0.0, 0.5)
|
||||
sgl.v3f_t2f(-1.0, 1.0, -1.0, -1.0, 1.0)
|
||||
sgl.v3f_t2f(-1.0, 1.0, 1.0, 1.0, 1.0)
|
||||
sgl.v3f_t2f( 1.0, 1.0, 1.0, 1.0, -1.0)
|
||||
sgl.v3f_t2f( 1.0, 1.0, -1.0, -1.0, -1.0)
|
||||
// vfmt on
|
||||
}
|
||||
sgl.end()
|
||||
}
|
||||
|
||||
fn a_matrix_whose_formatting_vfmt_will_not_destroy() {
|
||||
// vfmt off
|
||||
indices := [
|
||||
u16(0), 1, 2, 0, 2, 3,
|
||||
6, 5, 4, 7, 6, 4,
|
||||
8, 9, 10, 8, 10, 11,
|
||||
14, 13, 12, 15, 14, 12,
|
||||
16, 17, 18, 16, 18, 19,
|
||||
22, 21, 20, 23, 22, 20,
|
||||
]
|
||||
// vfmt on
|
||||
println(indices)
|
||||
}
|
66
vlib/v/fmt/vfmt_on_off.v
Normal file
66
vlib/v/fmt/vfmt_on_off.v
Normal file
@ -0,0 +1,66 @@
|
||||
module fmt
|
||||
|
||||
import v.util
|
||||
|
||||
// By default, vfmt *always reformats all source code passed to it* in an uniform way.
|
||||
// This works in the vast majority of the cases, providing a very nice default and
|
||||
// consistent look & feel for most V codebases, and eliminating countless hours of bike
|
||||
// shedding discussions about whether using spaces or tabs is better, and where {} and
|
||||
// () should be put, and whether comments should be aligned or not.
|
||||
//
|
||||
// However, there are indeed situations, where careful manual formatting can increase
|
||||
// understanding and maintainability of the code. For example, if you write matrix heavy
|
||||
// code, you may want to preserve the look of your carefully written beautiful matrices,
|
||||
// so that every reader of your code can bask in their glory.
|
||||
//
|
||||
// To enable such manual formatting, this file supports using `vfmt off` and `vfmt on`
|
||||
// line comments, which can be used for turning off and on vfmt *locally* in selected
|
||||
// blocks of code, while leaving the vast majority of the code, to be formatted by vfmt.
|
||||
//
|
||||
// TLDR:
|
||||
// All lines after `// vfmt off` are passed on *without any modification* to the output.
|
||||
// All lines after `// vfmt on` are going to be formatted, and then added to the output.
|
||||
|
||||
struct FormatState {
|
||||
mut:
|
||||
is_vfmt_on bool = true
|
||||
last_off_source_line int // the source line where // vfmt off was encountered first
|
||||
last_off_out_len int // f.out.len when // vfmt off was encountered first
|
||||
}
|
||||
|
||||
fn (mut fs FormatState) reset() {
|
||||
fs.is_vfmt_on = true
|
||||
fs.last_off_source_line = 0
|
||||
fs.last_off_out_len = 0
|
||||
}
|
||||
|
||||
pub fn (mut f Fmt) vfmt_off(off_line int) {
|
||||
// only trigger once on on->off edges:
|
||||
if !f.format_state.is_vfmt_on {
|
||||
return
|
||||
}
|
||||
f.format_state.is_vfmt_on = false
|
||||
f.format_state.last_off_source_line = off_line
|
||||
f.format_state.last_off_out_len = f.out.len
|
||||
}
|
||||
|
||||
pub fn (mut f Fmt) vfmt_on(on_line int) {
|
||||
// only trigger once on off->on edges:
|
||||
if f.format_state.is_vfmt_on {
|
||||
return
|
||||
}
|
||||
f.out.cut_to(f.format_state.last_off_out_len)
|
||||
f.out.writeln('')
|
||||
source_lines := f.get_source_lines()#[f.format_state.last_off_source_line + 1..on_line]
|
||||
for line in source_lines {
|
||||
f.out.writeln(line)
|
||||
}
|
||||
f.format_state.reset()
|
||||
}
|
||||
|
||||
pub fn (mut f Fmt) get_source_lines() []string {
|
||||
if f.file.path != '' {
|
||||
return unsafe { util.cached_file2sourcelines(f.file.path) }
|
||||
}
|
||||
return f.source_text.split('\n')
|
||||
}
|
@ -5,5 +5,5 @@ enum Foo {
|
||||
|
||||
fn main() {
|
||||
println('Foo = {Foo.def}')
|
||||
println('Foo = {unsafe{Foo(1)}}')
|
||||
println('Foo = {unsafe { Foo(1) }}')
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user