2024-08-02 19:35:30 +00:00
|
|
|
package hrend
|
|
|
|
|
|
|
|
import (
|
|
|
|
// "log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Figure out the minimum bounding box for a triangle defined by
|
|
|
|
// these vertices. Returns the top left and bottom right points,
|
|
|
|
// inclusive
|
|
|
|
func ComputeBoundingBox(v0, v1, v2 Vec2i) (Vec2i, Vec2i) {
|
|
|
|
return Vec2i{min(v0.X, v1.X, v2.X), min(v0.Y, v1.Y, v2.Y)},
|
|
|
|
Vec2i{max(v0.X, v1.X, v2.X), max(v0.Y, v1.Y, v2.Y)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The generic edge function, returning positive if P is on the right side of
|
|
|
|
// the line drawn between v1 and v2. This is counter clockwise
|
|
|
|
func EdgeFunction(v1, v2, p Vec2f) float32 {
|
|
|
|
return (p.X-v1.X)*(v2.Y-v1.Y) - (p.Y-v1.Y)*(v2.X-v1.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This computes the x and y per-pixel increment for the line going
|
|
|
|
// between v1 and v2 (also counter clockwise)
|
|
|
|
func EdgeIncrement(v1, v2 Vec2f) (float32, float32) {
|
|
|
|
return (v2.Y - v1.Y), -(v2.X - v1.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The generic edge function, returning positive if P is on the right side of
|
|
|
|
// the line drawn between v1 and v2. This is counter clockwise
|
|
|
|
func EdgeFunctioni(v1, v2, p Vec2i) int {
|
|
|
|
return (p.X-v1.X)*(v2.Y-v1.Y) - (p.Y-v1.Y)*(v2.X-v1.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This computes the x and y per-pixel increment for the line going
|
|
|
|
// between v1 and v2 (also counter clockwise)
|
|
|
|
func EdgeIncrementi(v1, v2 Vec2i) (int, int) {
|
|
|
|
return (v2.Y - v1.Y), -(v2.X - v1.X)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ZClip(v0f Vec3f, v1f Vec3f, v2f Vec3f) bool {
|
|
|
|
maxz := max(v0f.Z, v1f.Z, v2f.Z)
|
|
|
|
return maxz < 0 || maxz > 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func TriangleFlat(fb *RenderBuffer, color uint, v0f Vec3f, v1f Vec3f, v2f Vec3f) {
|
|
|
|
if ZClip(v0f, v1f, v2f) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v0 := v0f.ToVec2i()
|
|
|
|
v1 := v1f.ToVec2i()
|
|
|
|
v2 := v2f.ToVec2i()
|
|
|
|
r, g, b := Uint2Col(color)
|
|
|
|
boundsTL, boundsBR := ComputeBoundingBox(v0, v1, v2)
|
2024-08-02 21:55:21 +00:00
|
|
|
if boundsBR.X < 0 || boundsBR.Y < 0 || boundsTL.X >= int(fb.Width) || boundsTL.Y >= int(fb.Height) {
|
2024-08-02 19:35:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if boundsTL.Y < 0 {
|
|
|
|
boundsTL.Y = 0
|
|
|
|
}
|
|
|
|
if boundsTL.X < 0 {
|
|
|
|
boundsTL.X = 0
|
|
|
|
}
|
|
|
|
if boundsBR.Y >= int(fb.Height) {
|
|
|
|
boundsBR.Y = int(fb.Height - 1)
|
|
|
|
}
|
|
|
|
if boundsBR.X >= int(fb.Width) {
|
|
|
|
boundsBR.X = int(fb.Width - 1)
|
|
|
|
}
|
|
|
|
// Where to start our scanning
|
|
|
|
pstart := Vec2i{boundsTL.X, boundsTL.Y}
|
|
|
|
parea := EdgeFunctioni(v0, v1, v2)
|
|
|
|
invarea := 1 / float32(parea)
|
|
|
|
w0_y := EdgeFunctioni(v1, v2, pstart)
|
|
|
|
w1_y := EdgeFunctioni(v2, v0, pstart)
|
|
|
|
w2_y := EdgeFunctioni(v0, v1, pstart)
|
|
|
|
w0_xi, w0_yi := EdgeIncrementi(v1, v2)
|
|
|
|
w1_xi, w1_yi := EdgeIncrementi(v2, v0)
|
|
|
|
w2_xi, w2_yi := EdgeIncrementi(v0, v1)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
w0a := float32(w0) * invarea
|
|
|
|
w1a := float32(w1) * invarea
|
|
|
|
w2a := float32(w2) * invarea
|
|
|
|
pz := w0a*v0f.Z + w1a*v1f.Z + w2a*v2f.Z
|
|
|
|
if pz < fb.ZBuffer[x+y*fb.Width] {
|
|
|
|
fb.ZBuffer[x+y*fb.Width] = pz
|
|
|
|
fb.Data.Set(x, y, r, g, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w0 += w0_xi
|
|
|
|
w1 += w1_xi
|
|
|
|
w2 += w2_xi
|
|
|
|
}
|
|
|
|
w0_y += w0_yi
|
|
|
|
w1_y += w1_yi
|
|
|
|
w2_y += w2_yi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TriangleTextured(fb *RenderBuffer, texture Framebuffer, intensity float32, v0v Vertex, v1v Vertex, v2v Vertex) {
|
|
|
|
if ZClip(v0v.Pos, v1v.Pos, v2v.Pos) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v0 := v0v.Pos.ToVec2i()
|
|
|
|
v1 := v1v.Pos.ToVec2i()
|
|
|
|
v2 := v2v.Pos.ToVec2i()
|
|
|
|
boundsTL, boundsBR := ComputeBoundingBox(v0, v1, v2)
|
|
|
|
if boundsBR.Y < 0 || boundsBR.X < 0 || boundsTL.X >= int(fb.Width) || boundsTL.Y >= int(fb.Height) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
parea := EdgeFunctioni(v0, v1, v2)
|
|
|
|
if parea == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if boundsTL.Y < 0 {
|
|
|
|
boundsTL.Y = 0
|
|
|
|
}
|
|
|
|
if boundsTL.X < 0 {
|
|
|
|
boundsTL.X = 0
|
|
|
|
}
|
|
|
|
if boundsBR.Y >= int(fb.Height) {
|
|
|
|
boundsBR.Y = int(fb.Height - 1)
|
|
|
|
}
|
|
|
|
if boundsBR.X >= int(fb.Width) {
|
|
|
|
boundsBR.X = int(fb.Width - 1)
|
|
|
|
}
|
|
|
|
// Where to start our scanning
|
|
|
|
pstart := Vec2i{boundsTL.X, boundsTL.Y}
|
|
|
|
invarea := 1 / float32(parea)
|
|
|
|
w0_y := EdgeFunctioni(v1, v2, pstart)
|
|
|
|
w1_y := EdgeFunctioni(v2, v0, pstart)
|
|
|
|
w2_y := EdgeFunctioni(v0, v1, pstart)
|
|
|
|
w0_xi, w0_yi := EdgeIncrementi(v1, v2)
|
|
|
|
w1_xi, w1_yi := EdgeIncrementi(v2, v0)
|
|
|
|
w2_xi, w2_yi := EdgeIncrementi(v0, v1)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
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] {
|
|
|
|
fb.ZBuffer[x+y*fb.Width] = pz
|
|
|
|
r, g, b := 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),
|
|
|
|
)
|
|
|
|
fb.Data.Set(x, y, byte(float32(r)*intensity), byte(float32(g)*intensity), byte(float32(b)*intensity))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w0 += w0_xi
|
|
|
|
w1 += w1_xi
|
|
|
|
w2 += w2_xi
|
|
|
|
}
|
|
|
|
w0_y += w0_yi
|
|
|
|
w1_y += w1_yi
|
|
|
|
w2_y += w2_yi
|
|
|
|
}
|
|
|
|
}
|