Line drawing
This commit is contained in:
parent
415b817013
commit
4b09b01617
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,4 +4,5 @@
|
|||||||
*.gif
|
*.gif
|
||||||
*.jpg
|
*.jpg
|
||||||
*.jpeg
|
*.jpeg
|
||||||
|
*.prof
|
||||||
a.out
|
a.out
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
Width = 512
|
Width = 512
|
||||||
Height = 512
|
Height = 512
|
||||||
|
LineRepeat = 1_000_000
|
||||||
)
|
)
|
||||||
|
|
||||||
func must(err error) {
|
func must(err error) {
|
||||||
@ -41,10 +42,12 @@ func main() {
|
|||||||
|
|
||||||
log.Printf("Running render")
|
log.Printf("Running render")
|
||||||
|
|
||||||
// Just draw a simple line
|
// Just draw a simple line (a million times or something)
|
||||||
LineDumb(&fb, 0xFFFFFF, 100, 100, 350, 200)
|
for range LineRepeat {
|
||||||
LineDumb2(&fb, 0xFF0000, 120, 100, 200, 350)
|
LineDumb3(&fb, 0xFFFFFF, 100, 100, 350, 200)
|
||||||
LineDumb2(&fb, 0xFF0000, 350, 200, 100, 100) // backward first line
|
LineDumb3(&fb, 0xFF0000, 120, 100, 200, 350)
|
||||||
|
LineDumb3(&fb, 0xFF0000, 350, 200, 100, 100) // backward first line
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("Exporting ppm to stdout")
|
log.Printf("Exporting ppm to stdout")
|
||||||
fmt.Print(fb.ExportPPM())
|
fmt.Print(fb.ExportPPM())
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
// Draw a line using dumb
|
// Draw a line using dumb
|
||||||
func LineDumb(fb *Framebuffer, color uint, x0 uint, y0 uint, x1 uint, y1 uint) {
|
func LineDumb(fb *Framebuffer, color uint, x0 uint, y0 uint, x1 uint, y1 uint) {
|
||||||
var t float32
|
var t float32
|
||||||
@ -19,3 +23,30 @@ func LineDumb2(fb *Framebuffer, color uint, x0 uint, y0 uint, x1 uint, y1 uint)
|
|||||||
fb.Set(x, y, color)
|
fb.Set(x, y, color)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LineDumb3(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(x0-x1)) < math.Abs(float64(y0-y1)) {
|
||||||
|
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...
|
||||||
|
}
|
||||||
|
// 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++ {
|
||||||
|
t := float32(x-x0) / float32(x1-x0)
|
||||||
|
y := uint(float32(y0)*(1-t) + float32(y1)*t)
|
||||||
|
if steep {
|
||||||
|
fb.Set(y, uint(x), color)
|
||||||
|
} else {
|
||||||
|
fb.Set(uint(x), y, color)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
go build && ./tinyrender1 >image.ppm
|
echo "Building"
|
||||||
|
go build
|
||||||
|
echo "Running"
|
||||||
|
./tinyrender1 "$@" > image.ppm
|
||||||
|
Loading…
Reference in New Issue
Block a user