| 1 | /********************************************************************** |
| 2 | * Sokol 3d cube demo |
| 3 | * Copyright (c) 2021 Dario Deledda. All rights reserved. |
| 4 | * Use of this source code is governed by an MIT license |
| 5 | * that can be found in the LICENSE file. |
| 6 | * TODO: |
| 7 | * - add instancing |
| 8 | * - add an example with shaders |
| 9 | **********************************************************************/ |
| 10 | import gg |
| 11 | import math |
| 12 | import sokol.sapp |
| 13 | import sokol.gfx |
| 14 | import sokol.sgl |
| 15 | |
| 16 | const win_width = 800 |
| 17 | const win_height = 800 |
| 18 | const bg_color = gg.white |
| 19 | |
| 20 | struct App { |
| 21 | mut: |
| 22 | gg &gg.Context = unsafe { nil } |
| 23 | pip_3d sgl.Pipeline |
| 24 | texture gfx.Image |
| 25 | sampler gfx.Sampler |
| 26 | init_flag bool |
| 27 | frame_count int |
| 28 | mouse_x int = -1 |
| 29 | mouse_y int = -1 |
| 30 | } |
| 31 | |
| 32 | fn create_texture(w int, h int, buf &u8) (gfx.Image, gfx.Sampler) { |
| 33 | sz := w * h * 4 |
| 34 | mut img_desc := gfx.ImageDesc{ |
| 35 | width: w |
| 36 | height: h |
| 37 | num_mipmaps: 0 |
| 38 | // usage: .dynamic |
| 39 | label: &char(unsafe { nil }) |
| 40 | d3d11_texture: 0 |
| 41 | } |
| 42 | // comment, if .dynamic is enabled |
| 43 | img_desc.data.subimage[0][0] = gfx.Range{ |
| 44 | ptr: buf |
| 45 | size: usize(sz) |
| 46 | } |
| 47 | |
| 48 | sg_img := gfx.make_image(&img_desc) |
| 49 | |
| 50 | mut smp_desc := gfx.SamplerDesc{ |
| 51 | min_filter: .linear |
| 52 | mag_filter: .linear |
| 53 | wrap_u: .clamp_to_edge |
| 54 | wrap_v: .clamp_to_edge |
| 55 | } |
| 56 | |
| 57 | sg_smp := gfx.make_sampler(&smp_desc) |
| 58 | return sg_img, sg_smp |
| 59 | } |
| 60 | |
| 61 | fn draw_triangle() { |
| 62 | sgl.defaults() |
| 63 | sgl.begin_triangles() |
| 64 | sgl.v2f_c3b(0.0, 0.5, 255, 0, 0) |
| 65 | sgl.v2f_c3b(-0.5, -0.5, 0, 0, 255) |
| 66 | sgl.v2f_c3b(0.5, -0.5, 0, 255, 0) |
| 67 | sgl.end() |
| 68 | } |
| 69 | |
| 70 | // vertex specification for a cube with colored sides and texture coords |
| 71 | fn cube() { |
| 72 | sgl.begin_quads() |
| 73 | // edge color |
| 74 | sgl.c3f(1.0, 0.0, 0.0) |
| 75 | // edge coord |
| 76 | // x,y,z, texture cord: u,v |
| 77 | sgl.v3f_t2f(-1.0, 1.0, -1.0, -1.0, 1.0) |
| 78 | sgl.v3f_t2f(1.0, 1.0, -1.0, 1.0, 1.0) |
| 79 | sgl.v3f_t2f(1.0, -1.0, -1.0, 1.0, -1.0) |
| 80 | sgl.v3f_t2f(-1.0, -1.0, -1.0, -1.0, -1.0) |
| 81 | sgl.c3f(0.0, 1.0, 0.0) |
| 82 | sgl.v3f_t2f(-1.0, -1.0, 1.0, -1.0, 1.0) |
| 83 | sgl.v3f_t2f(1.0, -1.0, 1.0, 1.0, 1.0) |
| 84 | sgl.v3f_t2f(1.0, 1.0, 1.0, 1.0, -1.0) |
| 85 | sgl.v3f_t2f(-1.0, 1.0, 1.0, -1.0, -1.0) |
| 86 | sgl.c3f(0.0, 0.0, 1.0) |
| 87 | sgl.v3f_t2f(-1.0, -1.0, 1.0, -1.0, 1.0) |
| 88 | sgl.v3f_t2f(-1.0, 1.0, 1.0, 1.0, 1.0) |
| 89 | sgl.v3f_t2f(-1.0, 1.0, -1.0, 1.0, -1.0) |
| 90 | sgl.v3f_t2f(-1.0, -1.0, -1.0, -1.0, -1.0) |
| 91 | sgl.c3f(1.0, 0.5, 0.0) |
| 92 | sgl.v3f_t2f(1.0, -1.0, 1.0, -1.0, 1.0) |
| 93 | sgl.v3f_t2f(1.0, -1.0, -1.0, 1.0, 1.0) |
| 94 | sgl.v3f_t2f(1.0, 1.0, -1.0, 1.0, -1.0) |
| 95 | sgl.v3f_t2f(1.0, 1.0, 1.0, -1.0, -1.0) |
| 96 | sgl.c3f(0.0, 0.5, 1.0) |
| 97 | sgl.v3f_t2f(1.0, -1.0, -1.0, -1.0, 1.0) |
| 98 | sgl.v3f_t2f(1.0, -1.0, 1.0, 1.0, 1.0) |
| 99 | sgl.v3f_t2f(-1.0, -1.0, 1.0, 1.0, -1.0) |
| 100 | sgl.v3f_t2f(-1.0, -1.0, -1.0, -1.0, -1.0) |
| 101 | sgl.c3f(1.0, 0.0, 0.5) |
| 102 | sgl.v3f_t2f(-1.0, 1.0, -1.0, -1.0, 1.0) |
| 103 | sgl.v3f_t2f(-1.0, 1.0, 1.0, 1.0, 1.0) |
| 104 | sgl.v3f_t2f(1.0, 1.0, 1.0, 1.0, -1.0) |
| 105 | sgl.v3f_t2f(1.0, 1.0, -1.0, -1.0, -1.0) |
| 106 | sgl.end() |
| 107 | } |
| 108 | |
| 109 | fn draw_cubes(app App) { |
| 110 | rot := [f32(1.0) * (app.frame_count % 360), 0.5 * f32(app.frame_count % 360)] |
| 111 | |
| 112 | sgl.defaults() |
| 113 | sgl.load_pipeline(app.pip_3d) |
| 114 | |
| 115 | sgl.matrix_mode_projection() |
| 116 | sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 100.0) |
| 117 | |
| 118 | sgl.matrix_mode_modelview() |
| 119 | sgl.translate(0.0, 0.0, -12.0) |
| 120 | sgl.rotate(sgl.rad(rot[0]), 1.0, 0.0, 0.0) |
| 121 | sgl.rotate(sgl.rad(rot[1]), 0.0, 1.0, 0.0) |
| 122 | cube() |
| 123 | sgl.push_matrix() |
| 124 | sgl.translate(0.0, 0.0, 3.0) |
| 125 | sgl.scale(0.5, 0.5, 0.5) |
| 126 | sgl.rotate(-2.0 * sgl.rad(rot[0]), 1.0, 0.0, 0.0) |
| 127 | sgl.rotate(-2.0 * sgl.rad(rot[1]), 0.0, 1.0, 0.0) |
| 128 | cube() |
| 129 | sgl.push_matrix() |
| 130 | sgl.translate(0.0, 0.0, 3.0) |
| 131 | sgl.scale(0.5, 0.5, 0.5) |
| 132 | sgl.rotate(-3.0 * sgl.rad(2 * rot[0]), 1.0, 0.0, 0.0) |
| 133 | sgl.rotate(3.0 * sgl.rad(2 * rot[1]), 0.0, 0.0, 1.0) |
| 134 | cube() |
| 135 | sgl.pop_matrix() |
| 136 | sgl.pop_matrix() |
| 137 | } |
| 138 | |
| 139 | fn cube_t(r f32, g f32, b f32) { |
| 140 | sgl.begin_quads() |
| 141 | // edge color |
| 142 | sgl.c3f(r, g, b) |
| 143 | // edge coord |
| 144 | // x,y,z, texture cord: u,v |
| 145 | sgl.v3f_t2f(-1.0, 1.0, -1.0, 0.0, 0.25) |
| 146 | sgl.v3f_t2f(1.0, 1.0, -1.0, 0.25, 0.25) |
| 147 | sgl.v3f_t2f(1.0, -1.0, -1.0, 0.25, 0.0) |
| 148 | sgl.v3f_t2f(-1.0, -1.0, -1.0, 0.0, 0.0) |
| 149 | sgl.c3f(r, g, b) |
| 150 | sgl.v3f_t2f(-1.0, -1.0, 1.0, 0.0, 0.25) |
| 151 | sgl.v3f_t2f(1.0, -1.0, 1.0, 0.25, 0.25) |
| 152 | sgl.v3f_t2f(1.0, 1.0, 1.0, 0.25, 0.0) |
| 153 | sgl.v3f_t2f(-1.0, 1.0, 1.0, 0.0, 0.0) |
| 154 | sgl.c3f(r, g, b) |
| 155 | sgl.v3f_t2f(-1.0, -1.0, 1.0, 0.0, 0.25) |
| 156 | sgl.v3f_t2f(-1.0, 1.0, 1.0, 0.25, 0.25) |
| 157 | sgl.v3f_t2f(-1.0, 1.0, -1.0, 0.25, 0.0) |
| 158 | sgl.v3f_t2f(-1.0, -1.0, -1.0, 0.0, 0.0) |
| 159 | sgl.c3f(r, g, b) |
| 160 | sgl.v3f_t2f(1.0, -1.0, 1.0, 0.0, 0.25) |
| 161 | sgl.v3f_t2f(1.0, -1.0, -1.0, 0.25, 0.25) |
| 162 | sgl.v3f_t2f(1.0, 1.0, -1.0, 0.25, 0.0) |
| 163 | sgl.v3f_t2f(1.0, 1.0, 1.0, 0.0, 0.0) |
| 164 | sgl.c3f(r, g, b) |
| 165 | sgl.v3f_t2f(1.0, -1.0, -1.0, 0.0, 0.25) |
| 166 | sgl.v3f_t2f(1.0, -1.0, 1.0, 0.25, 0.25) |
| 167 | sgl.v3f_t2f(-1.0, -1.0, 1.0, 0.25, 0.0) |
| 168 | sgl.v3f_t2f(-1.0, -1.0, -1.0, 0.0, 0.0) |
| 169 | sgl.c3f(r, g, b) |
| 170 | sgl.v3f_t2f(-1.0, 1.0, -1.0, 0.0, 0.25) |
| 171 | sgl.v3f_t2f(-1.0, 1.0, 1.0, 0.25, 0.25) |
| 172 | sgl.v3f_t2f(1.0, 1.0, 1.0, 0.25, 0.0) |
| 173 | sgl.v3f_t2f(1.0, 1.0, -1.0, 0.0, 0.0) |
| 174 | sgl.end() |
| 175 | } |
| 176 | |
| 177 | fn draw_texture_cubes(app App) { |
| 178 | rot := [f32(app.mouse_x), f32(app.mouse_y)] |
| 179 | sgl.defaults() |
| 180 | sgl.load_pipeline(app.pip_3d) |
| 181 | |
| 182 | sgl.enable_texture() |
| 183 | sgl.texture(app.texture, app.sampler) |
| 184 | |
| 185 | sgl.matrix_mode_projection() |
| 186 | sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 100.0) |
| 187 | |
| 188 | sgl.matrix_mode_modelview() |
| 189 | sgl.translate(0.0, 0.0, -12.0) |
| 190 | sgl.rotate(sgl.rad(rot[0]), 1.0, 0.0, 0.0) |
| 191 | sgl.rotate(sgl.rad(rot[1]), 0.0, 1.0, 0.0) |
| 192 | cube_t(1, 1, 1) |
| 193 | sgl.push_matrix() |
| 194 | sgl.translate(0.0, 0.0, 3.0) |
| 195 | sgl.scale(0.5, 0.5, 0.5) |
| 196 | sgl.rotate(-2.0 * sgl.rad(rot[0]), 1.0, 0.0, 0.0) |
| 197 | sgl.rotate(-2.0 * sgl.rad(rot[1]), 0.0, 1.0, 0.0) |
| 198 | cube_t(1, 1, 1) |
| 199 | sgl.push_matrix() |
| 200 | sgl.translate(0.0, 0.0, 3.0) |
| 201 | sgl.scale(0.5, 0.5, 0.5) |
| 202 | sgl.rotate(-3.0 * sgl.rad(2 * rot[0]), 1.0, 0.0, 0.0) |
| 203 | sgl.rotate(3.0 * sgl.rad(2 * rot[1]), 0.0, 0.0, 1.0) |
| 204 | cube_t(1, 1, 1) |
| 205 | sgl.pop_matrix() |
| 206 | sgl.pop_matrix() |
| 207 | |
| 208 | sgl.disable_texture() |
| 209 | } |
| 210 | |
| 211 | fn cube_field(app App) { |
| 212 | rot := [f32(app.mouse_x), f32(app.mouse_y)] |
| 213 | xyz_sz := f32(2.0) |
| 214 | field_size := 20 |
| 215 | |
| 216 | sgl.defaults() |
| 217 | sgl.load_pipeline(app.pip_3d) |
| 218 | |
| 219 | sgl.enable_texture() |
| 220 | sgl.texture(app.texture, app.sampler) |
| 221 | |
| 222 | sgl.matrix_mode_projection() |
| 223 | sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 200.0) |
| 224 | |
| 225 | sgl.matrix_mode_modelview() |
| 226 | |
| 227 | sgl.translate(field_size, 0.0, -120.0) |
| 228 | sgl.rotate(sgl.rad(rot[0]), 0.0, 1.0, 0.0) |
| 229 | sgl.rotate(sgl.rad(rot[1]), 1.0, 0.0, 0.0) |
| 230 | |
| 231 | // draw field_size*field_size cubes |
| 232 | for y in 0 .. field_size { |
| 233 | for x in 0 .. field_size { |
| 234 | sgl.push_matrix() |
| 235 | z := f32(math.cos(f32(x * 2) / field_size) * math.sin(f32(y * 2) / field_size) * xyz_sz) * (xyz_sz * 5) |
| 236 | sgl.translate(x * xyz_sz, z, y * xyz_sz) |
| 237 | cube_t(f32(f32(x) / field_size), f32(f32(y) / field_size), 1) |
| 238 | sgl.pop_matrix() |
| 239 | } |
| 240 | } |
| 241 | sgl.disable_texture() |
| 242 | } |
| 243 | |
| 244 | fn frame(mut app App) { |
| 245 | ws := gg.window_size_real_pixels() |
| 246 | ratio := f32(ws.width) / ws.height |
| 247 | dw := ws.width |
| 248 | dh := ws.height |
| 249 | ww := int(dh / 3) // not a bug |
| 250 | hh := int(dh / 3) |
| 251 | x0 := int(f32(dw) * 0.05) |
| 252 | // x1 := dw/2 |
| 253 | y0 := 0 |
| 254 | y1 := int(f32(dh) * 0.5) |
| 255 | |
| 256 | app.gg.begin() |
| 257 | // sgl.defaults() |
| 258 | |
| 259 | // 2d triangle |
| 260 | sgl.viewport(x0, y0, ww, hh, true) |
| 261 | draw_triangle() |
| 262 | |
| 263 | // colored cubes with viewport |
| 264 | sgl.viewport(x0, y1, ww, hh, true) |
| 265 | draw_cubes(app) |
| 266 | |
| 267 | // textured cubed with viewport |
| 268 | sgl.viewport(0, int(dh / 5), dw, int(dh * ratio), true) |
| 269 | draw_texture_cubes(app) |
| 270 | |
| 271 | // textured field of cubes with viewport |
| 272 | sgl.viewport(0, int(dh / 5), dw, int(dh * ratio), true) |
| 273 | cube_field(app) |
| 274 | |
| 275 | app.frame_count++ |
| 276 | |
| 277 | app.gg.end() |
| 278 | } |
| 279 | |
| 280 | fn my_init(mut app App) { |
| 281 | app.init_flag = true |
| 282 | |
| 283 | // set max vertices, |
| 284 | // for a large number of the same type of object it is better use the instances!! |
| 285 | desc := sapp.create_desc() |
| 286 | gfx.setup(&desc) |
| 287 | sgl_desc := sgl.Desc{ |
| 288 | max_vertices: 50 * 65536 |
| 289 | } |
| 290 | sgl.setup(&sgl_desc) |
| 291 | |
| 292 | // 3d pipeline |
| 293 | mut pipdesc := gfx.PipelineDesc{} |
| 294 | unsafe { vmemset(&pipdesc, 0, int(sizeof(pipdesc))) } |
| 295 | |
| 296 | color_state := gfx.ColorTargetState{ |
| 297 | blend: gfx.BlendState{ |
| 298 | enabled: true |
| 299 | src_factor_rgb: .src_alpha |
| 300 | dst_factor_rgb: .one_minus_src_alpha |
| 301 | } |
| 302 | } |
| 303 | pipdesc.colors[0] = color_state |
| 304 | |
| 305 | pipdesc.depth = gfx.DepthState{ |
| 306 | write_enabled: true |
| 307 | compare: .less_equal |
| 308 | } |
| 309 | pipdesc.cull_mode = .back |
| 310 | app.pip_3d = sgl.make_pipeline(&pipdesc) |
| 311 | |
| 312 | // create chessboard texture 256*256 RGBA |
| 313 | w := 256 |
| 314 | h := 256 |
| 315 | sz := w * h * 4 |
| 316 | tmp_txt := unsafe { malloc(sz) } |
| 317 | mut i := 0 |
| 318 | for i < sz { |
| 319 | unsafe { |
| 320 | y := (i >> 0x8) >> 5 // 8 cell |
| 321 | x := (i & 0xFF) >> 5 // 8 cell |
| 322 | // upper left corner |
| 323 | if x == 0 && y == 0 { |
| 324 | tmp_txt[i] = u8(0xFF) |
| 325 | tmp_txt[i + 1] = u8(0) |
| 326 | tmp_txt[i + 2] = u8(0) |
| 327 | tmp_txt[i + 3] = u8(0xFF) |
| 328 | } |
| 329 | // low right corner |
| 330 | else if x == 7 && y == 7 { |
| 331 | tmp_txt[i] = u8(0) |
| 332 | tmp_txt[i + 1] = u8(0xFF) |
| 333 | tmp_txt[i + 2] = u8(0) |
| 334 | tmp_txt[i + 3] = u8(0xFF) |
| 335 | } else { |
| 336 | col := if ((x + y) & 1) == 1 { 0xFF } else { 0 } |
| 337 | tmp_txt[i] = u8(col) // red |
| 338 | tmp_txt[i + 1] = u8(col) // green |
| 339 | tmp_txt[i + 2] = u8(col) // blue |
| 340 | tmp_txt[i + 3] = u8(0xFF) // alpha |
| 341 | } |
| 342 | i += 4 |
| 343 | } |
| 344 | } |
| 345 | unsafe { |
| 346 | app.texture, app.sampler = create_texture(w, h, tmp_txt) |
| 347 | free(tmp_txt) |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | fn my_event_manager(mut ev gg.Event, mut app App) { |
| 352 | if ev.typ == .mouse_move { |
| 353 | app.mouse_x = int(ev.mouse_x) |
| 354 | app.mouse_y = int(ev.mouse_y) |
| 355 | } |
| 356 | if ev.typ == .touches_began || ev.typ == .touches_moved { |
| 357 | if ev.num_touches > 0 { |
| 358 | touch_point := ev.touches[0] |
| 359 | app.mouse_x = int(touch_point.pos_x) |
| 360 | app.mouse_y = int(touch_point.pos_y) |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | fn main() { |
| 366 | mut app := &App{} |
| 367 | app.gg = gg.new_context( |
| 368 | width: win_width |
| 369 | height: win_height |
| 370 | create_window: true |
| 371 | window_title: '3D Cube Demo' |
| 372 | user_data: app |
| 373 | bg_color: bg_color |
| 374 | frame_fn: frame |
| 375 | init_fn: my_init |
| 376 | event_fn: my_event_manager |
| 377 | ) |
| 378 | app.gg.run() |
| 379 | } |
| 380 | |