3dtrial/tinyrender3/main.go

130 lines
3.5 KiB
Go
Raw Normal View History

package main
import (
"flag"
"fmt"
"log"
"math"
2024-07-27 18:15:55 +00:00
//"math/rand"
"os"
"runtime/pprof" // For performance profiling (unnecessary)
)
const (
Width = 512
Height = 512
ObjectFile = "head.obj"
Repeat = 60
)
func must(err error) {
if err != nil {
panic(err)
}
}
// However flag works... idk
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var dozbuf = flag.Bool("zbuffer", false, "Write zbuffer instead of image")
var zcuthigh = flag.Float64("zcuthigh", math.MaxFloat32, "High cutoff for z (values above this will be removed)")
var zcutlow = flag.Float64("zcutlow", -math.MaxFloat32, "Low cutoff for z (values below are removed)")
var p6file = flag.String("p6file", "", "Output binary ppm to given file instead")
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")
2024-07-27 18:15:55 +00:00
light := Vec3f{0, 0, -1}
// 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]Vec3f
var hi = float32(fb.Height - 1)
minz := float32(math.MaxFloat32)
maxz := float32(-math.MaxFloat32)
for range Repeat {
fb.ResetZBuffer()
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 = (f[i].X + 1) * halfwidth
sc[i].Y = hi - (f[i].Y+1)*halfheight
2024-07-27 18:15:55 +00:00
// NOTE: WE USE NEGATIVE Z BECAUSE IT'S SUPPOSED TO BE DISTANCE! AS-IS, CLOSER
// POINTS HAVE HIGHER Z VLAUES
sc[i].Z = -f[i].Z // Pull Z value directly. This is fine, our z-buffer is currently float32
minz = min(minz, sc[i].Z)
maxz = max(maxz, sc[i].Z)
}
// TESTING
zch := float32(*zcuthigh)
zcl := float32(*zcutlow)
2024-07-27 18:15:55 +00:00
if -sc[0].Z > zch || -sc[1].Z > zch || -sc[2].Z > zch ||
-sc[0].Z < zcl || -sc[1].Z < zcl || -sc[2].Z < zcl {
continue
}
// To test something, we swap vertex 2 and 3
//sc[1], sc[2] = sc[2], sc[1]
2024-07-27 18:15:55 +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 {
Triangle3(&fb, Col2Uint(byte(255*intensity), byte(255*intensity), byte(255*intensity)), sc[0], sc[1], sc[2])
//Triangle3(&fb, uint(rand.Int()), sc[0], sc[1], sc[2])
//Triangle1(&fb, uint(rand.Int()), sc[0].ToVec2i(), sc[1].ToVec2i(), sc[2].ToVec2i())
//Triangle2(&fb, 0xFFFFFF, sc[0], sc[1], sc[2])
}
}
}
log.Print("Min/max z: ", minz, maxz)
//log.Print(fb.ZBuffer)
if *dozbuf {
log.Printf("Exporting zbuffer ppm to stdout")
fmt.Print(fb.ZBuffer_ExportPPM())
} else {
if *p6file != "" {
err := os.WriteFile(*p6file, fb.ExportPPMP6(), 0660)
must(err)
} else {
log.Printf("Exporting ppm to stdout")
fmt.Print(fb.ExportPPM())
}
}
log.Printf("Program end")
}