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 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 // Given some image data, return a string that is the ppm of it
func (fb *Framebuffer) ExportPPM() string { func (fb *Framebuffer) ExportPPM() string {
var result strings.Builder 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. // Read a face; in our example, it's always three sets.
// For THIS example, we throw away those other values // For THIS example, we throw away those other values
var face Facef var face Facef
for i := range 3 { var vi [3]int
var vi, ti0, ti1 int var ti int
_, err := fmt.Sscanf(line, "%d/%d/%d", &vi, &ti0, &ti1) _, 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 { if err != nil {
return nil, err return nil, err
} }
if vi > len(result.Vertices) || vi < 1 { for i := range 3 {
return nil, fmt.Errorf("Face vertex index out of bounds: %d", vi) 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) 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 err := dx + dy
for { for {
fb.Set(uint(x0), uint(y0), color) fb.SetSafe(uint(x0), uint(y0), color)
if x0 == x1 && y0 == y1 { if x0 == x1 && y0 == y1 {
break break
} }