Cleanup sweeping algo
This commit is contained in:
parent
7b2e52cedb
commit
37c6ce73a8
@ -49,6 +49,10 @@ func line(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i) {
|
|||||||
}*/
|
}*/
|
||||||
|
|
||||||
func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
|
func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
|
||||||
|
// The dude gets rid of "degenerate" triangles so... we do too?
|
||||||
|
if v2.Y == v1.Y && v1.Y == v0.Y {
|
||||||
|
return
|
||||||
|
}
|
||||||
// Very silly manual sorting by Y
|
// Very silly manual sorting by Y
|
||||||
if v2.Y < v0.Y {
|
if v2.Y < v0.Y {
|
||||||
v0, v2 = v2, v0
|
v0, v2 = v2, v0
|
||||||
@ -59,29 +63,30 @@ func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
|
|||||||
if v2.Y < v1.Y {
|
if v2.Y < v1.Y {
|
||||||
v1, v2 = v2, v1
|
v1, v2 = v2, v1
|
||||||
}
|
}
|
||||||
//log.Print(v0, v1, v2)
|
|
||||||
|
|
||||||
// Don't worry about speed for now...
|
|
||||||
var v02step, v01step, v12step, xlong, xshort float32
|
var v02step, v01step, v12step, xlong, xshort float32
|
||||||
|
|
||||||
xlong = float32(v0.X)
|
xlong = float32(v0.X)
|
||||||
xshort = xlong
|
xshort = xlong
|
||||||
|
|
||||||
|
// The first and last Y CAN'T be equal because sorting!!
|
||||||
|
if v1.Y == v0.Y {
|
||||||
|
xshort = float32(v1.X)
|
||||||
|
}
|
||||||
|
|
||||||
// We can check just for greater than because we sorted the vertices
|
// We can check just for greater than because we sorted the vertices
|
||||||
// Assume 02 is on the right(?) and 01 on the left
|
// 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
|
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
|
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
|
v12step = (float32(v2.X - v1.X)) / (float32(v2.Y-v1.Y) + 0.001) // second short side
|
||||||
xshort += v01step / 2
|
|
||||||
xlong += v02step / 2
|
|
||||||
|
|
||||||
for y := v0.Y; y <= v2.Y; y++ {
|
for y := v0.Y; y <= v2.Y; y++ {
|
||||||
xleft := int(math.Floor(float64(xshort)))
|
xleft := int(xshort)
|
||||||
xright := int(math.Floor(float64(xlong)))
|
xright := int(xlong)
|
||||||
if xleft > xright {
|
if xleft > xright {
|
||||||
xleft, xright = xright, xleft
|
xleft, xright = xright, xleft
|
||||||
}
|
}
|
||||||
// Draw a horizontal line from elft to right
|
// Draw a horizontal line from left to right
|
||||||
for x := xleft; x <= xright; x++ {
|
for x := xleft; x <= xright; x++ {
|
||||||
fb.SetSafe(uint(x), uint(y), color)
|
fb.SetSafe(uint(x), uint(y), color)
|
||||||
}
|
}
|
||||||
@ -92,8 +97,4 @@ func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
|
|||||||
xshort += v12step
|
xshort += v12step
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// line(fb, color, v0, v1)
|
|
||||||
// line(fb, color, v1, v2)
|
|
||||||
// line(fb, color, v2, v0)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user