1
0
Fork 0
forked from AbleOS/ableos

Cube work

This commit is contained in:
peony 2024-10-27 16:54:59 +01:00
parent e6b972a7f3
commit a95f1b5a4b
4 changed files with 62 additions and 5 deletions

View file

@ -44,7 +44,7 @@ put_line := mode.put_line
clear := mode.clear clear := mode.clear
put_surface := mode.put_surface put_surface := mode.put_surface
put_scaled := mode.put_scaled put_scaled := mode.put_scaled
put_tri_wireframe := mode.put_tri_wireframe put_tri := mode.put_tri
put_filled_tri := mode.put_filled_tri put_filled_tri := mode.put_filled_tri
// thanks peony for these three! // thanks peony for these three!
put_trirect := mode.put_trirect put_trirect := mode.put_trirect

View file

@ -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_dx := @as(int, @bitcast(p2.x - p0.x))
primary_dy := @as(int, @bitcast(p2.y - p0.y)) primary_dy := @as(int, @bitcast(p2.y - p0.y))
primary_x := p0.x 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 primary_error := @as(int, 2) * primary_dx - primary_dy
secondary_dx := @as(int, @bitcast(p1.x - p0.x)) secondary_dx := @as(int, @bitcast(p1.x - p0.x))

View file

@ -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") 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 { example := fn(): void {
screen := render.init(true) 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 { 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.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) render.sync(screen)
} }
return return

View file

@ -1,3 +1,3 @@
.{example} := @use("./examples/gravity.hb") .{example} := @use("./examples/cube.hb")
main := example main := example