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

examples, tests: small formatting cleanup in cube_glsl.v , remove already fixed skips in v test-cleancode, before // vfmt off and // vfmt on

This commit is contained in:
Delyan Angelov 2022-11-04 10:20:21 +02:00
parent fa7ff09370
commit 7f91db695c
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 169 additions and 165 deletions

View File

@ -39,14 +39,11 @@ const verify_known_failing_exceptions = [
'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',
'examples/sokol/06_obj_viewer/show_obj.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',
'vlib/builtin/int_test.v' /* special number formatting that should be tested */,
// TODOs and unfixed vfmt bugs
'vlib/v/gen/js/tests/js.v', /* local `hello` fn, gets replaced with module `hello` aliased as `hl` */
'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 */
]

View File

@ -102,48 +102,52 @@ fn update_text_texture(sg_img gfx.Image, w int, h int, buf &byte) {
fn draw_triangle() {
sgl.defaults()
sgl.begin_triangles()
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 )
{
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 )
}
sgl.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 co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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.end()
}
@ -163,56 +167,62 @@ fn draw_cubes(app App) {
sgl.rotate(sgl.rad(rot[1]), 0.0, 1.0, 0.0)
cube()
sgl.push_matrix()
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
sgl.rotate(-2.0 * sgl.rad(rot[0]), 1.0, 0.0, 0.0)
sgl.rotate(-2.0 * sgl.rad(rot[1]), 0.0, 1.0, 0.0)
cube()
sgl.push_matrix()
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
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)
cube()
sgl.pop_matrix()
{
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
sgl.rotate(-2.0 * sgl.rad(rot[0]), 1.0, 0.0, 0.0)
sgl.rotate(-2.0 * sgl.rad(rot[1]), 0.0, 1.0, 0.0)
cube()
sgl.push_matrix()
{
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
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)
cube()
}
sgl.pop_matrix()
}
sgl.pop_matrix()
}
fn cube_texture(r f32, g f32, b f32) {
sgl.begin_quads()
// edge color
sgl.c3f(r, g, b)
// edge coord
// x,y,z, texture cord: u,v
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 )
sgl.v3f_t2f(-1.0, -1.0, -1.0, 0.0 , 0.0 )
sgl.c3f(r, g, b)
sgl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 , 0.0 )
sgl.c3f(r, g, b)
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 )
sgl.v3f_t2f(-1.0, 1.0, 1.0, 0.0 , 0.0 )
sgl.c3f(r, g, b)
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 )
sgl.v3f_t2f(-1.0, -1.0, -1.0, 0.0 , 0.0 )
sgl.c3f(r, g, b)
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 )
sgl.v3f_t2f(1.0, 1.0, 1.0, 0.0 , 0.0 )
sgl.c3f(r, g, b)
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 )
sgl.v3f_t2f(-1.0, -1.0, -1.0, 0.0 , 0.0 )
sgl.c3f(r, g, b)
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 )
sgl.v3f_t2f( 1.0, 1.0, -1.0, 0.0 , 0.0 )
}
sgl.end()
}
@ -234,8 +244,8 @@ struct Vertex_t {
color u32
// u u16
// v u16
u f32
v f32
u f32
v f32
}
fn init_cube_glsl(mut app App) {
@ -289,7 +299,7 @@ fn init_cube_glsl(mut app App) {
// vert_buffer_desc.usage = .immutable
vbuf := gfx.make_buffer(&vert_buffer_desc)
/* create an index buffer for the cube */
// create an index buffer for the cube
indices := [
u16(0), 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
@ -299,7 +309,9 @@ fn init_cube_glsl(mut app App) {
22, 21, 20, 23, 22, 20
]
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)))
@ -347,46 +359,58 @@ fn draw_cube_glsl(app App) {
return
}
rot := [f32(app.mouse_y), f32(app.mouse_x)]
// clear
ws := gg.window_size_real_pixels()
// ratio := f32(ws.width)/ws.height
dw := f32(ws.width / 2)
dh := f32(ws.height / 2)
tr_matrix := m4.calc_tr_matrices(dw, dh, rot[0], rot[1], 2.0)
gfx.apply_viewport(ws.width / 2, 0, ws.width / 2, ws.height / 2, true)
// apply the pipline and bindings
gfx.apply_pipeline(app.cube_pip_glsl)
gfx.apply_bindings(app.cube_bind)
//***************
// Uniforms
//***************
// passing the view matrix as uniform
// res is a 4x4 matrix of f32 thus: 4*16 byte of size
vs_uniforms_range := gfx.Range{
ptr: &tr_matrix
size: usize(4 * 16)
mut color_action := gfx.ColorAttachmentAction{
action: unsafe { gfx.Action(C.SG_ACTION_DONTCARE) } // C.SG_ACTION_CLEAR)
value: gfx.Color{
r: 1.0
g: 1.0
b: 1.0
a: 1.0
}
}
gfx.apply_uniforms(.vs, C.SLOT_vs_params, &vs_uniforms_range)
mut pass_action := gfx.PassAction{}
pass_action.colors[0] = color_action
gfx.begin_default_pass(&pass_action, ws.width, ws.height)
{
rot := [f32(app.mouse_y), f32(app.mouse_x)]
// ratio := f32(ws.width)/ws.height
dw := f32(ws.width / 2)
dh := f32(ws.height / 2)
// fs uniforms
time_ticks := f32(time.ticks() - app.ticks) / 1000
mut text_res := [
f32(512),
512, /* x,y resolution to pass to FS */
time_ticks, /* time as f32 */
0 /* padding 4 Bytes == 1 f32 */,
]!
fs_uniforms_range := gfx.Range{
ptr: unsafe { &text_res }
size: usize(4 * 4)
tr_matrix := m4.calc_tr_matrices(dw, dh, rot[0], rot[1], 2.0)
gfx.apply_viewport(ws.width / 2, 0, ws.width / 2, ws.height / 2, true)
// apply the pipline and bindings
gfx.apply_pipeline(app.cube_pip_glsl)
gfx.apply_bindings(app.cube_bind)
// Uniforms:
// passing the view matrix as uniform
// res is a 4x4 matrix of f32 thus: 4*16 byte of size
vs_uniforms_range := gfx.Range{
ptr: &tr_matrix
size: usize(4 * 16)
}
gfx.apply_uniforms(.vs, C.SLOT_vs_params, &vs_uniforms_range)
// fs uniforms
time_ticks := f32(time.ticks() - app.ticks) / 1000
mut text_res := [
f32(512),
512, /* x,y resolution to pass to FS */
time_ticks, /* time as f32 */
0 /* padding 4 Bytes == 1 f32 */,
]!
fs_uniforms_range := gfx.Range{
ptr: unsafe { &text_res }
size: usize(4 * 4)
}
gfx.apply_uniforms(.fs, C.SLOT_fs_params, &fs_uniforms_range)
gfx.draw(0, (3 * 2) * 6, 1)
}
gfx.apply_uniforms(.fs, C.SLOT_fs_params, &fs_uniforms_range)
gfx.draw(0, (3 * 2) * 6, 1)
gfx.end_pass()
gfx.commit()
}
@ -407,19 +431,24 @@ fn draw_texture_cubes(app App) {
sgl.rotate(sgl.rad(rot[0]), 1.0, 0.0, 0.0)
sgl.rotate(sgl.rad(rot[1]), 0.0, 1.0, 0.0)
cube_texture(1, 1, 1)
sgl.push_matrix()
{
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
sgl.rotate(-2.0 * sgl.rad(rot[0]), 1.0, 0.0, 0.0)
sgl.rotate(-2.0 * sgl.rad(rot[1]), 0.0, 1.0, 0.0)
cube_texture(1,1,1)
cube_texture(1, 1, 1)
sgl.push_matrix()
{
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
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)
cube_texture(1,1,1)
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)
cube_texture(1, 1, 1)
}
sgl.pop_matrix()
}
sgl.pop_matrix()
sgl.disable_texture()
@ -440,35 +469,22 @@ fn frame(mut app App) {
// app.gg.begin()
app.gg.begin()
sgl.defaults()
{
sgl.defaults()
// 2d triangle
sgl.viewport(x0, y0, ww, hh, true)
draw_triangle()
// 2d triangle
sgl.viewport(x0, y0, ww, hh, true)
draw_triangle()
// colored cubes with viewport
sgl.viewport(x0, y1, ww, hh, true)
draw_cubes(app)
// colored cubes with viewport
sgl.viewport(x0, y1, ww, hh, true)
draw_cubes(app)
// textured cubed with viewport
sgl.viewport(0, int(dh / 5), dw, int(dh * ratio), true)
draw_texture_cubes(app)
app.gg.end()
// clear
mut color_action := gfx.ColorAttachmentAction{
action: unsafe { gfx.Action(C.SG_ACTION_DONTCARE) } // C.SG_ACTION_CLEAR)
value: gfx.Color{
r: 1.0
g: 1.0
b: 1.0
a: 1.0
}
// textured cubed with viewport
sgl.viewport(0, int(dh / 5), dw, int(dh * ratio), true)
draw_texture_cubes(app)
}
mut pass_action := gfx.PassAction{}
pass_action.colors[0] = color_action
gfx.begin_default_pass(&pass_action, ws.width, ws.height)
app.gg.end()
// glsl cube
draw_cube_glsl(app)
@ -477,9 +493,7 @@ fn frame(mut app App) {
}
/******************************************************************************
*
* Init / Cleanup
*
******************************************************************************/
fn my_init(mut app App) {
// set max vertices,
@ -537,9 +551,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
@ -554,9 +568,7 @@ fn my_init(mut app App) {
}
/******************************************************************************
*
* event
*
******************************************************************************/
fn my_event_manager(mut ev gg.Event, mut app App) {
if ev.typ == .mouse_move {
@ -572,11 +584,6 @@ fn my_event_manager(mut ev gg.Event, mut app App) {
}
}
/******************************************************************************
*
* Main
*
******************************************************************************/
fn main() {
// App init
mut app := &App{