From dab191f7ef358a865aa77e7fbe963ee698e5e7a0 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Tue, 23 Jul 2024 23:48:51 -0400 Subject: [PATCH] back to safety --- tinyrender1/main.go | 31 ++----------------------------- tinyrender1/render.go | 2 +- 2 files changed, 3 insertions(+), 30 deletions(-) diff --git a/tinyrender1/main.go b/tinyrender1/main.go index 77725f4..6b5a012 100644 --- a/tinyrender1/main.go +++ b/tinyrender1/main.go @@ -56,45 +56,18 @@ func main() { var x [3]int var y [3]int - var wi = int(fb.Width - 1) var hi = int(fb.Height - 1) for range Repeat { for _, f := range o.Faces { + // Precompute perspective for vertices to save time. Notice Z + // is not considered: is this orthographic projection? Yeah probably... for i := range 3 { // Triangles, bro x[i] = int((f[i].X + 1) * halfwidth) y[i] = hi - int((f[i].Y+1)*halfheight) - if x[i] > wi { - x[i] = wi - } else if x[i] < 0 { - x[i] = 0 - } - if y[i] > hi { - y[i] = hi - } else if y[i] < 0 { - y[i] = 0 - } } Bresenham2(&fb, 0xFFFFFF, x[0], y[0], x[1], y[1]) Bresenham2(&fb, 0xFFFFFF, x[1], y[1], x[2], y[2]) Bresenham2(&fb, 0xFFFFFF, x[2], y[2], x[0], y[0]) - // x0 := int((f[0].X + 1) * halfwidth) - // y0 := int((f[0].Y + 1) * halfheight) - // x1 := int((f[1].X + 1) * halfwidth) - // y1 := int((f[1].Y + 1) * halfheight) - // x2 := int((f[2].X + 1) * halfwidth) - // y2 := int((f[2].Y + 1) * halfheight) - - // Bresenham2(&fb, 0xFFFFFF, x0, int(fb.Height)-y0, x1, int(fb.Height)-y1) - // for i := range 3 { // Triangles, bro - // Bresenham2(&fb, 0xFFFFFF, x0, int(fb.Height)-y0, x1, int(fb.Height)-y1) - // // v0 := f[i] - // // v1 := f[(i+1)%3] - // // x0 := int((v0.X + 1) * halfwidth) - // // y0 := int((v0.Y + 1) * halfheight) - // // x1 := int((v1.X + 1) * halfwidth) - // // y1 := int((v1.Y + 1) * halfheight) - // Bresenham2(&fb, 0xFFFFFF, x0, int(fb.Height)-y0, x1, int(fb.Height)-y1) - // } } // Just draw a simple line (a million times or something) diff --git a/tinyrender1/render.go b/tinyrender1/render.go index d5b0026..bf5a23c 100644 --- a/tinyrender1/render.go +++ b/tinyrender1/render.go @@ -173,7 +173,7 @@ func Bresenham2(fb *Framebuffer, color uint, x0 int, y0 int, x1 int, y1 int) { err := dx + dy for { - fb.Set(uint(x0), uint(y0), color) + fb.SetSafe(uint(x0), uint(y0), color) if x0 == x1 && y0 == y1 { break }