From 0683976241dc9eaccb3071da021eec13db1436e6 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Fri, 26 Jul 2024 23:52:48 -0400 Subject: [PATCH] Nothing is making it faster --- tinyrender2/render.go | 56 ++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/tinyrender2/render.go b/tinyrender2/render.go index 2f22e0b..9b24563 100644 --- a/tinyrender2/render.go +++ b/tinyrender2/render.go @@ -87,9 +87,12 @@ func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) { if xleft > xright { xleft, xright = xright, xleft } + if xleft < 0 || xright >= int(fb.Width) { + continue + } // Draw a horizontal line from left to right for x := xleft; x <= xright; x++ { - fb.SetSafe(uint(x), uint(y), color) + fb.Set(uint(x), uint(y), color) } xlong += v02step if y < v1.Y { @@ -134,38 +137,43 @@ func EdgeIncrement(v1, v2 Vec2f) (float32, float32) { func Triangle2(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) { boundsTL, boundsBR := ComputeBoundingBox(v0, v1, v2) - const expand = (2 << 12) + if boundsTL.Y < 0 { + boundsTL.Y = 0 + } + if boundsTL.X < 0 { + boundsTL.X = 0 + } + if boundsBR.Y >= int(fb.Height) { + boundsBR.Y = int(fb.Height - 1) + } + if boundsBR.X >= int(fb.Width) { + boundsBR.X = int(fb.Width - 1) + } + // Where to start our scanning + pstart := Vec2f{float32(boundsTL.X) + 0.5, float32(boundsTL.Y) + 0.5} //log.Print(boundsTL, boundsBR) v0f := v0.ToF() v1f := v1.ToF() v2f := v2.ToF() - // Where to start our scanning - pstart := Vec2f{float32(boundsTL.X) + 0.5, float32(boundsTL.Y) + 0.5} - // parea := EdgeFunction(v0f, v1f, v2f) - // invarea := 1 / parea - w0_y := int(expand * EdgeFunction(v1f, v2f, pstart)) - w1_y := int(expand * EdgeFunction(v2f, v0f, pstart)) - w2_y := int(expand * EdgeFunction(v0f, v1f, pstart)) - w0_xit, w0_yit := EdgeIncrement(v1f, v2f) - w1_xit, w1_yit := EdgeIncrement(v2f, v0f) - w2_xit, w2_yit := EdgeIncrement(v0f, v1f) - w0_xi := int(expand * w0_xit) - w0_yi := int(expand * w0_yit) - w1_xi := int(expand * w1_xit) - w1_yi := int(expand * w1_yit) - w2_xi := int(expand * w2_xit) - w2_yi := int(expand * w2_yit) + parea := EdgeFunction(v0f, v1f, v2f) + invarea := 1 / parea + w0_y := EdgeFunction(v1f, v2f, pstart) + w1_y := EdgeFunction(v2f, v0f, pstart) + w2_y := EdgeFunction(v0f, v1f, pstart) + w0_xi, w0_yi := EdgeIncrement(v1f, v2f) + w1_xi, w1_yi := EdgeIncrement(v2f, v0f) + w2_xi, w2_yi := EdgeIncrement(v0f, v1f) - for y := boundsTL.Y; y <= boundsBR.Y; y++ { + for y := uint(boundsTL.Y); y <= uint(boundsBR.Y); y++ { w0 := w0_y w1 := w1_y w2 := w2_y - for x := boundsTL.X; x <= boundsBR.X; x++ { + for x := uint(boundsTL.X); x <= uint(boundsBR.X); x++ { if w0 >= 0 && w1 >= 0 && w2 >= 0 { - fb.SetSafe(uint(x), uint(y), color) - // w0a := w0 * invarea - // w1a := w1 * invarea - // w2a := w2 * invarea + w0a := w0 * invarea + w1a := w1 * invarea + w2a := w2 * invarea + fb.Set(x, y, Col2Uint(byte(255*w0a), byte(255*w1a), byte(255*w2a))) } w0 += w0_xi w1 += w1_xi