Skip to content

Commit

Permalink
Merge pull request #116 from quillaja/issue-29a-no-groups
Browse files Browse the repository at this point in the history
Issue 29a: errors when no object or group specified
  • Loading branch information
danaugrs committed Mar 14, 2019
2 parents 1b502de + 63c77b3 commit 70eaa04
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions loader/obj/obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,13 +489,19 @@ func (dec *Decoder) parseObject(fields []string) error {
if len(fields) < 1 {
return errors.New("Object line (o) with no fields")
}

dec.Objects = append(dec.Objects, makeObject(fields[0]))
dec.objCurrent = &dec.Objects[len(dec.Objects)-1]
return nil
}

// makes an Object with name.
func makeObject(name string) Object {
var ob Object
ob.Name = fields[0]
ob.Name = name
ob.Faces = make([]Face, 0)
ob.materials = make([]string, 0)
dec.Objects = append(dec.Objects, ob)
dec.objCurrent = &dec.Objects[len(dec.Objects)-1]
return nil
return ob
}

// Parses a vertex position line
Expand Down Expand Up @@ -553,6 +559,14 @@ func (dec *Decoder) parseTex(fields []string) error {
// f v1[/vt1][/vn1] v2[/vt2][/vn2] v3[/vt3][/vn3] ...
func (dec *Decoder) parseFace(fields []string) error {

// NOTE(quillaja): this wasn't really part of the original issue-29
if dec.objCurrent == nil {
// if a face line is encountered before a group (g) or object (o),
// create a new "default" object. This 'handles' the case when
// a g or o line is not specified (allowed in OBJ format)
dec.parseObject([]string{fmt.Sprintf("unnamed%d", dec.line)})
}

// If current object has no material, appends last material if defined
if len(dec.objCurrent.materials) == 0 && dec.matCurrent != nil {
dec.objCurrent.materials = append(dec.objCurrent.materials, dec.matCurrent.Name)
Expand Down Expand Up @@ -658,6 +672,11 @@ func (dec *Decoder) parseUsemtl(fields []string) error {
return dec.formatError("Usemtl with no fields")
}

// NOTE(quillaja): see similar nil test in parseFace()
if dec.objCurrent == nil {
dec.parseObject([]string{fmt.Sprintf("unnamed%d", dec.line)})
}

// Checks if this material has already been parsed
name := fields[0]
mat := dec.Materials[name]
Expand Down

0 comments on commit 70eaa04

Please sign in to comment.