Seemingly working...

This commit is contained in:
Carlos Sanchez 2024-07-27 14:15:55 -04:00
parent 12bbb61e8f
commit 7480b39456
2 changed files with 22 additions and 15 deletions

View File

@ -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])
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])
//}
}
}
}

View File

@ -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)