3dtrial/tinyrender4/main.go
2024-07-29 22:25:23 -04:00

137 lines
3.7 KiB
Go

package main
import (
"flag"
"fmt"
"image"
"log"
//"math"
//"math/rand"
"os"
"runtime/pprof" // For performance profiling (unnecessary)
_ "image/jpeg"
)
const (
Width = 512
Height = 512
NearClip = 0.1
FarClip = 5 // Because the head is so small and close
ObjectFile = "head.obj"
TextureFile = "../head.jpg"
)
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 p6file = flag.String("p6file", "", "Output binary ppm to given file instead")
var fov = flag.Float64("fov", 90, "Horizontal FOV in degrees")
var xofs = flag.Float64("xofs", 0, "Offset image by x")
var zofs = flag.Float64("zofs", -1.5, "Offset image by z (should be negative)")
var repeat = flag.Int("repeat", 60, "Amount of times to repeat render")
// 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)")
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, texture %s", ObjectFile, TextureFile)
of, err := os.Open(ObjectFile)
must(err)
defer of.Close()
o, err := ParseObj(of)
must(err)
jf, err := os.Open(TextureFile)
must(err)
defer jf.Close()
timg, _, err := image.Decode(jf)
must(err)
texture := NewTexture(timg, 4)
log.Printf("Running render")
light := Vec3f{0, 0, -1}
var projection Mat44f
var worldToCamera Mat44f
projection.SetProjection(float32(*fov), NearClip, FarClip)
worldToCamera.SetTranslation(float32(*xofs), 0, float32(*zofs))
// Premultiply all the translation/etc matrices. Why do we do world to camera THEN
// projection? I guess that makes sense actually, oops... projection is the last step.
screenmat := worldToCamera.Multiply(&projection)
// light = worldToCamera.MultiplyPoint3(light)
// light = light.Normalize()
halfwidth := float32(fb.Width / 2)
halfheight := float32(fb.Height / 2)
var sc [3]Vertex
var hi = float32(fb.Height - 1)
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...
var fpt [3]Vec3f
for i := range 3 { // Triangles, bro
fp := screenmat.MultiplyPoint3(f[i].Pos)
fpt[i] = worldToCamera.MultiplyPoint3(f[i].Pos)
sc[i] = f[i]
sc[i].Pos.X = (fp.X + 1) * halfwidth
sc[i].Pos.Y = hi - (fp.Y+1)*halfheight
sc[i].Pos.Z = fp.Z
// NOTE: WE USE NEGATIVE Z BECAUSE IT'S SUPPOSED TO BE DISTANCE! AS-IS, CLOSER
// POINTS HAVE HIGHER Z VLAUES
// sc[i].Pos.Z = -fp.Z // Pull Z value directly. This is fine, our z-buffer is currently float32
}
l1 := fpt[2].Sub(fpt[0])
n := l1.CrossProduct(fpt[1].Sub(fpt[0]))
n = n.Normalize()
intensity := n.MultSimp(&light)
if intensity > 0 {
Triangle3t(&fb, &texture, intensity, sc[0], sc[1], sc[2])
}
}
}
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")
}