Skip to content

Commit 70eaa04

Browse files
authored
Merge pull request #116 from quillaja/issue-29a-no-groups
Issue 29a: errors when no object or group specified
2 parents 1b502de + 63c77b3 commit 70eaa04

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

loader/obj/obj.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,13 +489,19 @@ func (dec *Decoder) parseObject(fields []string) error {
489489
if len(fields) < 1 {
490490
return errors.New("Object line (o) with no fields")
491491
}
492+
493+
dec.Objects = append(dec.Objects, makeObject(fields[0]))
494+
dec.objCurrent = &dec.Objects[len(dec.Objects)-1]
495+
return nil
496+
}
497+
498+
// makes an Object with name.
499+
func makeObject(name string) Object {
492500
var ob Object
493-
ob.Name = fields[0]
501+
ob.Name = name
494502
ob.Faces = make([]Face, 0)
495503
ob.materials = make([]string, 0)
496-
dec.Objects = append(dec.Objects, ob)
497-
dec.objCurrent = &dec.Objects[len(dec.Objects)-1]
498-
return nil
504+
return ob
499505
}
500506

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

562+
// NOTE(quillaja): this wasn't really part of the original issue-29
563+
if dec.objCurrent == nil {
564+
// if a face line is encountered before a group (g) or object (o),
565+
// create a new "default" object. This 'handles' the case when
566+
// a g or o line is not specified (allowed in OBJ format)
567+
dec.parseObject([]string{fmt.Sprintf("unnamed%d", dec.line)})
568+
}
569+
556570
// If current object has no material, appends last material if defined
557571
if len(dec.objCurrent.materials) == 0 && dec.matCurrent != nil {
558572
dec.objCurrent.materials = append(dec.objCurrent.materials, dec.matCurrent.Name)
@@ -658,6 +672,11 @@ func (dec *Decoder) parseUsemtl(fields []string) error {
658672
return dec.formatError("Usemtl with no fields")
659673
}
660674

675+
// NOTE(quillaja): see similar nil test in parseFace()
676+
if dec.objCurrent == nil {
677+
dec.parseObject([]string{fmt.Sprintf("unnamed%d", dec.line)})
678+
}
679+
661680
// Checks if this material has already been parsed
662681
name := fields[0]
663682
mat := dec.Materials[name]

0 commit comments

Comments
 (0)