REALLY don't know why w works but others don't...

This commit is contained in:
Carlos Sanchez 2024-08-05 04:22:15 -04:00
parent 0f235dba74
commit 6fb46021fe
5 changed files with 51 additions and 36 deletions

View File

@ -136,7 +136,7 @@ func NewTexture(texture image.Image, stride int) *SimpleFramebuffer {
// Fill zbuffer with pixels that are max distance away // Fill zbuffer with pixels that are max distance away
func (fb *RenderBuffer) ResetZBuffer() { func (fb *RenderBuffer) ResetZBuffer() {
for i := range fb.ZBuffer { for i := range fb.ZBuffer {
fb.ZBuffer[i] = 65535 //math.MaxFloat32 fb.ZBuffer[i] = 0 //65535 //math.MaxFloat32
} }
} }

View File

@ -110,30 +110,30 @@ func (m *Mat44f) SetProjection(fov float32, aspect float32, near float32, far fl
// OK apparently I suck, let's use somebody else's projection matrix: // OK apparently I suck, let's use somebody else's projection matrix:
m.ZeroFill() m.ZeroFill()
// fov = fov / 180 * math.Pi // Convert to radians fov = fov / 180 * math.Pi // Convert to radians
// e := float32(1 / math.Tan(float64(fov/2))) e := float32(1 / math.Tan(float64(fov/2)))
// m.Set(0, 0, e/aspect) m.Set(0, 0, e/aspect)
// m.Set(1, 1, e) m.Set(1, 1, e)
// m.Set(2, 2, (far+near)/(near-far)) m.Set(2, 2, (far+near)/(near-far))
// m.Set(2, 3, 2*far*near/(near-far)) m.Set(2, 3, 2*far*near/(near-far))
// m.Set(3, 2, -1) // Might need to be swapped m.Set(3, 2, -1) // Might need to be swapped
DEG2RAD := math.Acos(-1.0) / 180.0 // DEG2RAD := math.Acos(-1.0) / 180.0
tangent := math.Tan(float64(fov/2.0) * DEG2RAD) // tangent of half fovY // tangent := math.Tan(float64(fov/2.0) * DEG2RAD) // tangent of half fovY
top := near * float32(tangent) // half height of near plane // top := near * float32(tangent) // half height of near plane
right := top * aspect // half width of near plane // right := top * aspect // half width of near plane
// Column major maybe??? // // Column major maybe???
// n/r 0 0 0 // // n/r 0 0 0
// 0 n/t 0 0 // // 0 n/t 0 0
// 0 0 -(f+n)/(f-n) -1 // // 0 0 -(f+n)/(f-n) -1
// 0 0 -(2fn)/(f-n) 0 // // 0 0 -(2fn)/(f-n) 0
m.Set(0, 0, near/right) // m.Set(0, 0, near/right)
m.Set(1, 1, near/top) // m.Set(1, 1, near/top)
m.Set(2, 2, -(far+near)/(far-near)) // m.Set(2, 2, -(far+near)/(far-near))
m.Set(3, 2, -1) // m.Set(3, 2, -1)
m.Set(2, 3, -(2*far*near)/(far-near)) // m.Set(2, 3, -(2*far*near)/(far-near))
} }
func (m *Mat44f) SetViewport(tl Vec3f, br Vec3f) { //width, height, depth int) { func (m *Mat44f) SetViewport(tl Vec3f, br Vec3f) { //width, height, depth int) {

View File

@ -24,6 +24,7 @@ type Facei [3]struct {
type Facef [3]struct { type Facef [3]struct {
Pos Vec3f Pos Vec3f
W float32
Tex Vec3f Tex Vec3f
} }

View File

@ -172,6 +172,23 @@ func TriangleTextured(fb *RenderBuffer, texture Framebuffer, intensity float32,
w0_xi, w0_yi := EdgeIncrementi(v1, v2) w0_xi, w0_yi := EdgeIncrementi(v1, v2)
w1_xi, w1_yi := EdgeIncrementi(v2, v0) w1_xi, w1_yi := EdgeIncrementi(v2, v0)
w2_xi, w2_yi := EdgeIncrementi(v0, v1) w2_xi, w2_yi := EdgeIncrementi(v0, v1)
// I don't know what happened to my z but it's nearly unusable.
// I simply use w instead... don't know if that's ok
tiz0 := 1 / v0v.W
tiz1 := 1 / v1v.W
tiz2 := 1 / v2v.W
// tiz0 := v0v.Pos.Z
// tiz1 := v1v.Pos.Z
// tiz2 := v2v.Pos.Z
// tiz0 := 1 / v0v.Pos.Z
// tiz1 := 1 / v1v.Pos.Z
// tiz2 := 1 / v2v.Pos.Z
tiu0 := v0v.Tex.X * tiz0 //v0v.W //* tiz0
tiu1 := v1v.Tex.X * tiz1 //v1v.W //* tiz1
tiu2 := v2v.Tex.X * tiz2 //v2v.W //* tiz2
tiv0 := v0v.Tex.Y * tiz0 //v0v.W //* tiz0
tiv1 := v1v.Tex.Y * tiz1 //v1v.W //* tiz1
tiv2 := v2v.Tex.Y * tiz2 //v2v.W //* tiz2
for y := uint(boundsTL.Y); y <= uint(boundsBR.Y); y++ { for y := uint(boundsTL.Y); y <= uint(boundsBR.Y); y++ {
w0 := w0_y w0 := w0_y
@ -183,12 +200,16 @@ func TriangleTextured(fb *RenderBuffer, texture Framebuffer, intensity float32,
w0a := float32(w0) * invarea w0a := float32(w0) * invarea
w1a := float32(w1) * invarea w1a := float32(w1) * invarea
w2a := float32(w2) * invarea w2a := float32(w2) * invarea
pz := w0a*v0v.Pos.Z + w1a*v1v.Pos.Z + w2a*v2v.Pos.Z //pz := w0a*v0v.Pos.Z + w1a*v1v.Pos.Z + w2a*v2v.Pos.Z
if pz < fb.ZBuffer[x+y*fb.Width] { pz := w0a*tiz0 + w1a*tiz1 + w2a*tiz2
if pz > fb.ZBuffer[x+y*fb.Width] {
fb.ZBuffer[x+y*fb.Width] = pz fb.ZBuffer[x+y*fb.Width] = pz
//pw := w0a/tiz0 + w1a/tiz1 + w2a/tiz2
//pw := w0a/v0v.W + w1a/v1v.W + w2a/v2v.W
pz = 1 / pz
r, g, b := texture.GetUv( r, g, b := texture.GetUv(
(w0a*v0v.Tex.X + w1a*v1v.Tex.X + w2a*v2v.Tex.X), (w0a*tiu0+w1a*tiu1+w2a*tiu2)*pz,
(w0a*v0v.Tex.Y + w1a*v1v.Tex.Y + w2a*v2v.Tex.Y), (w0a*tiv0+w1a*tiv1+w2a*tiv2)*pz,
) )
fb.Data.Set(x, y, byte(float32(r)*intensity), byte(float32(g)*intensity), byte(float32(b)*intensity)) fb.Data.Set(x, y, byte(float32(r)*intensity), byte(float32(g)*intensity), byte(float32(b)*intensity))
} }
@ -220,6 +241,7 @@ func conditionalAddTriangle(sc []HVec3f, tx []Vec3f, out []Facef) []Facef {
for i := range 3 { for i := range 3 {
f[i].Pos = sc[i].MakeConventional() f[i].Pos = sc[i].MakeConventional()
f[i].Tex = tx[i] f[i].Tex = tx[i]
f[i].W = sc[i].W
} }
// Backface culling: no need to do anything with triangles facing the wrong way // Backface culling: no need to do anything with triangles facing the wrong way
if EdgeFunction(f[0].Pos, f[1].Pos, f[2].Pos) <= 0 { if EdgeFunction(f[0].Pos, f[1].Pos, f[2].Pos) <= 0 {
@ -296,21 +318,13 @@ func ClipFace(face Facei, vecs []HVec3f, texs []Vec3f) []Facef {
// the original B point that we used, since that's part of the other // the original B point that we used, since that's part of the other
// triangle. But simply replacing it will make the triangle invisible, // triangle. But simply replacing it will make the triangle invisible,
// since it inverts the winding order (I think) // since it inverts the winding order (I think)
//hfa.LerpSelf(hf[ci], tac)
hf[bi] = hfa hf[bi] = hfa
hf[bi].LerpSelf(&hf[ci], tac) hf[bi].LerpSelf(&hf[ci], tac)
tx[bi] = LerpVec3f(txa, tx[ci], tac) tx[bi] = LerpVec3f(txa, tx[ci], tac)
//sct[bi].Pos = LerpVec3f(sc[ai].Pos, sc[ci].Pos, tac)
//sct[bi].Tex = LerpVec3f(sc[ai].Tex, sc[ci].Tex, tac)
//w[bi] = LerpF32(wa, w[ci], tac)
// Now swap the a and b (or we could swap c and b) // Now swap the a and b (or we could swap c and b)
hf[ai], hf[bi] = hf[bi], hf[ai] hf[ai], hf[bi] = hf[bi], hf[ai]
tx[ai], tx[bi] = tx[bi], tx[ai] tx[ai], tx[bi] = tx[bi], tx[ai]
//w[ai], w[bi] = w[bi], w[ai]
//sct[ai], sct[bi] = sct[bi], sct[ai]
//outfaces = conditionalAddTriangle(sct, w, outfaces)
outfaces = conditionalAddTriangle(hf[:], tx[:], outfaces) outfaces = conditionalAddTriangle(hf[:], tx[:], outfaces)
} else if len(outers) != 3 { // Output the face itself, no modification } else if len(outers) != 3 { // Output the face itself, no modification

View File

@ -19,8 +19,8 @@ import (
) )
const ( const (
NearClip = 0.0001 NearClip = 0.01
FarClip = 10 FarClip = 100
Movement = 1.0 Movement = 1.0
Rotation = 0.25 Rotation = 0.25
LookLock = math.Pi / 32 LookLock = math.Pi / 32