3dtrial/tinyrender3_homework/main.go

120 lines
3.0 KiB
Go
Raw Normal View History

2024-07-28 22:01:15 +00:00
package main
import (
"flag"
"fmt"
"image"
"log"
2024-07-29 00:59:38 +00:00
//"math"
2024-07-28 22:01:15 +00:00
//"math/rand"
"os"
"runtime/pprof" // For performance profiling (unnecessary)
_ "image/jpeg"
)
const (
Width = 512
Height = 512
ObjectFile = "head.obj"
TextureFile = "head.jpg"
2024-07-29 00:59:38 +00:00
Repeat = 500
2024-07-28 22:01:15 +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")
var dozbuf = flag.Bool("zbuffer", false, "Write zbuffer instead of image")
var p6file = flag.String("p6file", "", "Output binary ppm to given file instead")
2024-07-29 00:59:38 +00:00
// 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)")
2024-07-28 22:01:15 +00:00
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()
2024-07-29 00:59:38 +00:00
timg, _, err := image.Decode(jf)
2024-07-28 22:01:15 +00:00
must(err)
2024-07-29 00:59:38 +00:00
texture := NewTexture(timg, 4)
2024-07-28 22:01:15 +00:00
log.Printf("Running render")
light := Vec3f{0, 0, -1}
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...
for i := range 3 { // Triangles, bro
sc[i] = f[i]
sc[i].Pos.X = (f[i].Pos.X + 1) * halfwidth
sc[i].Pos.Y = hi - (f[i].Pos.Y+1)*halfheight
// NOTE: WE USE NEGATIVE Z BECAUSE IT'S SUPPOSED TO BE DISTANCE! AS-IS, CLOSER
// POINTS HAVE HIGHER Z VLAUES
sc[i].Pos.Z = -f[i].Pos.Z // Pull Z value directly. This is fine, our z-buffer is currently float32
}
l1 := f[2].Pos.Sub(f[0].Pos)
n := l1.CrossProduct(f[1].Pos.Sub(f[0].Pos))
n = n.Normalize()
intensity := n.MultSimp(&light)
if intensity > 0 {
2024-07-29 00:59:38 +00:00
Triangle3t(&fb, &texture, intensity, sc[0], sc[1], sc[2])
2024-07-28 22:01:15 +00:00
//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])
}
}
}
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")
}