3dtrial/tinyrender4/main.go

137 lines
3.7 KiB
Go
Raw Normal View History

2024-07-29 01:19:28 +00:00
package main
import (
"flag"
"fmt"
"image"
"log"
//"math"
//"math/rand"
"os"
"runtime/pprof" // For performance profiling (unnecessary)
_ "image/jpeg"
)
const (
Width = 512
Height = 512
2024-07-29 02:28:35 +00:00
NearClip = 0.1
FarClip = 5 // Because the head is so small and close
2024-07-29 01:19:28 +00:00
ObjectFile = "head.obj"
2024-07-30 02:25:23 +00:00
TextureFile = "../head.jpg"
2024-07-29 01:19:28 +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 02:28:35 +00:00
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")
2024-07-29 01:19:28 +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)")
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}
2024-07-29 02:28:35 +00:00
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)
2024-07-29 02:46:44 +00:00
// light = worldToCamera.MultiplyPoint3(light)
// light = light.Normalize()
2024-07-29 02:28:35 +00:00
2024-07-29 01:19:28 +00:00
halfwidth := float32(fb.Width / 2)
halfheight := float32(fb.Height / 2)
var sc [3]Vertex
var hi = float32(fb.Height - 1)
2024-07-29 02:28:35 +00:00
for range *repeat {
2024-07-29 01:19:28 +00:00
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...
2024-07-29 02:46:44 +00:00
var fpt [3]Vec3f
2024-07-29 01:19:28 +00:00
for i := range 3 { // Triangles, bro
2024-07-29 02:28:35 +00:00
fp := screenmat.MultiplyPoint3(f[i].Pos)
2024-07-29 02:46:44 +00:00
fpt[i] = worldToCamera.MultiplyPoint3(f[i].Pos)
2024-07-29 01:19:28 +00:00
sc[i] = f[i]
2024-07-29 02:28:35 +00:00
sc[i].Pos.X = (fp.X + 1) * halfwidth
sc[i].Pos.Y = hi - (fp.Y+1)*halfheight
sc[i].Pos.Z = fp.Z
2024-07-29 01:19:28 +00:00
// NOTE: WE USE NEGATIVE Z BECAUSE IT'S SUPPOSED TO BE DISTANCE! AS-IS, CLOSER
// POINTS HAVE HIGHER Z VLAUES
2024-07-29 02:28:35 +00:00
// sc[i].Pos.Z = -fp.Z // Pull Z value directly. This is fine, our z-buffer is currently float32
2024-07-29 01:19:28 +00:00
}
2024-07-29 02:46:44 +00:00
l1 := fpt[2].Sub(fpt[0])
n := l1.CrossProduct(fpt[1].Sub(fpt[0]))
2024-07-29 01:19:28 +00:00
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")
}