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

examples: add examples/sokol/05_instancing_glsl, cleanup code (#8809)

This commit is contained in:
penguindark
2021-02-18 10:11:26 +01:00
committed by GitHub
parent b3a26ca0ce
commit 198b395cde
9 changed files with 883 additions and 353 deletions

View File

@ -68,7 +68,9 @@ mut:
ticks i64
}
// Texture functions
/******************************************************************************
* 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{
@ -108,9 +110,9 @@ fn update_text_texture(sg_img C.sg_image, w int, h int, buf byteptr) {
C.sg_update_image(sg_img, &tmp_sbc)
}
// Draw functions
/*
/******************************************************************************
* 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
@ -138,30 +140,36 @@ fn init_cube_glsl(mut app App) {
d := f32(1.0)
c := u32(0xFFFFFF_FF) // color RGBA8
vertices := [
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},
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},
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},
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},
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},
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 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{}
@ -174,7 +182,7 @@ fn init_cube_glsl(mut app App) {
// create an index buffer for the cube
indices := [
0, 1, 2, 0, 2, 3,
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,
@ -183,13 +191,13 @@ fn init_cube_glsl(mut app App) {
]
mut index_buffer_desc := C.sg_buffer_desc{}
unsafe { C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc)) }
index_buffer_desc.size = indices.len * int(sizeof(int))
unsafe {C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc))}
index_buffer_desc.size = indices.len * int(sizeof(u16))
index_buffer_desc.content = byteptr(indices.data)
index_buffer_desc.@type = .indexbuffer
index_buffer_desc.label = 'cube-indices'.str
index_buffer_desc.@type = .indexbuffer
index_buffer_desc.label = "cube-indices".str
ibuf := gfx.make_buffer(&index_buffer_desc)
// create shader
shader := gfx.make_shader(C.rt_shader_desc())
@ -198,13 +206,13 @@ fn init_cube_glsl(mut app App) {
pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
// the constants [C.ATTR_vs_pos, C.ATTR_vs_color0, C.ATTR_vs_texcoord0] are generated by sokol-shdc
pipdesc.layout.attrs[C.ATTR_vs_pos].format = .float3 // x,y,z as f32
pipdesc.layout.attrs[C.ATTR_vs_color0].format = .ubyte4n // color as u32
pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .float2 // u,v as f32
pipdesc.layout.attrs[C.ATTR_vs_pos ].format = .float3 // x,y,z as f32
pipdesc.layout.attrs[C.ATTR_vs_color0 ].format = .ubyte4n // color as u32
pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .float2 // u,v as f32
// pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .short2n // u,v as u16
pipdesc.shader = shader
pipdesc.index_type = .uint32
pipdesc.index_type = .uint16
pipdesc.depth_stencil = C.sg_depth_stencil_state{
depth_write_enabled: true
@ -222,18 +230,23 @@ fn init_cube_glsl(mut app App) {
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(m4.Vec4{ e: [f32(0.0), 0.0, 6, 0]! }, m4.Vec4{ e: [f32(0), 0, 0, 0]! }, m4.Vec4{ e: [f32(0), 1.0, 0, 0]! })
proj := m4.perspective(60, w/h, 0.01, 10.0)
view := m4.look_at(vec4(f32(0.0) ,0 , 6, 0), vec4(f32(0), 0, 0, 0), vec4(f32(0), 1, 0, 0))
view_proj := view * proj
rxm := m4.rotate(m4.rad(rx), m4.Vec4{ e: [f32(1), 0, 0, 0]! })
rym := m4.rotate(m4.rad(ry), m4.Vec4{ e: [f32(0), 1, 0, 0]! })
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(m4.Vec4{ e: [in_scale, in_scale, in_scale, 1]! })
model := rym * rxm
scale_m := m4.scale(vec4(in_scale, in_scale, in_scale, 1))
res := (scale_m * model) * view_proj
res := (scale_m * model) * view_proj
return res
}
@ -258,22 +271,22 @@ fn draw_cube_glsl(app App) {
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
gfx.apply_uniforms(C.SG_SHADERSTAGE_VS, C.SLOT_vs_params, &tr_matrix, 4 * 16)
// fragment shader uniforms
// *** 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 */
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 */,
0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h
]!
gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params, &tmp_fs_params, int(sizeof(tmp_fs_params)))
@ -303,7 +316,9 @@ fn frame(mut app App) {
app.frame_count++
}
// Init / Cleanup
/******************************************************************************
* 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!!
@ -326,22 +341,22 @@ fn my_init(mut app App) {
x := (i & 0xFF) >> 5 // 8 cell
// upper left corner
if x == 0 && y == 0 {
tmp_txt[i] = byte(0xFF)
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] = byte(0)
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] = byte(col) // red
tmp_txt[i + 1] = byte(col) // green
tmp_txt[i + 2] = byte(col) // blue
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
@ -360,7 +375,9 @@ fn cleanup(mut app App) {
gfx.shutdown()
}
// events handling:
/******************************************************************************
* 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)
@ -375,8 +392,10 @@ fn my_event_manager(mut ev gg.Event, mut app App) {
}
}
// [console] is needed for easier diagnostics on windows
[console]
/******************************************************************************
* Main
******************************************************************************/
[console] // is needed for easier diagnostics on windows
fn main() {
// App init
mut app := &App{