diff --git a/tinyrender2/main.go b/tinyrender2/main.go index b9d147d..3fa0aa0 100644 --- a/tinyrender2/main.go +++ b/tinyrender2/main.go @@ -12,7 +12,7 @@ const ( Width = 512 Height = 512 ObjectFile = "head.obj" - Repeat = 100_000 + Repeat = 1 //00_000 ) func must(err error) { diff --git a/tinyrender2/render.go b/tinyrender2/render.go index 56990c6..41c8dd1 100644 --- a/tinyrender2/render.go +++ b/tinyrender2/render.go @@ -44,8 +44,63 @@ func line(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i) { Bresenham2(fb, color, v0.X, v0.Y, v1.X, v1.Y) } +/*func LineSweep(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) { + +}*/ + func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) { - line(fb, color, v0, v1) - line(fb, color, v1, v2) - line(fb, color, v2, v0) + // Very silly manual sorting by Y + if v2.Y < v0.Y { + v0, v2 = v2, v0 + } + if v1.Y < v0.Y { + v0, v1 = v1, v0 + } + if v2.Y < v1.Y { + v1, v2 = v2, v1 + } + + // The lesson says to always figure out what's on the left. + // As such, we always go from left to right + // While drawing the straight line, go the opposite direction if the + // longer side is on the right (we go from long side to short) + var sweep int = 1 + if v2.X > v1.X { + sweep = -1 + } + + // Don't worry about speed for now... + dy := float32(v2.Y-v0.Y) - 0.5 + var v02step, v01step, v12step, xlong, xshort float32 + + xlong = float32(v0.X) + 0.5 + xshort = xlong + + // We can check just for greater than because we sorted the vertices + if dy > 0 { + v02step = (float32(v2.X-v0.X) - 0.5) / dy // long side always + v01step = (float32(v1.X-v0.X) - 0.5) / dy // first short side + v12step = (float32(v2.X-v1.X) - 0.5) / dy // second short side + xshort += v01step / 2 + xlong += v02step / 2 + } else { + + } + + for y := v0.Y; y <= v2.Y; y++ { + // Draw a horizontal line from + for x := int(math.Floor(float64(xlong))); x <= int(math.Floor(float64(xshort))); x += sweep { + fb.SetSafe(uint(x), uint(y), color) + } + xlong += v02step + if y < v1.Y { + xshort += v01step + } else { + xshort += v12step + } + } + + // line(fb, color, v0, v1) + // line(fb, color, v1, v2) + // line(fb, color, v2, v0) }