examples: fix rt_glsl.v

pull/8805/head
Alexander Medvednikov 2021-02-17 06:57:25 +01:00
parent ba131ce914
commit 023f6829a1
1 changed files with 260 additions and 236 deletions

View File

@ -36,11 +36,9 @@ import gg
import gg.m4
import gx
// import math
import sokol.sapp
import sokol.gfx
import sokol.sgl
import time
// GLSL Include and functions
@ -48,7 +46,9 @@ import time
#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_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_puppy_shader_desc() &C.sg_shader_desc
const (
@ -63,18 +63,14 @@ mut:
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
}
@ -144,7 +140,6 @@ struct Vertex_t {
y f32
z f32
color u32
// u u16 // for compatibility with D3D11
// v u16 // for compatibility with D3D11
u f32
@ -153,42 +148,35 @@ struct Vertex_t {
// march shader init
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 := 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},
]
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.content = byteptr(vertices.data)
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)
/* create an index buffer for the cube */
// create an index buffer for the cube
indices := [
u16(0), 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
u16(0),
1,
2,
0,
2,
3,
6,
5,
4,
7,
6,
4,
8,
9,
10,
8,
10,
11,
/*
u16(14), 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
@ -216,10 +219,10 @@ fn init_cube_glsl_m(mut app App) {
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.label = 'cube-indices'.str
ibuf := gfx.make_buffer(&index_buffer_desc)
/* create shader */
// create shader
shader := gfx.make_shader(C.rt_march_shader_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{
cull_mode: .back
}
pipdesc.label = "glsl_shader pipeline".str
pipdesc.label = 'glsl_shader pipeline'.str
mut bind := C.sg_bindings{}
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)
println("GLSL March init DONE!")
println('GLSL March init DONE!')
}
// putty shader init
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 := 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},
]
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.content = byteptr(vertices.data)
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)
/* create an index buffer for the cube */
// create an index buffer for the cube
indices := [
/*
u16(0), 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
*/
u16(14), 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
u16(14),
13,
12,
15,
14,
12,
16,
17,
18,
16,
18,
19,
22,
21,
20,
23,
22,
20,
]
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.content = byteptr(indices.data)
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)
/* create shader */
// create shader
shader := gfx.make_shader(C.rt_puppy_shader_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{
cull_mode: .back
}
pipdesc.label = "glsl_shader pipeline".str
pipdesc.label = 'glsl_shader pipeline'.str
mut bind := C.sg_bindings{}
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)
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 {
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
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), 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]! })
@ -404,13 +422,16 @@ fn draw_cube_glsl_m(app App){
// 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
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 */,
]!
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
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
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 */,
]!
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
*
******************************************************************************/
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 {
app.mouse_down = true
}
@ -606,7 +630,7 @@ fn main(){
gg: 0
}
app.gg = gg.new_context({
app.gg = gg.new_context(
width: win_width
height: win_height
use_ortho: true // This is needed for 2D drawing
@ -618,7 +642,7 @@ fn main(){
init_fn: my_init
cleanup_fn: cleanup
event_fn: my_event_manager
})
)
app.ticks = time.ticks()
app.gg.run()