diff --git a/tinyrender1/main.go b/tinyrender1/main.go index a93478a..ef64e7c 100644 --- a/tinyrender1/main.go +++ b/tinyrender1/main.go @@ -44,9 +44,9 @@ func main() { // Just draw a simple line (a million times or something) for range LineRepeat { - LineDumb3(&fb, 0xFFFFFF, 100, 100, 350, 200) - LineDumb3(&fb, 0xFF0000, 120, 100, 200, 350) - LineDumb3(&fb, 0xFF0000, 350, 200, 100, 100) // backward first line + LineDumb4(&fb, 0xFFFFFF, 100, 100, 350, 200) + LineDumb4(&fb, 0xFF0000, 120, 100, 200, 350) + LineDumb4(&fb, 0xFF0000, 350, 200, 100, 100) // backward first line } log.Printf("Exporting ppm to stdout") diff --git a/tinyrender1/render.go b/tinyrender1/render.go index 2ca7d89..791961f 100644 --- a/tinyrender1/render.go +++ b/tinyrender1/render.go @@ -50,3 +50,45 @@ func LineDumb3(fb *Framebuffer, color uint, x0 int, y0 int, x1 int, y1 int) { } } } + +func LineDumb4(fb *Framebuffer, color uint, x0 int, y0 int, x1 int, y1 int) { + steep := false + // This one makes sure that going pixel by pixel will not make holes in the + // other direction by always moving in the less steep direction. + if math.Abs(float64(x1-x0)) < math.Abs(float64(y1-y0)) { + x0, y0 = y0, x0 + x1, y1 = y1, x1 + steep = true + } + // Don't let lines be invisible, always go left to right + if x0 > x1 { // need to be left to right + x0, x1 = x1, x0 + y0, y1 = y1, y0 // Why are we swapping these? I guess because we're swapping the points... + } + dx := x1 - x0 // These MUST be POST calcs + dy := y1 - y0 + // Delta error or something? Basically, as we move in one direction, we get farther + // and farther off course if we never move in the OTHER direction. This is the amount + // of that per x direction. + derror := math.Abs(float64(dy) / float64(dx)) + y := uint(y0) + var err float64 + // Same as dumb2, going across pixel by pixel. But "x" here might actually be y + // because of the inverted direction + for x := x0; x <= x1; x++ { + if steep { + fb.Set(y, uint(x), color) + } else { + fb.Set(uint(x), y, color) + } + err += derror + if err > 0.5 { + if y1 > y0 { + y += 1 + } else { + y = y - 1 + } + err -= 1 + } + } +} diff --git a/tinyrender1/run.sh b/tinyrender1/run.sh index fd031c2..e8a1413 100755 --- a/tinyrender1/run.sh +++ b/tinyrender1/run.sh @@ -1,6 +1,11 @@ #!/bin/sh +if [ $# -ne 1 ]; then + echo "You must pass the basename for the .prof and .ppm" + exit 1 +fi + echo "Building" go build echo "Running" -./tinyrender1 "$@" > image.ppm +./tinyrender1 "-cpuprofile=$1.prof" >"$1.ppm"