@@ -102,16 +102,6 @@ func Decode(objpath string, mtlpath string) (*Decoder, error) {
102102 }
103103 defer fobj .Close ()
104104
105- // TODO: remove
106- // If path of material file not supplied,
107- // try to use the base name of the obj file
108- // if len(mtlpath) == 0 {
109- // dir, objfile := filepath.Split(objpath)
110- // ext := filepath.Ext(objfile)
111- // mtlpath = dir + objfile[:len(objfile)-len(ext)] + ".mtl"
112- // }
113-
114- fmt .Println ("USING TEST VERSION" ) // TODO: remove
115105 // Opens mtl file
116106 // if mtlpath=="", then os.Open() will produce an error,
117107 // causing fmtl to be nil
@@ -135,8 +125,9 @@ func Decode(objpath string, mtlpath string) (*Decoder, error) {
135125//
136126// Pass a valid io.Reader to override the materials defined in the OBJ file,
137127// or `nil` to use the materials listed in the OBJ's "mtllib" line (if present),
138- // or a default material as a last resort. No error will be returned for
139- // problems with materials--a gray default material will be used.
128+ // a ".mtl" file with the same name as the OBJ file if presemt, or a default
129+ // material as a last resort. No error will be returned for problems
130+ // with materials--a gray default material will be used if nothing else works.
140131func DecodeReader (objreader , mtlreader io.Reader ) (* Decoder , error ) {
141132
142133 dec := new (Decoder )
@@ -157,44 +148,66 @@ func DecodeReader(objreader, mtlreader io.Reader) (*Decoder, error) {
157148 // Parses mtl lines
158149 // 1) try passed in mtlreader,
159150 // 2) try file in mtllib line
160- // 3) use default material as last resort
151+ // 3) try <obj_filename>.mtl
152+ // 4) use default material as last resort
161153 dec .matCurrent = nil
162154 dec .line = 1
163155 // first try: use the material file passed in as an io.Reader
164156 err = dec .parse (mtlreader , dec .parseMtlLine )
165157 if err != nil {
166- // if mtlreader produces an error (eg. it's nil), try the file listed
158+
159+ // 2) if mtlreader produces an error (eg. it's nil), try the file listed
167160 // in the OBJ's matlib line, if it exists.
168161 if dec .Matlib != "" {
169162 // ... first need to get the path of the OBJ, since mtllib is relative
170163 var mtllibPath string
171164 if objf , ok := objreader .(* os.File ); ok {
172165 // NOTE (quillaja): this is a hack because we need the directory of
173166 // the OBJ, but can't get it any other way (dec.mtlDir isn't set
174- // until AFTER this function is finished)
167+ // until AFTER this function is finished).
175168 objdir := filepath .Dir (objf .Name ())
176169 mtllibPath = filepath .Join (objdir , dec .Matlib )
170+ dec .mtlDir = objdir // NOTE (quillaja): should this be set?
177171 }
178- fmt .Println ("mtllib:" , mtllibPath )
179172 mtlf , errMTL := os .Open (mtllibPath )
180173 defer mtlf .Close ()
181174 if errMTL == nil {
182- fmt .Println ("attempting to parse" , mtllibPath )
183175 err = dec .parse (mtlf , dec .parseMtlLine ) // will set err to nil if successful
184- fmt .Println ("error while parsing mtllib:" , err )
185176 }
186177 }
187178
188- // handle error(s) instead of simply passing it up the call stack.
179+ // 3) if the mtllib line fails try <obj_filename>.mtl in the same directory.
180+ // process is basically identical to the above code block.
181+ if err != nil {
182+ var mtlpath string
183+ if objf , ok := objreader .(* os.File ); ok {
184+ objdir := strings .TrimSuffix (objf .Name (), ".obj" )
185+ mtlpath = objdir + ".mtl"
186+ dec .mtlDir = objdir // NOTE (quillaja): should this be set?
187+ }
188+ mtlf , errMTL := os .Open (mtlpath )
189+ defer mtlf .Close ()
190+ if errMTL == nil {
191+ err = dec .parse (mtlf , dec .parseMtlLine ) // will set err to nil if successful
192+ if err == nil {
193+ // log a warning
194+ msg := fmt .Sprintf ("using material file %s" , mtlpath )
195+ dec .appendWarn (mtlType , msg )
196+ }
197+ }
198+ }
199+
200+ // 4) handle error(s) instead of simply passing it up the call stack.
189201 // range over the materials named in the OBJ file and substitute a default
190202 // But log that an error occured.
191203 if err != nil {
192204 for key := range dec .Materials {
193205 dec .Materials [key ] = defaultMat
194206 }
195- fmt .Println ("logged warning for last ditch effort" )
196- // NOTE (quillaja): could be an error of some custom type.
197- dec .appendWarn (mtlType , "unable to parse a mtl file for obj. using default material instead." )
207+ // NOTE (quillaja): could be an error of some custom type. But people
208+ // tend to ignore errors and pass them up the call stack instead
209+ // of handling them... so all this work would probably be wasted.
210+ dec .appendWarn (mtlType , "unable to parse a material file for obj. using default material instead." )
198211 }
199212 }
200213
0 commit comments