2024-07-26 21:32:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-27 03:15:16 +00:00
|
|
|
//"log"
|
2024-07-26 21:32:20 +00:00
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Bresenham2(fb *Framebuffer, color uint, x0 int, y0 int, x1 int, y1 int) {
|
|
|
|
dx := int(math.Abs(float64(x1 - x0)))
|
|
|
|
sx := -1
|
|
|
|
if x0 < x1 {
|
|
|
|
sx = 1
|
|
|
|
}
|
|
|
|
dy := -int(math.Abs(float64(y1 - y0)))
|
|
|
|
sy := -1
|
|
|
|
if y0 < y1 {
|
|
|
|
sy = 1
|
|
|
|
}
|
|
|
|
err := dx + dy
|
|
|
|
|
|
|
|
for {
|
|
|
|
fb.SetSafe(uint(x0), uint(y0), color)
|
|
|
|
if x0 == x1 && y0 == y1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
e2 := 2 * err
|
|
|
|
if e2 >= dy {
|
|
|
|
if x0 == x1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err += dy
|
|
|
|
x0 += sx
|
|
|
|
}
|
|
|
|
if e2 <= dx {
|
|
|
|
if y0 == y1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err += dx
|
|
|
|
y0 += sy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 21:48:47 +00:00
|
|
|
func line(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i) {
|
|
|
|
Bresenham2(fb, color, v0.X, v0.Y, v1.X, v1.Y)
|
|
|
|
}
|
|
|
|
|
2024-07-26 23:37:25 +00:00
|
|
|
/*func LineSweep(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
2024-07-26 21:48:47 +00:00
|
|
|
func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
|
2024-07-27 00:10:18 +00:00
|
|
|
// The dude gets rid of "degenerate" triangles so... we do too?
|
|
|
|
if v2.Y == v1.Y && v1.Y == v0.Y {
|
|
|
|
return
|
|
|
|
}
|
2024-07-26 23:37:25 +00:00
|
|
|
// Very silly manual sorting by Y
|
|
|
|
if v2.Y < v0.Y {
|
|
|
|
v0, v2 = v2, v0
|
|
|
|
}
|
|
|
|
if v1.Y < v0.Y {
|
|
|
|
v0, v1 = v1, v0
|
|
|
|
}
|
|
|
|
if v2.Y < v1.Y {
|
|
|
|
v1, v2 = v2, v1
|
|
|
|
}
|
|
|
|
|
|
|
|
var v02step, v01step, v12step, xlong, xshort float32
|
|
|
|
|
2024-07-26 23:57:36 +00:00
|
|
|
xlong = float32(v0.X)
|
2024-07-26 23:37:25 +00:00
|
|
|
xshort = xlong
|
|
|
|
|
2024-07-27 00:10:18 +00:00
|
|
|
// The first and last Y CAN'T be equal because sorting!!
|
|
|
|
if v1.Y == v0.Y {
|
|
|
|
xshort = float32(v1.X)
|
|
|
|
}
|
|
|
|
|
2024-07-26 23:37:25 +00:00
|
|
|
// We can check just for greater than because we sorted the vertices
|
2024-07-26 23:57:36 +00:00
|
|
|
// Assume 02 is on the right(?) and 01 on the left
|
|
|
|
v02step = (float32(v2.X - v0.X)) / (float32(v2.Y-v0.Y) + 0.001) // long side always
|
|
|
|
v01step = (float32(v1.X - v0.X)) / (float32(v1.Y-v0.Y) + 0.001) // first short side
|
|
|
|
v12step = (float32(v2.X - v1.X)) / (float32(v2.Y-v1.Y) + 0.001) // second short side
|
2024-07-26 23:37:25 +00:00
|
|
|
|
|
|
|
for y := v0.Y; y <= v2.Y; y++ {
|
2024-07-27 00:10:18 +00:00
|
|
|
xleft := int(xshort)
|
|
|
|
xright := int(xlong)
|
2024-07-26 23:45:12 +00:00
|
|
|
if xleft > xright {
|
|
|
|
xleft, xright = xright, xleft
|
|
|
|
}
|
2024-07-27 03:52:48 +00:00
|
|
|
if xleft < 0 || xright >= int(fb.Width) {
|
|
|
|
continue
|
|
|
|
}
|
2024-07-27 00:10:18 +00:00
|
|
|
// Draw a horizontal line from left to right
|
2024-07-26 23:45:12 +00:00
|
|
|
for x := xleft; x <= xright; x++ {
|
2024-07-27 03:52:48 +00:00
|
|
|
fb.Set(uint(x), uint(y), color)
|
2024-07-26 23:37:25 +00:00
|
|
|
}
|
|
|
|
xlong += v02step
|
|
|
|
if y < v1.Y {
|
|
|
|
xshort += v01step
|
|
|
|
} else {
|
|
|
|
xshort += v12step
|
|
|
|
}
|
|
|
|
}
|
2024-07-26 21:32:20 +00:00
|
|
|
}
|
2024-07-27 03:15:16 +00:00
|
|
|
|
|
|
|
// How does this work? Compare with your
|
|
|
|
// other barycentric function (in a different repo). In the original
|
|
|
|
// cpp code, they used an overloaded operator ^ to mean cross product
|
|
|
|
func Barycentric(v0, v1, v2, p Vec2i) Vec3f {
|
|
|
|
// WARN: Just not doing this one
|
|
|
|
u := Vec3f{}
|
|
|
|
if math.Abs(float64(u.Z)) < 1 {
|
|
|
|
return Vec3f{-1, 1, 1}
|
|
|
|
}
|
|
|
|
return Vec3f{1 - (u.X+u.Y)/u.Z, u.Y / u.Z, u.X / u.Z}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2024-07-27 05:16:49 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2024-07-27 03:15:16 +00:00
|
|
|
func Triangle2(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
|
|
|
|
boundsTL, boundsBR := ComputeBoundingBox(v0, v1, v2)
|
2024-07-27 03:52:48 +00:00
|
|
|
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
|
2024-07-27 05:16:49 +00:00
|
|
|
pstart := Vec2i{boundsTL.X, boundsTL.Y}
|
2024-07-27 03:15:16 +00:00
|
|
|
//log.Print(boundsTL, boundsBR)
|
2024-07-27 05:16:49 +00:00
|
|
|
// v0f := v0.ToF()
|
|
|
|
// v1f := v1.ToF()
|
|
|
|
// v2f := v2.ToF()
|
|
|
|
// parea := EdgeFunction(v0f, v1f, v2f)
|
|
|
|
// invarea := 1 / 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)
|
|
|
|
//dyi := int(fb.Width)
|
|
|
|
//dy := boundsTL.X + dyi*boundsTL.Y
|
2024-07-27 03:52:48 +00:00
|
|
|
|
|
|
|
for y := uint(boundsTL.Y); y <= uint(boundsBR.Y); y++ {
|
2024-07-27 03:15:16 +00:00
|
|
|
w0 := w0_y
|
|
|
|
w1 := w1_y
|
|
|
|
w2 := w2_y
|
2024-07-27 05:16:49 +00:00
|
|
|
//di := dy
|
|
|
|
//done := false
|
2024-07-27 03:52:48 +00:00
|
|
|
for x := uint(boundsTL.X); x <= uint(boundsBR.X); x++ {
|
2024-07-27 05:16:49 +00:00
|
|
|
if (w0 | w1 | w2) >= 0 {
|
|
|
|
//fb.Data[di] = color
|
|
|
|
fb.Set(x, y, color)
|
|
|
|
//done = true
|
|
|
|
// w0a := w0 * invarea
|
|
|
|
// w1a := w1 * invarea
|
|
|
|
// w2a := w2 * invarea
|
|
|
|
// fb.Set(x, y, Col2Uint(byte(255*w0a), byte(255*w1a), byte(255*w2a)))
|
|
|
|
} /*else if done {
|
|
|
|
break
|
|
|
|
}*/
|
|
|
|
//di += 1
|
2024-07-27 03:15:16 +00:00
|
|
|
w0 += w0_xi
|
|
|
|
w1 += w1_xi
|
|
|
|
w2 += w2_xi
|
|
|
|
}
|
2024-07-27 05:16:49 +00:00
|
|
|
//dy += dyi
|
2024-07-27 03:15:16 +00:00
|
|
|
w0_y += w0_yi
|
|
|
|
w1_y += w1_yi
|
|
|
|
w2_y += w2_yi
|
|
|
|
}
|
|
|
|
}
|