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

examples: fix rt_glsl.v

This commit is contained in:
Alexander Medvednikov 2021-02-17 06:57:25 +01:00
parent ba131ce914
commit 023f6829a1

View File

@ -36,11 +36,9 @@ import gg
import gg.m4 import gg.m4
import gx import gx
// import math // import math
import sokol.sapp import sokol.sapp
import sokol.gfx import sokol.gfx
import sokol.sgl import sokol.sgl
import time import time
// GLSL Include and functions // GLSL Include and functions
@ -48,7 +46,9 @@ import time
#flag -I @VROOT/. #flag -I @VROOT/.
#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_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) #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() &C.sg_shader_desc fn C.rt_march_shader_desc() &C.sg_shader_desc
fn C.rt_puppy_shader_desc() &C.sg_shader_desc fn C.rt_puppy_shader_desc() &C.sg_shader_desc
const ( const (
@ -63,18 +63,14 @@ mut:
texture C.sg_image texture C.sg_image
init_flag bool init_flag bool
frame_count int frame_count int
mouse_x int = -1 mouse_x int = -1
mouse_y int = -1 mouse_y int = -1
mouse_down bool mouse_down bool
// glsl // glsl
cube_pip_glsl C.sg_pipeline cube_pip_glsl C.sg_pipeline
cube_bind C.sg_bindings cube_bind C.sg_bindings
pipe map[string]C.sg_pipeline pipe map[string]C.sg_pipeline
bind map[string]C.sg_bindings bind map[string]C.sg_bindings
// time // time
ticks i64 ticks i64
} }
@ -144,7 +140,6 @@ struct Vertex_t {
y f32 y f32
z f32 z f32
color u32 color u32
// u u16 // for compatibility with D3D11 // u u16 // for compatibility with D3D11
// v u16 // for compatibility with D3D11 // v u16 // for compatibility with D3D11
u f32 u f32
@ -153,42 +148,35 @@ struct Vertex_t {
// march shader init // march shader init
fn init_cube_glsl_m(mut app App) { fn init_cube_glsl_m(mut app App) {
/* cube vertex buffer */ // cube vertex buffer
// d := u16(32767) // for compatibility with D3D11, 32767 stand for 1 // d := u16(32767) // for compatibility with D3D11, 32767 stand for 1
d := f32(1.0) d := f32(1.0)
c := u32(0xFFFFFF_FF) // color RGBA8 c := u32(0xFFFFFF_FF) // color RGBA8
vertices := [ vertices := [
Vertex_t{-1.0, -1.0, -1.0, c, 0, 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, 0},
Vertex_t{1.0, 1.0, -1.0, c, d, d}, 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, d},
Vertex_t{-1.0, -1.0, 1.0, c, 0, 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, 0},
Vertex_t{1.0, 1.0, 1.0, c, d, d}, 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, d},
Vertex_t{-1.0, -1.0, -1.0, c, 0, 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, 0},
Vertex_t{-1.0, 1.0, 1.0, c, d, d}, 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, d},
Vertex_t{1.0, -1.0, -1.0, c, 0, 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, 0},
Vertex_t{1.0, 1.0, 1.0, c, d, d}, 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, d},
Vertex_t{-1.0, -1.0, -1.0, c, 0, 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, 0},
Vertex_t{1.0, -1.0, 1.0, c, d, d}, 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, d},
Vertex_t{-1.0, 1.0, -1.0, c, 0, 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, 0},
Vertex_t{1.0, 1.0, 1.0, c, d, d}, 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, d},
] ]
mut vert_buffer_desc := C.sg_buffer_desc{} mut vert_buffer_desc := C.sg_buffer_desc{}
@ -196,14 +184,29 @@ fn init_cube_glsl_m(mut app App) {
vert_buffer_desc.size = vertices.len * int(sizeof(Vertex_t)) vert_buffer_desc.size = vertices.len * int(sizeof(Vertex_t))
vert_buffer_desc.content = byteptr(vertices.data) vert_buffer_desc.content = byteptr(vertices.data)
vert_buffer_desc.@type = .vertexbuffer vert_buffer_desc.@type = .vertexbuffer
vert_buffer_desc.label = "cube-vertices".str vert_buffer_desc.label = 'cube-vertices'.str
vbuf := gfx.make_buffer(&vert_buffer_desc) vbuf := gfx.make_buffer(&vert_buffer_desc)
/* create an index buffer for the cube */ // create an index buffer for the cube
indices := [ indices := [
u16(0), 1, 2, 0, 2, 3, u16(0),
6, 5, 4, 7, 6, 4, 1,
8, 9, 10, 8, 10, 11, 2,
0,
2,
3,
6,
5,
4,
7,
6,
4,
8,
9,
10,
8,
10,
11,
/* /*
u16(14), 13, 12, 15, 14, 12, u16(14), 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19, 16, 17, 18, 16, 18, 19,
@ -216,10 +219,10 @@ fn init_cube_glsl_m(mut app App) {
index_buffer_desc.size = indices.len * int(sizeof(u16)) index_buffer_desc.size = indices.len * int(sizeof(u16))
index_buffer_desc.content = byteptr(indices.data) index_buffer_desc.content = byteptr(indices.data)
index_buffer_desc.@type = .indexbuffer index_buffer_desc.@type = .indexbuffer
index_buffer_desc.label = "cube-indices".str index_buffer_desc.label = 'cube-indices'.str
ibuf := gfx.make_buffer(&index_buffer_desc) ibuf := gfx.make_buffer(&index_buffer_desc)
/* create shader */ // create shader
shader := gfx.make_shader(C.rt_march_shader_desc()) shader := gfx.make_shader(C.rt_march_shader_desc())
mut pipdesc := C.sg_pipeline_desc{} mut pipdesc := C.sg_pipeline_desc{}
@ -242,7 +245,7 @@ fn init_cube_glsl_m(mut app App) {
pipdesc.rasterizer = C.sg_rasterizer_state{ pipdesc.rasterizer = C.sg_rasterizer_state{
cull_mode: .back cull_mode: .back
} }
pipdesc.label = "glsl_shader pipeline".str pipdesc.label = 'glsl_shader pipeline'.str
mut bind := C.sg_bindings{} mut bind := C.sg_bindings{}
unsafe { C.memset(&bind, 0, sizeof(bind)) } unsafe { C.memset(&bind, 0, sizeof(bind)) }
@ -253,47 +256,40 @@ fn init_cube_glsl_m(mut app App) {
app.pipe['march'] = gfx.make_pipeline(&pipdesc) app.pipe['march'] = gfx.make_pipeline(&pipdesc)
println("GLSL March init DONE!") println('GLSL March init DONE!')
} }
// putty shader init // putty shader init
fn init_cube_glsl_p(mut app App) { fn init_cube_glsl_p(mut app App) {
/* cube vertex buffer */ // cube vertex buffer
// d := u16(32767) // for compatibility with D3D11, 32767 stand for 1 // d := u16(32767) // for compatibility with D3D11, 32767 stand for 1
d := f32(1.0) d := f32(1.0)
c := u32(0xFFFFFF_FF) // color RGBA8 c := u32(0xFFFFFF_FF) // color RGBA8
vertices := [ vertices := [
Vertex_t{-1.0, -1.0, -1.0, c, 0, 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, 0},
Vertex_t{1.0, 1.0, -1.0, c, d, d}, 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, d},
Vertex_t{-1.0, -1.0, 1.0, c, 0, 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, 0},
Vertex_t{1.0, 1.0, 1.0, c, d, d}, 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, d},
Vertex_t{-1.0, -1.0, -1.0, c, 0, 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, 0},
Vertex_t{-1.0, 1.0, 1.0, c, d, d}, 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, d},
Vertex_t{1.0, -1.0, -1.0, c, 0, 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, 0},
Vertex_t{1.0, 1.0, 1.0, c, d, d}, 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, d},
Vertex_t{-1.0, -1.0, -1.0, c, 0, 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, 0},
Vertex_t{1.0, -1.0, 1.0, c, d, d}, 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, d},
Vertex_t{-1.0, 1.0, -1.0, c, 0, 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, 0},
Vertex_t{1.0, 1.0, 1.0, c, d, d}, 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, d},
] ]
mut vert_buffer_desc := C.sg_buffer_desc{} mut vert_buffer_desc := C.sg_buffer_desc{}
@ -301,20 +297,34 @@ fn init_cube_glsl_p(mut app App) {
vert_buffer_desc.size = vertices.len * int(sizeof(Vertex_t)) vert_buffer_desc.size = vertices.len * int(sizeof(Vertex_t))
vert_buffer_desc.content = byteptr(vertices.data) vert_buffer_desc.content = byteptr(vertices.data)
vert_buffer_desc.@type = .vertexbuffer vert_buffer_desc.@type = .vertexbuffer
vert_buffer_desc.label = "cube-vertices".str vert_buffer_desc.label = 'cube-vertices'.str
vbuf := gfx.make_buffer(&vert_buffer_desc) vbuf := gfx.make_buffer(&vert_buffer_desc)
/* create an index buffer for the cube */ // create an index buffer for the cube
indices := [ indices := [
/* /*
u16(0), 1, 2, 0, 2, 3, u16(0), 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4, 6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11, 8, 9, 10, 8, 10, 11,
*/ */
u16(14), 13, 12, 15, 14, 12, u16(14),
16, 17, 18, 16, 18, 19, 13,
22, 21, 20, 23, 22, 20 12,
15,
14,
12,
16,
17,
18,
16,
18,
19,
22,
21,
20,
23,
22,
20,
] ]
mut index_buffer_desc := C.sg_buffer_desc{} mut index_buffer_desc := C.sg_buffer_desc{}
@ -322,10 +332,10 @@ fn init_cube_glsl_p(mut app App) {
index_buffer_desc.size = indices.len * int(sizeof(u16)) index_buffer_desc.size = indices.len * int(sizeof(u16))
index_buffer_desc.content = byteptr(indices.data) index_buffer_desc.content = byteptr(indices.data)
index_buffer_desc.@type = .indexbuffer index_buffer_desc.@type = .indexbuffer
index_buffer_desc.label = "cube-indices".str index_buffer_desc.label = 'cube-indices'.str
ibuf := gfx.make_buffer(&index_buffer_desc) ibuf := gfx.make_buffer(&index_buffer_desc)
/* create shader */ // create shader
shader := gfx.make_shader(C.rt_puppy_shader_desc()) shader := gfx.make_shader(C.rt_puppy_shader_desc())
mut pipdesc := C.sg_pipeline_desc{} mut pipdesc := C.sg_pipeline_desc{}
@ -348,7 +358,7 @@ fn init_cube_glsl_p(mut app App) {
pipdesc.rasterizer = C.sg_rasterizer_state{ pipdesc.rasterizer = C.sg_rasterizer_state{
cull_mode: .back cull_mode: .back
} }
pipdesc.label = "glsl_shader pipeline".str pipdesc.label = 'glsl_shader pipeline'.str
mut bind := C.sg_bindings{} mut bind := C.sg_bindings{}
unsafe { C.memset(&bind, 0, sizeof(bind)) } unsafe { C.memset(&bind, 0, sizeof(bind)) }
@ -359,16 +369,24 @@ fn init_cube_glsl_p(mut app App) {
app.pipe['puppy'] = gfx.make_pipeline(&pipdesc) app.pipe['puppy'] = gfx.make_pipeline(&pipdesc)
println("GLSL Puppy init DONE!") println('GLSL Puppy init DONE!')
} }
fn calc_tr_matrices(w f32, h f32, rx f32, ry f32, in_scale f32) m4.Mat4 { fn calc_tr_matrices(w f32, h f32, rx f32, ry f32, in_scale f32) m4.Mat4 {
proj := m4.perspective(60, w / h, 0.01, 10.0) 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]!}) 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]!
})
view_proj := view * proj view_proj := view * proj
rxm := m4.rotate(m4.rad(rx), m4.Vec4{e:[f32(1),0,0,0]!}) rxm := m4.rotate(m4.rad(rx), m4.Vec4{
rym := m4.rotate(m4.rad(ry), m4.Vec4{e:[f32(0),1,0,0]!}) e: [f32(1), 0, 0, 0]!
})
rym := m4.rotate(m4.rad(ry), m4.Vec4{
e: [f32(0), 1, 0, 0]!
})
model := rym * rxm model := rym * rxm
scale_m := m4.scale(m4.Vec4{ e: [in_scale, in_scale, in_scale, 1]! }) scale_m := m4.scale(m4.Vec4{ e: [in_scale, in_scale, in_scale, 1]! })
@ -404,13 +422,16 @@ fn draw_cube_glsl_m(app App){
// fragment shader uniforms // fragment shader uniforms
time_ticks := f32(time.ticks() - app.ticks) / 1000 time_ticks := f32(time.ticks() - app.ticks) / 1000
mut tmp_fs_params := [ mut tmp_fs_params := [
f32(ws.width), ws.height * ratio, // x,y resolution to pass to FS f32(ws.width),
0,0, // dont send mouse position ws.height * ratio, /* x,y resolution to pass to FS */
//app.mouse_x, // mouse x 0,
//ws.height - app.mouse_y*2, // mouse y scaled 0, /* dont send mouse position */
time_ticks, // time as f32 /* app.mouse_x, // mouse x */
app.frame_count, // frame count /* ws.height - app.mouse_y*2, // mouse y scaled */
0,0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h time_ticks, /* time as f32 */
app.frame_count, /* frame count */
0,
0 /* padding bytes , see "fs_params" struct paddings in rt_glsl.h */,
]! ]!
gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params_m, &tmp_fs_params, int(sizeof(tmp_fs_params))) gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params_m, &tmp_fs_params, int(sizeof(tmp_fs_params)))
@ -446,13 +467,16 @@ fn draw_cube_glsl_p(app App){
// fragment shader uniforms // fragment shader uniforms
time_ticks := f32(time.ticks() - app.ticks) / 1000 time_ticks := f32(time.ticks() - app.ticks) / 1000
mut tmp_fs_params := [ mut tmp_fs_params := [
f32(ws.width), ws.height * ratio, // x,y resolution to pass to FS f32(ws.width),
0,0, // dont send mouse position ws.height * ratio, /* x,y resolution to pass to FS */
//app.mouse_x, // mouse x 0,
//ws.height - app.mouse_y*2, // mouse y scaled 0, /* dont send mouse position */
time_ticks, // time as f32 /* app.mouse_x, // mouse x */
app.frame_count, // frame count /* ws.height - app.mouse_y*2, // mouse y scaled */
0,0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h time_ticks, /* time as f32 */
app.frame_count, /* frame count */
0,
0 /* padding bytes , see "fs_params" struct paddings in rt_glsl.h */,
]! ]!
gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params_p, &tmp_fs_params, int(sizeof(tmp_fs_params))) gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params_p, &tmp_fs_params, int(sizeof(tmp_fs_params)))
@ -574,7 +598,7 @@ fn cleanup(mut app App) {
* event * event
* *
******************************************************************************/ ******************************************************************************/
fn my_event_manager(mut ev sapp.Event, mut app App) { fn my_event_manager(mut ev gg.Event, mut app App) {
if ev.typ == .mouse_down { if ev.typ == .mouse_down {
app.mouse_down = true app.mouse_down = true
} }
@ -606,7 +630,7 @@ fn main(){
gg: 0 gg: 0
} }
app.gg = gg.new_context({ app.gg = gg.new_context(
width: win_width width: win_width
height: win_height height: win_height
use_ortho: true // This is needed for 2D drawing use_ortho: true // This is needed for 2D drawing
@ -618,7 +642,7 @@ fn main(){
init_fn: my_init init_fn: my_init
cleanup_fn: cleanup cleanup_fn: cleanup
event_fn: my_event_manager event_fn: my_event_manager
}) )
app.ticks = time.ticks() app.ticks = time.ticks()
app.gg.run() app.gg.run()