From a95f1b5a4b0f575701b575924b4ecc6e9a501a60 Mon Sep 17 00:00:00 2001 From: peony Date: Sun, 27 Oct 2024 16:54:59 +0100 Subject: [PATCH] Cube work --- sysdata/libraries/render/src/lib.hb | 2 +- sysdata/libraries/render/src/software.hb | 2 +- .../render_example/src/examples/cube.hb | 61 ++++++++++++++++++- sysdata/programs/render_example/src/main.hb | 2 +- 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/sysdata/libraries/render/src/lib.hb b/sysdata/libraries/render/src/lib.hb index aad9e1e..0e3cd69 100644 --- a/sysdata/libraries/render/src/lib.hb +++ b/sysdata/libraries/render/src/lib.hb @@ -44,7 +44,7 @@ put_line := mode.put_line clear := mode.clear put_surface := mode.put_surface put_scaled := mode.put_scaled -put_tri_wireframe := mode.put_tri_wireframe +put_tri := mode.put_tri put_filled_tri := mode.put_filled_tri // thanks peony for these three! put_trirect := mode.put_trirect diff --git a/sysdata/libraries/render/src/software.hb b/sysdata/libraries/render/src/software.hb index d205fbb..6c6b64c 100644 --- a/sysdata/libraries/render/src/software.hb +++ b/sysdata/libraries/render/src/software.hb @@ -358,7 +358,7 @@ put_filled_tri := fn(surface: Surface, p0: Vec2(uint), p1: Vec2(uint), p2: Vec2( primary_dx := @as(int, @bitcast(p2.x - p0.x)) primary_dy := @as(int, @bitcast(p2.y - p0.y)) primary_x := p0.x - primary_step := primary_dx / primary_dy + primary_step := @as(int, primary_dx) / @as(int, primary_dy) primary_error := @as(int, 2) * primary_dx - primary_dy secondary_dx := @as(int, @bitcast(p1.x - p0.x)) diff --git a/sysdata/programs/render_example/src/examples/cube.hb b/sysdata/programs/render_example/src/examples/cube.hb index 20e5e6d..1cde5a3 100644 --- a/sysdata/programs/render_example/src/examples/cube.hb +++ b/sysdata/programs/render_example/src/examples/cube.hb @@ -1,11 +1,68 @@ -.{Vec2} := @use("../../../../libraries/stn/src/lib.hb").math +.{Vec2} := @use("../../../../libraries/stn/src/lib.hb").math; +.{log, string, memory} := @use("../../../../libraries/stn/src/lib.hb"); +.{Color, Surface} := @use("../../../../libraries/render/src/lib.hb") render := @use("../../../../libraries/render/src/lib.hb") +Face := struct {a: uint, b: uint, c: uint, mode: u8, color: Color, texture: ^Surface} +WIREFRAME := @as(u8, 0) +FILLED := @as(u8, 1) +TEXTURED := @as(u8, 2) + +Vec3 := struct {x: int, y: int, z: int} + +vertecies := [Vec3].(.(-1, -1, -1), .(1, -1, -1), .(1, 1, -1), .(-1, 1, -1), .(-1, -1, 1), .(1, -1, 1), .(1, 1, 1), .(-1, 1, 1)) +projected := @as([Vec3; 8], idk) + +faces := [Face].(Face.(0, 1, 2, WIREFRAME, .(255, 0, 0, 255), @as(^Surface, idk))) + +d := @as(int, 1) + example := fn(): void { screen := render.init(true) + + unit := @as(int, 0) + half_width := @as(int, @intcast(screen.width / 2)) + half_height := @as(int, @intcast(screen.height / 2)) + if screen.width < screen.height { + unit = @intcast(half_width / 2) + } else { + unit = @intcast(half_height / 2) + } + + format_page := @as([u8; 1024], idk) loop { + index := 0 + loop if index == 8 break else { + vertex := vertecies[index] + vertex = .(vertex.x * unit, vertex.y * unit, vertex.z * unit + unit) + + vertex = .(vertex.x * d / (vertex.z / unit + d) + half_width, vertex.y * d / (vertex.z / unit + d) + half_height, vertex.z) + projected[index] = vertex + + log.info(string.display_int(vertex.x, @bitcast(&format_page), 10)) + log.info(string.display_int(vertex.y, @bitcast(&format_page), 10)) + + index += 1 + } + + index = 0 render.clear(screen, render.black) - render.put_filled_tri(screen, .(128, 256), .(512, 512), .(10, 10), .(255, 255, 255, 255)) + loop if index == 1 break else { + face := faces[index] + + a := projected[face.a] + b := projected[face.b] + c := projected[face.c] + + if face.mode == WIREFRAME { + render.put_tri(screen, .(@intcast(a.x), @intcast(a.y)), .(@intcast(b.x), @intcast(b.y)), .(@intcast(c.x), @intcast(c.y)), face.color) + } + + index += 1 + } + + + //render.put_filled_tri(screen, .(128, 256), .(512, 512), .(10, 10), .(255, 255, 255, 255)) render.sync(screen) } return diff --git a/sysdata/programs/render_example/src/main.hb b/sysdata/programs/render_example/src/main.hb index 2495e15..8f2d2c0 100644 --- a/sysdata/programs/render_example/src/main.hb +++ b/sysdata/programs/render_example/src/main.hb @@ -1,3 +1,3 @@ -.{example} := @use("./examples/gravity.hb") +.{example} := @use("./examples/cube.hb") main := example \ No newline at end of file