We have a head

This commit is contained in:
Carlos Sanchez 2024-07-23 23:01:31 -04:00
parent 330c448a5d
commit a8a90ea32b
3 changed files with 18 additions and 9 deletions

View File

@ -36,6 +36,13 @@ func (fb *Framebuffer) Set(x uint, y uint, color uint) {
fb.Data[x+y*fb.Width] = color
}
func (fb *Framebuffer) SetSafe(x uint, y uint, color uint) {
if x >= fb.Width || y >= fb.Height {
return
}
fb.Data[x+y*fb.Width] = color
}
// Given some image data, return a string that is the ppm of it
func (fb *Framebuffer) ExportPPM() string {
var result strings.Builder

View File

@ -64,16 +64,18 @@ func ParseObj(reader io.Reader) (*ObjModel, error) {
// Read a face; in our example, it's always three sets.
// For THIS example, we throw away those other values
var face Facef
for i := range 3 {
var vi, ti0, ti1 int
_, err := fmt.Sscanf(line, "%d/%d/%d", &vi, &ti0, &ti1)
var vi [3]int
var ti int
_, err := fmt.Sscanf(line, "%d/%d/%d %d/%d/%d %d/%d/%d",
&vi[0], &ti, &ti, &vi[1], &ti, &ti, &vi[2], &ti, &ti)
if err != nil {
return nil, err
}
if vi > len(result.Vertices) || vi < 1 {
return nil, fmt.Errorf("Face vertex index out of bounds: %d", vi)
for i := range 3 {
if vi[i] > len(result.Vertices) || vi[i] < 1 {
return nil, fmt.Errorf("Face vertex index out of bounds: %d", vi[i])
}
face[i] = result.Vertices[vi-1]
face[i] = result.Vertices[vi[i]-1]
}
result.Faces = append(result.Faces, face)
}

View File

@ -173,7 +173,7 @@ func Bresenham2(fb *Framebuffer, color uint, x0 int, y0 int, x1 int, y1 int) {
err := dx + dy
for {
fb.Set(uint(x0), uint(y0), color)
fb.SetSafe(uint(x0), uint(y0), color)
if x0 == x1 && y0 == y1 {
break
}