87 lines
2.0 KiB
Go
87 lines
2.0 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"os"
|
||
|
"runtime/pprof" // For performance profiling (unnecessary)
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
Width = 512
|
||
|
Height = 512
|
||
|
ObjectFile = "head.obj"
|
||
|
Repeat = 1_000
|
||
|
)
|
||
|
|
||
|
func must(err error) {
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// However flag works... idk
|
||
|
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
||
|
|
||
|
func main() {
|
||
|
log.Printf("Program start")
|
||
|
|
||
|
// Little section for doing cpu profiling. I guess that's all you have to do?
|
||
|
flag.Parse()
|
||
|
if *cpuprofile != "" {
|
||
|
log.Printf("CPU profiling requested, write to %s", *cpuprofile)
|
||
|
f, err := os.Create(*cpuprofile)
|
||
|
must(err)
|
||
|
defer f.Close()
|
||
|
err = pprof.StartCPUProfile(f)
|
||
|
must(err)
|
||
|
defer pprof.StopCPUProfile()
|
||
|
}
|
||
|
|
||
|
fb := NewFramebuffer(Width, Height)
|
||
|
|
||
|
log.Printf("Loading obj %s", ObjectFile)
|
||
|
|
||
|
of, err := os.Open(ObjectFile)
|
||
|
must(err)
|
||
|
defer of.Close()
|
||
|
o, err := ParseObj(of)
|
||
|
must(err)
|
||
|
|
||
|
log.Printf("Running render")
|
||
|
|
||
|
halfwidth := float32(fb.Width / 2)
|
||
|
halfheight := float32(fb.Height / 2)
|
||
|
|
||
|
var x [3]int
|
||
|
var y [3]int
|
||
|
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)
|
||
|
}
|
||
|
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])
|
||
|
}
|
||
|
|
||
|
// Just draw a simple line (a million times or something)
|
||
|
// // LineDumb5(&fb, 0xFFFFFF, 100, 100, 350, 200)
|
||
|
// // LineDumb5(&fb, 0xFF0000, 120, 100, 200, 350)
|
||
|
// // LineDumb5(&fb, 0xFF0000, 350, 200, 100, 100) // backward first line
|
||
|
// Bresenham2(&fb, 0xFFFFFF, 100, 100, 350, 200)
|
||
|
// Bresenham2(&fb, 0xFF0000, 120, 100, 200, 350)
|
||
|
// Bresenham2(&fb, 0xFF0000, 350, 200, 100, 100) // backward first line
|
||
|
}
|
||
|
|
||
|
log.Printf("Exporting ppm to stdout")
|
||
|
fmt.Print(fb.ExportPPM())
|
||
|
|
||
|
log.Printf("Program end")
|
||
|
}
|