From f5c4671bfbad96bf346bd7e9a21fc4317b4959df Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sat, 3 Dec 2022 17:00:20 +0530 Subject: Adds most of the tools --- v_windows/v/old/examples/sokol/01_cubes/cube.v | 432 +++++++++++++ .../examples/sokol/02_cubes_glsl/cube_glsl.glsl | 95 +++ .../v/old/examples/sokol/02_cubes_glsl/cube_glsl.v | 627 +++++++++++++++++++ v_windows/v/old/examples/sokol/02_cubes_glsl/v.mod | 0 .../sokol/03_march_tracing_glsl/rt_glsl.glsl | 695 +++++++++++++++++++++ .../examples/sokol/03_march_tracing_glsl/rt_glsl.v | 438 +++++++++++++ .../old/examples/sokol/03_march_tracing_glsl/v.mod | 0 .../examples/sokol/04_multi_shader_glsl/rt_glsl.v | 634 +++++++++++++++++++ .../sokol/04_multi_shader_glsl/rt_glsl_march.glsl | 695 +++++++++++++++++++++ .../sokol/04_multi_shader_glsl/rt_glsl_puppy.glsl | 568 +++++++++++++++++ .../old/examples/sokol/04_multi_shader_glsl/v.mod | 0 .../examples/sokol/05_instancing_glsl/rt_glsl.v | 521 +++++++++++++++ .../05_instancing_glsl/rt_glsl_instancing.glsl | 64 ++ .../v/old/examples/sokol/05_instancing_glsl/v.mod | 0 .../sokol/06_obj_viewer/assets/models/v.mtl | 20 + .../sokol/06_obj_viewer/assets/models/v.obj_ | 194 ++++++ .../old/examples/sokol/06_obj_viewer/gouraud.glsl | 108 ++++ .../examples/sokol/06_obj_viewer/modules/obj/obj.v | 595 ++++++++++++++++++ .../sokol/06_obj_viewer/modules/obj/rend.v | 297 +++++++++ .../sokol/06_obj_viewer/modules/obj/struct.v | 104 +++ .../sokol/06_obj_viewer/modules/obj/util.v | 44 ++ .../v/old/examples/sokol/06_obj_viewer/show_obj.v | 338 ++++++++++ v_windows/v/old/examples/sokol/06_obj_viewer/v.mod | 0 v_windows/v/old/examples/sokol/drawing.v | 75 +++ v_windows/v/old/examples/sokol/fonts.v | 164 +++++ v_windows/v/old/examples/sokol/freetype_raven.v | 153 +++++ .../sokol/particles/modules/particle/LICENSE | 21 + .../sokol/particles/modules/particle/color.v | 12 + .../sokol/particles/modules/particle/particle.v | 81 +++ .../sokol/particles/modules/particle/system.v | 99 +++ .../sokol/particles/modules/particle/v.mod | 0 .../sokol/particles/modules/particle/vec2/v.mod | 0 .../sokol/particles/modules/particle/vec2/vec2.v | 89 +++ .../v/old/examples/sokol/particles/particles.v | 155 +++++ v_windows/v/old/examples/sokol/sounds/melody.v | 75 +++ .../v/old/examples/sokol/sounds/simple_sin_tones.v | 42 ++ v_windows/v/old/examples/sokol/sounds/uhoh.wav | Bin 0 -> 68562 bytes v_windows/v/old/examples/sokol/sounds/wav_player.v | 209 +++++++ 38 files changed, 7644 insertions(+) create mode 100644 v_windows/v/old/examples/sokol/01_cubes/cube.v create mode 100644 v_windows/v/old/examples/sokol/02_cubes_glsl/cube_glsl.glsl create mode 100644 v_windows/v/old/examples/sokol/02_cubes_glsl/cube_glsl.v create mode 100644 v_windows/v/old/examples/sokol/02_cubes_glsl/v.mod create mode 100644 v_windows/v/old/examples/sokol/03_march_tracing_glsl/rt_glsl.glsl create mode 100644 v_windows/v/old/examples/sokol/03_march_tracing_glsl/rt_glsl.v create mode 100644 v_windows/v/old/examples/sokol/03_march_tracing_glsl/v.mod create mode 100644 v_windows/v/old/examples/sokol/04_multi_shader_glsl/rt_glsl.v create mode 100644 v_windows/v/old/examples/sokol/04_multi_shader_glsl/rt_glsl_march.glsl create mode 100644 v_windows/v/old/examples/sokol/04_multi_shader_glsl/rt_glsl_puppy.glsl create mode 100644 v_windows/v/old/examples/sokol/04_multi_shader_glsl/v.mod create mode 100644 v_windows/v/old/examples/sokol/05_instancing_glsl/rt_glsl.v create mode 100644 v_windows/v/old/examples/sokol/05_instancing_glsl/rt_glsl_instancing.glsl create mode 100644 v_windows/v/old/examples/sokol/05_instancing_glsl/v.mod create mode 100644 v_windows/v/old/examples/sokol/06_obj_viewer/assets/models/v.mtl create mode 100644 v_windows/v/old/examples/sokol/06_obj_viewer/assets/models/v.obj_ create mode 100644 v_windows/v/old/examples/sokol/06_obj_viewer/gouraud.glsl create mode 100644 v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/obj.v create mode 100644 v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/rend.v create mode 100644 v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/struct.v create mode 100644 v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/util.v create mode 100644 v_windows/v/old/examples/sokol/06_obj_viewer/show_obj.v create mode 100644 v_windows/v/old/examples/sokol/06_obj_viewer/v.mod create mode 100644 v_windows/v/old/examples/sokol/drawing.v create mode 100644 v_windows/v/old/examples/sokol/fonts.v create mode 100644 v_windows/v/old/examples/sokol/freetype_raven.v create mode 100644 v_windows/v/old/examples/sokol/particles/modules/particle/LICENSE create mode 100644 v_windows/v/old/examples/sokol/particles/modules/particle/color.v create mode 100644 v_windows/v/old/examples/sokol/particles/modules/particle/particle.v create mode 100644 v_windows/v/old/examples/sokol/particles/modules/particle/system.v create mode 100644 v_windows/v/old/examples/sokol/particles/modules/particle/v.mod create mode 100644 v_windows/v/old/examples/sokol/particles/modules/particle/vec2/v.mod create mode 100644 v_windows/v/old/examples/sokol/particles/modules/particle/vec2/vec2.v create mode 100644 v_windows/v/old/examples/sokol/particles/particles.v create mode 100644 v_windows/v/old/examples/sokol/sounds/melody.v create mode 100644 v_windows/v/old/examples/sokol/sounds/simple_sin_tones.v create mode 100644 v_windows/v/old/examples/sokol/sounds/uhoh.wav create mode 100644 v_windows/v/old/examples/sokol/sounds/wav_player.v (limited to 'v_windows/v/old/examples/sokol') diff --git a/v_windows/v/old/examples/sokol/01_cubes/cube.v b/v_windows/v/old/examples/sokol/01_cubes/cube.v new file mode 100644 index 0000000..c933dbf --- /dev/null +++ b/v_windows/v/old/examples/sokol/01_cubes/cube.v @@ -0,0 +1,432 @@ +/********************************************************************** +* +* Sokol 3d cube demo +* +* 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: +* - add instancing +* - add an exampel with shaders +**********************************************************************/ +import gg +import gx +import math +import sokol.sapp +import sokol.gfx +import sokol.sgl + +const ( + win_width = 800 + win_height = 800 + bg_color = gx.white +) + +struct App { +mut: + gg &gg.Context + pip_3d C.sgl_pipeline + texture C.sg_image + init_flag bool + frame_count int + mouse_x int = -1 + mouse_y int = -1 +} + +/****************************************************************************** +* +* Texture functions +* +******************************************************************************/ +fn create_texture(w int, h int, buf &u8) C.sg_image { + sz := w * h * 4 + mut img_desc := C.sg_image_desc{ + width: w + height: h + num_mipmaps: 0 + min_filter: .linear + mag_filter: .linear + // usage: .dynamic + wrap_u: .clamp_to_edge + wrap_v: .clamp_to_edge + label: &byte(0) + d3d11_texture: 0 + } + // commen if .dynamic is enabled + img_desc.data.subimage[0][0] = C.sg_range{ + ptr: buf + size: size_t(sz) + } + + sg_img := C.sg_make_image(&img_desc) + return sg_img +} + +fn destroy_texture(sg_img C.sg_image) { + C.sg_destroy_image(sg_img) +} + +// Use only if usage: .dynamic is enabled +fn update_text_texture(sg_img C.sg_image, w int, h int, buf &byte) { + sz := w * h * 4 + mut tmp_sbc := C.sg_image_data{} + tmp_sbc.subimage[0][0] = C.sg_range{ + ptr: buf + size: size_t(sz) + } + C.sg_update_image(sg_img, &tmp_sbc) +} + +/****************************************************************************** +* +* Draw functions +* +******************************************************************************/ +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.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 + 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) + sgl.end() +} + +fn draw_cubes(app App) { + rot := [f32(1.0) * (app.frame_count % 360), 0.5 * f32(app.frame_count % 360)] + // rot := [f32(app.mouse_x), f32(app.mouse_y)] + + sgl.defaults() + sgl.load_pipeline(app.pip_3d) + + sgl.matrix_mode_projection() + sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 100.0) + + sgl.matrix_mode_modelview() + sgl.translate(0.0, 0.0, -12.0) + 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() + 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.pop_matrix() +} + +fn cube_t(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.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() +} + +fn draw_texture_cubes(app App) { + rot := [f32(app.mouse_x), f32(app.mouse_y)] + sgl.defaults() + sgl.load_pipeline(app.pip_3d) + + sgl.enable_texture() + sgl.texture(app.texture) + + sgl.matrix_mode_projection() + sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 100.0) + + sgl.matrix_mode_modelview() + sgl.translate(0.0, 0.0, -12.0) + 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_t(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_t(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_t(1, 1, 1) + sgl.pop_matrix() + sgl.pop_matrix() + + sgl.disable_texture() +} + +fn cube_field(app App) { + rot := [f32(app.mouse_x), f32(app.mouse_y)] + xyz_sz := f32(2.0) + field_size := 20 + + sgl.defaults() + sgl.load_pipeline(app.pip_3d) + + sgl.enable_texture() + sgl.texture(app.texture) + + sgl.matrix_mode_projection() + sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 200.0) + + sgl.matrix_mode_modelview() + + sgl.translate(field_size, 0.0, -120.0) + sgl.rotate(sgl.rad(rot[0]), 0.0, 1.0, 0.0) + sgl.rotate(sgl.rad(rot[1]), 1.0, 0.0, 0.0) + + // draw field_size*field_size cubes + for y in 0 .. field_size { + for x in 0 .. field_size { + sgl.push_matrix() + z := f32(math.cos(f32(x * 2) / field_size) * math.sin(f32(y * 2) / field_size) * xyz_sz) * (xyz_sz * 5) + sgl.translate(x * xyz_sz, z, y * xyz_sz) + cube_t(f32(f32(x) / field_size), f32(f32(y) / field_size), 1) + sgl.pop_matrix() + } + } + sgl.disable_texture() +} + +fn frame(mut app App) { + ws := gg.window_size_real_pixels() + ratio := f32(ws.width) / ws.height + dw := ws.width + dh := ws.height + ww := int(dh / 3) // not a bug + hh := int(dh / 3) + x0 := int(f32(dw) * 0.05) + // x1 := dw/2 + y0 := 0 + y1 := int(f32(dh) * 0.5) + + app.gg.begin() + // sgl.defaults() + + // 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) + + // textured cubed with viewport + sgl.viewport(0, int(dh / 5), dw, int(dh * ratio), true) + draw_texture_cubes(app) + + // textured field of cubes with viewport + sgl.viewport(0, int(dh / 5), dw, int(dh * ratio), true) + cube_field(app) + + app.frame_count++ + + app.gg.end() +} + +/****************************************************************************** +* +* Init / Cleanup +* +******************************************************************************/ +fn my_init(mut app App) { + app.init_flag = true + + // set max vertices, + // for a large number of the same type of object it is better use the instances!! + desc := sapp.create_desc() + gfx.setup(&desc) + sgl_desc := C.sgl_desc_t{ + max_vertices: 50 * 65536 + } + sgl.setup(&sgl_desc) + + // 3d pipeline + mut pipdesc := C.sg_pipeline_desc{} + unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) } + + color_state := C.sg_color_state{ + blend: C.sg_blend_state{ + enabled: true + src_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_SRC_ALPHA) + dst_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA) + } + } + pipdesc.colors[0] = color_state + + pipdesc.depth = C.sg_depth_state{ + write_enabled: true + compare: gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL) + } + pipdesc.cull_mode = .back + app.pip_3d = sgl.make_pipeline(&pipdesc) + + // create chessboard texture 256*256 RGBA + w := 256 + h := 256 + sz := w * h * 4 + tmp_txt := unsafe { malloc(sz) } + mut i := 0 + for i < sz { + unsafe { + y := (i >> 0x8) >> 5 // 8 cell + x := (i & 0xFF) >> 5 // 8 cell + // upper left corner + if x == 0 && y == 0 { + tmp_txt[i] = byte(0xFF) + tmp_txt[i + 1] = byte(0) + tmp_txt[i + 2] = byte(0) + tmp_txt[i + 3] = byte(0xFF) + } + // low right corner + else if x == 7 && y == 7 { + tmp_txt[i] = byte(0) + tmp_txt[i + 1] = byte(0xFF) + tmp_txt[i + 2] = byte(0) + tmp_txt[i + 3] = byte(0xFF) + } else { + col := if ((x + y) & 1) == 1 { 0xFF } else { 0 } + tmp_txt[i] = byte(col) // red + tmp_txt[i + 1] = byte(col) // green + tmp_txt[i + 2] = byte(col) // blue + tmp_txt[i + 3] = byte(0xFF) // alpha + } + i += 4 + } + } + unsafe { + app.texture = create_texture(w, h, tmp_txt) + free(tmp_txt) + } +} + +fn cleanup(mut app App) { + gfx.shutdown() +} + +/****************************************************************************** +* +* event +* +******************************************************************************/ +fn my_event_manager(mut ev gg.Event, mut app App) { + if ev.typ == .mouse_move { + app.mouse_x = int(ev.mouse_x) + app.mouse_y = int(ev.mouse_y) + } + if ev.typ == .touches_began || ev.typ == .touches_moved { + if ev.num_touches > 0 { + touch_point := ev.touches[0] + app.mouse_x = int(touch_point.pos_x) + app.mouse_y = int(touch_point.pos_y) + } + } +} + +/****************************************************************************** +* +* Main +* +******************************************************************************/ +// is needed for easier diagnostics on windows +[console] +fn main() { + // App init + mut app := &App{ + gg: 0 + } + + app.gg = gg.new_context( + width: win_width + height: win_height + create_window: true + window_title: '3D Cube Demo' + user_data: app + bg_color: bg_color + frame_fn: frame + init_fn: my_init + cleanup_fn: cleanup + event_fn: my_event_manager + ) + + app.gg.run() +} diff --git a/v_windows/v/old/examples/sokol/02_cubes_glsl/cube_glsl.glsl b/v_windows/v/old/examples/sokol/02_cubes_glsl/cube_glsl.glsl new file mode 100644 index 0000000..e19e936 --- /dev/null +++ b/v_windows/v/old/examples/sokol/02_cubes_glsl/cube_glsl.glsl @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// Shader code for texcube-sapp sample. +// +// NOTE: This source file also uses the '#pragma sokol' form of the +// custom tags. +//------------------------------------------------------------------------------ +//#pragma sokol @ctype mat4 my_mat4 + +#pragma sokol @vs vs +uniform vs_params { + mat4 mvp; +}; + +in vec4 pos; +in vec4 color0; +in vec2 texcoord0; + +out vec4 color; +out vec2 uv; + +void main() { + gl_Position = mvp * pos; + color = color0; + uv = texcoord0; +} +#pragma sokol @end + +#pragma sokol @fs fs +uniform sampler2D tex; +uniform fs_params { + vec2 text_res; + float iTime; +}; + +in vec4 color; +in vec2 uv; +out vec4 frag_color; + +//********************************************************* +// RAY TRACE +// original code from: https://www.shadertoy.com/view/ldS3DW +//********************************************************* +float sphere(vec3 ray, vec3 dir, vec3 center, float radius) +{ + vec3 rc = ray-center; + float c = dot(rc, rc) - (radius*radius); + float b = dot(dir, rc); + float d = b*b - c; + float t = -b - sqrt(abs(d)); + float st = step(0.0, min(t,d)); + return mix(-1.0, t, st); +} + +vec3 background(float t, vec3 rd) +{ + vec3 light = normalize(vec3(sin(t), 0.6, cos(t))); + float sun = max(0.0, dot(rd, light)); + float sky = max(0.0, dot(rd, vec3(0.0, 1.0, 0.0))); + float ground = max(0.0, -dot(rd, vec3(0.0, 1.0, 0.0))); + return (pow(sun, 256.0)+0.2*pow(sun, 2.0))*vec3(2.0, 1.6, 1.0) + + pow(ground, 0.5)*vec3(0.4, 0.3, 0.2) + + pow(sky, 1.0)*vec3(0.5, 0.6, 0.7); +} + +vec4 mainImage(vec2 fragCoord) +{ + vec2 uv = (fragCoord-vec2(0.4,0.4))*2.0; + + //vec2 uv = (-1.0 + 2.0*fc.xy / text_res.xy) * vec2(text_res.x/text_res.y, 1.0); + vec3 ro = vec3(0.0, 0.0, -3.0); + vec3 rd = normalize(vec3(uv, 1.0)); + vec3 p = vec3(0.0, 0.0, 0.0); + float t = sphere(ro, rd, p, 1.0); + vec3 nml = normalize(p - (ro+rd*t)); + vec3 bgCol = background(iTime, rd); + rd = reflect(rd, nml); + vec3 col = background(iTime, rd) * vec3(0.9, 0.8, 1.0); + vec4 fragColor = vec4( mix(bgCol, col, step(0.0, t)), 1.0 ); + return fragColor; +} +//********************************************************* +//********************************************************* + +void main() { + vec4 c = color; + vec4 txt = texture(tex, uv/4.0); + c = txt * c; + vec4 col_ray = mainImage(uv); + float txt_mix = mod(iTime,5); + frag_color = c*txt_mix*0.1 + col_ray ; +} + +#pragma sokol @end + +#pragma sokol @program cube vs fs diff --git a/v_windows/v/old/examples/sokol/02_cubes_glsl/cube_glsl.v b/v_windows/v/old/examples/sokol/02_cubes_glsl/cube_glsl.v new file mode 100644 index 0000000..0be09a4 --- /dev/null +++ b/v_windows/v/old/examples/sokol/02_cubes_glsl/cube_glsl.v @@ -0,0 +1,627 @@ +/********************************************************************** +* +* Sokol 3d cube demo +* +* 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. +* +* HOW TO COMPILE SHADERS: +* - download the sokol shader convertor tool from https://github.com/floooh/sokol-tools-bin +* +* - compile the .glsl shader with: +* linux : sokol-shdc --input cube_glsl.glsl --output cube_glsl.h --slang glsl330 +* windows: sokol-shdc.exe --input cube_glsl.glsl --output cube_glsl.h --slang glsl330 +* +* --slang parameter can be: +* - glsl330: desktop GL +* - glsl100: GLES2 / WebGL +* - glsl300es: GLES3 / WebGL2 +* - hlsl4: D3D11 +* - hlsl5: D3D11 +* - metal_macos: Metal on macOS +* - metal_ios: Metal on iOS device +* - metal_sim: Metal on iOS simulator +* - wgpu: WebGPU +* +* you can have multiple platforms at the same time passing prameter like this: --slang glsl330:hlsl5:metal_macos +* for further infos have a look at the sokol shader tool docs. +* +* TODO: +* - add instancing +**********************************************************************/ +import gg +import gx +// import math +import sokol.sapp +import sokol.gfx +import sokol.sgl +import time +import gg.m4 + +// GLSL Include and functions +#flag -I @VMODROOT/. +#include "cube_glsl.h" #Please use sokol-shdc to generate the necessary cube_glsl.h file from cube_glsl.glsl (see the instructions at the top of this file) + +fn C.cube_shader_desc(gfx.Backend) &C.sg_shader_desc + +const ( + win_width = 800 + win_height = 800 + bg_color = gx.white +) + +struct App { +mut: + gg &gg.Context + pip_3d C.sgl_pipeline + texture C.sg_image + init_flag bool + frame_count int + mouse_x int = -1 + mouse_y int = -1 + // glsl + cube_pip_glsl C.sg_pipeline + cube_bind C.sg_bindings + // time + ticks i64 +} + +/****************************************************************************** +* +* Texture functions +* +******************************************************************************/ +fn create_texture(w int, h int, buf &byte) C.sg_image { + sz := w * h * 4 + mut img_desc := C.sg_image_desc{ + width: w + height: h + num_mipmaps: 0 + min_filter: .linear + mag_filter: .linear + // usage: .dynamic + wrap_u: .clamp_to_edge + wrap_v: .clamp_to_edge + label: &byte(0) + d3d11_texture: 0 + } + // comment if .dynamic is enabled + img_desc.data.subimage[0][0] = C.sg_range{ + ptr: buf + size: size_t(sz) + } + + sg_img := C.sg_make_image(&img_desc) + return sg_img +} + +fn destroy_texture(sg_img C.sg_image) { + C.sg_destroy_image(sg_img) +} + +// Use only if usage: .dynamic is enabled +fn update_text_texture(sg_img C.sg_image, w int, h int, buf &byte) { + sz := w * h * 4 + mut tmp_sbc := C.sg_image_data{} + tmp_sbc.subimage[0][0] = C.sg_range{ + ptr: buf + size: size_t(sz) + } + C.sg_update_image(sg_img, &tmp_sbc) +} + +/****************************************************************************** +* +* Draw functions +* +******************************************************************************/ +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.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 + 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) + sgl.end() +} + +fn draw_cubes(app App) { + rot := [f32(1.0) * (app.frame_count % 360), 0.5 * f32(app.frame_count % 360)] + // rot := [f32(app.mouse_x), f32(app.mouse_y)] + + sgl.defaults() + sgl.load_pipeline(app.pip_3d) + + sgl.matrix_mode_projection() + sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 100.0) + + sgl.matrix_mode_modelview() + sgl.translate(0.0, 0.0, -12.0) + 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() + 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.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.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() +} + +/* +Cube vertex buffer with packed vertex formats for color and texture coords. + Note that a vertex format which must be portable across all + backends must only use the normalized integer formats + (BYTE4N, UBYTE4N, SHORT2N, SHORT4N), which can be converted + to floating point formats in the vertex shader inputs. + The reason is that D3D11 cannot convert from non-normalized + formats to floating point inputs (only to integer inputs), + and WebGL2 / GLES2 don't support integer vertex shader inputs. +*/ + +struct Vertex_t { + x f32 + y f32 + z f32 + color u32 + // u u16 + // v u16 + u f32 + v f32 +} + +fn init_cube_glsl(mut app App) { + // cube vertex buffer + // d := u16(32767/8) // for compatibility with D3D11, 32767 stand for 1 + d := f32(1.0) // 0.05) + c := u32(0xFFFFFF_FF) // color RGBA8 + vertices := [ + // Face 0 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{ 1.0, -1.0, -1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, -1.0, c, d, d}, + Vertex_t{-1.0, 1.0, -1.0, c, 0, d}, + // Face 1 + Vertex_t{-1.0, -1.0, 1.0, c, 0, 0}, + Vertex_t{ 1.0, -1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{-1.0, 1.0, 1.0, c, 0, d}, + // Face 2 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, 1.0, -1.0, c, d, 0}, + Vertex_t{-1.0, 1.0, 1.0, c, d, d}, + Vertex_t{-1.0, -1.0, 1.0, c, 0, d}, + // Face 3 + Vertex_t{ 1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{ 1.0, 1.0, -1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, -1.0, 1.0, c, 0, d}, + // Face 4 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, -1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, -1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, -1.0, -1.0, c, 0, d}, + // Face 5 + Vertex_t{-1.0, 1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, 1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, 1.0, -1.0, c, 0, d}, + ] + + mut vert_buffer_desc := C.sg_buffer_desc{label: c'cube-vertices'} + unsafe { C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc)) } + + vert_buffer_desc.size = size_t(vertices.len * int(sizeof(Vertex_t))) + vert_buffer_desc.data = C.sg_range{ + ptr: vertices.data + size: size_t(vertices.len * int(sizeof(Vertex_t))) + } + + vert_buffer_desc.@type = .vertexbuffer + // vert_buffer_desc.usage = .immutable + vbuf := gfx.make_buffer(&vert_buffer_desc) + + /* create an index buffer for the cube */ + 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 + ] + + mut index_buffer_desc := C.sg_buffer_desc{label: c'cube-indices'} + unsafe { C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc)) } + + index_buffer_desc.size = size_t(indices.len * int(sizeof(u16))) + index_buffer_desc.data = C.sg_range{ + ptr: indices.data + size: size_t(indices.len * int(sizeof(u16))) + } + + index_buffer_desc.@type = .indexbuffer + ibuf := gfx.make_buffer(&index_buffer_desc) + + // create shader + shader := gfx.make_shader(C.cube_shader_desc(C.sg_query_backend())) + + mut pipdesc := C.sg_pipeline_desc{} + unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) } + + 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_texcoord0].format = .short2n // u,v as u16 + + pipdesc.shader = shader + pipdesc.index_type = .uint16 + + pipdesc.depth = C.sg_depth_state{ + write_enabled: true + compare: gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL) + } + pipdesc.cull_mode = .back + + pipdesc.label = 'glsl_shader pipeline'.str + + app.cube_bind.vertex_buffers[0] = vbuf + app.cube_bind.index_buffer = ibuf + app.cube_bind.fs_images[C.SLOT_tex] = app.texture + app.cube_pip_glsl = gfx.make_pipeline(&pipdesc) + println('GLSL init DONE!') +} + +fn draw_cube_glsl(app App) { + if app.init_flag == false { + return + } + + rot := [f32(app.mouse_y), f32(app.mouse_x)] + + 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 := C.sg_range{ + ptr: &tr_matrix + size: size_t(4 * 16) + } + gfx.apply_uniforms(C.SG_SHADERSTAGE_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 := C.sg_range{ + ptr: unsafe { &text_res } + size: size_t(4 * 4) + } + gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params, &fs_uniforms_range) + + gfx.draw(0, (3 * 2) * 6, 1) + gfx.end_pass() + gfx.commit() +} + +fn draw_texture_cubes(app App) { + rot := [f32(app.mouse_x), f32(app.mouse_y)] + sgl.defaults() + sgl.load_pipeline(app.pip_3d) + + sgl.enable_texture() + sgl.texture(app.texture) + + sgl.matrix_mode_projection() + sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 100.0) + + sgl.matrix_mode_modelview() + sgl.translate(0.0, 0.0, -12.0) + 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) + 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.pop_matrix() + sgl.pop_matrix() + + sgl.disable_texture() +} + +fn frame(mut app App) { + ws := gg.window_size_real_pixels() + ratio := f32(ws.width) / ws.height + dw := ws.width + dh := ws.height + ww := int(dh / 3) // not a bug + hh := int(dh / 3) + x0 := int(f32(dw) * 0.05) + // x1 := dw/2 + y0 := 0 + y1 := int(f32(dh) * 0.5) + + // app.gg.begin() + + app.gg.begin() + sgl.defaults() + + // 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) + + // 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 := C.sg_color_attachment_action{ + action: gfx.Action(C.SG_ACTION_DONTCARE) // C.SG_ACTION_CLEAR) + value: C.sg_color{ + r: 1.0 + g: 1.0 + b: 1.0 + a: 1.0 + } + } + mut pass_action := C.sg_pass_action{} + pass_action.colors[0] = color_action + gfx.begin_default_pass(&pass_action, ws.width, ws.height) + + // glsl cube + draw_cube_glsl(app) + + app.frame_count++ +} + +/****************************************************************************** +* +* Init / Cleanup +* +******************************************************************************/ +fn my_init(mut app App) { + // set max vertices, + // for a large number of the same type of object it is better use the instances!! + desc := sapp.create_desc() + gfx.setup(&desc) + sgl_desc := C.sgl_desc_t{ + max_vertices: 50 * 65536 + } + sgl.setup(&sgl_desc) + + // 3d pipeline + mut pipdesc := C.sg_pipeline_desc{} + unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) } + + color_state := C.sg_color_state{ + blend: C.sg_blend_state{ + enabled: true + src_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_SRC_ALPHA) + dst_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA) + } + } + pipdesc.colors[0] = color_state + + pipdesc.depth = C.sg_depth_state{ + write_enabled: true + compare: gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL) + } + pipdesc.cull_mode = .back + + app.pip_3d = sgl.make_pipeline(&pipdesc) + + // create chessboard texture 256*256 RGBA + w := 256 + h := 256 + sz := w * h * 4 + tmp_txt := unsafe { malloc(sz) } + mut i := 0 + for i < sz { + unsafe { + y := (i >> 0x8) >> 5 // 8 cell + x := (i & 0xFF) >> 5 // 8 cell + // upper left corner + if x == 0 && y == 0 { + tmp_txt[i] = byte(0xFF) + tmp_txt[i + 1] = byte(0) + tmp_txt[i + 2] = byte(0) + tmp_txt[i + 3] = byte(0xFF) + } + // low right corner + else if x == 7 && y == 7 { + tmp_txt[i + 0] = byte(0) + tmp_txt[i + 1] = byte(0xFF) + tmp_txt[i + 2] = byte(0) + tmp_txt[i + 3] = byte(0xFF) + } else { + col := if ((x + y) & 1) == 1 { 0xFF } else { 128 } + tmp_txt[i + 0] = byte(col) // red + tmp_txt[i + 1] = byte(col) // green + tmp_txt[i + 2] = byte(col) // blue + tmp_txt[i + 3] = byte(0xFF) // alpha + } + i += 4 + } + } + app.texture = create_texture(w, h, tmp_txt) + unsafe { free(tmp_txt) } + + // glsl + init_cube_glsl(mut app) + app.init_flag = true +} + +fn cleanup(mut app App) { + gfx.shutdown() +} + +/****************************************************************************** +* +* event +* +******************************************************************************/ +fn my_event_manager(mut ev gg.Event, mut app App) { + if ev.typ == .mouse_move { + app.mouse_x = int(ev.mouse_x) + app.mouse_y = int(ev.mouse_y) + } + if ev.typ == .touches_began || ev.typ == .touches_moved { + if ev.num_touches > 0 { + touch_point := ev.touches[0] + app.mouse_x = int(touch_point.pos_x) + app.mouse_y = int(touch_point.pos_y) + } + } +} + +/****************************************************************************** +* +* Main +* +******************************************************************************/ +[console] // is needed for easier diagnostics on windows +fn main() { + // App init + mut app := &App{ + gg: 0 + } + + mut a := [5]int{} + a[0] = 2 + println(a) + + app.gg = gg.new_context( + width: win_width + height: win_height + create_window: true + window_title: '3D Cube Demo' + user_data: app + bg_color: bg_color + frame_fn: frame + init_fn: my_init + cleanup_fn: cleanup + event_fn: my_event_manager + ) + + app.ticks = time.ticks() + app.gg.run() +} diff --git a/v_windows/v/old/examples/sokol/02_cubes_glsl/v.mod b/v_windows/v/old/examples/sokol/02_cubes_glsl/v.mod new file mode 100644 index 0000000..e69de29 diff --git a/v_windows/v/old/examples/sokol/03_march_tracing_glsl/rt_glsl.glsl b/v_windows/v/old/examples/sokol/03_march_tracing_glsl/rt_glsl.glsl new file mode 100644 index 0000000..3490873 --- /dev/null +++ b/v_windows/v/old/examples/sokol/03_march_tracing_glsl/rt_glsl.glsl @@ -0,0 +1,695 @@ +//------------------------------------------------------------------------------ +// Shader code for texcube-sapp sample. +// +// NOTE: This source file also uses the '#pragma sokol' form of the +// custom tags. +//------------------------------------------------------------------------------ +//#pragma sokol @ctype mat4 hmm_mat4 + +#pragma sokol @vs vs +uniform vs_params { + mat4 mvp; +}; + +in vec4 pos; +in vec4 color0; +in vec2 texcoord0; + +out vec4 color; +out vec2 uv; + +void main() { + gl_Position = mvp * pos; + color = color0; + uv = texcoord0; +} +#pragma sokol @end + +#pragma sokol @fs fs +uniform sampler2D tex; +uniform fs_params { + vec2 iResolution; + vec2 iMouse; + float iTime; + float iFrame; +}; + +in vec4 color; +in vec2 uv; +out vec4 frag_color; + +// change to 0 to 4 to increment the AntiAliasing, +// increase AA will SLOW the rendering!! +#define AA 1 + +//********************************************************* +// Ray Marching +// original code from: https://www.shadertoy.com/view/Xds3zN +//********************************************************* +// The MIT License +// Copyright © 2013 Inigo Quilez +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +// A list of useful distance function to simple primitives. All +// these functions (except for ellipsoid) return an exact +// euclidean distance, meaning they produce a better SDF than +// what you'd get if you were constructing them from boolean +// operations. +// +// More info here: +// +// https://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm + +//------------------------------------------------------------------ +float dot2( in vec2 v ) { return dot(v,v); } +float dot2( in vec3 v ) { return dot(v,v); } +float ndot( in vec2 a, in vec2 b ) { return a.x*b.x - a.y*b.y; } + +float sdPlane( vec3 p ) +{ + return p.y; +} + +float sdSphere( vec3 p, float s ) +{ + return length(p)-s; +} + +float sdBox( vec3 p, vec3 b ) +{ + vec3 d = abs(p) - b; + return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)); +} + +float sdBoundingBox( vec3 p, vec3 b, float e ) +{ + p = abs(p )-b; + vec3 q = abs(p+e)-e; + + return min(min( + length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), + length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), + length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); +} +float sdEllipsoid( in vec3 p, in vec3 r ) // approximated +{ + float k0 = length(p/r); + float k1 = length(p/(r*r)); + return k0*(k0-1.0)/k1; +} + +float sdTorus( vec3 p, vec2 t ) +{ + return length( vec2(length(p.xz)-t.x,p.y) )-t.y; +} + +float sdCappedTorus(in vec3 p, in vec2 sc, in float ra, in float rb) +{ + p.x = abs(p.x); + float k = (sc.y*p.x>sc.x*p.y) ? dot(p.xy,sc) : length(p.xy); + return sqrt( dot(p,p) + ra*ra - 2.0*ra*k ) - rb; +} + +float sdHexPrism( vec3 p, vec2 h ) +{ + vec3 q = abs(p); + + const vec3 k = vec3(-0.8660254, 0.5, 0.57735); + p = abs(p); + p.xy -= 2.0*min(dot(k.xy, p.xy), 0.0)*k.xy; + vec2 d = vec2( + length(p.xy - vec2(clamp(p.x, -k.z*h.x, k.z*h.x), h.x))*sign(p.y - h.x), + p.z-h.y ); + return min(max(d.x,d.y),0.0) + length(max(d,0.0)); +} + +float sdOctogonPrism( in vec3 p, in float r, float h ) +{ + const vec3 k = vec3(-0.9238795325, // sqrt(2+sqrt(2))/2 + 0.3826834323, // sqrt(2-sqrt(2))/2 + 0.4142135623 ); // sqrt(2)-1 + // reflections + p = abs(p); + p.xy -= 2.0*min(dot(vec2( k.x,k.y),p.xy),0.0)*vec2( k.x,k.y); + p.xy -= 2.0*min(dot(vec2(-k.x,k.y),p.xy),0.0)*vec2(-k.x,k.y); + // polygon side + p.xy -= vec2(clamp(p.x, -k.z*r, k.z*r), r); + vec2 d = vec2( length(p.xy)*sign(p.y), p.z-h ); + return min(max(d.x,d.y),0.0) + length(max(d,0.0)); +} + +float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) +{ + vec3 pa = p-a, ba = b-a; + float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); + return length( pa - ba*h ) - r; +} + +float sdRoundCone( in vec3 p, in float r1, float r2, float h ) +{ + vec2 q = vec2( length(p.xz), p.y ); + + float b = (r1-r2)/h; + float a = sqrt(1.0-b*b); + float k = dot(q,vec2(-b,a)); + + if( k < 0.0 ) return length(q) - r1; + if( k > a*h ) return length(q-vec2(0.0,h)) - r2; + + return dot(q, vec2(a,b) ) - r1; +} + +float sdRoundCone(vec3 p, vec3 a, vec3 b, float r1, float r2) +{ + // sampling independent computations (only depend on shape) + vec3 ba = b - a; + float l2 = dot(ba,ba); + float rr = r1 - r2; + float a2 = l2 - rr*rr; + float il2 = 1.0/l2; + + // sampling dependant computations + vec3 pa = p - a; + float y = dot(pa,ba); + float z = y - l2; + float x2 = dot2( pa*l2 - ba*y ); + float y2 = y*y*l2; + float z2 = z*z*l2; + + // single square root! + float k = sign(rr)*rr*rr*x2; + if( sign(z)*a2*z2 > k ) return sqrt(x2 + z2) *il2 - r2; + if( sign(y)*a2*y2 < k ) return sqrt(x2 + y2) *il2 - r1; + return (sqrt(x2*a2*il2)+y*rr)*il2 - r1; +} + +float sdTriPrism( vec3 p, vec2 h ) +{ + const float k = sqrt(3.0); + h.x *= 0.5*k; + p.xy /= h.x; + p.x = abs(p.x) - 1.0; + p.y = p.y + 1.0/k; + if( p.x+k*p.y>0.0 ) p.xy=vec2(p.x-k*p.y,-k*p.x-p.y)/2.0; + p.x -= clamp( p.x, -2.0, 0.0 ); + float d1 = length(p.xy)*sign(-p.y)*h.x; + float d2 = abs(p.z)-h.y; + return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.); +} + +// vertical +float sdCylinder( vec3 p, vec2 h ) +{ + vec2 d = abs(vec2(length(p.xz),p.y)) - h; + return min(max(d.x,d.y),0.0) + length(max(d,0.0)); +} + +// arbitrary orientation +float sdCylinder(vec3 p, vec3 a, vec3 b, float r) +{ + vec3 pa = p - a; + vec3 ba = b - a; + float baba = dot(ba,ba); + float paba = dot(pa,ba); + + float x = length(pa*baba-ba*paba) - r*baba; + float y = abs(paba-baba*0.5)-baba*0.5; + float x2 = x*x; + float y2 = y*y*baba; + float d = (max(x,y)<0.0)?-min(x2,y2):(((x>0.0)?x2:0.0)+((y>0.0)?y2:0.0)); + return sign(d)*sqrt(abs(d))/baba; +} + +// vertical +float sdCone( in vec3 p, in vec2 c, float h ) +{ + vec2 q = h*vec2(c.x,-c.y)/c.y; + vec2 w = vec2( length(p.xz), p.y ); + + vec2 a = w - q*clamp( dot(w,q)/dot(q,q), 0.0, 1.0 ); + vec2 b = w - q*vec2( clamp( w.x/q.x, 0.0, 1.0 ), 1.0 ); + float k = sign( q.y ); + float d = min(dot( a, a ),dot(b, b)); + float s = max( k*(w.x*q.y-w.y*q.x),k*(w.y-q.y) ); + return sqrt(d)*sign(s); +} + +float sdCappedCone( in vec3 p, in float h, in float r1, in float r2 ) +{ + vec2 q = vec2( length(p.xz), p.y ); + + vec2 k1 = vec2(r2,h); + vec2 k2 = vec2(r2-r1,2.0*h); + vec2 ca = vec2(q.x-min(q.x,(q.y < 0.0)?r1:r2), abs(q.y)-h); + vec2 cb = q - k1 + k2*clamp( dot(k1-q,k2)/dot2(k2), 0.0, 1.0 ); + float s = (cb.x < 0.0 && ca.y < 0.0) ? -1.0 : 1.0; + return s*sqrt( min(dot2(ca),dot2(cb)) ); +} + +float sdCappedCone(vec3 p, vec3 a, vec3 b, float ra, float rb) +{ + float rba = rb-ra; + float baba = dot(b-a,b-a); + float papa = dot(p-a,p-a); + float paba = dot(p-a,b-a)/baba; + + float x = sqrt( papa - paba*paba*baba ); + + float cax = max(0.0,x-((paba<0.5)?ra:rb)); + float cay = abs(paba-0.5)-0.5; + + float k = rba*rba + baba; + float f = clamp( (rba*(x-ra)+paba*baba)/k, 0.0, 1.0 ); + + float cbx = x-ra - f*rba; + float cby = paba - f; + + float s = (cbx < 0.0 && cay < 0.0) ? -1.0 : 1.0; + + return s*sqrt( min(cax*cax + cay*cay*baba, + cbx*cbx + cby*cby*baba) ); +} + +// c is the sin/cos of the desired cone angle +float sdSolidAngle(vec3 pos, vec2 c, float ra) +{ + vec2 p = vec2( length(pos.xz), pos.y ); + float l = length(p) - ra; + float m = length(p - c*clamp(dot(p,c),0.0,ra) ); + return max(l,m*sign(c.y*p.x-c.x*p.y)); +} + +float sdOctahedron(vec3 p, float s) +{ + p = abs(p); + float m = p.x + p.y + p.z - s; + +// exact distance +#if 0 + vec3 o = min(3.0*p - m, 0.0); + o = max(6.0*p - m*2.0 - o*3.0 + (o.x+o.y+o.z), 0.0); + return length(p - s*o/(o.x+o.y+o.z)); +#endif + +// exact distance +#if 1 + vec3 q; + if( 3.0*p.x < m ) q = p.xyz; + else if( 3.0*p.y < m ) q = p.yzx; + else if( 3.0*p.z < m ) q = p.zxy; + else return m*0.57735027; + float k = clamp(0.5*(q.z-q.y+s),0.0,s); + return length(vec3(q.x,q.y-s+k,q.z-k)); +#endif + +// bound, not exact +#if 0 + return m*0.57735027; +#endif +} + +float sdPyramid( in vec3 p, in float h ) +{ + float m2 = h*h + 0.25; + + // symmetry + p.xz = abs(p.xz); + p.xz = (p.z>p.x) ? p.zx : p.xz; + p.xz -= 0.5; + + // project into face plane (2D) + vec3 q = vec3( p.z, h*p.y - 0.5*p.x, h*p.x + 0.5*p.y); + + float s = max(-q.x,0.0); + float t = clamp( (q.y-0.5*p.z)/(m2+0.25), 0.0, 1.0 ); + + float a = m2*(q.x+s)*(q.x+s) + q.y*q.y; + float b = m2*(q.x+0.5*t)*(q.x+0.5*t) + (q.y-m2*t)*(q.y-m2*t); + + float d2 = min(q.y,-q.x*m2-q.y*0.5) > 0.0 ? 0.0 : min(a,b); + + // recover 3D and scale, and add sign + return sqrt( (d2+q.z*q.z)/m2 ) * sign(max(q.z,-p.y)); +} + +// la,lb=semi axis, h=height, ra=corner +float sdRhombus(vec3 p, float la, float lb, float h, float ra) +{ + p = abs(p); + vec2 b = vec2(la,lb); + float f = clamp( (ndot(b,b-2.0*p.xz))/dot(b,b), -1.0, 1.0 ); + vec2 q = vec2(length(p.xz-0.5*b*vec2(1.0-f,1.0+f))*sign(p.x*b.y+p.z*b.x-b.x*b.y)-ra, p.y-h); + return min(max(q.x,q.y),0.0) + length(max(q,0.0)); +} + +//------------------------------------------------------------------ + +vec2 opU( vec2 d1, vec2 d2 ) +{ + return (d1.x0.0 ) + { + tmax = min( tmax, tp1 ); + res = vec2( tp1, 1.0 ); + } + //else return res; + + // raymarch primitives + vec2 tb = iBox( ro-vec3(0.0,0.4,-0.5), rd, vec3(2.5,0.41,3.0) ); + if( tb.x0.0 && tb.x0.0 ) tmax = min( tmax, tp ); + + float res = 1.0; + float t = mint; + for( int i=ZERO; i<24; i++ ) + { + float h = map( ro + rd*t ).x; + float s = clamp(8.0*h/t,0.0,1.0); + res = min( res, s*s*(3.0-2.0*s) ); + t += clamp( h, 0.02, 0.2 ); + if( res<0.004 || t>tmax ) break; + } + return clamp( res, 0.0, 1.0 ); +} + +// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm +vec3 calcNormal( in vec3 pos ) +{ +#if 0 + vec2 e = vec2(1.0,-1.0)*0.5773*0.0005; + return normalize( e.xyy*map( pos + e.xyy ).x + + e.yyx*map( pos + e.yyx ).x + + e.yxy*map( pos + e.yxy ).x + + e.xxx*map( pos + e.xxx ).x ); +#else + // inspired by tdhooper and klems - a way to prevent the compiler from inlining map() 4 times + vec3 n = vec3(0.0); + for( int i=ZERO; i<4; i++ ) + { + vec3 e = 0.5773*(2.0*vec3((((i+3)>>1)&1),((i>>1)&1),(i&1))-1.0); + n += e*map(pos+0.0005*e).x; + //if( n.x+n.y+n.z>100.0 ) break; + } + return normalize(n); +#endif +} + +float calcAO( in vec3 pos, in vec3 nor ) +{ + float occ = 0.0; + float sca = 1.0; + for( int i=ZERO; i<5; i++ ) + { + float h = 0.01 + 0.12*float(i)/4.0; + float d = map( pos + h*nor ).x; + occ += (h-d)*sca; + sca *= 0.95; + if( occ>0.35 ) break; + } + return clamp( 1.0 - 3.0*occ, 0.0, 1.0 ) * (0.5+0.5*nor.y); +} + +// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm +float checkersGradBox( in vec2 p, in vec2 dpdx, in vec2 dpdy ) +{ + // filter kernel + vec2 w = abs(dpdx)+abs(dpdy) + 0.001; + // analytical integral (box filter) + vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w; + // xor pattern + return 0.5 - 0.5*i.x*i.y; +} + +vec3 render( in vec3 ro, in vec3 rd, in vec3 rdx, in vec3 rdy ) +{ + // background + vec3 col = vec3(0.7, 0.7, 0.9) - max(rd.y,0.0)*0.3; + + // raycast scene + vec2 res = raycast(ro,rd); + float t = res.x; + float m = res.y; + if( m>-0.5 ) + { + vec3 pos = ro + t*rd; + vec3 nor = (m<1.5) ? vec3(0.0,1.0,0.0) : calcNormal( pos ); + vec3 ref = reflect( rd, nor ); + + // material + col = 0.2 + 0.2*sin( m*2.0 + vec3(0.0,1.0,2.0) ); + float ks = 1.0; + + if( m<1.5 ) + { + // project pixel footprint into the plane + vec3 dpdx = ro.y*(rd/rd.y-rdx/rdx.y); + vec3 dpdy = ro.y*(rd/rd.y-rdy/rdy.y); + + float f = checkersGradBox( 3.0*pos.xz, 3.0*dpdx.xz, 3.0*dpdy.xz ); + col = 0.15 + f*vec3(0.05); + ks = 0.4; + } + + // lighting + float occ = calcAO( pos, nor ); + + vec3 lin = vec3(0.0); + + // sun + { + vec3 lig = normalize( vec3(-0.5, 0.4, -0.6) ); + vec3 hal = normalize( lig-rd ); + float dif = clamp( dot( nor, lig ), 0.0, 1.0 ); + //if( dif>0.0001 ) + dif *= calcSoftshadow( pos, lig, 0.02, 2.5 ); + float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),16.0); + spe *= dif; + spe *= 0.04+0.96*pow(clamp(1.0-dot(hal,lig),0.0,1.0),5.0); + lin += col*2.20*dif*vec3(1.30,1.00,0.70); + lin += 5.00*spe*vec3(1.30,1.00,0.70)*ks; + } + // sky + { + float dif = sqrt(clamp( 0.5+0.5*nor.y, 0.0, 1.0 )); + dif *= occ; + float spe = smoothstep( -0.2, 0.2, ref.y ); + spe *= dif; + spe *= 0.04+0.96*pow(clamp(1.0+dot(nor,rd),0.0,1.0), 5.0 ); + //if( spe>0.001 ) + spe *= calcSoftshadow( pos, ref, 0.02, 2.5 ); + lin += col*0.60*dif*vec3(0.40,0.60,1.15); + lin += 2.00*spe*vec3(0.40,0.60,1.30)*ks; + } + // back + { + float dif = clamp( dot( nor, normalize(vec3(0.5,0.0,0.6))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0); + dif *= occ; + lin += col*0.55*dif*vec3(0.25,0.25,0.25); + } + // sss + { + float dif = pow(clamp(1.0+dot(nor,rd),0.0,1.0),2.0); + dif *= occ; + lin += col*0.25*dif*vec3(1.00,1.00,1.00); + } + + col = lin; + + col = mix( col, vec3(0.7,0.7,0.9), 1.0-exp( -0.0001*t*t*t ) ); + } + + return vec3( clamp(col,0.0,1.0) ); +} + +mat3 setCamera( in vec3 ro, in vec3 ta, float cr ) +{ + vec3 cw = normalize(ta-ro); + vec3 cp = vec3(sin(cr), cos(cr),0.0); + vec3 cu = normalize( cross(cw,cp) ); + vec3 cv = ( cross(cu,cw) ); + return mat3( cu, cv, cw ); +} + +vec4 mainImage( vec2 fragCoord ) +{ + vec2 mo = iMouse.xy/iResolution.xy; + float time = 32.0 + iTime*1.5; + + // camera + vec3 ta = vec3( 0.5, -0.5, -0.6 ); + vec3 ro = ta + vec3( 4.5*cos(0.1*time + 7.0*mo.x), 1.3 + 2.0*mo.y, 4.5*sin(0.1*time + 7.0*mo.x) ); + // camera-to-world transformation + mat3 ca = setCamera( ro, ta, 0.0 ); + + vec3 tot = vec3(0.0); +#if AA>1 + for( int m=ZERO; m1 + } + tot /= float(AA*AA); +#endif + + //fragColor = vec4( tot, 1.0 ); + return vec4( tot, 1.0 ); +} + +//********************************************************* +// END Ray Marching +//********************************************************* + +void main() { + vec4 c = color; + vec4 txt = texture(tex, uv); + c = txt * c; + vec2 uv1 = uv * iResolution; + vec4 col_ray = mainImage(uv1); + + // use this to mix the chessboart texture with the ray marching + //frag_color = clamp(c*iMouse.y/512.0,0.0,1.0) * col_ray ; + + frag_color = c*0.00001 + col_ray ; +} + +#pragma sokol @end + +#pragma sokol @program rt vs fs diff --git a/v_windows/v/old/examples/sokol/03_march_tracing_glsl/rt_glsl.v b/v_windows/v/old/examples/sokol/03_march_tracing_glsl/rt_glsl.v new file mode 100644 index 0000000..73a85a3 --- /dev/null +++ b/v_windows/v/old/examples/sokol/03_march_tracing_glsl/rt_glsl.v @@ -0,0 +1,438 @@ +/********************************************************************** +* +* Sokol 3d cube demo +* +* 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. +* +* HOW TO COMPILE SHADERS: +* - download the sokol shader convertor tool from https://github.com/floooh/sokol-tools-bin +* +* - compile the .glsl shader with: +* linux : sokol-shdc --input rt_glsl.glsl --output rt_glsl.h --slang glsl330 +* windows: sokol-shdc.exe --input rt_glsl.glsl --output rt_glsl.h --slang glsl330 +* +* --slang parameter can be: +* - glsl330: desktop GL +* - glsl100: GLES2 / WebGL +* - glsl300es: GLES3 / WebGL2 +* - hlsl4: D3D11 +* - hlsl5: D3D11 +* - metal_macos: Metal on macOS +* - metal_ios: Metal on iOS device +* - metal_sim: Metal on iOS simulator +* - wgpu: WebGPU +* +* you can have multiple platforms at the same time passing parameters like this: --slang glsl330:hlsl5:metal_macos +* for further infos have a look at the sokol shader tool docs. +* +* TODO: +* - frame counter +**********************************************************************/ +import gg +import gg.m4 +import gx +// import math +import sokol.sapp +import sokol.gfx +import sokol.sgl +import time + +// GLSL Include and functions + +#flag -I @VMODROOT/. +#include "rt_glsl.h" #Please use sokol-shdc to generate the necessary rt_glsl.h file from rt_glsl.glsl (see the instructions at the top of this file) + +fn C.rt_shader_desc(gfx.Backend) &C.sg_shader_desc + +const ( + win_width = 800 + win_height = 800 + bg_color = gx.white +) + +struct App { +mut: + gg &gg.Context + texture C.sg_image + init_flag bool + frame_count int + + mouse_x int = -1 + mouse_y int = -1 + // glsl + cube_pip_glsl C.sg_pipeline + cube_bind C.sg_bindings + // time + ticks i64 +} + +/****************************************************************************** +* Texture functions +******************************************************************************/ +fn create_texture(w int, h int, buf &byte) C.sg_image { + sz := w * h * 4 + mut img_desc := C.sg_image_desc{ + width: w + height: h + num_mipmaps: 0 + min_filter: .linear + mag_filter: .linear + // usage: .dynamic + wrap_u: .clamp_to_edge + wrap_v: .clamp_to_edge + label: &byte(0) + d3d11_texture: 0 + } + // comment if .dynamic is enabled + img_desc.data.subimage[0][0] = C.sg_range{ + ptr: buf + size: size_t(sz) + } + + sg_img := C.sg_make_image(&img_desc) + return sg_img +} + +fn destroy_texture(sg_img C.sg_image) { + C.sg_destroy_image(sg_img) +} + +// Use only if usage: .dynamic is enabled +fn update_text_texture(sg_img C.sg_image, w int, h int, buf &byte) { + sz := w * h * 4 + mut tmp_sbc := C.sg_image_data{} + tmp_sbc.subimage[0][0] = C.sg_range{ + ptr: buf + size: size_t(sz) + } + C.sg_update_image(sg_img, &tmp_sbc) +} + +/****************************************************************************** +* Draw functions +****************************************************************************** +Cube vertex buffer with packed vertex formats for color and texture coords. + Note that a vertex format which must be portable across all + backends must only use the normalized integer formats + (BYTE4N, UBYTE4N, SHORT2N, SHORT4N), which can be converted + to floating point formats in the vertex shader inputs. + The reason is that D3D11 cannot convert from non-normalized + formats to floating point inputs (only to integer inputs), + and WebGL2 / GLES2 don't support integer vertex shader inputs. +*/ + +struct Vertex_t { + x f32 + y f32 + z f32 + color u32 + u f32 + v f32 + // u u16 // for compatibility with D3D11 + // v u16 // for compatibility with D3D11 +} + +fn init_cube_glsl(mut app App) { + // cube vertex buffer + // d := u16(32767) // for compatibility with D3D11, 32767 stand for 1 + d := f32(1.0) + c := u32(0xFFFFFF_FF) // color RGBA8 + vertices := [ + // Face 0 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{ 1.0, -1.0, -1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, -1.0, c, d, d}, + Vertex_t{-1.0, 1.0, -1.0, c, 0, d}, + // Face 1 + Vertex_t{-1.0, -1.0, 1.0, c, 0, 0}, + Vertex_t{ 1.0, -1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{-1.0, 1.0, 1.0, c, 0, d}, + // Face 2 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, 1.0, -1.0, c, d, 0}, + Vertex_t{-1.0, 1.0, 1.0, c, d, d}, + Vertex_t{-1.0, -1.0, 1.0, c, 0, d}, + // Face 3 + Vertex_t{ 1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{ 1.0, 1.0, -1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, -1.0, 1.0, c, 0, d}, + // Face 4 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, -1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, -1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, -1.0, -1.0, c, 0, d}, + // Face 5 + Vertex_t{-1.0, 1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, 1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, 1.0, -1.0, c, 0, d}, + ] + + mut vert_buffer_desc := C.sg_buffer_desc{label: c'cube-vertices'} + unsafe { C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc)) } + + vert_buffer_desc.size = size_t(vertices.len * int(sizeof(Vertex_t))) + vert_buffer_desc.data = C.sg_range{ + ptr: vertices.data + size: size_t(vertices.len * int(sizeof(Vertex_t))) + } + + vert_buffer_desc.@type = .vertexbuffer + vbuf := gfx.make_buffer(&vert_buffer_desc) + + // create an index buffer for the cube + 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, + ] + + mut index_buffer_desc := C.sg_buffer_desc{label: c'cube-indices'} + unsafe {C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc))} + + index_buffer_desc.size = size_t(indices.len * int(sizeof(u16))) + index_buffer_desc.data = C.sg_range{ + ptr: indices.data + size: size_t(indices.len * int(sizeof(u16))) + } + + index_buffer_desc.@type = .indexbuffer + ibuf := gfx.make_buffer(&index_buffer_desc) + + // create shader + shader := gfx.make_shader(C.rt_shader_desc(C.sg_query_backend())) + + mut pipdesc := C.sg_pipeline_desc{} + unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) } + 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_texcoord0].format = .short2n // u,v as u16 + + pipdesc.shader = shader + pipdesc.index_type = .uint16 + + pipdesc.depth = C.sg_depth_state{ + write_enabled: true + compare: gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL) + } + pipdesc.cull_mode = .back + + pipdesc.label = 'glsl_shader pipeline'.str + + app.cube_bind.vertex_buffers[0] = vbuf + app.cube_bind.index_buffer = ibuf + app.cube_bind.fs_images[C.SLOT_tex] = app.texture + app.cube_pip_glsl = gfx.make_pipeline(&pipdesc) + println('GLSL init DONE!') +} + +[inline] +fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 { + 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)) + 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 + scale_m := m4.scale(vec4(in_scale, in_scale, in_scale, 1)) + + res := (scale_m * model) * view_proj + return res +} + +fn draw_cube_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) + + // use the following commented lines to rotate the 3d glsl cube + // rot := [f32(app.mouse_y), f32(app.mouse_x)] + // calc_tr_matrices(dw, dh, rot[0], rot[1] ,2.3) + tr_matrix := calc_tr_matrices(dw, dh, 0, 0, 2.3) + gfx.apply_viewport(0, 0, ws.width, ws.height, true) + + // apply the pipline and bindings + gfx.apply_pipeline(app.cube_pip_glsl) + gfx.apply_bindings(app.cube_bind) + + // Uniforms + // *** vertex shadeer uniforms *** + // passing the view matrix as uniform + // res is a 4x4 matrix of f32 thus: 4*16 byte of size + vs_uniforms_range := C.sg_range{ + ptr: &tr_matrix + size: size_t(4 * 16) + } + gfx.apply_uniforms(C.SG_SHADERSTAGE_VS, C.SLOT_vs_params, &vs_uniforms_range) + + // *** fragment shader uniforms *** + time_ticks := f32(time.ticks() - app.ticks) / 1000 + mut tmp_fs_params := [ + f32(ws.width), + ws.height * ratio, // x,y resolution to pass to FS + 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 + ]! + fs_uniforms_range := C.sg_range{ + ptr: unsafe { &tmp_fs_params } + size: size_t(sizeof(tmp_fs_params)) + } + gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params, &fs_uniforms_range) + + // 3 vertices for triangle * 2 triangles per face * 6 faces = 36 vertices to draw + gfx.draw(0, (3 * 2) * 6, 1) + gfx.end_pass() + gfx.commit() +} + +fn frame(mut app App) { + ws := gg.window_size_real_pixels() + + // clear + mut color_action := C.sg_color_attachment_action{ + action: gfx.Action(C.SG_ACTION_CLEAR) + value: C.sg_color{ + r: 0.0 + g: 0.0 + b: 0.0 + a: 1.0 + } + } + + mut pass_action := C.sg_pass_action{} + pass_action.colors[0] = color_action + gfx.begin_default_pass(&pass_action, ws.width, ws.height) + + // glsl cube + draw_cube_glsl(app) + app.frame_count++ +} + +/****************************************************************************** +* Init / Cleanup +******************************************************************************/ +fn my_init(mut app App) { + // set max vertices, + // for a large number of the same type of object it is better use the instances!! + desc := sapp.create_desc() + gfx.setup(&desc) + sgl_desc := C.sgl_desc_t{ + max_vertices: 50 * 65536 + } + sgl.setup(&sgl_desc) + + // create chessboard texture 256*256 RGBA + w := 256 + h := 256 + sz := w * h * 4 + tmp_txt := unsafe { malloc(sz) } + mut i := 0 + for i < sz { + unsafe { + y := (i >> 0x8) >> 5 // 8 cell + x := (i & 0xFF) >> 5 // 8 cell + // upper left corner + if x == 0 && y == 0 { + tmp_txt[i + 0] = byte(0xFF) + tmp_txt[i + 1] = byte(0) + tmp_txt[i + 2] = byte(0) + tmp_txt[i + 3] = byte(0xFF) + } + // low right corner + else if x == 7 && y == 7 { + tmp_txt[i + 0] = byte(0) + tmp_txt[i + 1] = byte(0xFF) + tmp_txt[i + 2] = byte(0) + tmp_txt[i + 3] = byte(0xFF) + } else { + col := if ((x + y) & 1) == 1 { 0xFF } else { 128 } + tmp_txt[i + 0] = byte(col) // red + tmp_txt[i + 1] = byte(col) // green + tmp_txt[i + 2] = byte(col) // blue + tmp_txt[i + 3] = byte(0xFF) // alpha + } + i += 4 + } + } + unsafe { + app.texture = create_texture(w, h, tmp_txt) + free(tmp_txt) + } + // glsl + init_cube_glsl(mut app) + app.init_flag = true +} + +fn cleanup(mut app App) { + gfx.shutdown() +} + +/****************************************************************************** +* events handling +******************************************************************************/ +fn my_event_manager(mut ev gg.Event, mut app App) { + if ev.typ == .mouse_move { + app.mouse_x = int(ev.mouse_x) + app.mouse_y = int(ev.mouse_y) + } + if ev.typ == .touches_began || ev.typ == .touches_moved { + if ev.num_touches > 0 { + touch_point := ev.touches[0] + app.mouse_x = int(touch_point.pos_x) + app.mouse_y = int(touch_point.pos_y) + } + } +} + +/****************************************************************************** +* Main +******************************************************************************/ +[console] // is needed for easier diagnostics on windows +fn main() { + // App init + mut app := &App{ + gg: 0 + } + + app.gg = gg.new_context( + width: win_width + height: win_height + create_window: true + window_title: '3D Ray Marching Cube' + user_data: app + bg_color: bg_color + frame_fn: frame + init_fn: my_init + cleanup_fn: cleanup + event_fn: my_event_manager + ) + + app.ticks = time.ticks() + app.gg.run() +} diff --git a/v_windows/v/old/examples/sokol/03_march_tracing_glsl/v.mod b/v_windows/v/old/examples/sokol/03_march_tracing_glsl/v.mod new file mode 100644 index 0000000..e69de29 diff --git a/v_windows/v/old/examples/sokol/04_multi_shader_glsl/rt_glsl.v b/v_windows/v/old/examples/sokol/04_multi_shader_glsl/rt_glsl.v new file mode 100644 index 0000000..d19d199 --- /dev/null +++ b/v_windows/v/old/examples/sokol/04_multi_shader_glsl/rt_glsl.v @@ -0,0 +1,634 @@ +/********************************************************************** +* +* Sokol 3d cube multishader demo +* +* 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. +* +* HOW TO COMPILE SHADERS: +* - download the sokol shader convertor tool from https://github.com/floooh/sokol-tools-bin +* +* - compile the .glsl shared file with: +* linux : sokol-shdc --input rt_glsl_puppy.glsl --output rt_glsl_puppy.h --slang glsl330 + sokol-shdc --input rt_glsl_march.glsl --output rt_glsl_march.h --slang glsl330 +* windows: sokol-shdc.exe --input rt_glsl_puppy.glsl --output rt_glsl_puppy.h --slang glsl330 +* sokol-shdc.exe --input rt_glsl_march.glsl --output rt_glsl_march.h --slang glsl330 +* +* --slang parameter can be: +* - glsl330: desktop GL +* - glsl100: GLES2 / WebGL +* - glsl300es: GLES3 / WebGL2 +* - hlsl4: D3D11 +* - hlsl5: D3D11 +* - metal_macos: Metal on macOS +* - metal_ios: Metal on iOS device +* - metal_sim: Metal on iOS simulator +* - wgpu: WebGPU +* +* you can have multiple platforms at the same time passing parameters like this: --slang glsl330:hlsl5:metal_macos +* for further infos have a look at the sokol shader tool docs. +* +* TODO: +* - frame counter +**********************************************************************/ +import gg +import gg.m4 +import gx +// import math +import sokol.sapp +import sokol.gfx +import sokol.sgl +import time + +// GLSL Include and functions +#flag -I @VMODROOT/. +#include "rt_glsl_march.h" #Please use sokol-shdc to generate the necessary rt_glsl_march.h file from rt_glsl_march.glsl (see the instructions at the top of this file) +#include "rt_glsl_puppy.h" #Please use sokol-shdc to generate the necessary rt_glsl_puppy.h file from rt_glsl_puppy.glsl (see the instructions at the top of this file) +fn C.rt_march_shader_desc(gfx.Backend) &C.sg_shader_desc +fn C.rt_puppy_shader_desc(gfx.Backend) &C.sg_shader_desc + +const ( + win_width = 800 + win_height = 800 + bg_color = gx.white +) + +struct App { +mut: + gg &gg.Context + texture C.sg_image + init_flag bool + frame_count int + mouse_x int = -1 + mouse_y int = -1 + mouse_down bool + // glsl + cube_pip_glsl C.sg_pipeline + cube_bind C.sg_bindings + pipe map[string]C.sg_pipeline + bind map[string]C.sg_bindings + // time + ticks i64 +} + +/****************************************************************************** +* Texture functions +******************************************************************************/ +fn create_texture(w int, h int, buf byteptr) C.sg_image { + sz := w * h * 4 + mut img_desc := C.sg_image_desc{ + width: w + height: h + num_mipmaps: 0 + min_filter: .linear + mag_filter: .linear + // usage: .dynamic + wrap_u: .clamp_to_edge + wrap_v: .clamp_to_edge + label: &byte(0) + d3d11_texture: 0 + } + // comment if .dynamic is enabled + img_desc.data.subimage[0][0] = C.sg_range{ + ptr: buf + size: size_t(sz) + } + + sg_img := C.sg_make_image(&img_desc) + return sg_img +} + +fn destroy_texture(sg_img C.sg_image) { + C.sg_destroy_image(sg_img) +} + +// Use only if usage: .dynamic is enabled +fn update_text_texture(sg_img C.sg_image, w int, h int, buf byteptr) { + sz := w * h * 4 + mut tmp_sbc := C.sg_image_data{} + tmp_sbc.subimage[0][0] = C.sg_range{ + ptr: buf + size: size_t(sz) + } + C.sg_update_image(sg_img, &tmp_sbc) +} + +/****************************************************************************** +* Draw functions +****************************************************************************** +Cube vertex buffer with packed vertex formats for color and texture coords. + Note that a vertex format which must be portable across all + backends must only use the normalized integer formats + (BYTE4N, UBYTE4N, SHORT2N, SHORT4N), which can be converted + to floating point formats in the vertex shader inputs. + The reason is that D3D11 cannot convert from non-normalized + formats to floating point inputs (only to integer inputs), + and WebGL2 / GLES2 don't support integer vertex shader inputs. +*/ +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 +} + +// march shader init +fn init_cube_glsl_m(mut app App) { + // cube vertex buffer + // d := u16(32767) // for compatibility with D3D11, 32767 stand for 1 + d := f32(1.0) + c := u32(0xFFFFFF_FF) // color RGBA8 + vertices := [ + // Face 0 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{ 1.0, -1.0, -1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, -1.0, c, d, d}, + Vertex_t{-1.0, 1.0, -1.0, c, 0, d}, + // Face 1 + Vertex_t{-1.0, -1.0, 1.0, c, 0, 0}, + Vertex_t{ 1.0, -1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{-1.0, 1.0, 1.0, c, 0, d}, + // Face 2 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, 1.0, -1.0, c, d, 0}, + Vertex_t{-1.0, 1.0, 1.0, c, d, d}, + Vertex_t{-1.0, -1.0, 1.0, c, 0, d}, + // Face 3 + Vertex_t{ 1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{ 1.0, 1.0, -1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, -1.0, 1.0, c, 0, d}, + // Face 4 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, -1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, -1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, -1.0, -1.0, c, 0, d}, + // Face 5 + Vertex_t{-1.0, 1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, 1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, 1.0, -1.0, c, 0, d}, + ] + + mut vert_buffer_desc := C.sg_buffer_desc{label: c'cube-vertices'} + unsafe { C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc)) } + vert_buffer_desc.size = size_t(vertices.len * int(sizeof(Vertex_t))) + vert_buffer_desc.data = C.sg_range{ + ptr: vertices.data + size: size_t(vertices.len * int(sizeof(Vertex_t))) + } + vert_buffer_desc.@type = .vertexbuffer + vbuf := gfx.make_buffer(&vert_buffer_desc) + + /* create an index buffer for the cube */ + indices := [ + 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 +*/ + ] + + mut index_buffer_desc := C.sg_buffer_desc{label: c'cube-indices'} + unsafe { C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc)) } + index_buffer_desc.size = size_t(indices.len * int(sizeof(u16))) + index_buffer_desc.data = C.sg_range{ + ptr: indices.data + size: size_t(indices.len * int(sizeof(u16))) + } + index_buffer_desc.@type = .indexbuffer + ibuf := gfx.make_buffer(&index_buffer_desc) + + // create shader + shader := gfx.make_shader(C.rt_march_shader_desc(C.sg_query_backend())) + + mut pipdesc := C.sg_pipeline_desc{} + unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) } + pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t)) + + // 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 + + pipdesc.shader = shader + pipdesc.index_type = .uint16 + + pipdesc.depth = C.sg_depth_state{ + write_enabled: true + compare: gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL) + } + pipdesc.cull_mode = .back + pipdesc.label = 'glsl_shader pipeline'.str + + mut bind := C.sg_bindings{} + unsafe { C.memset(&bind, 0, sizeof(bind)) } + bind.vertex_buffers[0] = vbuf + bind.index_buffer = ibuf + bind.fs_images[C.SLOT_tex] = app.texture + app.bind['march'] = bind + + app.pipe['march'] = gfx.make_pipeline(&pipdesc) + + println('GLSL March init DONE!') +} + +// putty shader init +fn init_cube_glsl_p(mut app App) { + // cube vertex buffer + // d := u16(32767) // for compatibility with D3D11, 32767 stand for 1 + d := f32(1.0) + c := u32(0xFFFFFF_FF) // color RGBA8 + vertices := [ + // Face 0 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{ 1.0, -1.0, -1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, -1.0, c, d, d}, + Vertex_t{-1.0, 1.0, -1.0, c, 0, d}, + // Face 1 + Vertex_t{-1.0, -1.0, 1.0, c, 0, 0}, + Vertex_t{ 1.0, -1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{-1.0, 1.0, 1.0, c, 0, d}, + // Face 2 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, 1.0, -1.0, c, d, 0}, + Vertex_t{-1.0, 1.0, 1.0, c, d, d}, + Vertex_t{-1.0, -1.0, 1.0, c, 0, d}, + // Face 3 + Vertex_t{ 1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{ 1.0, 1.0, -1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, -1.0, 1.0, c, 0, d}, + // Face 4 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, -1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, -1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, -1.0, -1.0, c, 0, d}, + // Face 5 + Vertex_t{-1.0, 1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, 1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, 1.0, -1.0, c, 0, d}, + ] + + mut vert_buffer_desc := C.sg_buffer_desc{label: c'cube-vertices'} + unsafe { C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc)) } + vert_buffer_desc.size = size_t(vertices.len * int(sizeof(Vertex_t))) + vert_buffer_desc.data = C.sg_range{ + ptr: vertices.data + size: size_t(vertices.len * int(sizeof(Vertex_t))) + } + vert_buffer_desc.@type = .vertexbuffer + vbuf := gfx.make_buffer(&vert_buffer_desc) + + /* create an index buffer for the cube */ + indices := [ +/* + 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 + + ] + + mut index_buffer_desc := C.sg_buffer_desc{label: c'cube-indices'} + unsafe { C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc)) } + index_buffer_desc.size = size_t(indices.len * int(sizeof(u16))) + index_buffer_desc.data = C.sg_range{ + ptr: indices.data + size: size_t(indices.len * int(sizeof(u16))) + } + index_buffer_desc.@type = .indexbuffer + ibuf := gfx.make_buffer(&index_buffer_desc) + + // create shader + shader := gfx.make_shader(C.rt_puppy_shader_desc(C.sg_query_backend())) + + mut pipdesc := C.sg_pipeline_desc{} + unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) } + pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t)) + + // 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 + + pipdesc.shader = shader + pipdesc.index_type = .uint16 + + pipdesc.depth = C.sg_depth_state{ + write_enabled: true + compare: gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL) + } + pipdesc.cull_mode = .back + + pipdesc.label = 'glsl_shader pipeline'.str + + mut bind := C.sg_bindings{} + unsafe { C.memset(&bind, 0, sizeof(bind)) } + bind.vertex_buffers[0] = vbuf + bind.index_buffer = ibuf + bind.fs_images[C.SLOT_tex] = app.texture + app.bind['puppy'] = bind + + app.pipe['puppy'] = gfx.make_pipeline(&pipdesc) + + println('GLSL Puppy init DONE!') +} + +[inline] +fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 { + 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)) + 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 + scale_m := m4.scale(vec4(in_scale, in_scale, in_scale, 1)) + + res := (scale_m * model) * view_proj + return res +} + +// march triangles draw +fn draw_cube_glsl_m(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) + + rot := [f32(app.mouse_y), f32(app.mouse_x)] + tr_matrix := calc_tr_matrices(dw, dh, rot[0], rot[1], 2.3) + + gfx.apply_pipeline(app.pipe['march']) + gfx.apply_bindings(app.bind['march']) + + // Uniforms + // *** vertex shadeer uniforms *** + // passing the view matrix as uniform + // res is a 4x4 matrix of f32 thus: 4*16 byte of size + vs_uniforms_range := C.sg_range{ + ptr: &tr_matrix + size: size_t(4 * 16) + } + gfx.apply_uniforms(C.SG_SHADERSTAGE_VS, C.SLOT_vs_params_m, &vs_uniforms_range) + + // *** fragment shader uniforms *** + time_ticks := f32(time.ticks() - app.ticks) / 1000 + mut tmp_fs_params := [ + f32(ws.width), + 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.frame_count, // frame count + 0, + 0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h + ]! + fs_uniforms_range := C.sg_range{ + ptr: unsafe { &tmp_fs_params } + size: size_t(sizeof(tmp_fs_params)) + } + gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params_p, &fs_uniforms_range) + + // 3 vertices for triangle * 2 triangles per face * 6 faces = 36 vertices to draw + gfx.draw(0, (3 * 2) * 3, 1) +} + +// puppy triangles draw +fn draw_cube_glsl_p(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) + + rot := [f32(app.mouse_y), f32(app.mouse_x)] + tr_matrix := calc_tr_matrices(dw, dh, rot[0], rot[1], 2.3) + + // apply the pipline and bindings + gfx.apply_pipeline(app.pipe['puppy']) + gfx.apply_bindings(app.bind['puppy']) + + // Uniforms + // *** vertex shadeer uniforms *** + // passing the view matrix as uniform + // res is a 4x4 matrix of f32 thus: 4*16 byte of size + vs_uniforms_range := C.sg_range{ + ptr: &tr_matrix + size: size_t(4 * 16) + } + gfx.apply_uniforms(C.SG_SHADERSTAGE_VS, C.SLOT_vs_params_p, &vs_uniforms_range) + + // *** fragment shader uniforms *** + time_ticks := f32(time.ticks() - app.ticks) / 1000 + mut tmp_fs_params := [ + f32(ws.width), + 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.frame_count, // frame count + 0, + 0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h + ]! + fs_uniforms_range := C.sg_range{ + ptr: unsafe { &tmp_fs_params } + size: size_t(sizeof(tmp_fs_params)) + } + gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params_p, &fs_uniforms_range) + + // 3 vertices for triangle * 2 triangles per face * 6 faces = 36 vertices to draw + gfx.draw(0, (3 * 2) * 3, 1) +} + +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) + + gfx.apply_viewport(0, 0, ws.width, ws.height, true) +} + +fn draw_end_glsl(app App) { + gfx.end_pass() + gfx.commit() +} + +fn frame(mut app App) { + ws := gg.window_size_real_pixels() + + // clear + mut color_action := C.sg_color_attachment_action{ + action: gfx.Action(C.SG_ACTION_CLEAR) + value: C.sg_color{ + r: 0.0 + g: 0.0 + b: 0.0 + a: 1.0 + } + } + mut pass_action := C.sg_pass_action{} + pass_action.colors[0] = color_action + gfx.begin_default_pass(&pass_action, ws.width, ws.height) + + /* + // glsl cube + if app.frame_count % 1 == 1{ + draw_cube_glsl_m(app) + } else { + draw_cube_glsl_p(app) + } + */ + draw_start_glsl(app) + draw_cube_glsl_m(app) + draw_cube_glsl_p(app) + draw_end_glsl(app) + app.frame_count++ +} + +/****************************************************************************** +* Init / Cleanup +******************************************************************************/ +fn my_init(mut app App) { + // set max vertices, + // for a large number of the same type of object it is better use the instances!! + desc := sapp.create_desc() + gfx.setup(&desc) + sgl_desc := C.sgl_desc_t{ + max_vertices: 50 * 65536 + } + sgl.setup(&sgl_desc) + + // create chessboard texture 256*256 RGBA + w := 256 + h := 256 + sz := w * h * 4 + tmp_txt := unsafe { malloc(sz) } + mut i := 0 + for i < sz { + unsafe { + y := (i >> 0x8) >> 5 // 8 cell + x := (i & 0xFF) >> 5 // 8 cell + // upper left corner + if x == 0 && y == 0 { + tmp_txt[i + 0] = byte(0xFF) + tmp_txt[i + 1] = byte(0) + tmp_txt[i + 2] = byte(0) + tmp_txt[i + 3] = byte(0xFF) + } + // low right corner + else if x == 7 && y == 7 { + tmp_txt[i + 0] = byte(0) + tmp_txt[i + 1] = byte(0xFF) + tmp_txt[i + 2] = byte(0) + tmp_txt[i + 3] = byte(0xFF) + } else { + col := if ((x + y) & 1) == 1 { 0xFF } else { 128 } + tmp_txt[i + 0] = byte(col) // red + tmp_txt[i + 1] = byte(col) // green + tmp_txt[i + 2] = byte(col) // blue + tmp_txt[i + 3] = byte(0xFF) // alpha + } + i += 4 + } + } + app.texture = create_texture(w, h, tmp_txt) + unsafe { free(tmp_txt) } + + // glsl + init_cube_glsl_m(mut app) + init_cube_glsl_p(mut app) + app.init_flag = true +} + +fn cleanup(mut app App) { + gfx.shutdown() +} + +/****************************************************************************** +* events handling +******************************************************************************/ +fn my_event_manager(mut ev gg.Event, mut app App) { + if ev.typ == .mouse_down { + app.mouse_down = true + } + if ev.typ == .mouse_up { + app.mouse_down = false + } + if app.mouse_down == true && ev.typ == .mouse_move { + app.mouse_x = int(ev.mouse_x) + app.mouse_y = int(ev.mouse_y) + } + if ev.typ == .touches_began || ev.typ == .touches_moved { + if ev.num_touches > 0 { + touch_point := ev.touches[0] + app.mouse_x = int(touch_point.pos_x) + app.mouse_y = int(touch_point.pos_y) + } + } +} + +/****************************************************************************** +* Main +******************************************************************************/ +[console] // is needed for easier diagnostics on windows +fn main() { + // App init + mut app := &App{ + gg: 0 + } + + app.gg = gg.new_context( + width: win_width + height: win_height + create_window: true + window_title: '3D Dual shader Cube - click and rotate with the mouse' + user_data: app + bg_color: bg_color + frame_fn: frame + init_fn: my_init + cleanup_fn: cleanup + event_fn: my_event_manager + ) + + app.ticks = time.ticks() + app.gg.run() +} diff --git a/v_windows/v/old/examples/sokol/04_multi_shader_glsl/rt_glsl_march.glsl b/v_windows/v/old/examples/sokol/04_multi_shader_glsl/rt_glsl_march.glsl new file mode 100644 index 0000000..ed76e25 --- /dev/null +++ b/v_windows/v/old/examples/sokol/04_multi_shader_glsl/rt_glsl_march.glsl @@ -0,0 +1,695 @@ +//------------------------------------------------------------------------------ +// Shader code for texcube-sapp sample. +// +// NOTE: This source file also uses the '#pragma sokol' form of the +// custom tags. +//------------------------------------------------------------------------------ +//#pragma sokol @ctype mat4 hmm_mat4 + +#pragma sokol @vs vs_m +uniform vs_params_m { + mat4 mvp; +}; + +in vec4 pos; +in vec4 color0; +in vec2 texcoord0; + +out vec4 color; +out vec2 uv; + +void main() { + gl_Position = mvp * pos; + color = color0; + uv = texcoord0; +} +#pragma sokol @end + +#pragma sokol @fs fs_m +uniform sampler2D tex; +uniform fs_params_m { + vec2 iResolution; + vec2 iMouse; + float iTime; + float iFrame; +}; + +in vec4 color; +in vec2 uv; +out vec4 frag_color; + +// change to 0 to 4 to increment the AntiAliasing, +// increase AA will SLOW the rendering!! +#define AA 1 + +//********************************************************* +// Ray Marching +// original code from: https://www.shadertoy.com/view/Xds3zN +//********************************************************* +// The MIT License +// Copyright © 2013 Inigo Quilez +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +// A list of useful distance function to simple primitives. All +// these functions (except for ellipsoid) return an exact +// euclidean distance, meaning they produce a better SDF than +// what you'd get if you were constructing them from boolean +// operations. +// +// More info here: +// +// https://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm + +//------------------------------------------------------------------ +float dot2( in vec2 v ) { return dot(v,v); } +float dot2( in vec3 v ) { return dot(v,v); } +float ndot( in vec2 a, in vec2 b ) { return a.x*b.x - a.y*b.y; } + +float sdPlane( vec3 p ) +{ + return p.y; +} + +float sdSphere( vec3 p, float s ) +{ + return length(p)-s; +} + +float sdBox( vec3 p, vec3 b ) +{ + vec3 d = abs(p) - b; + return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)); +} + +float sdBoundingBox( vec3 p, vec3 b, float e ) +{ + p = abs(p )-b; + vec3 q = abs(p+e)-e; + + return min(min( + length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), + length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), + length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); +} +float sdEllipsoid( in vec3 p, in vec3 r ) // approximated +{ + float k0 = length(p/r); + float k1 = length(p/(r*r)); + return k0*(k0-1.0)/k1; +} + +float sdTorus( vec3 p, vec2 t ) +{ + return length( vec2(length(p.xz)-t.x,p.y) )-t.y; +} + +float sdCappedTorus(in vec3 p, in vec2 sc, in float ra, in float rb) +{ + p.x = abs(p.x); + float k = (sc.y*p.x>sc.x*p.y) ? dot(p.xy,sc) : length(p.xy); + return sqrt( dot(p,p) + ra*ra - 2.0*ra*k ) - rb; +} + +float sdHexPrism( vec3 p, vec2 h ) +{ + vec3 q = abs(p); + + const vec3 k = vec3(-0.8660254, 0.5, 0.57735); + p = abs(p); + p.xy -= 2.0*min(dot(k.xy, p.xy), 0.0)*k.xy; + vec2 d = vec2( + length(p.xy - vec2(clamp(p.x, -k.z*h.x, k.z*h.x), h.x))*sign(p.y - h.x), + p.z-h.y ); + return min(max(d.x,d.y),0.0) + length(max(d,0.0)); +} + +float sdOctogonPrism( in vec3 p, in float r, float h ) +{ + const vec3 k = vec3(-0.9238795325, // sqrt(2+sqrt(2))/2 + 0.3826834323, // sqrt(2-sqrt(2))/2 + 0.4142135623 ); // sqrt(2)-1 + // reflections + p = abs(p); + p.xy -= 2.0*min(dot(vec2( k.x,k.y),p.xy),0.0)*vec2( k.x,k.y); + p.xy -= 2.0*min(dot(vec2(-k.x,k.y),p.xy),0.0)*vec2(-k.x,k.y); + // polygon side + p.xy -= vec2(clamp(p.x, -k.z*r, k.z*r), r); + vec2 d = vec2( length(p.xy)*sign(p.y), p.z-h ); + return min(max(d.x,d.y),0.0) + length(max(d,0.0)); +} + +float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) +{ + vec3 pa = p-a, ba = b-a; + float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); + return length( pa - ba*h ) - r; +} + +float sdRoundCone( in vec3 p, in float r1, float r2, float h ) +{ + vec2 q = vec2( length(p.xz), p.y ); + + float b = (r1-r2)/h; + float a = sqrt(1.0-b*b); + float k = dot(q,vec2(-b,a)); + + if( k < 0.0 ) return length(q) - r1; + if( k > a*h ) return length(q-vec2(0.0,h)) - r2; + + return dot(q, vec2(a,b) ) - r1; +} + +float sdRoundCone(vec3 p, vec3 a, vec3 b, float r1, float r2) +{ + // sampling independent computations (only depend on shape) + vec3 ba = b - a; + float l2 = dot(ba,ba); + float rr = r1 - r2; + float a2 = l2 - rr*rr; + float il2 = 1.0/l2; + + // sampling dependant computations + vec3 pa = p - a; + float y = dot(pa,ba); + float z = y - l2; + float x2 = dot2( pa*l2 - ba*y ); + float y2 = y*y*l2; + float z2 = z*z*l2; + + // single square root! + float k = sign(rr)*rr*rr*x2; + if( sign(z)*a2*z2 > k ) return sqrt(x2 + z2) *il2 - r2; + if( sign(y)*a2*y2 < k ) return sqrt(x2 + y2) *il2 - r1; + return (sqrt(x2*a2*il2)+y*rr)*il2 - r1; +} + +float sdTriPrism( vec3 p, vec2 h ) +{ + const float k = sqrt(3.0); + h.x *= 0.5*k; + p.xy /= h.x; + p.x = abs(p.x) - 1.0; + p.y = p.y + 1.0/k; + if( p.x+k*p.y>0.0 ) p.xy=vec2(p.x-k*p.y,-k*p.x-p.y)/2.0; + p.x -= clamp( p.x, -2.0, 0.0 ); + float d1 = length(p.xy)*sign(-p.y)*h.x; + float d2 = abs(p.z)-h.y; + return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.); +} + +// vertical +float sdCylinder( vec3 p, vec2 h ) +{ + vec2 d = abs(vec2(length(p.xz),p.y)) - h; + return min(max(d.x,d.y),0.0) + length(max(d,0.0)); +} + +// arbitrary orientation +float sdCylinder(vec3 p, vec3 a, vec3 b, float r) +{ + vec3 pa = p - a; + vec3 ba = b - a; + float baba = dot(ba,ba); + float paba = dot(pa,ba); + + float x = length(pa*baba-ba*paba) - r*baba; + float y = abs(paba-baba*0.5)-baba*0.5; + float x2 = x*x; + float y2 = y*y*baba; + float d = (max(x,y)<0.0)?-min(x2,y2):(((x>0.0)?x2:0.0)+((y>0.0)?y2:0.0)); + return sign(d)*sqrt(abs(d))/baba; +} + +// vertical +float sdCone( in vec3 p, in vec2 c, float h ) +{ + vec2 q = h*vec2(c.x,-c.y)/c.y; + vec2 w = vec2( length(p.xz), p.y ); + + vec2 a = w - q*clamp( dot(w,q)/dot(q,q), 0.0, 1.0 ); + vec2 b = w - q*vec2( clamp( w.x/q.x, 0.0, 1.0 ), 1.0 ); + float k = sign( q.y ); + float d = min(dot( a, a ),dot(b, b)); + float s = max( k*(w.x*q.y-w.y*q.x),k*(w.y-q.y) ); + return sqrt(d)*sign(s); +} + +float sdCappedCone( in vec3 p, in float h, in float r1, in float r2 ) +{ + vec2 q = vec2( length(p.xz), p.y ); + + vec2 k1 = vec2(r2,h); + vec2 k2 = vec2(r2-r1,2.0*h); + vec2 ca = vec2(q.x-min(q.x,(q.y < 0.0)?r1:r2), abs(q.y)-h); + vec2 cb = q - k1 + k2*clamp( dot(k1-q,k2)/dot2(k2), 0.0, 1.0 ); + float s = (cb.x < 0.0 && ca.y < 0.0) ? -1.0 : 1.0; + return s*sqrt( min(dot2(ca),dot2(cb)) ); +} + +float sdCappedCone(vec3 p, vec3 a, vec3 b, float ra, float rb) +{ + float rba = rb-ra; + float baba = dot(b-a,b-a); + float papa = dot(p-a,p-a); + float paba = dot(p-a,b-a)/baba; + + float x = sqrt( papa - paba*paba*baba ); + + float cax = max(0.0,x-((paba<0.5)?ra:rb)); + float cay = abs(paba-0.5)-0.5; + + float k = rba*rba + baba; + float f = clamp( (rba*(x-ra)+paba*baba)/k, 0.0, 1.0 ); + + float cbx = x-ra - f*rba; + float cby = paba - f; + + float s = (cbx < 0.0 && cay < 0.0) ? -1.0 : 1.0; + + return s*sqrt( min(cax*cax + cay*cay*baba, + cbx*cbx + cby*cby*baba) ); +} + +// c is the sin/cos of the desired cone angle +float sdSolidAngle(vec3 pos, vec2 c, float ra) +{ + vec2 p = vec2( length(pos.xz), pos.y ); + float l = length(p) - ra; + float m = length(p - c*clamp(dot(p,c),0.0,ra) ); + return max(l,m*sign(c.y*p.x-c.x*p.y)); +} + +float sdOctahedron(vec3 p, float s) +{ + p = abs(p); + float m = p.x + p.y + p.z - s; + +// exact distance +#if 0 + vec3 o = min(3.0*p - m, 0.0); + o = max(6.0*p - m*2.0 - o*3.0 + (o.x+o.y+o.z), 0.0); + return length(p - s*o/(o.x+o.y+o.z)); +#endif + +// exact distance +#if 1 + vec3 q; + if( 3.0*p.x < m ) q = p.xyz; + else if( 3.0*p.y < m ) q = p.yzx; + else if( 3.0*p.z < m ) q = p.zxy; + else return m*0.57735027; + float k = clamp(0.5*(q.z-q.y+s),0.0,s); + return length(vec3(q.x,q.y-s+k,q.z-k)); +#endif + +// bound, not exact +#if 0 + return m*0.57735027; +#endif +} + +float sdPyramid( in vec3 p, in float h ) +{ + float m2 = h*h + 0.25; + + // symmetry + p.xz = abs(p.xz); + p.xz = (p.z>p.x) ? p.zx : p.xz; + p.xz -= 0.5; + + // project into face plane (2D) + vec3 q = vec3( p.z, h*p.y - 0.5*p.x, h*p.x + 0.5*p.y); + + float s = max(-q.x,0.0); + float t = clamp( (q.y-0.5*p.z)/(m2+0.25), 0.0, 1.0 ); + + float a = m2*(q.x+s)*(q.x+s) + q.y*q.y; + float b = m2*(q.x+0.5*t)*(q.x+0.5*t) + (q.y-m2*t)*(q.y-m2*t); + + float d2 = min(q.y,-q.x*m2-q.y*0.5) > 0.0 ? 0.0 : min(a,b); + + // recover 3D and scale, and add sign + return sqrt( (d2+q.z*q.z)/m2 ) * sign(max(q.z,-p.y)); +} + +// la,lb=semi axis, h=height, ra=corner +float sdRhombus(vec3 p, float la, float lb, float h, float ra) +{ + p = abs(p); + vec2 b = vec2(la,lb); + float f = clamp( (ndot(b,b-2.0*p.xz))/dot(b,b), -1.0, 1.0 ); + vec2 q = vec2(length(p.xz-0.5*b*vec2(1.0-f,1.0+f))*sign(p.x*b.y+p.z*b.x-b.x*b.y)-ra, p.y-h); + return min(max(q.x,q.y),0.0) + length(max(q,0.0)); +} + +//------------------------------------------------------------------ + +vec2 opU( vec2 d1, vec2 d2 ) +{ + return (d1.x0.0 ) + { + tmax = min( tmax, tp1 ); + res = vec2( tp1, 1.0 ); + } + //else return res; + + // raymarch primitives + vec2 tb = iBox( ro-vec3(0.0,0.4,-0.5), rd, vec3(2.5,0.41,3.0) ); + if( tb.x0.0 && tb.x0.0 ) tmax = min( tmax, tp ); + + float res = 1.0; + float t = mint; + for( int i=ZERO; i<24; i++ ) + { + float h = map( ro + rd*t ).x; + float s = clamp(8.0*h/t,0.0,1.0); + res = min( res, s*s*(3.0-2.0*s) ); + t += clamp( h, 0.02, 0.2 ); + if( res<0.004 || t>tmax ) break; + } + return clamp( res, 0.0, 1.0 ); +} + +// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm +vec3 calcNormal( in vec3 pos ) +{ +#if 0 + vec2 e = vec2(1.0,-1.0)*0.5773*0.0005; + return normalize( e.xyy*map( pos + e.xyy ).x + + e.yyx*map( pos + e.yyx ).x + + e.yxy*map( pos + e.yxy ).x + + e.xxx*map( pos + e.xxx ).x ); +#else + // inspired by tdhooper and klems - a way to prevent the compiler from inlining map() 4 times + vec3 n = vec3(0.0); + for( int i=ZERO; i<4; i++ ) + { + vec3 e = 0.5773*(2.0*vec3((((i+3)>>1)&1),((i>>1)&1),(i&1))-1.0); + n += e*map(pos+0.0005*e).x; + //if( n.x+n.y+n.z>100.0 ) break; + } + return normalize(n); +#endif +} + +float calcAO( in vec3 pos, in vec3 nor ) +{ + float occ = 0.0; + float sca = 1.0; + for( int i=ZERO; i<5; i++ ) + { + float h = 0.01 + 0.12*float(i)/4.0; + float d = map( pos + h*nor ).x; + occ += (h-d)*sca; + sca *= 0.95; + if( occ>0.35 ) break; + } + return clamp( 1.0 - 3.0*occ, 0.0, 1.0 ) * (0.5+0.5*nor.y); +} + +// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm +float checkersGradBox( in vec2 p, in vec2 dpdx, in vec2 dpdy ) +{ + // filter kernel + vec2 w = abs(dpdx)+abs(dpdy) + 0.001; + // analytical integral (box filter) + vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w; + // xor pattern + return 0.5 - 0.5*i.x*i.y; +} + +vec3 render( in vec3 ro, in vec3 rd, in vec3 rdx, in vec3 rdy ) +{ + // background + vec3 col = vec3(0.7, 0.7, 0.9) - max(rd.y,0.0)*0.3; + + // raycast scene + vec2 res = raycast(ro,rd); + float t = res.x; + float m = res.y; + if( m>-0.5 ) + { + vec3 pos = ro + t*rd; + vec3 nor = (m<1.5) ? vec3(0.0,1.0,0.0) : calcNormal( pos ); + vec3 ref = reflect( rd, nor ); + + // material + col = 0.2 + 0.2*sin( m*2.0 + vec3(0.0,1.0,2.0) ); + float ks = 1.0; + + if( m<1.5 ) + { + // project pixel footprint into the plane + vec3 dpdx = ro.y*(rd/rd.y-rdx/rdx.y); + vec3 dpdy = ro.y*(rd/rd.y-rdy/rdy.y); + + float f = checkersGradBox( 3.0*pos.xz, 3.0*dpdx.xz, 3.0*dpdy.xz ); + col = 0.15 + f*vec3(0.05); + ks = 0.4; + } + + // lighting + float occ = calcAO( pos, nor ); + + vec3 lin = vec3(0.0); + + // sun + { + vec3 lig = normalize( vec3(-0.5, 0.4, -0.6) ); + vec3 hal = normalize( lig-rd ); + float dif = clamp( dot( nor, lig ), 0.0, 1.0 ); + //if( dif>0.0001 ) + dif *= calcSoftshadow( pos, lig, 0.02, 2.5 ); + float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),16.0); + spe *= dif; + spe *= 0.04+0.96*pow(clamp(1.0-dot(hal,lig),0.0,1.0),5.0); + lin += col*2.20*dif*vec3(1.30,1.00,0.70); + lin += 5.00*spe*vec3(1.30,1.00,0.70)*ks; + } + // sky + { + float dif = sqrt(clamp( 0.5+0.5*nor.y, 0.0, 1.0 )); + dif *= occ; + float spe = smoothstep( -0.2, 0.2, ref.y ); + spe *= dif; + spe *= 0.04+0.96*pow(clamp(1.0+dot(nor,rd),0.0,1.0), 5.0 ); + //if( spe>0.001 ) + spe *= calcSoftshadow( pos, ref, 0.02, 2.5 ); + lin += col*0.60*dif*vec3(0.40,0.60,1.15); + lin += 2.00*spe*vec3(0.40,0.60,1.30)*ks; + } + // back + { + float dif = clamp( dot( nor, normalize(vec3(0.5,0.0,0.6))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0); + dif *= occ; + lin += col*0.55*dif*vec3(0.25,0.25,0.25); + } + // sss + { + float dif = pow(clamp(1.0+dot(nor,rd),0.0,1.0),2.0); + dif *= occ; + lin += col*0.25*dif*vec3(1.00,1.00,1.00); + } + + col = lin; + + col = mix( col, vec3(0.7,0.7,0.9), 1.0-exp( -0.0001*t*t*t ) ); + } + + return vec3( clamp(col,0.0,1.0) ); +} + +mat3 setCamera( in vec3 ro, in vec3 ta, float cr ) +{ + vec3 cw = normalize(ta-ro); + vec3 cp = vec3(sin(cr), cos(cr),0.0); + vec3 cu = normalize( cross(cw,cp) ); + vec3 cv = ( cross(cu,cw) ); + return mat3( cu, cv, cw ); +} + +vec4 mainImage( vec2 fragCoord ) +{ + vec2 mo = iMouse.xy/iResolution.xy; + float time = 32.0 + iTime*1.5; + + // camera + vec3 ta = vec3( 0.5, -0.5, -0.6 ); + vec3 ro = ta + vec3( 4.5*cos(0.1*time + 7.0*mo.x), 1.3 + 2.0*mo.y, 4.5*sin(0.1*time + 7.0*mo.x) ); + // camera-to-world transformation + mat3 ca = setCamera( ro, ta, 0.0 ); + + vec3 tot = vec3(0.0); +#if AA>1 + for( int m=ZERO; m1 + } + tot /= float(AA*AA); +#endif + + //fragColor = vec4( tot, 1.0 ); + return vec4( tot, 1.0 ); +} + +//********************************************************* +// END Ray Marching +//********************************************************* + +void main() { + vec4 c = color; + vec4 txt = texture(tex, uv); + c = txt * c; + vec2 uv1 = uv * iResolution; + vec4 col_ray = mainImage(uv1); + + // use this to mix the chessboart texture with the ray marching + //frag_color = clamp(c*iMouse.y/512.0,0.0,1.0) * col_ray ; + + frag_color = c*0.00001 + col_ray ; +} + +#pragma sokol @end + +#pragma sokol @program rt_march vs_m fs_m diff --git a/v_windows/v/old/examples/sokol/04_multi_shader_glsl/rt_glsl_puppy.glsl b/v_windows/v/old/examples/sokol/04_multi_shader_glsl/rt_glsl_puppy.glsl new file mode 100644 index 0000000..bea60d5 --- /dev/null +++ b/v_windows/v/old/examples/sokol/04_multi_shader_glsl/rt_glsl_puppy.glsl @@ -0,0 +1,568 @@ +//------------------------------------------------------------------------------ +// Shader code for texcube-sapp sample. +// +// NOTE: This source file also uses the '#pragma sokol' form of the +// custom tags. +//------------------------------------------------------------------------------ +//#pragma sokol @ctype mat4 hmm_mat4 + +#pragma sokol @vs vs_p +uniform vs_params_p { + mat4 mvp; +}; + +in vec4 pos; +in vec4 color0; +in vec2 texcoord0; + +out vec4 color; +out vec2 uv; + +void main() { + gl_Position = mvp * pos; + color = color0; + uv = texcoord0; +} +#pragma sokol @end + +#pragma sokol @fs fs_p +uniform sampler2D tex; +uniform fs_params_p { + vec2 iResolution; + vec2 iMouse; + float iTime; + float iFrame; +}; + +in vec4 color; +in vec2 uv; +out vec4 frag_color; + +// change to 0 to 4 to increment the AntiAliasing, +// increase AA will SLOW the rendering!! +#define AA 1 + +//********************************************************* +// Ray Marching +// original code from: https://www.shadertoy.com/view/Xds3zN +//********************************************************* +// Created by inigo quilez - iq/2019 +// I share this piece (art and code) here in Shadertoy and through its Public API, only for educational purposes. +// You cannot use, share or host this piece or modifications of it as part of your own commercial or non-commercial product, website or project. +// You can share a link to it or an unmodified screenshot of it provided you attribute "by Inigo Quilez, @iquilezles and iquilezles.org". +// If you are a teacher, lecturer, educator or similar and these conditions are too restrictive for your needs, please contact me and we'll work it out. + + +// An animation test - a happy and blobby creature +// jumping and looking around. It gets off-model very +// often, but it looks good enough I think. +// +// Making-of with math/shader/art explanations (6 hours +// long): https://www.youtube.com/watch?v=Cfe5UQ-1L9Q +// +// Video capture: https://www.youtube.com/watch?v=s_UOFo2IULQ +// +// Buy a metal print here: https://www.redbubble.com/i/metal-print/Happy-Jumping-by-InigoQuilez/43594745.0JXQP + +//------------------------------------------------------------------ + + +// http://iquilezles.org/www/articles/smin/smin.htm +float smin( float a, float b, float k ) +{ + float h = max(k-abs(a-b),0.0); + return min(a, b) - h*h*0.25/k; +} + +// http://iquilezles.org/www/articles/smin/smin.htm +vec2 smin( vec2 a, vec2 b, float k ) +{ + float h = clamp( 0.5+0.5*(b.x-a.x)/k, 0.0, 1.0 ); + return mix( b, a, h ) - k*h*(1.0-h); +} + +// http://iquilezles.org/www/articles/smin/smin.htm +float smax( float a, float b, float k ) +{ + float h = max(k-abs(a-b),0.0); + return max(a, b) + h*h*0.25/k; +} + +// http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm +float sdSphere( vec3 p, float s ) +{ + return length(p)-s; +} + +// http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm +float sdEllipsoid( in vec3 p, in vec3 r ) // approximated +{ + float k0 = length(p/r); + float k1 = length(p/(r*r)); + return k0*(k0-1.0)/k1; +} + +vec2 sdStick(vec3 p, vec3 a, vec3 b, float r1, float r2) // approximated +{ + vec3 pa = p-a, ba = b-a; + float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); + return vec2( length( pa - ba*h ) - mix(r1,r2,h*h*(3.0-2.0*h)), h ); +} + +// http://iquilezles.org/www/articles/smin/smin.htm +vec4 opU( vec4 d1, vec4 d2 ) +{ + return (d1.x0.0 ) tmax = min( tmax, tp ); + #endif + + // raymarch scene + float t = tmin; + for( int i=0; i<256 && t0.0 ) tmax = min( tmax, tp ); + #endif + + float t = 0.02; + for( int i=0; i<50; i++ ) + { + float h = map( ro + rd*t, time ).x; + res = min( res, mix(1.0,16.0*h/t, hsha) ); + t += clamp( h, 0.05, 0.40 ); + if( res<0.005 || t>tmax ) break; + } + return clamp( res, 0.0, 1.0 ); +} + +// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm +vec3 calcNormal( in vec3 pos, float time ) +{ + +#if 0 + vec2 e = vec2(1.0,-1.0)*0.5773*0.001; + return normalize( e.xyy*map( pos + e.xyy, time ).x + + e.yyx*map( pos + e.yyx, time ).x + + e.yxy*map( pos + e.yxy, time ).x + + e.xxx*map( pos + e.xxx, time ).x ); +#else + // inspired by tdhooper and klems - a way to prevent the compiler from inlining map() 4 times + vec3 n = vec3(0.0); + for( int i=ZERO; i<4; i++ ) + { + vec3 e = 0.5773*(2.0*vec3((((i+3)>>1)&1),((i>>1)&1),(i&1))-1.0); + n += e*map(pos+0.001*e,time).x; + } + return normalize(n); +#endif +} + +float calcOcclusion( in vec3 pos, in vec3 nor, float time ) +{ + float occ = 0.0; + float sca = 1.0; + for( int i=ZERO; i<5; i++ ) + { + float h = 0.01 + 0.11*float(i)/4.0; + vec3 opos = pos + h*nor; + float d = map( opos, time ).x; + occ += (h-d)*sca; + sca *= 0.95; + } + return clamp( 1.0 - 2.0*occ, 0.0, 1.0 ); +} + +vec3 render( in vec3 ro, in vec3 rd, float time ) +{ + // sky dome + vec3 col = vec3(0.5, 0.8, 0.9) - max(rd.y,0.0)*0.5; + // sky clouds + vec2 uv = 1.5*rd.xz/rd.y; + float cl = 1.0*(sin(uv.x)+sin(uv.y)); uv *= mat2(0.8,0.6,-0.6,0.8)*2.1; + cl += 0.5*(sin(uv.x)+sin(uv.y)); + col += 0.1*(-1.0+2.0*smoothstep(-0.1,0.1,cl-0.4)); + // sky horizon + col = mix( col, vec3(0.5, 0.7, .9), exp(-10.0*max(rd.y,0.0)) ); + + + // scene geometry + vec4 res = raycast(ro,rd, time); + if( res.y>-0.5 ) + { + float t = res.x; + vec3 pos = ro + t*rd; + vec3 nor = calcNormal( pos, time ); + vec3 ref = reflect( rd, nor ); + float focc = res.w; + + // material + col = vec3(0.2); + float ks = 1.0; + + if( res.y>4.5 ) // candy + { + col = vec3(0.14,0.048,0.0); + vec2 id = floor(5.0*pos.xz+0.5); + col += 0.036*cos((id.x*11.1+id.y*37.341) + vec3(0.0,1.0,2.0) ); + col = max(col,0.0); + focc = clamp(4.0*res.z,0.0,1.0); + } + else if( res.y>3.5 ) // eyeball + { + col = vec3(0.0); + } + else if( res.y>2.5 ) // iris + { + col = vec3(0.4); + } + else if( res.y>1.5 ) // body + { + col = mix(vec3(0.144,0.09,0.0036),vec3(0.36,0.1,0.04),res.z*res.z); + col = mix(col,vec3(0.14,0.09,0.06)*2.0, (1.0-res.z)*smoothstep(-0.15, 0.15, -href)); + } + else // terrain + { + // base green + col = vec3(0.05,0.09,0.02); + float f = 0.2*(-1.0+2.0*smoothstep(-0.2,0.2,sin(18.0*pos.x)+sin(18.0*pos.y)+sin(18.0*pos.z))); + col += f*vec3(0.06,0.06,0.02); + ks = 0.5 + pos.y*0.15; + + // footprints + vec2 mp = vec2(pos.x-0.5*(mod(floor(pos.z+0.5),2.0)*2.0-1.0), fract(pos.z+0.5)-0.5 ); + float mark = 1.0-smoothstep(0.1, 0.5, length(mp)); + mark *= smoothstep(0.0, 0.1, floor(time) - floor(pos.z+0.5) ); + col *= mix( vec3(1.0), vec3(0.5,0.5,0.4), mark ); + ks *= 1.0-0.5*mark; + } + + // lighting (sun, sky, bounce, back, sss) + float occ = calcOcclusion( pos, nor, time )*focc; + float fre = clamp(1.0+dot(nor,rd),0.0,1.0); + + vec3 sun_lig = normalize( vec3(0.6, 0.35, 0.5) ); + float sun_dif = clamp(dot( nor, sun_lig ), 0.0, 1.0 ); + vec3 sun_hal = normalize( sun_lig-rd ); + float sun_sha = calcSoftshadow( pos, sun_lig, time ); + float sun_spe = ks*pow(clamp(dot(nor,sun_hal),0.0,1.0),8.0)*sun_dif*(0.04+0.96*pow(clamp(1.0+dot(sun_hal,rd),0.0,1.0),5.0)); + float sky_dif = sqrt(clamp( 0.5+0.5*nor.y, 0.0, 1.0 )); + float sky_spe = ks*smoothstep( 0.0, 0.5, ref.y )*(0.04+0.96*pow(fre,4.0)); + float bou_dif = sqrt(clamp( 0.1-0.9*nor.y, 0.0, 1.0 ))*clamp(1.0-0.1*pos.y,0.0,1.0); + float bac_dif = clamp(0.1+0.9*dot( nor, normalize(vec3(-sun_lig.x,0.0,-sun_lig.z))), 0.0, 1.0 ); + float sss_dif = fre*sky_dif*(0.25+0.75*sun_dif*sun_sha); + + vec3 lin = vec3(0.0); + lin += sun_dif*vec3(8.10,6.00,4.20)*vec3(sun_sha,sun_sha*sun_sha*0.5+0.5*sun_sha,sun_sha*sun_sha); + lin += sky_dif*vec3(0.50,0.70,1.00)*occ; + lin += bou_dif*vec3(0.20,0.70,0.10)*occ; + lin += bac_dif*vec3(0.45,0.35,0.25)*occ; + lin += sss_dif*vec3(3.25,2.75,2.50)*occ; + col = col*lin; + col += sun_spe*vec3(9.90,8.10,6.30)*sun_sha; + col += sky_spe*vec3(0.20,0.30,0.65)*occ*occ; + + col = pow(col,vec3(0.8,0.9,1.0) ); + + // fog + col = mix( col, vec3(0.5,0.7,0.9), 1.0-exp( -0.0001*t*t*t ) ); + } + + return col; +} + +mat3 setCamera( in vec3 ro, in vec3 ta, float cr ) +{ + vec3 cw = normalize(ta-ro); + vec3 cp = vec3(sin(cr), cos(cr),0.0); + vec3 cu = normalize( cross(cw,cp) ); + vec3 cv = ( cross(cu,cw) ); + return mat3( cu, cv, cw ); +} + +//void mainImage( out vec4 fragColor, in vec2 fragCoord ) +vec4 mainImage( vec2 fragCoord ) +{ + vec3 tot = vec3(0.0); +#if AA>1 + for( int m=ZERO; m1 + } + tot /= float(AA*AA); +#endif + + // s-surve + tot = clamp(tot,0.0,1.0); + tot = tot*tot*(3.0-2.0*tot); + + // vignetting + vec2 q = fragCoord/iResolution.xy; + tot *= 0.5 + 0.5*pow(16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.25); + + // output + //fragColor = vec4( tot, 1.0 ); + return vec4( tot, 1.0 ); +} + +//********************************************************* +// END Ray Marching +//********************************************************* + +void main() { + vec4 c = color; + vec4 txt = texture(tex, uv); + c = txt * c; + vec2 uv1 = uv * iResolution; + vec4 col_ray = mainImage(uv1); + + // use this to mix the chessboart texture with the ray marching + //frag_color = clamp(c*iMouse.y/512.0,0.0,1.0) * col_ray ; + + frag_color = c*0.00001 + col_ray ; +} + +#pragma sokol @end + +#pragma sokol @program rt_puppy vs_p fs_p diff --git a/v_windows/v/old/examples/sokol/04_multi_shader_glsl/v.mod b/v_windows/v/old/examples/sokol/04_multi_shader_glsl/v.mod new file mode 100644 index 0000000..e69de29 diff --git a/v_windows/v/old/examples/sokol/05_instancing_glsl/rt_glsl.v b/v_windows/v/old/examples/sokol/05_instancing_glsl/rt_glsl.v new file mode 100644 index 0000000..f06a8e3 --- /dev/null +++ b/v_windows/v/old/examples/sokol/05_instancing_glsl/rt_glsl.v @@ -0,0 +1,521 @@ +/********************************************************************** +* +* Sokol 3d cube multishader demo +* +* 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. +* +* HOW TO COMPILE SHADERS: +* - download the sokol shader convertor tool from https://github.com/floooh/sokol-tools-bin +* +* - compile the .glsl shared file with: +* linux : sokol-shdc --input rt_glsl_instancing.glsl --output rt_glsl_instancing.h --slang glsl330 +* windows: sokol-shdc.exe --input rt_glsl_instancing.glsl --output rt_glsl_instancing.h --slang glsl330 +* +* --slang parameter can be: +* - glsl330: desktop GL +* - glsl100: GLES2 / WebGL +* - glsl300es: GLES3 / WebGL2 +* - hlsl4: D3D11 +* - hlsl5: D3D11 +* - metal_macos: Metal on macOS +* - metal_ios: Metal on iOS device +* - metal_sim: Metal on iOS simulator +* - wgpu: WebGPU +* +* you can have multiple platforms at the same time passing parameters like this: --slang glsl330:hlsl5:metal_macos +* for further infos have a look at the sokol shader tool docs. +* +* TODO: +* - frame counter +**********************************************************************/ +import gg +import gg.m4 +import gx +import math + +import sokol.gfx +//import sokol.sgl + +import time + +const ( + win_width = 800 + win_height = 800 + bg_color = gx.white + num_inst = 16384 +) + +struct App { +mut: + gg &gg.Context + texture C.sg_image + init_flag bool + frame_count int + + mouse_x int = -1 + mouse_y int = -1 + mouse_down bool + + // glsl + cube_pip_glsl C.sg_pipeline + cube_bind C.sg_bindings + + pipe map[string]C.sg_pipeline + bind map[string]C.sg_bindings + + // time + ticks i64 + + // instances + inst_pos [num_inst]m4.Vec4 + + // camera + camera_x f32 + camera_z f32 +} + +/****************************************************************************** +* GLSL Include and functions +******************************************************************************/ +#flag -I @VMODROOT/. +#include "rt_glsl_instancing.h" #Please use sokol-shdc to generate the necessary rt_glsl_march.h file from rt_glsl_march.glsl (see the instructions at the top of this file) +fn C.instancing_shader_desc(gfx.Backend) &C.sg_shader_desc + +/****************************************************************************** +* Texture functions +******************************************************************************/ +fn create_texture(w int, h int, buf byteptr) C.sg_image{ + sz := w * h * 4 + mut img_desc := C.sg_image_desc{ + width: w + height: h + num_mipmaps: 0 + min_filter: .linear + mag_filter: .linear + //usage: .dynamic + wrap_u: .clamp_to_edge + wrap_v: .clamp_to_edge + label: &byte(0) + d3d11_texture: 0 + } + // comment if .dynamic is enabled + img_desc.data.subimage[0][0] = C.sg_range{ + ptr: buf + size: size_t(sz) + } + + sg_img := C.sg_make_image(&img_desc) + return sg_img +} + +fn destroy_texture(sg_img C.sg_image){ + C.sg_destroy_image(sg_img) +} + +// Use only if usage: .dynamic is enabled +fn update_text_texture(sg_img C.sg_image, w int, h int, buf byteptr){ + sz := w * h * 4 + mut tmp_sbc := C.sg_image_data{} + tmp_sbc.subimage[0][0] = C.sg_range{ + ptr: buf + size: size_t(sz) + } + C.sg_update_image(sg_img, &tmp_sbc) +} + +/****************************************************************************** +* Draw functions +****************************************************************************** + Cube vertex buffer with packed vertex formats for color and texture coords. + Note that a vertex format which must be portable across all + backends must only use the normalized integer formats + (BYTE4N, UBYTE4N, SHORT2N, SHORT4N), which can be converted + to floating point formats in the vertex shader inputs. + The reason is that D3D11 cannot convert from non-normalized + formats to floating point inputs (only to integer inputs), + and WebGL2 / GLES2 don't support integer vertex shader inputs. +*/ + +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 +} + +// 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 + d := f32(1.0) + c := u32(0xFFFFFF_FF) // color RGBA8 + vertices := [ + // Face 0 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{ 1.0, -1.0, -1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, -1.0, c, d, d}, + Vertex_t{-1.0, 1.0, -1.0, c, 0, d}, + // Face 1 + Vertex_t{-1.0, -1.0, 1.0, c, 0, 0}, + Vertex_t{ 1.0, -1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{-1.0, 1.0, 1.0, c, 0, d}, + // Face 2 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, 1.0, -1.0, c, d, 0}, + Vertex_t{-1.0, 1.0, 1.0, c, d, d}, + Vertex_t{-1.0, -1.0, 1.0, c, 0, d}, + // Face 3 + Vertex_t{ 1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{ 1.0, 1.0, -1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, -1.0, 1.0, c, 0, d}, + // Face 4 + Vertex_t{-1.0, -1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, -1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, -1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, -1.0, -1.0, c, 0, d}, + // Face 5 + Vertex_t{-1.0, 1.0, -1.0, c, 0, 0}, + Vertex_t{-1.0, 1.0, 1.0, c, d, 0}, + Vertex_t{ 1.0, 1.0, 1.0, c, d, d}, + Vertex_t{ 1.0, 1.0, -1.0, c, 0, d}, + ] + + mut vert_buffer_desc := C.sg_buffer_desc{label: c'cube-vertices'} + unsafe {C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc))} + vert_buffer_desc.size = size_t(vertices.len * int(sizeof(Vertex_t))) + vert_buffer_desc.data = C.sg_range{ + ptr: vertices.data + size: size_t(vertices.len * int(sizeof(Vertex_t))) + } + vert_buffer_desc.@type = .vertexbuffer + vbuf := gfx.make_buffer(&vert_buffer_desc) + + /* create an instance buffer for the cube */ + mut inst_buffer_desc := C.sg_buffer_desc{label: c'instance-data'} + unsafe {C.memset(&inst_buffer_desc, 0, sizeof(inst_buffer_desc))} + + inst_buffer_desc.size = size_t(num_inst * int(sizeof(m4.Vec4))) + 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 */ + 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 + ] + + mut index_buffer_desc := C.sg_buffer_desc{label: c'cube-indices'} + unsafe {C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc))} + index_buffer_desc.size = size_t(indices.len * int(sizeof(u16))) + index_buffer_desc.data = C.sg_range{ + ptr: indices.data + size: size_t(indices.len * int(sizeof(u16))) + } + index_buffer_desc.@type = .indexbuffer + ibuf := gfx.make_buffer(&index_buffer_desc) + + /* create shader */ + shader := gfx.make_shader(C.instancing_shader_desc(C.sg_query_backend())) + + mut pipdesc := C.sg_pipeline_desc{} + unsafe {C.memset(&pipdesc, 0, sizeof(pipdesc))} + pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t)) + + // 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 + pipdesc.layout.attrs[C.ATTR_vs_i_color0 ].format = .ubyte4n // color as u32 + pipdesc.layout.attrs[C.ATTR_vs_i_pos ].buffer_index = 0 + pipdesc.layout.attrs[C.ATTR_vs_i_texcoord0].format = .float2 // u,v as f32 + pipdesc.layout.attrs[C.ATTR_vs_i_pos ].buffer_index = 0 + + // instancing + // the constant ATTR_vs_i_inst_pos is generated by sokol-shdc + pipdesc.layout.buffers[1].stride = int(sizeof(m4.Vec4)) + 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 + + pipdesc.shader = shader + pipdesc.index_type = .uint16 + + pipdesc.depth = C.sg_depth_state{ + write_enabled: true + compare: gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL) + } + pipdesc.cull_mode = .back + + pipdesc.label = "glsl_shader pipeline".str + + mut bind := C.sg_bindings{} + unsafe {C.memset(&bind, 0, 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!") +} + +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, 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]!}) + + model := rym * rxm + scale_m := m4.scale(m4.Vec4{e:[in_scale, in_scale, in_scale, 1]!}) + + res := (scale_m * model)* view_proj + return res +} + +// triangles draw +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) + dh := f32(ws.height / 2) + + rot := [f32(app.mouse_y), f32(app.mouse_x)] + tr_matrix := calc_tr_matrices(dw, dh, rot[0], rot[1], 2.3) + + gfx.apply_pipeline(app.pipe['inst']) + gfx.apply_bindings(app.bind['inst']) + + //*************** + // Instancing + //*************** + // passing the instancing to the vs + 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 { + 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 + // sombrero function + //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) + 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]!} + } + range := C.sg_range{ + ptr: unsafe { &app.inst_pos } + size: size_t(num_inst * int(sizeof(m4.Vec4))) + } + gfx.update_buffer(app.bind['inst'].vertex_buffers[1], &range ) + + // Uniforms + // *** vertex shadeer uniforms *** + // passing the view matrix as uniform + // res is a 4x4 matrix of f32 thus: 4*16 byte of size + vs_uniforms_range := C.sg_range{ + ptr: unsafe { &tr_matrix } + size: size_t(4 * 16) + } + gfx.apply_uniforms(C.SG_SHADERSTAGE_VS, C.SLOT_vs_params_i, &vs_uniforms_range) + +/* + // *** fragment shader uniforms *** + time_ticks := f32(time.ticks() - app.ticks) / 1000 + mut tmp_fs_params := [ + f32(ws.width), 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.frame_count, // frame count + 0,0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h + ]! + fs_uniforms_range := C.sg_range{ + ptr: unsafe { &tmp_fs_params } + size: size_t(sizeof(tmp_fs_params)) + } + gfx.apply_uniforms(C.SG_SHADERSTAGE_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){ + 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) + + gfx.apply_viewport(0, 0, ws.width, ws.height, true) +} + +fn draw_end_glsl(app App){ + gfx.end_pass() + gfx.commit() +} + +fn frame(mut app App) { + ws := gg.window_size_real_pixels() + + // clear + mut color_action := C.sg_color_attachment_action{ + action: gfx.Action(C.SG_ACTION_CLEAR) + value: C.sg_color{ + r: 0.0 + g: 0.0 + b: 0.0 + a: 1.0 + } + } + mut pass_action := C.sg_pass_action{} + pass_action.colors[0] = color_action + gfx.begin_default_pass(&pass_action, ws.width, ws.height) + + draw_start_glsl(app) + draw_cube_glsl_i(mut app) + draw_end_glsl(app) + app.frame_count++ +} + +/****************************************************************************** +* Init / Cleanup +******************************************************************************/ +fn my_init(mut app App) { + // create chessboard texture 256*256 RGBA + w := 256 + h := 256 + sz := w * h * 4 + tmp_txt := unsafe { malloc(sz) } + mut i := 0 + for i < sz { + unsafe { + y := (i >> 0x8) >> 5 // 8 cell + x := (i & 0xFF) >> 5 // 8 cell + // upper left corner + if x == 0 && y == 0 { + tmp_txt[i + 0] = byte(0xFF) + tmp_txt[i + 1] = byte(0) + tmp_txt[i + 2] = byte(0) + tmp_txt[i + 3] = byte(0xFF) + } + // low right corner + else if x == 7 && y == 7 { + tmp_txt[i + 0] = byte(0) + tmp_txt[i + 1] = byte(0xFF) + tmp_txt[i + 2] = byte(0) + tmp_txt[i + 3] = byte(0xFF) + } else { + col := if ((x + y) & 1) == 1 { 0xFF } else { 128 } + tmp_txt[i + 0] = byte(col) // red + tmp_txt[i + 1] = byte(col) // green + tmp_txt[i + 2] = byte(col) // blue + tmp_txt[i + 3] = byte(0xFF) // alpha + } + i += 4 + } + } + unsafe { + app.texture = create_texture(w, h, tmp_txt) + free(tmp_txt) + } + + // glsl + init_cube_glsl_i(mut app) + app.init_flag = true +} + +fn cleanup(mut app App) { + gfx.shutdown() +} + +/****************************************************************************** +* events handling +******************************************************************************/ +fn my_event_manager(mut ev gg.Event, mut app App) { + if ev.typ == .mouse_down{ + app.mouse_down = true + } + if ev.typ == .mouse_up{ + app.mouse_down = false + } + if app.mouse_down == true && ev.typ == .mouse_move { + app.mouse_x = int(ev.mouse_x) + app.mouse_y = int(ev.mouse_y) + } + if ev.typ == .touches_began || ev.typ == .touches_moved { + if ev.num_touches > 0 { + touch_point := ev.touches[0] + app.mouse_x = int(touch_point.pos_x) + app.mouse_y = int(touch_point.pos_y) + } + } + + // keyboard + if ev.typ == .key_down { + step := f32(1.0) + match ev.key_code { + .w { app.camera_z += step } + .s { app.camera_z -= step } + .a { app.camera_x -= step } + .d { app.camera_x += step } + else{} + } + } +} + +/****************************************************************************** +* Main +******************************************************************************/ +[console] // is needed for easier diagnostics on windows +fn main(){ + // App init + mut app := &App{ + gg: 0 + } + + app.gg = gg.new_context({ + width: win_width + height: win_height + create_window: true + window_title: 'Instancing Cube' + user_data: app + bg_color: bg_color + frame_fn: frame + init_fn: my_init + cleanup_fn: cleanup + event_fn: my_event_manager + }) + + app.ticks = time.ticks() + app.gg.run() +} diff --git a/v_windows/v/old/examples/sokol/05_instancing_glsl/rt_glsl_instancing.glsl b/v_windows/v/old/examples/sokol/05_instancing_glsl/rt_glsl_instancing.glsl new file mode 100644 index 0000000..0e780b3 --- /dev/null +++ b/v_windows/v/old/examples/sokol/05_instancing_glsl/rt_glsl_instancing.glsl @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// Shader code for texcube-sapp sample. +// +// NOTE: This source file also uses the '#pragma sokol' form of the +// custom tags. +//------------------------------------------------------------------------------ +//#pragma sokol @ctype mat4 hmm_mat4 + +#pragma sokol @vs vs_i +uniform vs_params_i { + mat4 mvp; +}; + +in vec4 pos; +in vec4 color0; +in vec2 texcoord0; + +in vec4 inst_pos; + +out vec4 color; +out vec4 color_inst; +out vec2 uv; + +const vec4 palette[10] = vec4[10]( + vec4(1,0,0,1), + vec4(0,1,0,1), + vec4(0,0,1,1), + vec4(1,1,0,1), + vec4(0,1,1,1), + vec4(1,1,1,1), + vec4(0,0,0,1), + vec4(0.2,0.2,0.2,1), + vec4(0.3,0.3,0.3,1), + vec4(0.9,0.9,0.9,1) +); + +void main() { + vec4 delta_pos = vec4(inst_pos.xyz,0); + float w = inst_pos.w; + color_inst = palette[int(w)]; + gl_Position = mvp * (pos + delta_pos); + color = color0; + uv = texcoord0/4; +} +#pragma sokol @end + +#pragma sokol @fs fs_i +uniform sampler2D tex; + +in vec4 color; +in vec4 color_inst; +in vec2 uv; +out vec4 frag_color; + +void main() { + vec4 c = color; + vec4 txt = texture(tex, uv); + c = txt * c * color_inst; + frag_color = c ; +} + +#pragma sokol @end + +#pragma sokol @program instancing vs_i fs_i diff --git a/v_windows/v/old/examples/sokol/05_instancing_glsl/v.mod b/v_windows/v/old/examples/sokol/05_instancing_glsl/v.mod new file mode 100644 index 0000000..e69de29 diff --git a/v_windows/v/old/examples/sokol/06_obj_viewer/assets/models/v.mtl b/v_windows/v/old/examples/sokol/06_obj_viewer/assets/models/v.mtl new file mode 100644 index 0000000..47d0381 --- /dev/null +++ b/v_windows/v/old/examples/sokol/06_obj_viewer/assets/models/v.mtl @@ -0,0 +1,20 @@ +# Blender MTL File: 'v-logo.blend' +# Material Count: 2 +newmtl Material.001 +Ns 96.078431 +Ka 0.2 0.2 0.2 +Kd 0.365 0.529 0.749 +Ks 0.85 0.85 0.85 +Ke 0 0 0 +Ni 1 +d 1 +illum 1 +newmtl Material.002 +Ns 96.078431 +Ka 0.1 0.1 0.1 +Kd 0.325 0.42 0.541 +Ks 0.85 0.85 0.85 +Ke 0 0 0 +Ni 1 +d 1 +illum 1 \ No newline at end of file diff --git a/v_windows/v/old/examples/sokol/06_obj_viewer/assets/models/v.obj_ b/v_windows/v/old/examples/sokol/06_obj_viewer/assets/models/v.obj_ new file mode 100644 index 0000000..d25cc0a --- /dev/null +++ b/v_windows/v/old/examples/sokol/06_obj_viewer/assets/models/v.obj_ @@ -0,0 +1,194 @@ +# Blender v2.92.0 V Logo +mtllib v.mtl +o Vleft.020_Plane.030 +v -1.379 6.295 0.625 +v -1.389 6.019 -3.177 +v -1.442 6.063 -3.177 +v -1.433 6.339 0.625 +v -4.038 6.499 0.662 +v -4.037 6.537 0.661 +v -4.047 6.261 -3.141 +v -4.048 6.222 -3.140 +v -3.900 6.637 0.658 +v -3.910 6.360 -3.144 +v -3.974 6.342 -3.142 +v -3.964 6.618 0.660 +v -4.012 6.582 0.661 +v -4.022 6.306 -3.142 +v -1.273 6.135 0.624 +v -1.283 5.859 -3.178 +v -1.321 5.933 -3.178 +v -1.311 6.209 0.624 +v 1.288 -1.652 0.018 +v 1.282 -1.628 -3.759 +v -1.510 6.387 0.625 +v -1.520 6.111 -3.177 +v -1.559 6.128 -3.176 +v -1.550 6.404 0.626 +v -1.103 -1.603 0.043 +v -1.113 -1.579 -3.759 +v -1.082 -1.597 -3.759 +v -1.072 -1.620 0.043 +v -1.127 -1.583 0.043 +v -1.137 -1.560 -3.759 +v -1.042 -1.631 0.043 +v -1.052 -1.608 -3.759 +v -1.490 6.095 -3.177 +v -1.480 6.372 0.625 +v -1.148 -1.563 0.043 +v -1.158 -1.540 -3.759 +v -1.164 -1.541 0.043 +v -1.174 -1.518 -3.759 +v -1.171 -1.528 0.043 +v -1.181 -1.504 -3.759 +v -1.347 5.974 -3.178 +v -1.337 6.250 0.624 +v -4.034 6.459 0.662 +v -4.044 6.183 -3.140 +vn 0.634 0.771 -0.057 +vn -0.999 0.034 0.000 +vn -0.278 0.958 -0.068 +vn -0.868 0.494 -0.033 +vn 0.889 0.455 -0.035 +vn 0.947 0.319 -0.012 +vn 0.399 0.914 -0.067 +vn -0.502 -0.864 -0.004 +vn 0.097 0.992 -0.072 +vn -0.630 -0.776 -0.003 +vn -0.009 -0.999 -0.006 +vn 0.564 0.822 -0.061 +vn -0.693 -0.720 -0.002 +vn -0.805 -0.592 -0.001 +vn -0.888 -0.458 -0.000 +vn 0.843 0.535 -0.041 +vn -0.993 -0.116 0.011 +vn 0.731 0.680 -0.051 +vn -0.938 -0.344 0.013 +vn 0.450 0.890 -0.065 +vn -0.604 0.794 -0.056 +vn 0.005 -0.072 0.997 +vn -0.000 0.075 -0.997 +vn -0.329 -0.944 -0.005 +usemtl Material.001 +s 1 +f 1//1 2//1 3//1 4//1 +f 5//2 6//2 7//2 8//2 +f 9//3 10//3 11//3 12//3 +f 6//4 13//4 14//4 7//4 +f 15//5 16//5 17//5 18//5 +f 19//6 20//6 16//6 15//6 +f 21//7 22//7 23//7 24//7 +f 25//8 26//8 27//8 28//8 +f 24//9 23//9 10//9 9//9 +f 29//10 30//10 26//10 25//10 +f 19//11 31//11 32//11 20//11 +f 4//12 3//12 33//12 34//12 +f 35//13 36//13 30//13 29//13 +f 37//14 38//14 36//14 35//14 +f 39//15 40//15 38//15 37//15 +f 18//16 17//16 41//16 42//16 +f 43//17 5//17 8//17 44//17 +f 42//18 41//18 2//18 1//18 +f 39//19 43//19 44//19 40//19 +f 34//20 33//20 22//20 21//20 +f 13//21 12//21 11//21 14//21 +f 15//22 18//22 42//22 1//22 4//22 34//22 21//22 24//22 9//22 12//22 13//22 6//22 5//22 43//22 39//22 37//22 35//22 29//22 25//22 28//22 31//22 19//22 +f 22//23 33//23 3//23 2//23 41//23 17//23 16//23 20//23 32//23 27//23 26//23 30//23 36//23 38//23 40//23 44//23 8//23 7//23 14//23 11//23 10//23 23//23 +f 28//24 27//24 32//24 31//24 +o Vleft.019_Plane.029 +v 1.617 6.547 -4.385 +v 1.627 6.270 -0.582 +v 1.681 6.314 -0.582 +v 1.671 6.591 -4.385 +v 4.277 6.750 -4.422 +v 4.276 6.789 -4.421 +v 4.286 6.513 -0.618 +v 4.287 6.474 -0.618 +v 1.280 -1.685 -3.803 +v 1.290 -1.656 -0.000 +v -1.044 -1.625 -0.000 +v -1.059 -1.615 -3.788 +v 4.250 6.834 -4.420 +v 4.260 6.557 -0.617 +v 1.512 6.387 -4.384 +v 1.522 6.110 -0.581 +v 1.559 6.184 -0.581 +v 1.550 6.461 -4.384 +v 1.410 -1.581 -3.803 +v 4.273 6.711 -4.422 +v 4.282 6.434 -0.619 +v 1.419 -1.552 -0.000 +v 1.749 6.639 -4.385 +v 1.758 6.362 -0.582 +v 1.798 6.380 -0.582 +v 1.788 6.656 -4.386 +v 1.342 -1.657 -3.803 +v 1.351 -1.628 -0.000 +v 1.321 -1.645 -0.000 +v 1.311 -1.674 -3.803 +v 4.148 6.612 -0.615 +v 4.139 6.888 -4.418 +v 4.213 6.593 -0.617 +v 4.203 6.870 -4.420 +v 1.366 -1.637 -3.803 +v 1.375 -1.608 -0.000 +v 1.728 6.347 -0.582 +v 1.719 6.623 -4.385 +v 1.386 -1.617 -3.803 +v 1.396 -1.588 -0.000 +v 1.402 -1.595 -3.803 +v 1.412 -1.566 -0.000 +v 1.585 6.225 -0.581 +v 1.575 6.501 -4.384 +vn -0.634 0.771 0.057 +vn 0.999 0.034 -0.000 +vn -0.021 -0.999 0.002 +vn 0.868 0.494 0.033 +vn -0.889 0.455 0.035 +vn 0.943 -0.332 -0.013 +vn -0.399 0.914 0.067 +vn 0.502 -0.864 0.005 +vn -0.097 0.992 0.072 +vn 0.278 0.958 0.068 +vn 0.630 -0.776 0.004 +vn -0.564 0.822 0.061 +vn 0.693 -0.720 0.003 +vn -0.950 0.311 0.014 +vn 0.805 -0.592 0.002 +vn 0.888 -0.458 0.001 +vn -0.843 0.535 0.041 +vn 0.993 -0.116 -0.011 +vn -0.731 0.680 0.051 +vn -0.450 0.890 0.065 +vn 0.604 0.794 0.056 +vn -0.005 -0.070 -0.997 +vn 0.000 -0.000 1.000 +vn 0.001 0.072 0.997 +vn 0.329 -0.944 0.006 +usemtl Material.002 +s 1 +f 45//25 46//25 47//25 48//25 +f 49//26 50//26 51//26 52//26 +f 53//27 54//27 55//27 56//27 +f 50//28 57//28 58//28 51//28 +f 59//29 60//29 61//29 62//29 +f 63//30 64//30 65//30 66//30 +f 67//31 68//31 69//31 70//31 +f 71//32 72//32 73//32 74//32 +f 70//33 69//33 75//33 76//33 +f 76//34 75//34 77//34 78//34 +f 79//35 80//35 72//35 71//35 +f 48//36 47//36 81//36 82//36 +f 83//37 84//37 80//37 79//37 +f 56//38 55//38 60//38 59//38 +f 85//39 86//39 84//39 83//39 +f 63//40 66//40 86//40 85//40 +f 62//41 61//41 87//41 88//41 +f 64//42 49//42 52//42 65//42 +f 88//43 87//43 46//43 45//43 +f 82//44 81//44 68//44 67//44 +f 57//45 78//45 77//45 58//45 +f 63//46 85//46 83//46 79//46 71//46 74//46 53//46 56//46 59//46 62//46 88//46 45//46 48//46 82//46 67//46 70//46 76//46 78//46 57//46 50//46 49//46 64//46 +f 66//47 84//47 86//47 +f 66//48 65//48 52//48 51//48 58//48 77//48 75//48 69//48 68//48 81//48 47//48 46//48 87//48 61//48 60//48 55//48 54//48 73//48 72//48 80//48 84//48 +f 74//49 73//49 54//49 53//49 diff --git a/v_windows/v/old/examples/sokol/06_obj_viewer/gouraud.glsl b/v_windows/v/old/examples/sokol/06_obj_viewer/gouraud.glsl new file mode 100644 index 0000000..7f36499 --- /dev/null +++ b/v_windows/v/old/examples/sokol/06_obj_viewer/gouraud.glsl @@ -0,0 +1,108 @@ +//#pragma sokol @ctype mat4 hmm_mat4 + +#pragma sokol @vs vs + +uniform vs_params { + mat4 u_MVMatrix; // A constant representing the combined model/view matrix. + mat4 u_MVPMatrix; // A constant representing the combined model/view/projection matrix. + mat4 u_NMatrix; // A constant representing the Normal Matrix +}; + +in vec4 a_Position; // Per-vertex position information we will pass in. +in vec3 a_Normal; // Per-vertex normal information we will pass in. +in vec4 a_Color; // Per-vertex color information we will pass in. +in vec2 a_Texcoord0; + +out vec3 v_Position; // This will be passed into the fragment shader. +out vec4 v_Color; // This will be passed into the fragment shader. +out vec3 v_Normal; // This will be passed into the fragment shader. +out vec3 v_Normal1; +out vec2 uv; // This will be passed into the fragment shader. + +// The entry point for our vertex shader. +void main() +{ + // Transform the vertex into eye space. + v_Position = vec3(u_MVMatrix * a_Position); + // Pass through the color. + v_Color = a_Color; + // calc eye space normal + v_Normal = vec3(u_NMatrix * vec4(a_Normal, 1.0)); + // texture coord + uv = a_Texcoord0; + + v_Normal1 = normalize(vec3(u_MVMatrix * vec4(a_Normal, 1.0))); + + // gl_Position is a special variable used to store the final position. + // Multiply the vertex by the matrix to get the final point in normalized screen coordinates. + gl_Position = u_MVPMatrix * a_Position; +} + +#pragma sokol @end + +#pragma sokol @fs fs +//precision mediump float; // Set the default precision to medium. We don't need as high of a precision in the fragment shader +uniform sampler2D tex; +uniform fs_params { + vec4 u_LightPos; // The position of the light in eye space. + vec4 ambientColor; + vec4 diffuseColor; + vec4 specularColor; + +}; +in vec3 v_Position; // Interpolated position for this fragment. +in vec4 v_Color; // This is the color from the vertex shader interpolated across the triangle per fragment. +in vec3 v_Normal; // Interpolated normal for this fragment. +in vec3 v_Normal1; +in vec2 uv; +out vec4 frag_color; + +vec3 lightDirection = -u_LightPos.xyz;// vec3(0.0, -0.5, 0.5); +//const vec4 ambientColor = vec4(0.094, 0.0, 0.0, 1.0); +//const vec4 diffuseColor = vec4(0.5, 0.0, 0.0, 1.0); +//const vec4 specularColor = vec4(1.0, 1.0, 1.0, 1.0); +//const float shininess = 10.0; +const vec4 lightColor = vec4(1.0, 1.0, 1.0, 1.0); + +vec3 phongBRDF(vec3 lightDir, vec3 viewDir, vec3 normal, vec3 phongDiffuseCol, vec3 phongSpecularCol, float phongShininess) { + vec3 color = phongDiffuseCol; + vec3 reflectDir = reflect(-lightDir, normal); + float specDot = max(dot(reflectDir, viewDir), 0.0); + color += pow(specDot, phongShininess) * phongSpecularCol; + return color; +} + +vec4 getPhong(in vec4 diffuseColor) { + vec3 lightDir = normalize(-lightDirection); + vec3 viewDir = normalize(-v_Position); + vec3 n = normalize(v_Normal); + + vec3 luminance = ambientColor.rgb * 0.5; + + float illuminance = dot(lightDir, n); + if(illuminance > 0.0) { + // we save specular shiness in specularColor.a + vec3 brdf = phongBRDF(lightDir, viewDir, n, diffuseColor.rgb, specularColor.rgb, specularColor.a * 1000); + luminance += brdf * illuminance * lightColor.rgb; + } + + vec4 outColor = vec4(luminance,1.0); + return outColor; +} + +// The entry point for our fragment shader. +void main() +{ + vec4 txt = texture(tex, uv); + + // Directional light + float directional = dot(normalize(v_Normal1), normalize(vec3(0,0.5,1))) ; + directional = directional * 0.15; + + // Multiply the color by the diffuse illumination level to get final output color. + frag_color = vec4(clamp(directional + txt.rgb * getPhong(diffuseColor).rgb,0,1), txt.a * diffuseColor.a); + +} +#pragma sokol @end + +#pragma sokol @program gouraud vs fs \ No newline at end of file diff --git a/v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/obj.v b/v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/obj.v new file mode 100644 index 0000000..1d239a0 --- /dev/null +++ b/v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/obj.v @@ -0,0 +1,595 @@ +module obj + +/********************************************************************** +* +* .obj loader +* +* 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: +**********************************************************************/ +import gg.m4 +import strconv + +enum F_state { + start + first + ints + decimals + exp_start + exp_sign + exp_int +} + +// read a int from a string +fn get_int(s string, start_index int) (int, int) { + mut i := start_index + mut res := 0 + mut sgn := 1 + + mut state := F_state.start + for true { + if i >= s.len { + break + } + c := s[i] + if state == .start { + match c { + `+` { + i++ + state = .ints + continue + } + `-` { + sgn = -1 + i++ + state = .ints + continue + } + `0`...`9` { + state = .ints + } + ` `, `\t` { + i++ + continue + } + else { // no number found + break + } + } + } + + if state == .ints { + match c { + `0`...`9` { + // println("$res => ${(int(c) - 48)}") + res = res * 10 + (int(c) - 48) + i++ + continue + } + else { + break + } + } + } + } + // println("---") + return res * sgn, i +} + +// reas a float number from a string +fn get_float(s string, start_index int) (f64, int) { + mut i1 := start_index //+ 1 + for i1 < s.len && s[i1] in [` `, `\t`] { + i1++ + } + mut i := i1 + for i < s.len { + if s[i] in [` `, `\t`] { + break + } + i++ + } + // println(" get_float: ($start_index,$i) [${s[start_index..i]}]") + // f_res := strconv.atof_quick(s[start_index..i]) + f_res := strconv.atof_quick(s[i1..i]) + return f_res, i +} + +// read 3 f32 in sequence from a string +fn parse_3f(row string, start_index int) m4.Vec4 { + // println(row) + mut i := start_index //+ 1 + mut f1 := f64(0) + mut f2 := f64(0) + f0, mut p := get_float(row, i) + // print("Here f0: $f0 $p ") + f1, p = get_float(row, p + 1) + // print("Here f1: $f1 $p ") + f2, p = get_float(row, p + 1) + // print("Here f2: $f2 $p ") + return m4.Vec4{ + e: [f32(f0), f32(f1), f32(f2), 1]! + } +} + +// reas a sequence of f32 from a string +fn (mut m ObjPart) parse_floats(row string, start_index int) m4.Vec4 { + mut i := start_index //+ 1 + mut res_f := f64(0) + mut res := m4.Vec4{ + e: [f32(0), 0, 0, 1]! + } + mut c := 0 + for true { + res_f, i = get_float(row, i) + unsafe { + res.e[c] = f32(res_f) + } + c++ + i++ + if i >= row.len { + break + } + } + return res +} + +// read and manage all the faes from an .obj file data +fn (mut p Part) parse_faces(row string, start_index int, obj ObjPart) { + mut i := start_index + 1 + mut res := [][3]int{} + mut v := 0 + mut t := 0 + mut n := 0 + // println("row: ${row[i..]}") + for true { + t = 0 + n = 0 + if i >= row.len { + break + } + mut c := row[i] + if (c > `9` || c < `0`) && c != `-` { + i++ + continue + } + v, i = get_int(row, i) + if i < row.len && row[i] == `/` { + if row[i + 1] != `/` { + t, i = get_int(row, i + 1) + if i < row.len && row[i] == `/` { + n, i = get_int(row, i + 1) + } + } else { + i++ + n, i = get_int(row, i + 1) + } + } + // manage negative indexes + // NOTE: not well suporeted now + if v < 0 { + // println("${obj.v.len} ${obj.v.len-c}") + v = obj.v.len - v + 1 + // exit(0) + } + if n < 0 { + n = obj.vn.len - n + 1 + } + if t < 0 { + t = obj.vt.len - t + 1 + } + res << [v - 1, n - 1, t - 1]! + } + // println("ok res: ${res}") + // println(p.faces.len) + p.faces << res +} + +// parse the obj file, if single_material is true it use only one default material +pub fn (mut obj_part ObjPart) parse_obj_buffer(rows []string, single_material bool) { + mut mat_count := 0 + mut row_count := 0 + default_part := Part{ + name: 'default part' + } + obj_part.part << default_part + // println("OBJ file has ${rows.len} rows") + for c, row in rows { + // println("$c $row") + mut i := 0 + row_count++ + for true { + if i >= row.len { + break + } + match row[i] { + `s` { + break + } + `m` { + if row[i..i + 6] == 'mtllib' { + obj_part.material_file = row[i + 7..].trim_space() + obj_part.load_materials() + } + break + } + `o`, `g` { + mut part := Part{} + part.name = row[i + 1..].trim_space() + obj_part.part << part + mat_count = 0 + break + } + `u` { + if single_material == false && row[i..i + 6] == 'usemtl' { + material := row[i + 7..].trim_space() + // println("material: $material") + // manage multiple materials in an part + if obj_part.part[obj_part.part.len - 1].material.len > 0 { + mat_count++ + mut part := Part{} + if mat_count > 1 { + li := obj_part.part[obj_part.part.len - 1].name.last_index('_m') or { + obj_part.part[obj_part.part.len - 1].name.len - 1 + } + part.name = obj_part.part[obj_part.part.len - 1].name[..li] + + '_m${mat_count:02}' + } else { + part.name = obj_part.part[obj_part.part.len - 1].name + '_m01' + } + obj_part.part << part + } + obj_part.part[obj_part.part.len - 1].material = material + } + break + } + `v` { + i++ + match row[i] { + // normals + `n` { + obj_part.vn << parse_3f(row, i + 2) + // println("Vertex line: $c") + break + } + // parameteres uvw + `p` { + obj_part.vp << parse_3f(row, i + 2) + // println("Vertex line: ${obj_part.vp.len}") + break + } + // texture uvw + `t` { + obj_part.vt << obj_part.parse_floats(row, i + 2) + // println("Vertex line: $c") + break + } + else { + obj_part.v << parse_3f(row, i + 1) + // println("$row => ${obj_part.v[obj_part.v.len-1]}") + break + } + } + } + `f` { + // println("$c $row") + obj_part.part[obj_part.part.len - 1].parse_faces(row, i, obj_part) + // println(obj_part.part[obj_part.part.len - 1].faces.len) + // println("Faces line: $c") + break + } + // end of the line, comments + `\n`, `#` { + break + } + else {} + } + i++ + } + // if c == 2 { break } + if c % 100000 == 0 && c > 0 { + println('$c rows parsed') + } + } + println('$row_count .obj Rows parsed') + // remove default part if empty + if obj_part.part.len > 1 && obj_part.part[0].faces.len == 0 { + obj_part.part = obj_part.part[1..] + } +} + +// load the materials if found the .mtl file +fn (mut obj_part ObjPart) load_materials() { + rows := read_lines_from_file(obj_part.material_file) + println('Material file [$obj_part.material_file] $rows.len Rows.') + for row in rows { + // println("$row") + mut i := 0 + for true { + if i >= row.len { + break + } + match row[i] { + `n` { + if row[i..i + 6] == 'newmtl' { + name := row[i + 6..].trim_space() + mut mat := Material{ + name: name + } + obj_part.mat << mat + break + } + } + `K` { + if row[i + 1] !in [`a`, `d`, `e`, `s`] { + break + } + k_name := row[i..i + 2] + i += 3 + value := parse_3f(row, i) + obj_part.mat[obj_part.mat.len - 1].ks[k_name] = value + break + } + `N` { + n_name := row[i..i + 2] + i += 3 + value, _ := get_float(row, i) + obj_part.mat[obj_part.mat.len - 1].ns[n_name] = f32(value) + break + } + `m` { + if row[i..i + 4] == 'map_' { + name := row[i..i + 6] + if (i + 7) < row.len { + file_name := row[i + 7..].trim_space() + obj_part.mat[obj_part.mat.len - 1].maps[name] = file_name + } + break + } + } + // trasparency + `d` { + if row[i + 1] == ` ` { + value, _ := get_float(row, i + 2) + obj_part.mat[obj_part.mat.len - 1].ns['Tr'] = f32(value) + } + } + `T` { + if row[i + 1] == `r` { + value, _ := get_float(row, i + 3) + obj_part.mat[obj_part.mat.len - 1].ns['Tr'] = f32(1.0 - value) + } + } + // end of the line, comments + `\n`, `#` { + break + } + ` `, `\t` { + i++ + continue + } + else { + break + } + } + i++ + } + } + + // create map material name => material index + for i, m in obj_part.mat { + if m.name !in obj_part.mat_map { + obj_part.mat_map[m.name] = i + } + } + + println('Material Loading Done!') +} + +//============================================================================== +// Sokol data +//============================================================================== + +// vertex data struct +pub struct Vertex_pnct { +pub mut: + x f32 // poistion + y f32 + z f32 + nx f32 // normal + ny f32 + nz f32 + color u32 = 0xFFFFFFFF // color + u f32 // uv + v f32 + // u u16 // for compatibility with D3D11 + // v u16 // for compatibility with D3D11 +} + +// struct used to pass the data to the sokol calls +pub struct Skl_buffer { +pub mut: + vbuf []Vertex_pnct + ibuf []u32 + n_vertex u32 +} + +// transforms data from .obj format to buffer ready to be used in the render +pub fn (mut obj_part ObjPart) get_buffer(in_part_list []int) Skl_buffer { + // in_part := 0 + mut v_count_index := 0 + mut out_buf := Skl_buffer{} + + mut cache := map[string]int{} + mut cache_hit := 0 + + // has_normals := obj_part.vn.len > 0 + // has_uvs := obj_part.vt.len > 0 + + for in_part in in_part_list { + part := obj_part.part[in_part] + for fc, face in part.faces { + // println("$fc $face") + // default 3 faces + mut v_seq := [0, 1, 2] + if face.len == 4 { + v_seq = [0, 1, 2, 0, 2, 3] + } + + // if big faces => use the fan of triangles as solution + // Note: this trick doesn't work with concave faces + if face.len > 4 { + v_seq = [] + mut i := 1 + for i < (face.len - 1) { + v_seq << 0 + v_seq << i + v_seq << (i + 1) + i++ + } + // println("BIG FACES! ${fc} ${face.len} v_seq:${v_seq.len}") + } + + // no vertex index, generate normals + if face[0][1] == -1 && face.len >= 3 { + mut v_count := 0 + v0 := face[v_count + 0][0] + v1 := face[v_count + 1][0] + v2 := face[v_count + 2][0] + + vec0 := obj_part.v[v2] - obj_part.v[v1] + vec1 := obj_part.v[v0] - obj_part.v[v1] + tmp_normal := vec0 % vec1 + + for v_count < face.len { + obj_part.vn << tmp_normal + obj_part.part[in_part].faces[fc][v_count][1] = obj_part.vn.len - 1 + v_count++ + } + } + + for vertex_index in v_seq { + // position + if vertex_index >= face.len { + continue + } + v_index := face[vertex_index][0] // vertex index + n_index := face[vertex_index][1] // normal index + t_index := face[vertex_index][2] // uv texture index + key := '${v_index}_${n_index}_$t_index' + if key !in cache { + cache[key] = v_count_index + mut pnct := Vertex_pnct{ + x: obj_part.v[v_index].e[0] + y: obj_part.v[v_index].e[1] + z: obj_part.v[v_index].e[2] + } + // normal + if n_index >= 0 { + pnct.nx = obj_part.vn[n_index].e[0] + pnct.ny = obj_part.vn[n_index].e[1] + pnct.nz = obj_part.vn[n_index].e[2] + } + // texture uv + if t_index >= 0 { + pnct.u = obj_part.vt[t_index].e[0] + pnct.v = obj_part.vt[t_index].e[1] + } + + out_buf.vbuf << pnct + out_buf.ibuf << u32(v_count_index) + v_count_index++ + } else { + // println("Cache used! $key") + out_buf.ibuf << u32(cache[key]) + cache_hit++ + } + } + } + } + + /* + println("------------") + for c1, x1 in out_buf.vbuf[..10] { + println("$c1 $x1") + } + println(out_buf.ibuf[..10]) + */ + // println("vbuf size: ${out_buf.vbuf.len} ibuf size: ${out_buf.ibuf.len} Cache hit: $cache_hit") + out_buf.n_vertex = u32(out_buf.ibuf.len) + return out_buf +} + +//============================================================================== +// Utility +//============================================================================== +// print on the console the summary of the .obj model loaded +pub fn (obj_part ObjPart) summary() { + println('---- Stats ----') + println('vertices: $obj_part.v.len') + println('normals : $obj_part.vn.len') + println('uv : $obj_part.vt.len') + println('parts : $obj_part.part.len') + // Parts + println('---- Parts ----') + for c, x in obj_part.part { + println('${c:3} [${x.name:-16}] mat:[${x.material:-10}] ${x.faces.len:7} faces') + } + // Materials + println('---- Materials ----') + println('Material dict: $obj_part.mat_map.keys()') + for c, mat in obj_part.mat { + println('${c:3} [${mat.name:-16}]') + for k, v in mat.ks { + print('$k = $v') + } + for k, v in mat.ns { + println('$k = $v') + } + for k, v in mat.maps { + println('$k = $v') + } + } +} + +// debug test function, do not remove. +pub fn tst() { + /* + //fname := "capsule.obj" + //fname := "Forklift.obj" + fname := "cube.obj" + //fname := "Orange Robot 3D ObjPart.obj" + + mut obj := ObjPart{} + buf := os.read_lines(fname) or { panic(err.msg) } + obj.parse_obj_buffer(buf) + obj.summary() + */ + /* + a :="f 7048 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7003" + mut f1 := 0 + mut f2 := 0 + f0,mut p := get_int(a,1) + f1, p = get_int(a,p) + f2, p = get_int(a,p) + println("res: ${f0} ${f1} ${f2}") + */ + /* + a :="v -0 0.107769 -0.755914" + println("${parse_3f(a,1)}") + */ + /* + ort := m4.ortho(0,300,0,200,0,0) + println(ort) + a := m4.vec3(0,0,0) + println("a: $a") + res := m4.mul_vec(ort, a) + println("res:\n${res}") + */ + s := 'K 1 1 1' + r := strconv.atof_quick(s[1..s.len - 1]) + println(r) +} diff --git a/v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/rend.v b/v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/rend.v new file mode 100644 index 0000000..d4ea3bc --- /dev/null +++ b/v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/rend.v @@ -0,0 +1,297 @@ +/********************************************************************** +* +* .obj loader +* +* 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 obj + +import sokol.gfx +import gg.m4 +import math +import stbi + +/****************************************************************************** +* Texture functions +******************************************************************************/ +pub fn create_texture(w int, h int, buf &byte) C.sg_image { + sz := w * h * 4 + mut img_desc := C.sg_image_desc{ + width: w + height: h + num_mipmaps: 0 + min_filter: .linear + mag_filter: .linear + // usage: .dynamic + wrap_u: .clamp_to_edge + wrap_v: .clamp_to_edge + label: &byte(0) + d3d11_texture: 0 + } + // comment if .dynamic is enabled + img_desc.data.subimage[0][0] = C.sg_range{ + ptr: buf + size: size_t(sz) + } + + sg_img := C.sg_make_image(&img_desc) + return sg_img +} + +pub fn destroy_texture(sg_img C.sg_image) { + C.sg_destroy_image(sg_img) +} + +pub fn load_texture(file_name string) C.sg_image { + buffer := read_bytes_from_file(file_name) + stbi.set_flip_vertically_on_load(true) + img := stbi.load_from_memory(buffer.data, buffer.len) or { + eprintln('Texure file: [$file_name] ERROR!') + exit(0) + } + res := create_texture(int(img.width), int(img.height), img.data) + img.free() + return res +} + +/****************************************************************************** +* Pipeline +******************************************************************************/ +pub fn (mut obj_part ObjPart) create_pipeline(in_part []int, shader C.sg_shader, texture C.sg_image) Render_data { + mut res := Render_data{} + obj_buf := obj_part.get_buffer(in_part) + res.n_vert = obj_buf.n_vertex + res.material = obj_part.part[in_part[0]].material + + // vertex buffer + mut vert_buffer_desc := C.sg_buffer_desc{ + label: 0 + } + unsafe { C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc)) } + + vert_buffer_desc.size = size_t(obj_buf.vbuf.len * int(sizeof(Vertex_pnct))) + vert_buffer_desc.data = C.sg_range{ + ptr: obj_buf.vbuf.data + size: size_t(obj_buf.vbuf.len * int(sizeof(Vertex_pnct))) + } + + vert_buffer_desc.@type = .vertexbuffer + vert_buffer_desc.label = 'vertbuf_part_${in_part:03}'.str + vbuf := gfx.make_buffer(&vert_buffer_desc) + + // index buffer + mut index_buffer_desc := C.sg_buffer_desc{ + label: 0 + } + unsafe { C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc)) } + + index_buffer_desc.size = size_t(obj_buf.ibuf.len * int(sizeof(u32))) + index_buffer_desc.data = C.sg_range{ + ptr: obj_buf.ibuf.data + size: size_t(obj_buf.ibuf.len * int(sizeof(u32))) + } + + index_buffer_desc.@type = .indexbuffer + index_buffer_desc.label = 'indbuf_part_${in_part:03}'.str + ibuf := gfx.make_buffer(&index_buffer_desc) + + mut pipdesc := C.sg_pipeline_desc{} + unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) } + pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_pnct)) + + // the constants [C.ATTR_vs_a_Position, C.ATTR_vs_a_Color, C.ATTR_vs_a_Texcoord0] are generated by sokol-shdc + pipdesc.layout.attrs[C.ATTR_vs_a_Position].format = .float3 // x,y,z as f32 + pipdesc.layout.attrs[C.ATTR_vs_a_Normal].format = .float3 // x,y,z as f32 + pipdesc.layout.attrs[C.ATTR_vs_a_Color].format = .ubyte4n // color as u32 + pipdesc.layout.attrs[C.ATTR_vs_a_Texcoord0].format = .float2 // u,v as f32 + // pipdesc.layout.attrs[C.ATTR_vs_a_Texcoord0].format = .short2n // u,v as u16 + pipdesc.index_type = .uint32 + + color_state := C.sg_color_state{ + blend: C.sg_blend_state{ + enabled: true + src_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_SRC_ALPHA) + dst_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA) + } + } + pipdesc.colors[0] = color_state + + pipdesc.depth = C.sg_depth_state{ + write_enabled: true + compare: gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL) + } + pipdesc.cull_mode = .front + + pipdesc.label = 'pip_part_${in_part:03}'.str + + // shader + pipdesc.shader = shader + + res.bind.vertex_buffers[0] = vbuf + res.bind.index_buffer = ibuf + res.bind.fs_images[C.SLOT_tex] = texture + res.pipeline = gfx.make_pipeline(&pipdesc) + // println('Buffers part [$in_part] init done!') + + return res +} + +/****************************************************************************** +* Render functions +******************************************************************************/ +// agregate all the part by materials +pub fn (mut obj_part ObjPart) init_render_data(texture C.sg_image) { + // create shader + // One shader for all the model + shader := gfx.make_shader(C.gouraud_shader_desc(gfx.query_backend())) + + mut part_dict := map[string][]int{} + for i, p in obj_part.part { + if p.faces.len > 0 { + part_dict[p.material] << i + } + } + obj_part.rend_data.clear() + // println("Material dict: ${obj_part.mat_map.keys()}") + + for k, v in part_dict { + // println("$k => Parts $v") + + mut txt := texture + + if k in obj_part.mat_map { + mat_map := obj_part.mat[obj_part.mat_map[k]] + if 'map_Kd' in mat_map.maps { + file_name := mat_map.maps['map_Kd'] + if file_name in obj_part.texture { + txt = obj_part.texture[file_name] + // println("Texture [${file_name}] => from CACHE") + } else { + txt = load_texture(file_name) + obj_part.texture[file_name] = txt + // println("Texture [${file_name}] => LOADED") + } + } + } + // key := obj_part.texture.keys()[0] + // obj_part.rend_data << obj_part.create_pipeline(v, shader, obj_part.texture[key]) + obj_part.rend_data << obj_part.create_pipeline(v, shader, txt) + } + // println("Texture array len: ${obj_part.texture.len}") + // println("Calc bounding box.") + obj_part.calc_bbox() + println('init_render_data DONE!') +} + +pub fn (obj_part ObjPart) bind_and_draw(rend_data_index int, in_data Shader_data) u32 { + // apply the pipline and bindings + mut part_render_data := obj_part.rend_data[rend_data_index] + + // pass light position + mut tmp_fs_params := Tmp_fs_param{} + tmp_fs_params.ligth = in_data.fs_data.ligth + + if part_render_data.material in obj_part.mat_map { + mat_index := obj_part.mat_map[part_render_data.material] + mat := obj_part.mat[mat_index] + + // ambient + tmp_fs_params.ka = in_data.fs_data.ka + if 'Ka' in mat.ks { + tmp_fs_params.ka = mat.ks['Ka'] + } + + // specular + tmp_fs_params.ks = in_data.fs_data.ks + if 'Ks' in mat.ks { + tmp_fs_params.ks = mat.ks['Ks'] + } + + // specular exponent Ns + if 'Ns' in mat.ns { + tmp_fs_params.ks.e[3] = mat.ns['Ns'] / 1000.0 + } else { + // defautl value is 10 + tmp_fs_params.ks.e[3] = f32(10) / 1000.0 + } + + // diffuse + tmp_fs_params.kd = in_data.fs_data.kd + if 'Kd' in mat.ks { + tmp_fs_params.kd = mat.ks['Kd'] + } + + // alpha/transparency + if 'Tr' in mat.ns { + tmp_fs_params.kd.e[3] = mat.ns['Tr'] + } + } + + gfx.apply_pipeline(part_render_data.pipeline) + gfx.apply_bindings(part_render_data.bind) + + vs_uniforms_range := C.sg_range{ + ptr: in_data.vs_data + size: size_t(in_data.vs_len) + } + fs_uniforms_range := C.sg_range{ + ptr: unsafe { &tmp_fs_params } + size: size_t(in_data.fs_len) + } + + gfx.apply_uniforms(C.SG_SHADERSTAGE_VS, C.SLOT_vs_params, &vs_uniforms_range) + gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params, &fs_uniforms_range) + gfx.draw(0, int(part_render_data.n_vert), 1) + return part_render_data.n_vert +} + +pub fn (obj_part ObjPart) bind_and_draw_all(in_data Shader_data) u32 { + mut n_vert := u32(0) + // println("Parts: ${obj_part.rend_data.len}") + for i, _ in obj_part.rend_data { + n_vert += obj_part.bind_and_draw(i, in_data) + } + return n_vert +} + +pub fn (mut obj_part ObjPart) calc_bbox() { + obj_part.max = m4.Vec4{ + e: [f32(-math.max_f32), -math.max_f32, -math.max_f32, 0]! + } + obj_part.min = m4.Vec4{ + e: [f32(math.max_f32), math.max_f32, math.max_f32, 0]! + } + for v in obj_part.v { + if v.e[0] > obj_part.max.e[0] { + obj_part.max.e[0] = v.e[0] + } + if v.e[1] > obj_part.max.e[1] { + obj_part.max.e[1] = v.e[1] + } + if v.e[2] > obj_part.max.e[2] { + obj_part.max.e[2] = v.e[2] + } + + if v.e[0] < obj_part.min.e[0] { + obj_part.min.e[0] = v.e[0] + } + if v.e[1] < obj_part.min.e[1] { + obj_part.min.e[1] = v.e[1] + } + if v.e[2] < obj_part.min.e[2] { + obj_part.min.e[2] = v.e[2] + } + } + val1 := obj_part.max.mod3() + val2 := obj_part.min.mod3() + if val1 > val2 { + obj_part.radius = f32(val1) + } else { + obj_part.radius = f32(val2) + } + // println("BBox: ${obj_part.min} <=> ${obj_part.max}\nRadius: ${obj_part.radius}") +} diff --git a/v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/struct.v b/v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/struct.v new file mode 100644 index 0000000..55a528b --- /dev/null +++ b/v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/struct.v @@ -0,0 +1,104 @@ +/********************************************************************** +* +* .obj loader +* +* 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 obj + +import gg.m4 + +// part struct mantain the fae indexes list +pub struct Part { +pub mut: + faces [][][3]int // v n t index order, if -1 not available + name string + material string +} + +// materias struct, all Ks and Ns are stored as maps of string +pub struct Material { +pub mut: + name string + ks map[string]m4.Vec4 + ns map[string]f32 + maps map[string]string +} + +// render data used for the rendering +pub struct Render_data { +pub mut: + pipeline C.sg_pipeline + bind C.sg_bindings + n_vert u32 + material string +} + +// base object parts struct +pub struct ObjPart { +pub mut: + v []m4.Vec4 // position + vn []m4.Vec4 // normals + vp []m4.Vec4 // vertex params + vt []m4.Vec4 // textures + + name string + part []Part // parts of the ObjPart + mat []Material // list of the materials of the ObjPart + mat_map map[string]int // maping material name to its material index + texture map[string]C.sg_image // GPU loaded texture map + material_file string // .mtl file name for the .obj + + rend_data []Render_data // render data used for the rendering + + t_m m4.Mat4 = m4.unit_m4() // transform matrix for this ObjPart + // child []ObjPart // childs + // stats + min m4.Vec4 // min 3d position in the ObjPart + max m4.Vec4 // max 3d position in the ObjPart + radius f32 // bounding circle radius of the ObjPart +} + +// used in to pass the matrices to the shader +pub struct Mats { +pub mut: + mv m4.Mat4 + mvp m4.Mat4 + nm m4.Mat4 +} + +// data passed to the vertex shader +pub struct Tmp_vs_param { +pub mut: + mv m4.Mat4 + mvp m4.Mat4 + nm m4.Mat4 +} + +// data passed to the pixel shader +pub struct Tmp_fs_param { +pub mut: + ligth m4.Vec4 + ka m4.Vec4 = m4.Vec4{ + e: [f32(0.1), 0.0, 0.0, 1.0]! + } + kd m4.Vec4 = m4.Vec4{ + e: [f32(0.5), 0.5, 0.5, 1.0]! + } + ks m4.Vec4 = m4.Vec4{ + e: [f32(1.0), 1.0, 1.0, 1.0]! + } +} + +// shader data for the rendering +pub struct Shader_data { +pub mut: + vs_data &Tmp_vs_param + vs_len int + fs_data &Tmp_fs_param + fs_len int +} diff --git a/v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/util.v b/v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/util.v new file mode 100644 index 0000000..a1e596a --- /dev/null +++ b/v_windows/v/old/examples/sokol/06_obj_viewer/modules/obj/util.v @@ -0,0 +1,44 @@ +module obj + +import os + +// read a file as single lines +pub fn read_lines_from_file(file_path string) []string { + mut path := '' + mut rows := []string{} + $if android { + path = 'models/' + file_path + bts := os.read_apk_asset(path) or { + eprintln('File [$path] NOT FOUND!') + return rows + } + rows = bts.bytestr().split_into_lines() + } $else { + path = os.resource_abs_path('assets/models/' + file_path) + rows = os.read_lines(path) or { + eprintln('File [$path] NOT FOUND! file_path: $file_path') + return rows + } + } + return rows +} + +// read a file as []byte +pub fn read_bytes_from_file(file_path string) []byte { + mut path := '' + mut buffer := []byte{} + $if android { + path = 'models/' + file_path + buffer = os.read_apk_asset(path) or { + eprintln('Texure file: [$path] NOT FOUND!') + exit(0) + } + } $else { + path = os.resource_abs_path('assets/models/' + file_path) + buffer = os.read_bytes(path) or { + eprintln('Texure file: [$path] NOT FOUND!') + exit(0) + } + } + return buffer +} diff --git a/v_windows/v/old/examples/sokol/06_obj_viewer/show_obj.v b/v_windows/v/old/examples/sokol/06_obj_viewer/show_obj.v new file mode 100644 index 0000000..e2e0be3 --- /dev/null +++ b/v_windows/v/old/examples/sokol/06_obj_viewer/show_obj.v @@ -0,0 +1,338 @@ +/********************************************************************** +* +* .obj viewer +* +* 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. +* +* Example .obj model of V from SurmanPP +* +* HOW TO COMPILE SHADERS: +* - download the sokol shader convertor tool from https://github.com/floooh/sokol-tools-bin +* +* - compile the .glsl shader with: +* linux : sokol-shdc --input gouraud.glsl --output gouraud.h --slang glsl330 +* windows: sokol-shdc.exe --input gouraud.glsl --output gouraud.h --slang glsl330 +* +* --slang parameter can be: +* - glsl330: desktop GL +* - glsl100: GLES2 / WebGL +* - glsl300es: GLES3 / WebGL2 +* - hlsl4: D3D11 +* - hlsl5: D3D11 +* - metal_macos: Metal on macOS +* - metal_ios: Metal on iOS device +* - metal_sim: Metal on iOS simulator +* - wgpu: WebGPU +* +* you can have multiple platforms at the same time passing parameters like this: --slang glsl330:hlsl5:metal_macos +* for further infos have a look at the sokol shader tool docs. +* +* ALTERNATIVE .OBJ MODELS: +* you can load alternative models putting them in the "assets/model" folder with or without their .mtl file. +* use the program help for further instructions. +* +* TODO: +* - frame counter +**********************************************************************/ +import gg +import gg.m4 +import gx +import math +import sokol.sapp +import sokol.gfx +import sokol.sgl +import time +import os +import obj + +// GLSL Include and functions + +#flag -I @VMODROOT/. +#include "gouraud.h" #Please use sokol-shdc to generate the necessary rt_glsl.h file from rt_glsl.glsl (see the instructions at the top of this file) + +fn C.gouraud_shader_desc(gfx.Backend) &C.sg_shader_desc + +const ( + win_width = 600 + win_height = 600 + bg_color = gx.white +) + +struct App { +mut: + gg &gg.Context + texture C.sg_image + init_flag bool + frame_count int + + mouse_x int = -1 + mouse_y int = -1 + scroll_y int // mouse wheel value + // time + ticks i64 + // model + obj_part &obj.ObjPart + n_vertex u32 + // init parameters + file_name string + single_material_flag bool +} + +/****************************************************************************** +* Draw functions +******************************************************************************/ +[inline] +fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 { + return m4.Vec4{ + e: [x, y, z, w]! + } +} + +fn calc_matrices(w f32, h f32, rx f32, ry f32, in_scale f32, pos m4.Vec4) obj.Mats { + proj := m4.perspective(60, w / h, 0.01, 100.0) // set far plane to 100 fro the zoom function + 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_pos := m4.unit_m4().translate(pos) + + model_m := (rym * rxm) * model_pos + scale_m := m4.scale(vec4(in_scale, in_scale, in_scale, 1)) + + mv := scale_m * model_m // model view + nm := mv.inverse().transpose() // normal matrix + mvp := mv * view_proj // model view projection + + return obj.Mats{ + mv: mv + mvp: mvp + nm: nm + } +} + +fn draw_model(app App, model_pos m4.Vec4) u32 { + if app.init_flag == false { + return 0 + } + + ws := gg.window_size_real_pixels() + dw := ws.width / 2 + dh := ws.height / 2 + + mut scale := f32(1) + if app.obj_part.radius > 1 { + scale = 1 / (app.obj_part.radius) + } else { + scale = app.obj_part.radius + } + scale *= 3 + + // *** vertex shader uniforms *** + rot := [f32(app.mouse_y), f32(app.mouse_x)] + mut zoom_scale := scale + f32(app.scroll_y) / (app.obj_part.radius * 4) + mats := calc_matrices(dw, dh, rot[0], rot[1], zoom_scale, model_pos) + + mut tmp_vs_param := obj.Tmp_vs_param{ + mv: mats.mv + mvp: mats.mvp + nm: mats.nm + } + + // *** fragment shader uniforms *** + time_ticks := f32(time.ticks() - app.ticks) / 1000 + radius_light := f32(app.obj_part.radius) + x_light := f32(math.cos(time_ticks) * radius_light) + z_light := f32(math.sin(time_ticks) * radius_light) + + mut tmp_fs_params := obj.Tmp_fs_param{} + tmp_fs_params.ligth = m4.vec3(x_light, radius_light, z_light) + + sd := obj.Shader_data{ + vs_data: unsafe { &tmp_vs_param } + vs_len: int(sizeof(tmp_vs_param)) + fs_data: unsafe { &tmp_fs_params } + fs_len: int(sizeof(tmp_fs_params)) + } + + return app.obj_part.bind_and_draw_all(sd) +} + +fn frame(mut app App) { + ws := gg.window_size_real_pixels() + + // clear + mut color_action := C.sg_color_attachment_action{ + action: gfx.Action(C.SG_ACTION_CLEAR) + value: C.sg_color{ + r: 0.0 + g: 0.0 + b: 0.0 + a: 1.0 + } + } + + mut pass_action := C.sg_pass_action{} + pass_action.colors[0] = color_action + gfx.begin_default_pass(&pass_action, ws.width, ws.height) + + // render the data + draw_start_glsl(app) + draw_model(app, m4.Vec4{}) + // uncoment if you want a raw benchmark mode + /* + mut n_vertex_drawn := u32(0) + n_x_obj := 20 + + for x in 0..n_x_obj { + for z in 0..30 { + for y in 0..4 { + n_vertex_drawn += draw_model(app, m4.Vec4{e:[f32((x-(n_x_obj>>1))*3),-3 + y*3,f32(-6*z),1]!}) + } + } + } + */ + draw_end_glsl(app) + + // println("v:$n_vertex_drawn") + app.frame_count++ +} + +fn draw_start_glsl(app App) { + if app.init_flag == false { + return + } + ws := gg.window_size_real_pixels() + gfx.apply_viewport(0, 0, ws.width, ws.height, true) +} + +fn draw_end_glsl(app App) { + gfx.end_pass() + gfx.commit() +} + +/****************************************************************************** +* Init / Cleanup +******************************************************************************/ +fn my_init(mut app App) { + mut object := &obj.ObjPart{} + obj_file_lines := obj.read_lines_from_file(app.file_name) + object.parse_obj_buffer(obj_file_lines, app.single_material_flag) + object.summary() + app.obj_part = object + + // set max vertices, + // for a large number of the same type of object it is better use the instances!! + desc := sapp.create_desc() + gfx.setup(&desc) + sgl_desc := C.sgl_desc_t{ + max_vertices: 128 * 65536 + } + sgl.setup(&sgl_desc) + + // 1x1 pixel white, default texture + unsafe { + tmp_txt := malloc(4) + tmp_txt[0] = byte(0xFF) + tmp_txt[1] = byte(0xFF) + tmp_txt[2] = byte(0xFF) + tmp_txt[3] = byte(0xFF) + app.texture = obj.create_texture(1, 1, tmp_txt) + free(tmp_txt) + } + // glsl + app.obj_part.init_render_data(app.texture) + app.init_flag = true +} + +fn cleanup(mut app App) { + gfx.shutdown() + /* + for _, mat in app.obj_part.texture { + obj.destroy_texture(mat) + } + */ +} + +/****************************************************************************** +* events handling +******************************************************************************/ +fn my_event_manager(mut ev gg.Event, mut app App) { + if ev.typ == .mouse_move { + app.mouse_x = int(ev.mouse_x) + app.mouse_y = int(ev.mouse_y) + } + + if ev.scroll_y != 0 { + app.scroll_y += int(ev.scroll_y) + } + + if ev.typ == .touches_began || ev.typ == .touches_moved { + if ev.num_touches > 0 { + touch_point := ev.touches[0] + app.mouse_x = int(touch_point.pos_x) + app.mouse_y = int(touch_point.pos_y) + } + } +} + +/****************************************************************************** +* Main +******************************************************************************/ +// is needed for easier diagnostics on windows +[console] +fn main() { + /* + obj.tst() + exit(0) + */ + + // App init + mut app := &App{ + gg: 0 + obj_part: 0 + } + + app.file_name = 'v.obj_' // default object is the v logo + + app.single_material_flag = false + $if !android { + if os.args.len > 3 || (os.args.len >= 2 && os.args[1] in ['-h', '--help', '\\?', '-?']) { + eprintln('Usage:\nshow_obj [file_name:string] [single_material_flag:(true|false)]\n') + eprintln('file_name : name of the .obj file, it must be in the folder "assets/models"') + eprintln(' if no file name is passed the default V logo will be showed.') + eprintln(' if you want custom models you can put them in the folder "assets/models".') + eprintln("single_material_flag: if true the viewer use for all the model's parts the default material\n") + exit(0) + } + + if os.args.len >= 2 { + app.file_name = os.args[1] + } + if os.args.len >= 3 { + app.single_material_flag = os.args[2].bool() + } + println('Loading model: $app.file_name') + println('Using single material: $app.single_material_flag') + } + + app.gg = gg.new_context( + width: win_width + height: win_height + create_window: true + window_title: 'V Wavefront OBJ viewer - Use the mouse wheel to zoom' + user_data: app + bg_color: bg_color + frame_fn: frame + init_fn: my_init + cleanup_fn: cleanup + event_fn: my_event_manager + ) + + app.ticks = time.ticks() + app.gg.run() +} diff --git a/v_windows/v/old/examples/sokol/06_obj_viewer/v.mod b/v_windows/v/old/examples/sokol/06_obj_viewer/v.mod new file mode 100644 index 0000000..e69de29 diff --git a/v_windows/v/old/examples/sokol/drawing.v b/v_windows/v/old/examples/sokol/drawing.v new file mode 100644 index 0000000..e1b779b --- /dev/null +++ b/v_windows/v/old/examples/sokol/drawing.v @@ -0,0 +1,75 @@ +import sokol +import sokol.sapp +import sokol.gfx +import sokol.sgl + +struct AppState { + pass_action C.sg_pass_action +} + +const ( + used_import = sokol.used_import +) + +fn main() { + state := &AppState{ + pass_action: gfx.create_clear_pass(0.1, 0.1, 0.1, 1.0) + } + title := 'Sokol Drawing Template' + desc := C.sapp_desc{ + user_data: state + init_userdata_cb: init + frame_userdata_cb: frame + window_title: title.str + html5_canvas_name: title.str + } + sapp.run(&desc) +} + +fn init(user_data voidptr) { + desc := sapp.create_desc() // C.sg_desc{ + gfx.setup(&desc) + sgl_desc := C.sgl_desc_t{} + sgl.setup(&sgl_desc) +} + +fn frame(user_data voidptr) { + // println('frame') + state := &AppState(user_data) + draw() + gfx.begin_default_pass(&state.pass_action, sapp.width(), sapp.height()) + sgl.draw() + gfx.end_pass() + gfx.commit() +} + +fn draw() { + // first, reset and setup ortho projection + sgl.defaults() + sgl.matrix_mode_projection() + sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0) + sgl.c4b(255, 0, 0, 128) + draw_hollow_rect(10, 10, 100, 30) + sgl.c4b(25, 150, 0, 128) + draw_filled_rect(10, 150, 80, 40) + // line(0, 0, 500, 500) +} + +fn draw_hollow_rect(x f32, y f32, w f32, h f32) { + sgl.begin_line_strip() + sgl.v2f(x, y) + sgl.v2f(x + w, y) + sgl.v2f(x + w, y + h) + sgl.v2f(x, y + h) + sgl.v2f(x, y) + sgl.end() +} + +fn draw_filled_rect(x f32, y f32, w f32, h f32) { + sgl.begin_quads() + sgl.v2f(x, y) + sgl.v2f(x + w, y) + sgl.v2f(x + w, y + h) + sgl.v2f(x, y + h) + sgl.end() +} diff --git a/v_windows/v/old/examples/sokol/fonts.v b/v_windows/v/old/examples/sokol/fonts.v new file mode 100644 index 0000000..1cf2170 --- /dev/null +++ b/v_windows/v/old/examples/sokol/fonts.v @@ -0,0 +1,164 @@ +import sokol +import sokol.sapp +import sokol.gfx +import sokol.sgl +import sokol.sfons +import os + +struct AppState { +mut: + pass_action C.sg_pass_action + fons &C.FONScontext + font_normal int +} + +fn main() { + mut color_action := C.sg_color_attachment_action{ + action: gfx.Action(C.SG_ACTION_CLEAR) + value: C.sg_color{ + r: 0.3 + g: 0.3 + b: 0.32 + a: 1.0 + } + } + mut pass_action := C.sg_pass_action{} + pass_action.colors[0] = color_action + state := &AppState{ + pass_action: pass_action + fons: &C.FONScontext(0) + } + title := 'V Metal/GL Text Rendering' + desc := C.sapp_desc{ + user_data: state + init_userdata_cb: init + frame_userdata_cb: frame + window_title: title.str + html5_canvas_name: title.str + } + sapp.run(&desc) +} + +fn init(mut state AppState) { + desc := sapp.create_desc() + gfx.setup(&desc) + s := &C.sgl_desc_t{} + C.sgl_setup(s) + state.fons = sfons.create(512, 512, 1) + // or use DroidSerif-Regular.ttf + if bytes := os.read_bytes(os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf')) { + println('loaded font: $bytes.len') + state.font_normal = C.fonsAddFontMem(state.fons, c'sans', bytes.data, bytes.len, + false) + } +} + +fn frame(user_data voidptr) { + mut state := &AppState(user_data) + state.render_font() + gfx.begin_default_pass(&state.pass_action, sapp.width(), sapp.height()) + sgl.draw() + gfx.end_pass() + gfx.commit() +} + +fn (state &AppState) render_font() { + mut sx := 0.0 + mut sy := 0.0 + mut dx := 0.0 + mut dy := 0.0 + lh := f32(0.0) + white := C.sfons_rgba(255, 255, 255, 255) + black := C.sfons_rgba(0, 0, 0, 255) + brown := C.sfons_rgba(192, 128, 0, 128) + blue := C.sfons_rgba(0, 192, 255, 255) + state.fons.clear_state() + sgl.defaults() + sgl.matrix_mode_projection() + sgl.ortho(0.0, f32(C.sapp_width()), f32(C.sapp_height()), 0.0, -1.0, 1.0) + sx = 0 + sy = 50 + dx = sx + dy = sy + state.fons.set_font(state.font_normal) + state.fons.set_size(100.0) + ascender := f32(0.0) + descender := f32(0.0) + state.fons.vert_metrics(&ascender, &descender, &lh) + dx = sx + dy += lh + C.fonsSetColor(state.fons, white) + dx = C.fonsDrawText(state.fons, dx, dy, c'The quick ', C.NULL) + C.fonsSetFont(state.fons, state.font_normal) + C.fonsSetSize(state.fons, 48.0) + C.fonsSetColor(state.fons, brown) + dx = C.fonsDrawText(state.fons, dx, dy, c'brown ', C.NULL) + C.fonsSetFont(state.fons, state.font_normal) + C.fonsSetSize(state.fons, 24.0) + C.fonsSetColor(state.fons, white) + dx = C.fonsDrawText(state.fons, dx, dy, c'fox ', C.NULL) + dx = sx + dy += lh * 1.2 + C.fonsSetSize(state.fons, 20.0) + C.fonsSetFont(state.fons, state.font_normal) + C.fonsSetColor(state.fons, blue) + C.fonsDrawText(state.fons, dx, dy, c'Now is the time for all good men to come to the aid of the party.', + C.NULL) + dx = 300 + dy = 350 + C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE) + C.fonsSetSize(state.fons, 60.0) + C.fonsSetFont(state.fons, state.font_normal) + C.fonsSetColor(state.fons, white) + C.fonsSetSpacing(state.fons, 5.0) + C.fonsSetBlur(state.fons, 6.0) + C.fonsDrawText(state.fons, dx, dy, c'Blurry...', C.NULL) + dx = 300 + dy += 50.0 + C.fonsSetSize(state.fons, 28.0) + C.fonsSetFont(state.fons, state.font_normal) + C.fonsSetColor(state.fons, white) + C.fonsSetSpacing(state.fons, 0.0) + C.fonsSetBlur(state.fons, 3.0) + C.fonsDrawText(state.fons, dx, dy + 2, c'DROP SHADOW', C.NULL) + C.fonsSetColor(state.fons, black) + C.fonsSetBlur(state.fons, 0) + C.fonsDrawText(state.fons, dx, dy, c'DROP SHADOW', C.NULL) + C.fonsSetSize(state.fons, 18.0) + C.fonsSetFont(state.fons, state.font_normal) + C.fonsSetColor(state.fons, white) + dx = 50 + dy = 350 + line(f32(dx - 10), f32(dy), f32(dx + 250), f32(dy)) + C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP) + dx = C.fonsDrawText(state.fons, dx, dy, c'Top', C.NULL) + dx += 10 + C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_MIDDLE) + dx = C.fonsDrawText(state.fons, dx, dy, c'Middle', C.NULL) + dx += 10 + C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE) + dx = C.fonsDrawText(state.fons, dx, dy, c'Baseline', C.NULL) + dx += 10 + C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BOTTOM) + C.fonsDrawText(state.fons, dx, dy, c'Bottom', C.NULL) + dx = 150 + dy = 400 + line(f32(dx), f32(dy - 30), f32(dx), f32(dy + 80.0)) + C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE) + C.fonsDrawText(state.fons, dx, dy, c'Left', C.NULL) + dy += 30 + C.fonsSetAlign(state.fons, C.FONS_ALIGN_CENTER | C.FONS_ALIGN_BASELINE) + C.fonsDrawText(state.fons, dx, dy, c'Center', C.NULL) + dy += 30 + C.fonsSetAlign(state.fons, C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_BASELINE) + C.fonsDrawText(state.fons, dx, dy, c'Right', C.NULL) + C.sfons_flush(state.fons) +} + +fn line(sx f32, sy f32, ex f32, ey f32) { + sgl.begin_lines() + sgl.c4b(255, 255, 0, 128) + sgl.v2f(sx, sy) + sgl.v2f(ex, ey) + sgl.end() +} diff --git a/v_windows/v/old/examples/sokol/freetype_raven.v b/v_windows/v/old/examples/sokol/freetype_raven.v new file mode 100644 index 0000000..d65b01f --- /dev/null +++ b/v_windows/v/old/examples/sokol/freetype_raven.v @@ -0,0 +1,153 @@ +import sokol +import sokol.sapp +import sokol.gfx +import sokol.sgl +import sokol.sfons +import os +import time + +const ( + text = ' +Once upon a midnight dreary, while I pondered, weak and weary, +Over many a quaint and curious volume of forgotten lore— + While I nodded, nearly napping, suddenly there came a tapping, +As of some one gently rapping, rapping at my chamber door. +“’Tis some visitor,” I muttered, “tapping at my chamber door— + Only this and nothing more.” + + Ah, distinctly I remember it was in the bleak December; +And each separate dying ember wrought its ghost upon the floor. + Eagerly I wished the morrow;—vainly I had sought to borrow + From my books surcease of sorrow—sorrow for the lost Lenore— +For the rare and radiant maiden whom the angels name Lenore— + Nameless here for evermore. + + And the silken, sad, uncertain rustling of each purple curtain +Thrilled me—filled me with fantastic terrors never felt before; + So that now, to still the beating of my heart, I stood repeating + “’Tis some visitor entreating entrance at my chamber door— +Some late visitor entreating entrance at my chamber door;— + This it is and nothing more.” + + Presently my soul grew stronger; hesitating then no longer, +“Sir,” said I, “or Madam, truly your forgiveness I implore; + But the fact is I was napping, and so gently you came rapping, + And so faintly you came tapping, tapping at my chamber door, +That I scarce was sure I heard you”—here I opened wide the door;— + Darkness there and nothing more. + +Deep into that darkness peering, long I stood there wondering, fearing, +Doubting, dreaming dreams no mortal ever dared to dream before; + But the silence was unbroken, and the stillness gave no token, + And the only word there spoken was the whispered word, “Lenore?” +This I whispered, and an echo murmured back the word, “Lenore!”— + Merely this and nothing more. + + Back into the chamber turning, all my soul within me burning, +Soon again I heard a tapping somewhat louder than before. + “Surely,” said I, “surely that is something at my window lattice; + Let me see, then, what thereat is, and this mystery explore— +Let my heart be still a moment and this mystery explore;— + ’Tis the wind and nothing more!” +' + lines = text.split('\n') +) + +struct AppState { +mut: + pass_action C.sg_pass_action + fons &C.FONScontext + font_normal int + inited bool +} + +fn main() { + mut color_action := C.sg_color_attachment_action{ + action: gfx.Action(C.SG_ACTION_CLEAR) + value: C.sg_color{ + r: 1.0 + g: 1.0 + b: 1.0 + a: 1.0 + } + } + mut pass_action := C.sg_pass_action{} + pass_action.colors[0] = color_action + state := &AppState{ + pass_action: pass_action + fons: &C.FONScontext(0) + } + title := 'V Metal/GL Text Rendering' + desc := C.sapp_desc{ + user_data: state + init_userdata_cb: init + frame_userdata_cb: frame + window_title: title.str + html5_canvas_name: title.str + width: 600 + height: 700 + high_dpi: true + } + sapp.run(&desc) +} + +fn init(user_data voidptr) { + mut state := &AppState(user_data) + desc := sapp.create_desc() + gfx.setup(&desc) + s := &C.sgl_desc_t{} + C.sgl_setup(s) + state.fons = sfons.create(512, 512, 1) + // or use DroidSerif-Regular.ttf + if bytes := os.read_bytes(os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf')) { + println('loaded font: $bytes.len') + state.font_normal = C.fonsAddFontMem(state.fons, c'sans', bytes.data, bytes.len, + false) + } +} + +fn frame(user_data voidptr) { + t := time.ticks() + mut state := &AppState(user_data) + state.render_font() + gfx.begin_default_pass(&state.pass_action, sapp.width(), sapp.height()) + sgl.draw() + gfx.end_pass() + gfx.commit() + println(time.ticks() - t) +} + +const ( + black = C.sfons_rgba(0, 0, 0, 255) +) + +fn (mut state AppState) render_font() { + lh := 30 + mut dy := lh + if !state.inited { + state.fons.clear_state() + sgl.defaults() + sgl.matrix_mode_projection() + sgl.ortho(0.0, f32(C.sapp_width()), f32(C.sapp_height()), 0.0, -1.0, 1.0) + state.fons.set_font(state.font_normal) + state.fons.set_size(100.0) + C.fonsSetColor(state.fons, black) + C.fonsSetFont(state.fons, state.font_normal) + C.fonsSetSize(state.fons, 35.0) + state.inited = true + } + + for line in lines { + C.fonsDrawText(state.fons, 40, dy, line.str, C.NULL) + dy += lh + } + C.sfons_flush(state.fons) +} + +fn line(sx f32, sy f32, ex f32, ey f32) { + sgl.begin_lines() + sgl.c4b(255, 255, 0, 128) + sgl.v2f(sx, sy) + sgl.v2f(ex, ey) + sgl.end() +} diff --git a/v_windows/v/old/examples/sokol/particles/modules/particle/LICENSE b/v_windows/v/old/examples/sokol/particles/modules/particle/LICENSE new file mode 100644 index 0000000..1595a57 --- /dev/null +++ b/v_windows/v/old/examples/sokol/particles/modules/particle/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Lars Pontoppidan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/v_windows/v/old/examples/sokol/particles/modules/particle/color.v b/v_windows/v/old/examples/sokol/particles/modules/particle/color.v new file mode 100644 index 0000000..535e48a --- /dev/null +++ b/v_windows/v/old/examples/sokol/particles/modules/particle/color.v @@ -0,0 +1,12 @@ +// Copyright(C) 2019 Lars Pontoppidan. All rights reserved. +// Use of this source code is governed by an MIT license file distributed with this software package +module particle + +// * Color +pub struct Color { +mut: + r byte + g byte + b byte + a byte +} diff --git a/v_windows/v/old/examples/sokol/particles/modules/particle/particle.v b/v_windows/v/old/examples/sokol/particles/modules/particle/particle.v new file mode 100644 index 0000000..ac172b9 --- /dev/null +++ b/v_windows/v/old/examples/sokol/particles/modules/particle/particle.v @@ -0,0 +1,81 @@ +// Copyright(C) 2019 Lars Pontoppidan. All rights reserved. +// Use of this source code is governed by an MIT license file distributed with this software package +module particle + +import particle.vec2 +import sokol.sgl + +const ( + default_life_time = 1000 + default_v_color = Color{93, 136, 193, 255} +) + +// * Module public +pub fn new(location vec2.Vec2) &Particle { + p := &Particle{ + location: location + velocity: vec2.Vec2{0, 0} + acceleration: vec2.Vec2{0, 0} + color: particle.default_v_color + life_time: particle.default_life_time + life_time_init: particle.default_life_time + } + return p +} + +fn remap(v f64, min f64, max f64, new_min f64, new_max f64) f64 { + return (((v - min) * (new_max - new_min)) / (max - min)) + new_min +} + +// Particle +pub struct Particle { +mut: + location vec2.Vec2 + velocity vec2.Vec2 + acceleration vec2.Vec2 + color Color + life_time f64 + life_time_init f64 +} + +pub fn (mut p Particle) update(dt f64) { + mut acc := p.acceleration + acc.multiply_f64(dt) + p.velocity = p.velocity.add(acc) + p.location = p.location.add(p.velocity) + lt := p.life_time - (1000 * dt) + if lt > 0 { + p.life_time = lt + p.color.r = p.color.r - 1 // byte(remap(p.life_time,0.0,p.life_time_init,0,p.color.r)) + p.color.g = p.color.g - 1 // byte(remap(p.life_time,0.0,p.life_time_init,0,p.color.g)) + p.color.b = p.color.b - 1 // byte(remap(p.life_time,0.0,p.life_time_init,0,p.color.b)) + p.color.a = byte(int(remap(p.life_time, 0.0, p.life_time_init, 0, 255))) - 10 + } else { + p.life_time = 0 + } +} + +pub fn (p Particle) is_dead() bool { + return p.life_time <= 0.0 +} + +pub fn (p Particle) draw() { + l := p.location + sgl.c4b(p.color.r, p.color.g, p.color.b, p.color.a) + lx := f32(l.x) + ly := f32(l.y) + sgl.v2f(lx, ly) + sgl.v2f(lx + 2, ly) + sgl.v2f(lx + 2, ly + 2) + sgl.v2f(lx, ly + 2) +} + +pub fn (mut p Particle) reset() { + p.location.zero() + p.acceleration.zero() + p.velocity.zero() + // p.color = Color{93, 136, 193, 255} + p.color = particle.default_v_color + p.life_time = particle.default_life_time + p.life_time_init = p.life_time +} diff --git a/v_windows/v/old/examples/sokol/particles/modules/particle/system.v b/v_windows/v/old/examples/sokol/particles/modules/particle/system.v new file mode 100644 index 0000000..4f0382b --- /dev/null +++ b/v_windows/v/old/examples/sokol/particles/modules/particle/system.v @@ -0,0 +1,99 @@ +// Copyright(C) 2019 Lars Pontoppidan. All rights reserved. +// Use of this source code is governed by an MIT license file distributed with this software package +module particle + +import particle.vec2 +import rand +import sokol.sgl + +pub struct SystemConfig { + pool int +} + +pub struct System { + width int + height int +mut: + pool []&Particle + bin []&Particle +} + +pub fn (mut s System) init(sc SystemConfig) { + for i := 0; i < sc.pool; i++ { + p := new(vec2.Vec2{f32(s.width) * 0.5, f32(s.height) * 0.5}) + s.bin << p + } +} + +pub fn (mut s System) update(dt f64) { + mut p := &Particle(0) + for i := 0; i < s.pool.len; i++ { + p = s.pool[i] + p.update(dt) + if p.is_dead() { + s.bin << p + s.pool.delete(i) + } + } +} + +pub fn (s System) draw() { + sgl.begin_quads() + for p in s.pool { + p.draw() + } + sgl.end() +} + +pub fn (mut s System) reset() { + for i in 0 .. s.pool.len { + mut p := s.pool[i] + p.reset() + p.life_time = 0 + } + for i in 0 .. s.bin.len { + mut p := s.pool[i] + p.reset() + p.life_time = 0 + } +} + +pub fn (mut s System) explode(x f32, y f32) { + mut reserve := 500 + center := vec2.Vec2{x, y} + mut p := &Particle(0) + for i := 0; i < s.bin.len && reserve > 0; i++ { + p = s.bin[i] + p.reset() + p.location.from(center) + p.acceleration = vec2.Vec2{rand.f32_in_range(-0.5, 0.5), rand.f32_in_range(-0.5, + 0.5)} + p.velocity = vec2.Vec2{rand.f32_in_range(-0.5, 0.5), rand.f32_in_range(-0.5, 0.5)} + p.life_time = rand.f64_in_range(500, 2000) + s.pool << p + s.bin.delete(i) + reserve-- + } +} + +pub fn (mut s System) free() { + for p in s.pool { + if p == 0 { + print(ptr_str(p) + ' ouch') + continue + } + unsafe { free(p) } + } + s.pool.clear() + for p in s.bin { + if p == 0 { + print(ptr_str(p) + ' ouch') + continue + } + unsafe { + // println('Freeing from bin') + free(p) + } + } + s.bin.clear() +} diff --git a/v_windows/v/old/examples/sokol/particles/modules/particle/v.mod b/v_windows/v/old/examples/sokol/particles/modules/particle/v.mod new file mode 100644 index 0000000..e69de29 diff --git a/v_windows/v/old/examples/sokol/particles/modules/particle/vec2/v.mod b/v_windows/v/old/examples/sokol/particles/modules/particle/vec2/v.mod new file mode 100644 index 0000000..e69de29 diff --git a/v_windows/v/old/examples/sokol/particles/modules/particle/vec2/vec2.v b/v_windows/v/old/examples/sokol/particles/modules/particle/vec2/vec2.v new file mode 100644 index 0000000..ec41485 --- /dev/null +++ b/v_windows/v/old/examples/sokol/particles/modules/particle/vec2/vec2.v @@ -0,0 +1,89 @@ +// Copyright(C) 2019 Lars Pontoppidan. All rights reserved. +// Use of this source code is governed by an MIT license file distributed with this software package +module vec2 + +pub struct Vec2 { +pub mut: + x f64 + y f64 +} + +pub fn (mut v Vec2) zero() { + v.x = 0.0 + v.y = 0.0 +} + +pub fn (mut v Vec2) from(src Vec2) { + v.x = src.x + v.y = src.y +} + +// * Addition +// + operator overload. Adds two vectors +pub fn (v1 Vec2) + (v2 Vec2) Vec2 { + return Vec2{v1.x + v2.x, v1.y + v2.y} +} + +pub fn (v Vec2) add(vector Vec2) Vec2 { + return Vec2{v.x + vector.x, v.y + vector.y} +} + +pub fn (v Vec2) add_f64(scalar f64) Vec2 { + return Vec2{v.x + scalar, v.y + scalar} +} + +pub fn (mut v Vec2) plus(vector Vec2) { + v.x += vector.x + v.y += vector.y +} + +pub fn (mut v Vec2) plus_f64(scalar f64) { + v.x += scalar + v.y += scalar +} + +// * Subtraction +pub fn (v1 Vec2) - (v2 Vec2) Vec2 { + return Vec2{v1.x - v2.x, v1.y - v2.y} +} + +pub fn (v Vec2) sub(vector Vec2) Vec2 { + return Vec2{v.x - vector.x, v.y - vector.y} +} + +pub fn (v Vec2) sub_f64(scalar f64) Vec2 { + return Vec2{v.x - scalar, v.y - scalar} +} + +pub fn (mut v Vec2) subtract(vector Vec2) { + v.x -= vector.x + v.y -= vector.y +} + +pub fn (mut v Vec2) subtract_f64(scalar f64) { + v.x -= scalar + v.y -= scalar +} + +// * Multiplication +pub fn (v1 Vec2) * (v2 Vec2) Vec2 { + return Vec2{v1.x * v2.x, v1.y * v2.y} +} + +pub fn (v Vec2) mul(vector Vec2) Vec2 { + return Vec2{v.x * vector.x, v.y * vector.y} +} + +pub fn (v Vec2) mul_f64(scalar f64) Vec2 { + return Vec2{v.x * scalar, v.y * scalar} +} + +pub fn (mut v Vec2) multiply(vector Vec2) { + v.x *= vector.x + v.y *= vector.y +} + +pub fn (mut v Vec2) multiply_f64(scalar f64) { + v.x *= scalar + v.y *= scalar +} diff --git a/v_windows/v/old/examples/sokol/particles/particles.v b/v_windows/v/old/examples/sokol/particles/particles.v new file mode 100644 index 0000000..05b0cb3 --- /dev/null +++ b/v_windows/v/old/examples/sokol/particles/particles.v @@ -0,0 +1,155 @@ +// Copyright(C) 2019 Lars Pontoppidan. All rights reserved. +// Use of this source code is governed by an MIT license file distributed with this software package +module main + +import time +import sokol +import sokol.sapp +import sokol.gfx +import sokol.sgl +import particle + +const ( + used_import = sokol.used_import +) + +fn main() { + mut app := &App{ + width: 800 + height: 400 + pass_action: gfx.create_clear_pass(0.1, 0.1, 0.1, 1.0) + } + app.init() + app.run() +} + +struct App { + pass_action C.sg_pass_action +mut: + width int + height int + frame i64 + last i64 + ps particle.System + alpha_pip C.sgl_pipeline +} + +fn (mut a App) init() { + a.frame = 0 + a.last = time.ticks() + a.ps = particle.System{ + width: a.width + height: a.height + } + a.ps.init(particle.SystemConfig{ + pool: 20000 + }) +} + +fn (mut a App) cleanup() { + a.ps.free() +} + +fn (mut a App) run() { + title := 'V Particle Example' + desc := C.sapp_desc{ + width: a.width + height: a.height + user_data: a + init_userdata_cb: init + frame_userdata_cb: frame + event_userdata_cb: event + window_title: title.str + html5_canvas_name: title.str + cleanup_userdata_cb: cleanup + } + sapp.run(&desc) +} + +fn (a App) draw() { + sgl.load_pipeline(a.alpha_pip) + a.ps.draw() +} + +fn init(user_data voidptr) { + mut app := &App(user_data) + desc := sapp.create_desc() + gfx.setup(&desc) + sgl_desc := C.sgl_desc_t{ + max_vertices: 50 * 65536 + } + sgl.setup(&sgl_desc) + mut pipdesc := C.sg_pipeline_desc{} + unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) } + + color_state := C.sg_color_state{ + blend: C.sg_blend_state{ + enabled: true + src_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_SRC_ALPHA) + dst_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA) + } + } + pipdesc.colors[0] = color_state + + app.alpha_pip = sgl.make_pipeline(&pipdesc) +} + +fn cleanup(user_data voidptr) { + mut app := &App(user_data) + app.cleanup() + gfx.shutdown() +} + +fn frame(user_data voidptr) { + mut app := &App(user_data) + app.width = sapp.width() + app.height = sapp.height() + t := time.ticks() + dt := f64(t - app.last) / 1000.0 + app.ps.update(dt) + draw(app) + gfx.begin_default_pass(&app.pass_action, app.width, app.height) + sgl.default_pipeline() + sgl.draw() + gfx.end_pass() + gfx.commit() + app.frame++ + app.last = t +} + +fn event(ev &C.sapp_event, mut app App) { + if ev.@type == .mouse_move { + app.ps.explode(ev.mouse_x, ev.mouse_y) + } + if ev.@type == .mouse_up || ev.@type == .mouse_down { + if ev.mouse_button == .left { + is_pressed := ev.@type == .mouse_down + if is_pressed { + app.ps.explode(ev.mouse_x, ev.mouse_y) + } + } + } + if ev.@type == .key_up || ev.@type == .key_down { + if ev.key_code == .r { + is_pressed := ev.@type == .key_down + if is_pressed { + app.ps.reset() + } + } + } + if ev.@type == .touches_began || ev.@type == .touches_moved { + if ev.num_touches > 0 { + touch_point := ev.touches[0] + app.ps.explode(touch_point.pos_x, touch_point.pos_y) + } + } +} + +fn draw(a &App) { + sgl.defaults() + sgl.matrix_mode_projection() + sgl.ortho(0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0) + sgl.push_matrix() + a.draw() + sgl.pop_matrix() +} diff --git a/v_windows/v/old/examples/sokol/sounds/melody.v b/v_windows/v/old/examples/sokol/sounds/melody.v new file mode 100644 index 0000000..aa5ebc4 --- /dev/null +++ b/v_windows/v/old/examples/sokol/sounds/melody.v @@ -0,0 +1,75 @@ +import gg +import gx +import sokol.audio + +const credits = 'Based on the ByteBeat formula from: https://www.youtube.com/watch?v=V4GfkFbDojc \n "Techno" by Gabriel Miceli' + +struct AppState { +mut: + gframe int // the current graphical frame + frame_0 int // offset of the current audio frames, relative to the start of the music + frames [2048]f32 // a copy of the last rendered audio frames + gg &gg.Context // used for drawing +} + +fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int, mut acontext AppState) { + mut soundbuffer := unsafe { buffer } + for frame := 0; frame < num_frames; frame++ { + t := int(f32(acontext.frame_0 + frame) * 0.245) + // "Techno" by Gabriel Miceli + y := (t * (((t / 10 | 0) ^ ((t / 10 | 0) - 1280)) % 11) / 2 & 127) + + (t * (((t / 640 | 0) ^ ((t / 640 | 0) - 2)) % 13) / 2 & 127) + for ch := 0; ch < num_channels; ch++ { + idx := frame * num_channels + ch + unsafe { + a := f32(byte(y) - 127) / 255.0 + soundbuffer[idx] = a + acontext.frames[idx & 2047] = a + } + } + } + acontext.frame_0 += num_frames +} + +fn main() { + println(credits) + mut state := &AppState{ + gg: 0 + } + audio.setup( + stream_userdata_cb: my_audio_stream_callback + user_data: state + ) + state.gg = gg.new_context( + bg_color: gx.rgb(50, 50, 50) + width: 1024 + height: 400 + create_window: true + window_title: 'ByteBeat Music' + frame_fn: graphics_frame + user_data: state + ) + state.gg.run() + audio.shutdown() +} + +fn graphics_frame(mut state AppState) { + state.gframe++ + state.gg.begin() + state.draw() + state.gg.end() +} + +[inline] +fn (mut state AppState) bsample(idx int) byte { + return byte(127 + state.frames[(state.gframe + idx) & 2047] * 128) +} + +fn (mut state AppState) draw() { + // first, reset and setup ortho projection + for x in 0 .. 1024 { + mut y := 100 * (state.frames[2 * x] + state.frames[2 * x + 1]) + state.gg.draw_line(x, 200, x, 200 + y, gx.rgba(state.bsample(x), state.bsample(x + 300), + state.bsample(x + 700), 255)) + } +} diff --git a/v_windows/v/old/examples/sokol/sounds/simple_sin_tones.v b/v_windows/v/old/examples/sokol/sounds/simple_sin_tones.v new file mode 100644 index 0000000..c327117 --- /dev/null +++ b/v_windows/v/old/examples/sokol/sounds/simple_sin_tones.v @@ -0,0 +1,42 @@ +import time +import math +import sokol.audio + +const ( + sw = time.new_stopwatch() + sw_start_ms = sw.elapsed().milliseconds() +) + +[inline] +fn sintone(periods int, frame int, num_frames int) f32 { + return math.sinf(f32(periods) * (2 * math.pi) * f32(frame) / f32(num_frames)) +} + +fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int) { + ms := sw.elapsed().milliseconds() - sw_start_ms + unsafe { + mut soundbuffer := buffer + for frame := 0; frame < num_frames; frame++ { + for ch := 0; ch < num_channels; ch++ { + idx := frame * num_channels + ch + if ms < 250 { + soundbuffer[idx] = 0.5 * sintone(20, frame, num_frames) + } else if ms < 300 { + soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames) + } else if ms < 1500 { + soundbuffer[idx] *= sintone(22, frame, num_frames) + } else { + soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames) + } + } + } + } +} + +fn main() { + audio.setup( + stream_cb: my_audio_stream_callback + ) + time.sleep(2000 * time.millisecond) + audio.shutdown() +} diff --git a/v_windows/v/old/examples/sokol/sounds/uhoh.wav b/v_windows/v/old/examples/sokol/sounds/uhoh.wav new file mode 100644 index 0000000..cff9e3c Binary files /dev/null and b/v_windows/v/old/examples/sokol/sounds/uhoh.wav differ diff --git a/v_windows/v/old/examples/sokol/sounds/wav_player.v b/v_windows/v/old/examples/sokol/sounds/wav_player.v new file mode 100644 index 0000000..3c9419b --- /dev/null +++ b/v_windows/v/old/examples/sokol/sounds/wav_player.v @@ -0,0 +1,209 @@ +import os +import time +import sokol.audio + +struct Player { +mut: + samples []f32 + pos int + finished bool +} + +fn main() { + if os.args.len < 2 { + eprintln('Usage: play_wav file1.wav file2.wav ...') + play_sounds([os.resource_abs_path('uhoh.wav')]) ? + exit(1) + } + play_sounds(os.args[1..]) ? +} + +fn play_sounds(files []string) ? { + mut player := Player{} + player.init() + for f in files { + if !os.exists(f) || os.is_dir(f) { + eprintln('skipping "$f" (does not exist)') + continue + } + fext := os.file_ext(f).to_lower() + if fext != '.wav' { + eprintln('skipping "$f" (not a .wav file)') + continue + } + player.play_wav_file(f) ? + } + player.stop() +} + +// +fn audio_player_callback(buffer &f32, num_frames int, num_channels int, mut p Player) { + if p.finished { + return + } + ntotal := num_channels * num_frames + nremaining := p.samples.len - p.pos + nsamples := if nremaining < ntotal { nremaining } else { ntotal } + if nsamples <= 0 { + p.finished = true + return + } + unsafe { C.memcpy(buffer, &p.samples[p.pos], nsamples * int(sizeof(f32))) } + p.pos += nsamples +} + +fn (mut p Player) init() { + audio.setup( + num_channels: 2 + stream_userdata_cb: audio_player_callback + user_data: p + ) +} + +fn (mut p Player) stop() { + audio.shutdown() + p.free() +} + +fn (mut p Player) play_wav_file(fpath string) ? { + println('> play_wav_file: $fpath') + samples := read_wav_file_samples(fpath) ? + p.finished = true + p.samples << samples + p.finished = false + for !p.finished { + time.sleep(16 * time.millisecond) + } + p.free() +} + +fn (mut p Player) free() { + p.finished = false + p.samples = []f32{} + p.pos = 0 +} + +// The read_wav_file_samples function below is based on the following sources: +// http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html +// http://www.lightlink.com/tjweber/StripWav/WAVE.html +// http://www.lightlink.com/tjweber/StripWav/Canon.html +// https://tools.ietf.org/html/draft-ema-vpim-wav-00 +// NB: > The chunks MAY appear in any order except that the Format chunk +// > MUST be placed before the Sound data chunk (but not necessarily +// > contiguous to the Sound data chunk). +struct RIFFHeader { + riff [4]byte + file_size u32 + form_type [4]byte +} + +struct RIFFChunkHeader { + chunk_type [4]byte + chunk_size u32 + chunk_data voidptr +} + +struct RIFFFormat { + format_tag u16 // PCM = 1; Values other than 1 indicate some form of compression. + nchannels u16 // Nc ; 1 = mono ; 2 = stereo + sample_rate u32 // F + avg_bytes_per_second u32 // F * M*Nc + nblock_align u16 // M*Nc + bits_per_sample u16 // 8 * M + cbsize u16 // Size of the extension: 22 + valid_bits_per_sample u16 // at most 8*M + channel_mask u32 // Speaker position mask + sub_format [16]byte // GUID +} + +fn read_wav_file_samples(fpath string) ?[]f32 { + mut res := []f32{} + // eprintln('> read_wav_file_samples: $fpath -------------------------------------------------') + mut bytes := os.read_bytes(fpath) ? + mut pbytes := &byte(bytes.data) + mut offset := u32(0) + rh := unsafe { &RIFFHeader(pbytes) } + // eprintln('rh: $rh') + if rh.riff != [byte(`R`), `I`, `F`, `F`]! { + return error('WAV should start with `RIFF`') + } + if rh.form_type != [byte(`W`), `A`, `V`, `E`]! { + return error('WAV should have `WAVE` form type') + } + if rh.file_size + 8 != bytes.len { + return error('WAV should have valid lenght') + } + offset += sizeof(RIFFHeader) + mut rf := &RIFFFormat(0) + for { + if offset >= bytes.len { + break + } + // + ch := unsafe { &RIFFChunkHeader(pbytes + offset) } + offset += 8 + ch.chunk_size + // eprintln('ch: $ch') + // eprintln('p: $pbytes | offset: $offset | bytes.len: $bytes.len') + // //////// + if ch.chunk_type == [byte(`L`), `I`, `S`, `T`]! { + continue + } + // + if ch.chunk_type == [byte(`i`), `d`, `3`, ` `]! { + continue + } + // + if ch.chunk_type == [byte(`f`), `m`, `t`, ` `]! { + // eprintln('`fmt ` chunk') + rf = unsafe { &RIFFFormat(&ch.chunk_data) } + // eprintln('fmt riff format: $rf') + if rf.format_tag != 1 { + return error('only PCM encoded WAVs are supported') + } + if rf.nchannels < 1 || rf.nchannels > 2 { + return error('only mono or stereo WAVs are supported') + } + if rf.bits_per_sample !in [u16(8), 16] { + return error('only 8 or 16 bits per sample WAVs are supported') + } + continue + } + // + if ch.chunk_type == [byte(`d`), `a`, `t`, `a`]! { + if rf == 0 { + return error('`data` chunk should be after `fmt ` chunk') + } + // eprintln('`fmt ` chunk: $rf\n`data` chunk: $ch') + mut doffset := 0 + mut dp := unsafe { &byte(&ch.chunk_data) } + for doffset < ch.chunk_size { + for c := 0; c < rf.nchannels; c++ { + mut x := f32(0.0) + mut step := 0 + ppos := unsafe { dp + doffset } + if rf.bits_per_sample == 8 { + d8 := unsafe { &byte(ppos) } + x = (f32(*d8) - 128) / 128.0 + step = 1 + doffset++ + } + if rf.bits_per_sample == 16 { + d16 := unsafe { &i16(ppos) } + x = f32(*d16) / 32768.0 + step = 2 + } + doffset += step + if doffset < ch.chunk_size { + res << x + if rf.nchannels == 1 { + // Duplicating single channel mono sounds, + // produces a stereo sound, simplifying further processing: + res << x + } + } + } + } + } + } + return res +} -- cgit v1.2.3