2024-07-26 21:32:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2024-07-27 05:16:49 +00:00
|
|
|
// "math/rand"
|
2024-07-26 21:32:20 +00:00
|
|
|
"os"
|
|
|
|
"runtime/pprof" // For performance profiling (unnecessary)
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Width = 512
|
|
|
|
Height = 512
|
|
|
|
ObjectFile = "head.obj"
|
2024-07-27 05:16:49 +00:00
|
|
|
Repeat = 600
|
2024-07-26 21:32:20 +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")
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-07-27 05:16:49 +00:00
|
|
|
log.Printf("Loading obj %s", ObjectFile)
|
2024-07-26 21:32:20 +00:00
|
|
|
|
2024-07-27 05:16:49 +00:00
|
|
|
of, err := os.Open(ObjectFile)
|
|
|
|
must(err)
|
|
|
|
defer of.Close()
|
|
|
|
o, err := ParseObj(of)
|
|
|
|
must(err)
|
2024-07-26 21:32:20 +00:00
|
|
|
|
|
|
|
log.Printf("Running render")
|
|
|
|
|
2024-07-27 05:16:49 +00:00
|
|
|
light := Vec3f{0, 0, -1}
|
2024-07-26 21:48:47 +00:00
|
|
|
|
2024-07-27 05:16:49 +00:00
|
|
|
// for range Repeat {
|
|
|
|
// Triangle2(&fb, 0xFF0000, Vec2i{10, 70}, Vec2i{50, 160}, Vec2i{70, 80})
|
|
|
|
// Triangle2(&fb, 0xFFFFFF, Vec2i{180, 50}, Vec2i{150, 1}, Vec2i{70, 180})
|
|
|
|
// Triangle2(&fb, 0x00FF00, Vec2i{180, 150}, Vec2i{120, 160}, Vec2i{130, 180})
|
|
|
|
// }
|
|
|
|
|
|
|
|
halfwidth := float32(fb.Width / 2)
|
|
|
|
halfheight := float32(fb.Height / 2)
|
|
|
|
var sc [3]Vec2i
|
|
|
|
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
|
|
|
|
sc[i].X = int((f[i].X + 1) * halfwidth)
|
|
|
|
sc[i].Y = hi - int((f[i].Y+1)*halfheight)
|
2024-07-26 21:32:20 +00:00
|
|
|
}
|
2024-07-27 05:16:49 +00:00
|
|
|
l1 := f[2].Sub(f[0])
|
|
|
|
n := l1.CrossProduct(f[1].Sub(f[0]))
|
|
|
|
n = n.Normalize()
|
|
|
|
intensity := n.MultSimp(&light)
|
|
|
|
if intensity > 0 {
|
|
|
|
Triangle2(&fb, Col2Uint(byte(255*intensity), byte(255*intensity), byte(255*intensity)), sc[0], sc[1], sc[2])
|
|
|
|
//Triangle2(&fb, 0xFFFFFF, sc[0], sc[1], sc[2])
|
|
|
|
}
|
|
|
|
// Vec2i{10, 70}, Vec2i{50, 160}, Vec2i{70, 80})
|
|
|
|
// 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])
|
2024-07-26 21:32:20 +00:00
|
|
|
}
|
2024-07-27 05:16:49 +00:00
|
|
|
}
|
2024-07-26 21:32:20 +00:00
|
|
|
|
|
|
|
log.Printf("Exporting ppm to stdout")
|
|
|
|
fmt.Print(fb.ExportPPM())
|
|
|
|
|
|
|
|
log.Printf("Program end")
|
|
|
|
}
|