examples: support better placment and scaling on nonsquare viewports in cube.v (#8513)
parent
17062dc5c8
commit
975206f38e
|
@ -31,7 +31,7 @@ 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
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ fn create_texture(w int, h int, buf byteptr) C.sg_image{
|
||||||
ptr: buf
|
ptr: buf
|
||||||
size: sz
|
size: sz
|
||||||
}
|
}
|
||||||
|
|
||||||
sg_img := C.sg_make_image(&img_desc)
|
sg_img := C.sg_make_image(&img_desc)
|
||||||
return sg_img
|
return sg_img
|
||||||
}
|
}
|
||||||
|
@ -136,7 +136,7 @@ fn cube() {
|
||||||
fn draw_cubes(app App) {
|
fn draw_cubes(app App) {
|
||||||
rot := [f32(1.0)*(app.frame_count % 360), 0.5*f32(app.frame_count%360)]
|
rot := [f32(1.0)*(app.frame_count % 360), 0.5*f32(app.frame_count%360)]
|
||||||
//rot := [f32(app.mouse_x), f32(app.mouse_y)]
|
//rot := [f32(app.mouse_x), f32(app.mouse_y)]
|
||||||
|
|
||||||
sgl.defaults()
|
sgl.defaults()
|
||||||
sgl.load_pipeline(app.pip_3d)
|
sgl.load_pipeline(app.pip_3d)
|
||||||
|
|
||||||
|
@ -240,7 +240,7 @@ fn cube_field(app App){
|
||||||
rot := [f32(app.mouse_x), f32(app.mouse_y)]
|
rot := [f32(app.mouse_x), f32(app.mouse_y)]
|
||||||
xyz_sz := f32(2.0)
|
xyz_sz := f32(2.0)
|
||||||
field_size := 20
|
field_size := 20
|
||||||
|
|
||||||
sgl.defaults()
|
sgl.defaults()
|
||||||
sgl.load_pipeline(app.pip_3d)
|
sgl.load_pipeline(app.pip_3d)
|
||||||
|
|
||||||
|
@ -249,13 +249,13 @@ fn cube_field(app App){
|
||||||
|
|
||||||
sgl.matrix_mode_projection()
|
sgl.matrix_mode_projection()
|
||||||
sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 200.0)
|
sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 200.0)
|
||||||
|
|
||||||
sgl.matrix_mode_modelview()
|
sgl.matrix_mode_modelview()
|
||||||
|
|
||||||
sgl.translate(field_size, 0.0, -120.0)
|
sgl.translate(field_size, 0.0, -120.0)
|
||||||
sgl.rotate(sgl.rad(rot[0]), 0.0, 1.0, 0.0)
|
sgl.rotate(sgl.rad(rot[0]), 0.0, 1.0, 0.0)
|
||||||
sgl.rotate(sgl.rad(rot[1]), 1.0, 0.0, 0.0)
|
sgl.rotate(sgl.rad(rot[1]), 1.0, 0.0, 0.0)
|
||||||
|
|
||||||
// draw field_size*field_size cubes
|
// draw field_size*field_size cubes
|
||||||
for y in 0..field_size {
|
for y in 0..field_size {
|
||||||
for x in 0..field_size {
|
for x in 0..field_size {
|
||||||
|
@ -265,41 +265,43 @@ fn cube_field(app App){
|
||||||
cube_t(f32(f32(x)/field_size), f32(f32(y)/field_size),1)
|
cube_t(f32(f32(x)/field_size), f32(f32(y)/field_size),1)
|
||||||
sgl.pop_matrix()
|
sgl.pop_matrix()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sgl.disable_texture()
|
sgl.disable_texture()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn frame(mut app App) {
|
fn frame(mut app App) {
|
||||||
dw := app.gg.width
|
ws := gg.window_size()
|
||||||
dh := app.gg.height
|
ratio := f32(ws.width)/ws.height
|
||||||
ww := dh/2 /* not a bug */
|
dw := ws.width
|
||||||
hh := dh/2
|
dh := ws.height
|
||||||
x0 := dw/2 - hh
|
ww := int(dh/3) /* not a bug */
|
||||||
|
hh := int(dh/3)
|
||||||
|
x0 := int(f32(dw) * 0.05)
|
||||||
//x1 := dw/2
|
//x1 := dw/2
|
||||||
y0 := 0
|
y0 := 0
|
||||||
y1 := dh/2
|
y1 := int(f32(dh) * 0.5)
|
||||||
|
|
||||||
app.gg.begin()
|
app.gg.begin()
|
||||||
//sgl.defaults()
|
//sgl.defaults()
|
||||||
|
|
||||||
// 2d triangle
|
// 2d triangle
|
||||||
sgl.viewport(x0, y0, ww, hh, true)
|
sgl.viewport(x0, y0, ww, hh, true)
|
||||||
draw_triangle()
|
draw_triangle()
|
||||||
|
|
||||||
// colored cubes with viewport
|
// colored cubes with viewport
|
||||||
sgl.viewport(x0, y1, ww, hh, true)
|
sgl.viewport(x0, y1, ww, hh, true)
|
||||||
draw_cubes(app)
|
draw_cubes(app)
|
||||||
|
|
||||||
// textured cubed with viewport
|
// textured cubed with viewport
|
||||||
sgl.viewport(0, 0, dw, dh, true)
|
sgl.viewport(0, int(dh/5), dw, int(dh*ratio), true)
|
||||||
draw_texture_cubes(app)
|
draw_texture_cubes(app)
|
||||||
|
|
||||||
// textured field of cubes with viewport
|
// textured field of cubes with viewport
|
||||||
sgl.viewport(0, 0, dw, dh, true)
|
sgl.viewport(0, int(dh/5), dw, int(dh*ratio), true)
|
||||||
cube_field(app)
|
cube_field(app)
|
||||||
|
|
||||||
app.frame_count++
|
app.frame_count++
|
||||||
|
|
||||||
app.gg.end()
|
app.gg.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -310,7 +312,7 @@ fn frame(mut app App) {
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
fn my_init(mut app App) {
|
fn my_init(mut app App) {
|
||||||
app.init_flag = true
|
app.init_flag = true
|
||||||
|
|
||||||
// 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()
|
||||||
|
@ -319,7 +321,7 @@ fn my_init(mut app App) {
|
||||||
max_vertices: 50 * 65536
|
max_vertices: 50 * 65536
|
||||||
}
|
}
|
||||||
sgl.setup(&sgl_desc)
|
sgl.setup(&sgl_desc)
|
||||||
|
|
||||||
// 3d pipeline
|
// 3d pipeline
|
||||||
mut pipdesc := C.sg_pipeline_desc{}
|
mut pipdesc := C.sg_pipeline_desc{}
|
||||||
unsafe {C.memset(&pipdesc, 0, sizeof(pipdesc))}
|
unsafe {C.memset(&pipdesc, 0, sizeof(pipdesc))}
|
||||||
|
@ -334,7 +336,7 @@ fn my_init(mut app App) {
|
||||||
cull_mode: .back
|
cull_mode: .back
|
||||||
}
|
}
|
||||||
app.pip_3d = sgl.make_pipeline(&pipdesc)
|
app.pip_3d = sgl.make_pipeline(&pipdesc)
|
||||||
|
|
||||||
// create chessboard texture 256*256 RGBA
|
// create chessboard texture 256*256 RGBA
|
||||||
w := 256
|
w := 256
|
||||||
h := 256
|
h := 256
|
||||||
|
@ -359,7 +361,7 @@ fn my_init(mut app App) {
|
||||||
tmp_txt[i+2] = byte(0)
|
tmp_txt[i+2] = byte(0)
|
||||||
tmp_txt[i+3] = byte(0xFF)
|
tmp_txt[i+3] = byte(0xFF)
|
||||||
} else {
|
} else {
|
||||||
col := if ((x+y) & 1) == 1 {0xFF} else {0}
|
col := if ((x+y) & 1) == 1 {0xFF} else {0}
|
||||||
tmp_txt[i ] = byte(col) // red
|
tmp_txt[i ] = byte(col) // red
|
||||||
tmp_txt[i+1] = byte(col) // green
|
tmp_txt[i+1] = byte(col) // green
|
||||||
tmp_txt[i+2] = byte(col) // blue
|
tmp_txt[i+2] = byte(col) // blue
|
||||||
|
@ -386,6 +388,13 @@ fn my_event_manager(mut ev sapp.Event, mut app App) {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
if ev.typ == .touches_began || ev.typ == .touches_moved {
|
||||||
|
if ev.num_touches > 0 {
|
||||||
|
touch_point := ev.touches[0]
|
||||||
|
app.mouse_x = int(touch_point.pos_x)
|
||||||
|
app.mouse_y = int(touch_point.pos_y)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
|
|
Loading…
Reference in New Issue