From ecc9011b48a3453b98fb75e8593cbb5495df0e36 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Sun, 28 Jul 2024 20:59:38 -0400 Subject: [PATCH] Finished homework again --- tinyrender3_homework/image.go | 25 +++++++++++++++++++++++ tinyrender3_homework/main.go | 37 ++++++++-------------------------- tinyrender3_homework/render.go | 23 +++++++-------------- tinyrender3_homework/run.sh | 4 ++-- 4 files changed, 42 insertions(+), 47 deletions(-) diff --git a/tinyrender3_homework/image.go b/tinyrender3_homework/image.go index 33d17f0..ac69d47 100644 --- a/tinyrender3_homework/image.go +++ b/tinyrender3_homework/image.go @@ -5,6 +5,7 @@ import ( "fmt" "image/color" //"log" + "image" "math" "strings" ) @@ -43,6 +44,24 @@ func NewFramebuffer(width uint, height uint) Framebuffer { } } +func NewTexture(texture image.Image, skip int) Framebuffer { + bounds := texture.Bounds() + width := bounds.Dx() / skip + height := bounds.Dy() / skip + result := Framebuffer{ + Data: make([]uint, width*height), + Width: uint(width), + Height: uint(height), + } + for y := bounds.Min.Y; y < bounds.Max.Y; y += skip { + for x := bounds.Min.X; x < bounds.Max.X; x += skip { + col := texture.At(x, y) + result.Set(uint(x/skip), uint(y/skip), Color2Uint(col)) + } + } + return result +} + // Fill zbuffer with pixels that are max distance away func (fb *Framebuffer) ResetZBuffer() { for i := range fb.ZBuffer { @@ -50,6 +69,12 @@ func (fb *Framebuffer) ResetZBuffer() { } } +func (fb *Framebuffer) GetUv(u float32, v float32) uint { + x := uint(float32(fb.Width) * u) + y := uint(float32(fb.Height) * (1 - v)) + return fb.Data[x+y*fb.Width] +} + // Sure hope this gets inlined... func (fb *Framebuffer) Set(x uint, y uint, color uint) { fb.Data[x+y*fb.Width] = color diff --git a/tinyrender3_homework/main.go b/tinyrender3_homework/main.go index 236dd60..fc461b3 100644 --- a/tinyrender3_homework/main.go +++ b/tinyrender3_homework/main.go @@ -5,7 +5,7 @@ import ( "fmt" "image" "log" - "math" + //"math" //"math/rand" "os" "runtime/pprof" // For performance profiling (unnecessary) @@ -18,7 +18,7 @@ const ( Height = 512 ObjectFile = "head.obj" TextureFile = "head.jpg" - Repeat = 60 + Repeat = 500 ) func must(err error) { @@ -30,10 +30,11 @@ func must(err error) { // 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") +// 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") @@ -62,25 +63,18 @@ func main() { jf, err := os.Open(TextureFile) must(err) defer jf.Close() - texture, _, err := image.Decode(jf) + timg, _, err := image.Decode(jf) must(err) + texture := NewTexture(timg, 4) 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 { @@ -93,25 +87,14 @@ func main() { // 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, sc[0], sc[1], sc[2]) + Triangle3t(&fb, &texture, 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]) @@ -119,10 +102,6 @@ func main() { } } - 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()) diff --git a/tinyrender3_homework/render.go b/tinyrender3_homework/render.go index 154cdc3..284ed12 100644 --- a/tinyrender3_homework/render.go +++ b/tinyrender3_homework/render.go @@ -2,7 +2,6 @@ package main import ( //"log" - "image" "math" ) @@ -271,7 +270,7 @@ func Triangle3(fb *Framebuffer, color uint, v0f Vec3f, v1f Vec3f, v2f Vec3f) { } } -func Triangle3t(fb *Framebuffer, texture image.Image, intensity float32, v0v Vertex, v1v Vertex, v2v Vertex) { +func Triangle3t(fb *Framebuffer, texture *Framebuffer, intensity float32, v0v Vertex, v1v Vertex, v2v Vertex) { v0 := v0v.Pos.ToVec2i() v1 := v1v.Pos.ToVec2i() v2 := v2v.Pos.ToVec2i() @@ -304,32 +303,24 @@ func Triangle3t(fb *Framebuffer, texture image.Image, intensity float32, v0v Ver w1_xi, w1_yi := EdgeIncrementi(v2, v0) w2_xi, w2_yi := EdgeIncrementi(v0, v1) - bounds := texture.Bounds() - tx := bounds.Min.X - ty := bounds.Min.Y - tw := bounds.Dx() - th := bounds.Dy() - for y := uint(boundsTL.Y); y <= uint(boundsBR.Y); y++ { w0 := w0_y w1 := w1_y w2 := w2_y for x := uint(boundsTL.X); x <= uint(boundsBR.X); x++ { if (w0 | w1 | w2) >= 0 { - //fb.Data[di] = color - //done = true w0a := float32(w0) * invarea w1a := float32(w1) * invarea w2a := float32(w2) * invarea pz := w0a*v0v.Pos.Z + w1a*v1v.Pos.Z + w2a*v2v.Pos.Z if pz < fb.ZBuffer[x+y*fb.Width] { - //log.Print(pz) fb.ZBuffer[x+y*fb.Width] = pz - txo := int(float32(tw) * (w0a*v0v.Tex.X + w1a*v1v.Tex.X + w2a*v2v.Tex.X)) - tyo := int(float32(th) * (1 - (w0a*v0v.Tex.Y + w1a*v1v.Tex.Y + w2a*v2v.Tex.Y))) - col := texture.At(tx+txo, ty+tyo) - //c := texture.At() - fb.Set(x, y, Color2Uint(col)) //uint(texture.Bounds().Dx()) + col := texture.GetUv( + (w0a*v0v.Tex.X + w1a*v1v.Tex.X + w2a*v2v.Tex.X), + (w0a*v0v.Tex.Y + w1a*v1v.Tex.Y + w2a*v2v.Tex.Y), + ) + r, g, b := Uint2Col(col) + fb.Set(x, y, Col2Uint(byte(float32(r)*intensity), byte(float32(g)*intensity), byte(float32(b)*intensity))) //uint(texture.Bounds().Dx()) //0xF) // fb.Set(x, y, Col2Uint(byte(255*w0a), byte(255*w1a), byte(255*w2a))) } // fb.Set(x, y, Col2Uint(byte(255*w0a), byte(255*w1a), byte(255*w2a))) diff --git a/tinyrender3_homework/run.sh b/tinyrender3_homework/run.sh index 54af6fc..6285d03 100755 --- a/tinyrender3_homework/run.sh +++ b/tinyrender3_homework/run.sh @@ -10,5 +10,5 @@ fi echo "Building" go build -o render echo "Running" -./render "-cpuprofile=$1.prof" >"$1.ppm" -./render "-zbuffer" >"$1_zbuffer.ppm" +./render "-cpuprofile=$1.prof" "-p6file=$1.ppm" +# ./render "-zbuffer" >"$1_zbuffer.ppm"