Oops, sucking
This commit is contained in:
parent
516dc262eb
commit
e473fe838f
@ -12,7 +12,7 @@ const (
|
|||||||
Width = 512
|
Width = 512
|
||||||
Height = 512
|
Height = 512
|
||||||
ObjectFile = "head.obj"
|
ObjectFile = "head.obj"
|
||||||
Repeat = 100_000
|
Repeat = 1 //00_000
|
||||||
)
|
)
|
||||||
|
|
||||||
func must(err error) {
|
func must(err error) {
|
||||||
|
@ -44,8 +44,63 @@ func line(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i) {
|
|||||||
Bresenham2(fb, color, v0.X, v0.Y, v1.X, v1.Y)
|
Bresenham2(fb, color, v0.X, v0.Y, v1.X, v1.Y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*func LineSweep(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 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) {
|
||||||
line(fb, color, v0, v1)
|
// Very silly manual sorting by Y
|
||||||
line(fb, color, v1, v2)
|
if v2.Y < v0.Y {
|
||||||
line(fb, color, v2, v0)
|
v0, v2 = v2, v0
|
||||||
|
}
|
||||||
|
if v1.Y < v0.Y {
|
||||||
|
v0, v1 = v1, v0
|
||||||
|
}
|
||||||
|
if v2.Y < v1.Y {
|
||||||
|
v1, v2 = v2, v1
|
||||||
|
}
|
||||||
|
|
||||||
|
// The lesson says to always figure out what's on the left.
|
||||||
|
// As such, we always go from left to right
|
||||||
|
// While drawing the straight line, go the opposite direction if the
|
||||||
|
// longer side is on the right (we go from long side to short)
|
||||||
|
var sweep int = 1
|
||||||
|
if v2.X > v1.X {
|
||||||
|
sweep = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't worry about speed for now...
|
||||||
|
dy := float32(v2.Y-v0.Y) - 0.5
|
||||||
|
var v02step, v01step, v12step, xlong, xshort float32
|
||||||
|
|
||||||
|
xlong = float32(v0.X) + 0.5
|
||||||
|
xshort = xlong
|
||||||
|
|
||||||
|
// We can check just for greater than because we sorted the vertices
|
||||||
|
if dy > 0 {
|
||||||
|
v02step = (float32(v2.X-v0.X) - 0.5) / dy // long side always
|
||||||
|
v01step = (float32(v1.X-v0.X) - 0.5) / dy // first short side
|
||||||
|
v12step = (float32(v2.X-v1.X) - 0.5) / dy // second short side
|
||||||
|
xshort += v01step / 2
|
||||||
|
xlong += v02step / 2
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
for y := v0.Y; y <= v2.Y; y++ {
|
||||||
|
// Draw a horizontal line from
|
||||||
|
for x := int(math.Floor(float64(xlong))); x <= int(math.Floor(float64(xshort))); x += sweep {
|
||||||
|
fb.SetSafe(uint(x), uint(y), color)
|
||||||
|
}
|
||||||
|
xlong += v02step
|
||||||
|
if y < v1.Y {
|
||||||
|
xshort += v01step
|
||||||
|
} else {
|
||||||
|
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