examples: fix and vfmt cube_glsl example

pull/8805/head
Alexander Medvednikov 2021-02-17 06:47:41 +01:00
parent d4a05bebde
commit 60a8881326
1 changed files with 242 additions and 225 deletions

View File

@ -33,7 +33,6 @@
import gg
import gx
// import math
import sokol.sapp
import sokol.gfx
import sokol.sgl
@ -43,6 +42,7 @@ import gg.m4
// GLSL Include and functions
#flag -I @VROOT/.
#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() &C.sg_shader_desc
const (
@ -58,14 +58,11 @@ mut:
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
}
@ -252,7 +249,6 @@ struct Vertex_t {
y f32
z f32
color u32
// u u16
// v u16
u f32
@ -260,37 +256,31 @@ struct Vertex_t {
}
fn init_cube_glsl(mut app App) {
/* cube vertex buffer */
// 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 := [
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},
@ -303,17 +293,47 @@ fn init_cube_glsl(mut app App) {
vert_buffer_desc.content = byteptr(vertices.data)
vert_buffer_desc.@type = .vertexbuffer
// vert_buffer_desc.usage = .immutable
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,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
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{}
@ -321,10 +341,10 @@ fn init_cube_glsl(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.cube_shader_desc())
mut pipdesc := C.sg_pipeline_desc{}
@ -347,13 +367,13 @@ fn init_cube_glsl(mut app App) {
pipdesc.rasterizer = C.sg_rasterizer_state{
cull_mode: .back
}
pipdesc.label = "glsl_shader pipeline".str
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!")
println('GLSL init DONE!')
}
fn draw_cube_glsl(app App) {
@ -385,9 +405,10 @@ fn draw_cube_glsl(app App){
// 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
f32(512),
512, /* x,y resolution to pass to FS */
time_ticks, /* time as f32 */
0 /* padding 4 Bytes == 1 f32 */,
]!
gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params, &text_res, 4 * 4)
@ -435,7 +456,7 @@ fn frame(mut app App) {
ratio := f32(ws.width) / ws.height
dw := ws.width
dh := ws.height
ww := int(dh/3) /* not a bug */
ww := int(dh / 3) // not a bug
hh := int(dh / 3)
x0 := int(f32(dw) * 0.05)
// x1 := dw/2
@ -476,9 +497,7 @@ fn frame(mut app App) {
// glsl cube
draw_cube_glsl(app)
app.frame_count++
}
/******************************************************************************
@ -487,8 +506,6 @@ fn frame(mut app App) {
*
******************************************************************************/
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()
@ -563,7 +580,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_move {
app.mouse_x = int(ev.mouse_x)
app.mouse_y = int(ev.mouse_y)
@ -593,7 +610,7 @@ fn main(){
a[0] = 2
println(a)
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
@ -605,7 +622,7 @@ fn main(){
init_fn: my_init
cleanup_fn: cleanup
event_fn: my_event_manager
})
)
app.ticks = time.ticks()
app.gg.run()