3dtrial/tinyrender1/main.go

114 lines
2.8 KiB
Go
Raw Normal View History

2024-07-23 23:00:19 +00:00
package main
import (
2024-07-23 23:37:02 +00:00
"flag"
2024-07-23 23:00:19 +00:00
"fmt"
"log"
2024-07-23 23:37:02 +00:00
"os"
2024-07-24 03:46:18 +00:00
"runtime/pprof" // For performance profiling (unnecessary)
2024-07-23 23:00:19 +00:00
)
const (
2024-07-24 00:19:03 +00:00
Width = 512
Height = 512
2024-07-24 02:53:18 +00:00
ObjectFile = "head.obj"
2024-07-24 03:46:18 +00:00
Repeat = 1_000
2024-07-23 23:00:19 +00:00
)
2024-07-23 23:37:02 +00:00
func must(err error) {
if err != nil {
panic(err)
}
}
// However flag works... idk
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
2024-07-23 23:00:19 +00:00
func main() {
log.Printf("Program start")
2024-07-23 23:37:02 +00:00
// 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()
}
2024-07-23 23:00:19 +00:00
fb := NewFramebuffer(Width, Height)
2024-07-23 23:37:02 +00:00
2024-07-24 02:53:18 +00:00
log.Printf("Loading obj %s", ObjectFile)
of, err := os.Open(ObjectFile)
must(err)
defer of.Close()
o, err := ParseObj(of)
must(err)
2024-07-23 23:37:02 +00:00
log.Printf("Running render")
2024-07-24 03:46:18 +00:00
halfwidth := float32(fb.Width / 2)
halfheight := float32(fb.Height / 2)
var x [3]int
var y [3]int
var wi = int(fb.Width - 1)
var hi = int(fb.Height - 1)
for range Repeat {
for _, f := range o.Faces {
for i := range 3 { // Triangles, bro
x[i] = int((f[i].X + 1) * halfwidth)
y[i] = hi - int((f[i].Y+1)*halfheight)
if x[i] > wi {
x[i] = wi
} else if x[i] < 0 {
x[i] = 0
}
if y[i] > hi {
y[i] = hi
} else if y[i] < 0 {
y[i] = 0
}
}
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])
// x0 := int((f[0].X + 1) * halfwidth)
// y0 := int((f[0].Y + 1) * halfheight)
// x1 := int((f[1].X + 1) * halfwidth)
// y1 := int((f[1].Y + 1) * halfheight)
// x2 := int((f[2].X + 1) * halfwidth)
// y2 := int((f[2].Y + 1) * halfheight)
// Bresenham2(&fb, 0xFFFFFF, x0, int(fb.Height)-y0, x1, int(fb.Height)-y1)
// for i := range 3 { // Triangles, bro
// Bresenham2(&fb, 0xFFFFFF, x0, int(fb.Height)-y0, x1, int(fb.Height)-y1)
// // v0 := f[i]
// // v1 := f[(i+1)%3]
// // x0 := int((v0.X + 1) * halfwidth)
// // y0 := int((v0.Y + 1) * halfheight)
// // x1 := int((v1.X + 1) * halfwidth)
// // y1 := int((v1.Y + 1) * halfheight)
// Bresenham2(&fb, 0xFFFFFF, x0, int(fb.Height)-y0, x1, int(fb.Height)-y1)
// }
2024-07-24 02:53:18 +00:00
}
2024-07-23 23:37:02 +00:00
2024-07-24 03:46:18 +00:00
// 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
}
2024-07-24 02:53:18 +00:00
2024-07-23 23:37:02 +00:00
log.Printf("Exporting ppm to stdout")
2024-07-23 23:00:19 +00:00
fmt.Print(fb.ExportPPM())
2024-07-23 23:37:02 +00:00
2024-07-23 23:00:19 +00:00
log.Printf("Program end")
}