From 7480b394568f2b01d4482d2362d250351033c3cb Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Sat, 27 Jul 2024 14:15:55 -0400 Subject: [PATCH] Seemingly working... --- tinyrender3/main.go | 32 +++++++++++++++++--------------- tinyrender3/render.go | 5 +++++ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/tinyrender3/main.go b/tinyrender3/main.go index 3b57720..b2b4ce0 100644 --- a/tinyrender3/main.go +++ b/tinyrender3/main.go @@ -5,7 +5,7 @@ import ( "fmt" "log" "math" - "math/rand" + //"math/rand" "os" "runtime/pprof" // For performance profiling (unnecessary) ) @@ -57,7 +57,7 @@ func main() { log.Printf("Running render") - //light := Vec3f{0, 0, -1} + light := Vec3f{0, 0, -1} // for range Repeat { // Triangle2(&fb, 0xFF0000, Vec2i{10, 70}, Vec2i{50, 160}, Vec2i{70, 80}) @@ -79,30 +79,32 @@ func main() { for i := range 3 { // Triangles, bro sc[i].X = (f[i].X + 1) * halfwidth sc[i].Y = hi - (f[i].Y+1)*halfheight - sc[i].Z = f[i].Z // Pull Z value directly. This is fine, our z-buffer is currently float32 + // NOTE: WE USE NEGATIVE Z BECAUSE IT'S SUPPOSED TO BE DISTANCE! AS-IS, CLOSER + // POINTS HAVE HIGHER Z VLAUES + sc[i].Z = -f[i].Z // Pull Z value directly. This is fine, our z-buffer is currently float32 minz = min(minz, sc[i].Z) maxz = max(maxz, sc[i].Z) } // TESTING zch := float32(*zcuthigh) zcl := float32(*zcutlow) - if sc[0].Z > zch || sc[1].Z > zch || sc[2].Z > zch || - sc[0].Z < zcl || sc[1].Z < zcl || sc[2].Z < zcl { + if -sc[0].Z > zch || -sc[1].Z > zch || -sc[2].Z > zch || + -sc[0].Z < zcl || -sc[1].Z < zcl || -sc[2].Z < zcl { continue } // To test something, we swap vertex 2 and 3 //sc[1], sc[2] = sc[2], sc[1] - //l1 := f[2].Sub(f[0]) - //n := l1.CrossProduct(f[1].Sub(f[0])) - //n = n.Normalize() - //intensity := n.MultSimp(&light) - //if intensity > 0 { - //Triangle3(&fb, Col2Uint(byte(255*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]) - //} + l1 := f[2].Sub(f[0]) + n := l1.CrossProduct(f[1].Sub(f[0])) + n = n.Normalize() + intensity := n.MultSimp(&light) + if intensity > 0 { + Triangle3(&fb, Col2Uint(byte(255*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]) + } } } diff --git a/tinyrender3/render.go b/tinyrender3/render.go index 358690c..da22028 100644 --- a/tinyrender3/render.go +++ b/tinyrender3/render.go @@ -228,6 +228,11 @@ func Triangle3(fb *Framebuffer, color uint, v0f Vec3f, v1f Vec3f, v2f Vec3f) { // Where to start our scanning pstart := Vec2i{boundsTL.X, boundsTL.Y} parea := EdgeFunctioni(v0, v1, v2) + // if parea < 0 { + // v1, v2 = v2, v1 + // v1f, v2f = v2f, v1f + // parea = EdgeFunctioni(v0, v1, v2) + // } invarea := 1 / float32(parea) w0_y := EdgeFunctioni(v1, v2, pstart) w1_y := EdgeFunctioni(v2, v0, pstart)