/
obj.go
896 lines (825 loc) · 23.2 KB
/
obj.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
// Copyright (c) 2019, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This package is based extensively on https://github.com/g3n/engine :
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package obj is used to parse the Wavefront OBJ file format (*.obj), including
// associated materials (*.mtl). Not all features of the OBJ format are
// supported. Basic format info: https://en.wikipedia.org/wiki/Wavefront_.obj_file
package obj
import (
"bufio"
"errors"
"fmt"
"image/color"
"io"
"io/fs"
"math"
"path/filepath"
"strconv"
"strings"
"cogentcore.org/core/colors"
"cogentcore.org/core/gox/dirs"
"cogentcore.org/core/math32"
"cogentcore.org/core/xyz"
)
func init() {
xyz.Decoders[".obj"] = &Decoder{}
}
// Decoder contains all decoded data from the obj and mtl files.
// It also implements the xyz.Decoder interface and an instance
// is registered to handle .obj files.
type Decoder struct {
FSys fs.FS // filesystem, used for all loading
Objfile string // .obj filename within FSys
Objects []Object // decoded objects
Matlib string // name of the material lib
Materials map[string]*Material // maps material name to object
Vertices math32.ArrayF32 // vertices positions array
Normals math32.ArrayF32 // vertices normals
Uvs math32.ArrayF32 // vertices texture coordinates
Warnings []string // warning messages
line uint // current line number
objCurrent *Object // current object
matCurrent *Material // current material
smoothCurrent bool // current smooth state
}
func (dec *Decoder) New() xyz.Decoder {
di := new(Decoder)
di.Objects = make([]Object, 0)
di.Warnings = make([]string, 0)
di.Materials = make(map[string]*Material)
di.Vertices = math32.NewArrayF32(0, 0)
di.Normals = math32.NewArrayF32(0, 0)
di.Uvs = math32.NewArrayF32(0, 0)
di.line = 1
return di
}
// Destroy deletes data created during loading
func (dec *Decoder) Destroy() {
for oi := range dec.Objects {
ob := &dec.Objects[oi]
ob.Destroy()
}
dec.Objects = nil
dec.Warnings = nil
dec.Materials = nil
dec.Vertices = nil
dec.Normals = nil
dec.Uvs = nil
dec.objCurrent = nil
dec.matCurrent = nil
}
func (dec *Decoder) Desc() string {
return ".obj = Wavefront OBJ format, including associated materials (.mtl) which can be specified as second file name -- otherwise auto-searched based on .obj filename, or a default material is used. Only supports Object-level data, not full Scene (camera, lights etc)."
}
func (dec *Decoder) HasScene() bool {
return false
}
func (dec *Decoder) SetFileFS(fsys fs.FS, fname string) ([]string, error) {
dec.FSys = fsys
exists, err := dirs.FileExistsFS(dec.FSys, fname)
if !exists {
return nil, errors.New("xyz obj.Decoder: " + err.Error())
}
mtlf := strings.TrimSuffix(fname, ".obj") + ".mtl"
exists, _ = dirs.FileExistsFS(dec.FSys, fname)
if exists {
return []string{fname, mtlf}, nil
} else {
return []string{fname}, nil
}
}
// Decode reads the given data and decodes into Decoder tmp vars.
// if 2 args are passed, first is .obj and second is .mtl
func (dec *Decoder) Decode(rs []io.Reader) error {
nf := len(rs)
if nf == 0 {
return errors.New("xyz obj.Decoder: no readers passed")
}
// Parses obj lines
err := dec.parse(rs[0], dec.parseObjLine)
if err != nil {
return err
}
dec.matCurrent = nil
dec.line = 1
useDef := nf == 1
if nf > 1 {
err = dec.parse(rs[1], dec.parseMtlLine)
if err != nil {
useDef = true
}
}
if useDef {
for key := range dec.Materials {
dec.Materials[key] = defaultMat
}
}
return nil
}
// Object contains all information about one decoded object
type Object struct {
Name string // Object name
Faces []Face // Faces
materials []string // Materials used in this object
}
func (ob *Object) Destroy() {
ob.materials = nil
for fi := range ob.Faces {
fc := &ob.Faces[fi]
fc.Destroy()
}
ob.Faces = nil
}
// Face contains all information about an object face
type Face struct {
Vertices []int // Indices to the face vertices
Uvs []int // Indices to the face UV coordinates
Normals []int // Indices to the face normals
Material string // Material name
Smooth bool // Smooth face
}
func (fc *Face) Destroy() {
fc.Vertices = nil
fc.Uvs = nil
fc.Normals = nil
}
// Material contains all information about an object material
type Material struct {
Name string // Material name
Illum int // Illumination model
Opacity float32 // Opacity factor
Refraction float32 // Refraction factor
Shininess float32 // Shininess (specular exponent)
Ambient color.RGBA // Ambient color reflectivity
Diffuse color.RGBA // Diffuse color reflectivity
Specular color.RGBA // Specular color reflectivity
Emissive color.RGBA // Emissive color
MapKd string // Texture file linked to diffuse color
Tiling xyz.Tiling // Tiling parameters: repeat and offset
}
// Light gray default material used as when other materials cannot be loaded.
var defaultMat = &Material{
Diffuse: color.RGBA{R: 0xA0, G: 0xA0, B: 0xA0, A: 0xFF},
Ambient: color.RGBA{R: 0xA0, G: 0xA0, B: 0xA0, A: 0xFF},
Specular: color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0xFF},
Shininess: 30.0,
}
// Local constants
const (
blanks = "\r\n\t "
invINDEX = math.MaxUint32
objType = "obj"
mtlType = "mtl"
)
// SetScene sets group with with all the decoded objects.
func (dec *Decoder) SetScene(sc *xyz.Scene) {
gp := xyz.NewGroup(sc, dec.Objfile)
dec.SetGroup(sc, gp)
}
// SetGroup sets group with with all the decoded objects.
// calls Destroy after to free memory
func (dec *Decoder) SetGroup(sc *xyz.Scene, gp *xyz.Group) {
for i := range dec.Objects {
obj := &dec.Objects[i]
if len(obj.Faces) == 0 {
continue
}
objgp := xyz.NewGroup(gp, obj.Name)
dec.SetObject(sc, objgp, obj)
}
dec.Destroy()
}
// SetObject sets the object as a group with each xyz.Solid as a mesh with unique material
func (dec *Decoder) SetObject(sc *xyz.Scene, objgp *xyz.Group, ob *Object) {
matName := ""
var sld *xyz.Solid
var ms *xyz.GenMesh
sldidx := 0
idxs := make([]int, 0, 4)
for fi := range ob.Faces {
face := &ob.Faces[fi]
if face.Material != matName || sld == nil {
sldnm := fmt.Sprintf("%s_%d", ob.Name, sldidx)
ms = &xyz.GenMesh{}
ms.Nm = sldnm
sc.AddMeshUnique(ms)
sld = xyz.NewSolid(objgp, sldnm).SetMesh(ms)
matName = face.Material
dec.SetMat(sc, sld, matName)
sldidx++
}
// Copy face vertices to geometry
// https://stackoverflow.com/questions/23723993/converting-quadriladerals-in-an-obj-file-into-triangles
// logic for 0, i, i+1
// note: the last comment at the end is *incorrect* -- it really is the triangle fans as impl here:
idxs = idxs[:3]
idxs[0] = dec.copyVertex(ms, face, 0)
idxs[1] = dec.copyVertex(ms, face, 1)
idxs[2] = dec.copyVertex(ms, face, 2)
dec.addNorms(ms, 0, 1, 2, idxs)
for idx := 2; idx < len(face.Vertices); idx++ {
dec.setIndex(ms, face, 0, &idxs)
dec.setIndex(ms, face, idx-1, &idxs)
dec.setIndex(ms, face, idx, &idxs)
dec.addNorms(ms, idx-3, idx-1, idx, idxs)
}
}
}
func (dec *Decoder) addNorms(ms *xyz.GenMesh, ai, bi, ci int, idxs []int) {
if ms.Norm.Size() >= ms.Vtx.Size() {
return
}
var a, b, c math32.Vector3
ms.Vtx.GetVector3(3*idxs[ai], &a)
ms.Vtx.GetVector3(3*idxs[bi], &b)
ms.Vtx.GetVector3(3*idxs[ci], &c)
nrm := math32.Normal(a, b, c)
for {
ms.Norm.AppendVector3(nrm)
if ms.Norm.Size() >= ms.Vtx.Size() {
break
}
}
}
func (dec *Decoder) setIndex(ms *xyz.GenMesh, face *Face, idx int, idxs *[]int) {
if len(*idxs) > idx {
ms.Index.Append(uint32((*idxs)[idx]))
} else {
*idxs = append(*idxs, dec.copyVertex(ms, face, idx))
}
}
func (dec *Decoder) copyVertex(ms *xyz.GenMesh, face *Face, idx int) int {
var vector3 math32.Vector3
var vector2 math32.Vector2
vidx := ms.Vtx.Size() / 3
// Copy vertex position and append to geometry
dec.Vertices.GetVector3(3*face.Vertices[idx], &vector3)
ms.Vtx.AppendVector3(vector3)
// Copy vertex normal and append to geometry
if face.Normals[idx] != invINDEX {
i := 3 * face.Normals[idx]
if dec.Normals.Size() > i {
dec.Normals.GetVector3(i, &vector3)
}
ms.Norm.AppendVector3(vector3)
}
// Copy vertex uv and append to geometry
if face.Uvs[idx] != invINDEX {
i := 2 * face.Uvs[idx]
if dec.Uvs.Size() > i {
dec.Uvs.GetVector2(i, &vector2)
}
ms.Tex.AppendVector2(vector2)
}
ms.Index.Append(uint32(vidx))
return vidx
}
// SetMat sets the material for object
func (dec *Decoder) SetMat(sc *xyz.Scene, sld *xyz.Solid, matnm string) {
mat := dec.Materials[matnm]
if mat == nil {
mat = defaultMat
// log warning
msg := fmt.Sprintf("could not find material: %s for object %s. using default material.", matnm, sld.Name())
dec.appendWarn(objType, msg)
}
sld.Mat.Defaults()
sld.Mat.CullBack = false // obj files do not reliably work with this on!
sld.Mat.Color = mat.Diffuse
if mat.Opacity > 0 {
sld.Mat.Color.A = uint8(mat.Opacity * float32(0xFF))
}
// sld.Mat.Specular = mat.Specular
if mat.Shininess != 0 {
sld.Mat.Shiny = mat.Shininess
}
// Loads material textures if specified
dec.loadTex(sc, sld, mat.MapKd, mat)
}
// loadTex loads given texture file
func (dec *Decoder) loadTex(sc *xyz.Scene, sld *xyz.Solid, texfn string, mat *Material) {
if texfn == "" {
return
}
_, tfn := filepath.Split(texfn)
tf, err := sc.TextureByNameTry(tfn)
if err != nil {
tf = xyz.NewTextureFileFS(dec.FSys, sc, tfn, texfn)
}
sld.Mat.SetTexture(tf)
if mat.Tiling.Repeat.X > 0 {
sld.Mat.Tiling.Repeat = mat.Tiling.Repeat
}
sld.Mat.Tiling.Off = mat.Tiling.Off
}
// parse reads the lines from the specified reader and dispatch them
// to the specified line parser.
func (dec *Decoder) parse(reader io.Reader, parseLine func(string) error) error {
bufin := bufio.NewReader(reader)
dec.line = 1
for {
// Reads next line and abort on errors (not EOF)
line, err := bufin.ReadString('\n')
if err != nil && err != io.EOF {
return err
}
// Parses the line
line = strings.Trim(line, blanks)
perr := parseLine(line)
if perr != nil {
return perr
}
// If EOF ends of parsing.
if err == io.EOF {
break
}
dec.line++
}
return nil
}
// Parses obj file line, dispatching to specific parsers
func (dec *Decoder) parseObjLine(line string) error {
// Ignore empty lines
fields := strings.Fields(line)
if len(fields) == 0 {
return nil
}
// Ignore comment lines
ltype := fields[0]
if strings.HasPrefix(ltype, "#") {
return nil
}
switch ltype {
// Material library
case "mtllib":
return dec.parseMatlib(fields[1:])
// Object name
case "o":
return dec.parseObject(fields[1:])
// Group names. We are considering "group" the same as "object"
// This may not be right
case "g":
return dec.parseObject(fields[1:])
// Vertex coordinate
case "v":
return dec.parseVertex(fields[1:])
// Vertex normal coordinate
case "vn":
return dec.parseNormal(fields[1:])
// Vertex texture coordinate
case "vt":
return dec.parseTex(fields[1:])
// Face vertex
case "f":
return dec.parseFace(fields[1:])
// Use material
case "usemtl":
return dec.parseUsemtl(fields[1:])
// Smooth
case "s":
return dec.parseSmooth(fields[1:])
default:
dec.appendWarn(objType, "field not supported: "+ltype)
}
return nil
}
// Parses a mtllib line:
// mtllib <name>
func (dec *Decoder) parseMatlib(fields []string) error {
if len(fields) < 1 {
return errors.New("Material library (mtllib) with no fields")
}
dec.Matlib = fields[0]
return nil
}
// Parses an object line:
// o <name>
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 = name
ob.Faces = make([]Face, 0)
ob.materials = make([]string, 0)
return ob
}
// Parses a vertex position line
// v <x> <y> <z> [w]
func (dec *Decoder) parseVertex(fields []string) error {
if len(fields) < 3 {
return errors.New("Less than 3 vertices in 'v' line")
}
for _, f := range fields[:3] {
val, err := strconv.ParseFloat(f, 32)
if err != nil {
return err
}
dec.Vertices.Append(float32(val))
}
return nil
}
// Parses a vertex normal line
// vn <x> <y> <z>
func (dec *Decoder) parseNormal(fields []string) error {
if len(fields) < 3 {
return errors.New("Less than 3 normals in 'vn' line")
}
for _, f := range fields[:3] {
val, err := strconv.ParseFloat(f, 32)
if err != nil {
return err
}
dec.Normals.Append(float32(val))
}
return nil
}
// Parses a vertex texture coordinate line:
// vt <u> <v> <w>
func (dec *Decoder) parseTex(fields []string) error {
if len(fields) < 2 {
return errors.New("Less than 2 texture coords. in 'vt' line")
}
for _, f := range fields[:2] {
val, err := strconv.ParseFloat(f, 32)
if err != nil {
return err
}
dec.Uvs.Append(float32(val))
}
return nil
}
// parseFace parses a face decription line:
// f v1[/vt1][/vn1] v2[/vt2][/vn2] v3[/vt3][/vn3] ...
func (dec *Decoder) parseFace(fields []string) error {
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)
}
if len(fields) < 3 {
return dec.formatError("Face line with less 3 fields")
}
var face Face
face.Vertices = make([]int, len(fields))
face.Uvs = make([]int, len(fields))
face.Normals = make([]int, len(fields))
if dec.matCurrent != nil {
face.Material = dec.matCurrent.Name
} else {
// TODO (quillaja): do something better than spamming warnings for each line
// dec.appendWarn(objType, "No material defined")
face.Material = "internal default" // causes error on in NewGeom() if ""
// dec.matCurrent = defaultMat
}
face.Smooth = dec.smoothCurrent
for pos, f := range fields {
// Separate the current field in its components: v vt vn
vfields := strings.Split(f, "/")
if len(vfields) < 1 {
return dec.formatError("Face field with no parts")
}
// Get the index of this vertex position (must always exist)
val, err := strconv.ParseInt(vfields[0], 10, 32)
if err != nil {
return err
}
// Positive index is an absolute vertex index
if val > 0 {
face.Vertices[pos] = int(val - 1)
// Negative vertex index is relative to the last parsed vertex
} else if val < 0 {
current := (len(dec.Vertices) / 3) - 1
face.Vertices[pos] = current + int(val) + 1
// Vertex index could never be 0
} else {
return dec.formatError("Face vertex index value equal to 0")
}
// Get the index of this vertex UV coordinate (optional)
if len(vfields) > 1 && len(vfields[1]) > 0 {
val, err := strconv.ParseInt(vfields[1], 10, 32)
if err != nil {
return err
}
// Positive index is an absolute UV index
if val > 0 {
face.Uvs[pos] = int(val - 1)
// Negative vertex index is relative to the last parsed uv
} else if val < 0 {
current := (len(dec.Uvs) / 2) - 1
face.Uvs[pos] = current + int(val) + 1
// UV index could never be 0
} else {
return dec.formatError("Face uv index value equal to 0")
}
} else {
face.Uvs[pos] = invINDEX
}
// Get the index of this vertex normal (optional)
if len(vfields) >= 3 {
val, err = strconv.ParseInt(vfields[2], 10, 32)
if err != nil {
return err
}
// Positive index is an absolute normal index
if val > 0 {
face.Normals[pos] = int(val - 1)
// Negative vertex index is relative to the last parsed normal
} else if val < 0 {
current := (len(dec.Normals) / 3) - 1
face.Normals[pos] = current + int(val) + 1
// Normal index could never be 0
} else {
return dec.formatError("Face normal index value equal to 0")
}
} else {
face.Normals[pos] = invINDEX
}
}
// Appends this face to the current object
dec.objCurrent.Faces = append(dec.objCurrent.Faces, face)
return nil
}
// parseUsemtl parses a "usemtl" decription line:
// usemtl <name>
func (dec *Decoder) parseUsemtl(fields []string) error {
if len(fields) < 1 {
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]
// Creates material descriptor
if mat == nil {
mat = new(Material)
mat.Name = name
dec.Materials[name] = mat
}
dec.objCurrent.materials = append(dec.objCurrent.materials, name)
// Set this as the current material
dec.matCurrent = mat
return nil
}
// parseSmooth parses a "s" decription line:
// s <0|1>
func (dec *Decoder) parseSmooth(fields []string) error {
if len(fields) < 1 {
return dec.formatError("'s' with no fields")
}
if fields[0] == "0" || fields[0] == "off" {
dec.smoothCurrent = false
return nil
}
if fields[0] == "1" || fields[0] == "on" {
dec.smoothCurrent = true
return nil
}
return dec.formatError("'s' with invalid value")
}
/******************************************************************************
mtl parse functions
*/
// Parses material file line, dispatching to specific parsers
func (dec *Decoder) parseMtlLine(line string) error {
// Ignore empty lines
fields := strings.Fields(line)
if len(fields) == 0 {
return nil
}
// Ignore comment lines
ltype := fields[0]
if strings.HasPrefix(ltype, "#") {
return nil
}
switch ltype {
case "newmtl":
return dec.parseNewmtl(fields[1:])
case "d":
return dec.parseDissolve(fields[1:])
case "Ka":
return dec.parseKa(fields[1:])
case "Kd":
return dec.parseKd(fields[1:])
case "Ke":
return dec.parseKe(fields[1:])
case "Ks":
return dec.parseKs(fields[1:])
case "Ni":
return dec.parseNi(fields[1:])
case "Ns":
return dec.parseNs(fields[1:])
case "illum":
return dec.parseIllum(fields[1:])
case "map_Kd":
return dec.parseMapKd(fields[1:])
default:
dec.appendWarn(mtlType, "field not supported: "+ltype)
}
return nil
}
// Parses new material definition
// newmtl <mat_name>
func (dec *Decoder) parseNewmtl(fields []string) error {
if len(fields) < 1 {
return dec.formatError("newmtl with no fields")
}
// Checks if material has already been seen
name := fields[0]
mat := dec.Materials[name]
// Creates material descriptor
if mat == nil {
mat = new(Material)
mat.Name = name
dec.Materials[name] = mat
}
dec.matCurrent = mat
return nil
}
// Parses the dissolve factor (opacity)
// d <factor>
func (dec *Decoder) parseDissolve(fields []string) error {
if len(fields) < 1 {
return dec.formatError("'d' with no fields")
}
val, err := strconv.ParseFloat(fields[0], 32)
if err != nil {
return dec.formatError("'d' parse float error")
}
dec.matCurrent.Opacity = float32(val)
return nil
}
// Parses ambient reflectivity:
// Ka r g b
func (dec *Decoder) parseKa(fields []string) error {
if len(fields) < 3 {
return dec.formatError("'Ka' with less than 3 fields")
}
var clrs [3]float32
for pos, f := range fields[:3] {
val, err := strconv.ParseFloat(f, 32)
if err != nil {
return err
}
clrs[pos] = float32(val)
}
dec.matCurrent.Ambient = colors.FromRGBAF32(clrs[0], clrs[1], clrs[2], 1)
return nil
}
// Parses diffuse reflectivity:
// Kd r g b
func (dec *Decoder) parseKd(fields []string) error {
if len(fields) < 3 {
return dec.formatError("'Kd' with less than 3 fields")
}
var clrs [3]float32
for pos, f := range fields[:3] {
val, err := strconv.ParseFloat(f, 32)
if err != nil {
return err
}
clrs[pos] = float32(val)
}
dec.matCurrent.Diffuse = colors.FromRGBAF32(clrs[0], clrs[1], clrs[2], 1)
return nil
}
// Parses emissive color:
// Ke r g b
func (dec *Decoder) parseKe(fields []string) error {
if len(fields) < 3 {
return dec.formatError("'Ke' with less than 3 fields")
}
var clrs [3]float32
for pos, f := range fields[:3] {
val, err := strconv.ParseFloat(f, 32)
if err != nil {
return err
}
clrs[pos] = float32(val)
}
dec.matCurrent.Emissive = colors.FromRGBAF32(clrs[0], clrs[1], clrs[2], 1)
return nil
}
// Parses specular reflectivity:
// Ks r g b
func (dec *Decoder) parseKs(fields []string) error {
if len(fields) < 3 {
return dec.formatError("'Ks' with less than 3 fields")
}
var clrs [3]float32
for pos, f := range fields[:3] {
val, err := strconv.ParseFloat(f, 32)
if err != nil {
return err
}
clrs[pos] = float32(val)
}
dec.matCurrent.Specular = colors.FromRGBAF32(clrs[0], clrs[1], clrs[2], 1)
return nil
}
// Parses optical density, also known as index of refraction
// Ni <optical_density>
func (dec *Decoder) parseNi(fields []string) error {
if len(fields) < 1 {
return dec.formatError("'Ni' with no fields")
}
val, err := strconv.ParseFloat(fields[0], 32)
if err != nil {
return dec.formatError("'d' parse float error")
}
dec.matCurrent.Refraction = float32(val)
return nil
}
// Parses specular exponent
// Ns <specular_exponent>
func (dec *Decoder) parseNs(fields []string) error {
if len(fields) < 1 {
return dec.formatError("'Ns' with no fields")
}
val, err := strconv.ParseFloat(fields[0], 32)
if err != nil {
return dec.formatError("'d' parse float error")
}
dec.matCurrent.Shininess = float32(val)
return nil
}
// Parses illumination model (0 to 10)
// illum <ilum_#>
func (dec *Decoder) parseIllum(fields []string) error {
if len(fields) < 1 {
return dec.formatError("'illum' with no fields")
}
val, err := strconv.ParseUint(fields[0], 10, 32)
if err != nil {
return dec.formatError("'d' parse int error")
}
dec.matCurrent.Illum = int(val)
return nil
}
// Parses color texture linked to the diffuse reflectivity of the material
// map_Kd [-options] <filename>
func (dec *Decoder) parseMapKd(fields []string) error {
if len(fields) < 1 {
return dec.formatError("No fields")
}
nf := len(fields)
for i := 0; i < nf; i++ {
f := fields[i]
if f[0] == '-' {
switch f {
case "-s":
r1, _ := strconv.ParseFloat(fields[i+1], 32)
r2 := r1
i++
if len(fields) > i+2 {
rt, err := strconv.ParseFloat(fields[i+2], 32)
if err == nil {
r2 = rt
i++
}
}
dec.matCurrent.Tiling.Repeat.Set(float32(r1), float32(r2))
case "-o":
r1, _ := strconv.ParseFloat(fields[i+1], 32)
r2 := r1
i++
if len(fields) > i+2 {
rt, err := strconv.ParseFloat(fields[i+2], 32)
if err == nil {
r2 = rt
i++
}
}
dec.matCurrent.Tiling.Off.Set(float32(r1), float32(r2))
}
} else {
dec.matCurrent.MapKd = f
}
}
return nil
}
func (dec *Decoder) formatError(msg string) error {
return fmt.Errorf("%s in line:%d", msg, dec.line)
}
func (dec *Decoder) appendWarn(ftype string, msg string) {
wline := fmt.Sprintf("%s(%d): %s", ftype, dec.line, msg)
dec.Warnings = append(dec.Warnings, wline)
}