examples: fix and vfmt cube_glsl example
parent
d4a05bebde
commit
60a8881326
|
@ -33,7 +33,6 @@
|
||||||
import gg
|
import gg
|
||||||
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
|
||||||
|
@ -43,6 +42,7 @@ import gg.m4
|
||||||
// GLSL Include and functions
|
// GLSL Include and functions
|
||||||
#flag -I @VROOT/.
|
#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)
|
#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
|
fn C.cube_shader_desc() &C.sg_shader_desc
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -58,14 +58,11 @@ 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
|
||||||
|
|
||||||
// glsl
|
// glsl
|
||||||
cube_pip_glsl C.sg_pipeline
|
cube_pip_glsl C.sg_pipeline
|
||||||
cube_bind C.sg_bindings
|
cube_bind C.sg_bindings
|
||||||
|
|
||||||
// time
|
// time
|
||||||
ticks i64
|
ticks i64
|
||||||
}
|
}
|
||||||
|
@ -252,7 +249,6 @@ struct Vertex_t {
|
||||||
y f32
|
y f32
|
||||||
z f32
|
z f32
|
||||||
color u32
|
color u32
|
||||||
|
|
||||||
// u u16
|
// u u16
|
||||||
// v u16
|
// v u16
|
||||||
u f32
|
u f32
|
||||||
|
@ -260,37 +256,31 @@ struct Vertex_t {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_cube_glsl(mut app App) {
|
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 := u16(32767/8) // for compatibility with D3D11, 32767 stand for 1
|
||||||
d := f32(1.0) // 0.05)
|
d := f32(1.0) // 0.05)
|
||||||
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},
|
||||||
|
@ -303,17 +293,47 @@ fn init_cube_glsl(mut app App) {
|
||||||
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.usage = .immutable
|
// 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)
|
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,
|
||||||
14, 13, 12, 15, 14, 12,
|
0,
|
||||||
16, 17, 18, 16, 18, 19,
|
2,
|
||||||
22, 21, 20, 23, 22, 20
|
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{}
|
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.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.cube_shader_desc())
|
shader := gfx.make_shader(C.cube_shader_desc())
|
||||||
|
|
||||||
mut pipdesc := C.sg_pipeline_desc{}
|
mut pipdesc := C.sg_pipeline_desc{}
|
||||||
|
@ -347,13 +367,13 @@ fn init_cube_glsl(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
|
||||||
|
|
||||||
app.cube_bind.vertex_buffers[0] = vbuf
|
app.cube_bind.vertex_buffers[0] = vbuf
|
||||||
app.cube_bind.index_buffer = ibuf
|
app.cube_bind.index_buffer = ibuf
|
||||||
app.cube_bind.fs_images[C.SLOT_tex] = app.texture
|
app.cube_bind.fs_images[C.SLOT_tex] = app.texture
|
||||||
app.cube_pip_glsl = gfx.make_pipeline(&pipdesc)
|
app.cube_pip_glsl = gfx.make_pipeline(&pipdesc)
|
||||||
println("GLSL init DONE!")
|
println('GLSL init DONE!')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_cube_glsl(app App) {
|
fn draw_cube_glsl(app App) {
|
||||||
|
@ -385,9 +405,10 @@ fn draw_cube_glsl(app App){
|
||||||
// fs uniforms
|
// fs uniforms
|
||||||
time_ticks := f32(time.ticks() - app.ticks) / 1000
|
time_ticks := f32(time.ticks() - app.ticks) / 1000
|
||||||
mut text_res := [
|
mut text_res := [
|
||||||
f32(512), 512, // x,y resolution to pass to FS
|
f32(512),
|
||||||
time_ticks, // time as f32
|
512, /* x,y resolution to pass to FS */
|
||||||
0 // padding 4 Bytes == 1 f32
|
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)
|
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
|
ratio := f32(ws.width) / ws.height
|
||||||
dw := ws.width
|
dw := ws.width
|
||||||
dh := ws.height
|
dh := ws.height
|
||||||
ww := int(dh/3) /* not a bug */
|
ww := int(dh / 3) // not a bug
|
||||||
hh := int(dh / 3)
|
hh := int(dh / 3)
|
||||||
x0 := int(f32(dw) * 0.05)
|
x0 := int(f32(dw) * 0.05)
|
||||||
// x1 := dw/2
|
// x1 := dw/2
|
||||||
|
@ -476,9 +497,7 @@ fn frame(mut app App) {
|
||||||
// glsl cube
|
// glsl cube
|
||||||
draw_cube_glsl(app)
|
draw_cube_glsl(app)
|
||||||
|
|
||||||
|
|
||||||
app.frame_count++
|
app.frame_count++
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
|
@ -487,8 +506,6 @@ fn frame(mut app App) {
|
||||||
*
|
*
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
fn my_init(mut app App) {
|
fn my_init(mut app App) {
|
||||||
|
|
||||||
|
|
||||||
// set max vertices,
|
// set max vertices,
|
||||||
// for a large number of the same type of object it is better use the instances!!
|
// for a large number of the same type of object it is better use the instances!!
|
||||||
desc := sapp.create_desc()
|
desc := sapp.create_desc()
|
||||||
|
@ -563,7 +580,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_move {
|
if ev.typ == .mouse_move {
|
||||||
app.mouse_x = int(ev.mouse_x)
|
app.mouse_x = int(ev.mouse_x)
|
||||||
app.mouse_y = int(ev.mouse_y)
|
app.mouse_y = int(ev.mouse_y)
|
||||||
|
@ -593,7 +610,7 @@ fn main(){
|
||||||
a[0] = 2
|
a[0] = 2
|
||||||
println(a)
|
println(a)
|
||||||
|
|
||||||
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
|
||||||
|
@ -605,7 +622,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()
|
||||||
|
|
Loading…
Reference in New Issue