141 lines
3.8 KiB
Go
141 lines
3.8 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
|
||
|
ObjectFile = "head.obj"
|
||
|
TextureFile = "head.jpg"
|
||
|
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, 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()
|
||
|
texture, _, err := image.Decode(jf)
|
||
|
must(err)
|
||
|
|
||
|
log.Printf("Running render")
|
||
|
|
||
|
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]Vertex
|
||
|
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] = 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
|
||
|
minz = min(minz, sc[i].Pos.Z)
|
||
|
maxz = max(maxz, sc[i].Pos.Z)
|
||
|
}
|
||
|
// TESTING
|
||
|
zch := float32(*zcuthigh)
|
||
|
zcl := float32(*zcutlow)
|
||
|
if -sc[0].Pos.Z > zch || -sc[1].Pos.Z > zch || -sc[2].Pos.Z > zch ||
|
||
|
-sc[0].Pos.Z < zcl || -sc[1].Pos.Z < zcl || -sc[2].Pos.Z < zcl {
|
||
|
continue
|
||
|
}
|
||
|
// To test something, we swap vertex 2 and 3
|
||
|
//sc[1], sc[2] = sc[2], sc[1]
|
||
|
|
||
|
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 {
|
||
|
Triangle3t(&fb, texture, 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")
|
||
|
}
|