DecodeReader decodes the specified obj and mtl readers returning a decoder object and an error if a problem was encoutered while parsing the OBJ. Pass a valid io.Reader to override the materials defined in the OBJ file, or `nil` to use the materials listed in the OBJ's "mtllib" line (if present), a
(objreader, mtlreader io.Reader)
| 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. |
| 131 | func DecodeReader(objreader, mtlreader io.Reader) (*Decoder, error) { |
| 132 | |
| 133 | dec := new(Decoder) |
| 134 | dec.Objects = make([]Object, 0) |
| 135 | dec.Warnings = make([]string, 0) |
| 136 | dec.Materials = make(map[string]*Material) |
| 137 | dec.Vertices = math32.NewArrayF32(0, 0) |
| 138 | dec.Normals = math32.NewArrayF32(0, 0) |
| 139 | dec.Uvs = math32.NewArrayF32(0, 0) |
| 140 | dec.line = 1 |
| 141 | |
| 142 | // Parses obj lines |
| 143 | err := dec.parse(objreader, dec.parseObjLine) |
| 144 | if err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | |
| 148 | // Parses mtl lines |
| 149 | // 1) try passed in mtlreader, |
| 150 | // 2) try file in mtllib line |
| 151 | // 3) try <obj_filename>.mtl |
| 152 | // 4) use default material as last resort |
| 153 | dec.matCurrent = nil |
| 154 | dec.line = 1 |
| 155 | // first try: use the material file passed in as an io.Reader |
| 156 | err = dec.parse(mtlreader, dec.parseMtlLine) |
| 157 | if err != nil { |
| 158 | |
| 159 | // 2) if mtlreader produces an error (eg. it's nil), try the file listed |
| 160 | // in the OBJ's matlib line, if it exists. |
| 161 | if dec.Matlib != "" { |
| 162 | // ... first need to get the path of the OBJ, since mtllib is relative |
| 163 | var mtllibPath string |
| 164 | if objf, ok := objreader.(*os.File); ok { |
| 165 | // NOTE (quillaja): this is a hack because we need the directory of |
| 166 | // the OBJ, but can't get it any other way (dec.mtlDir isn't set |
| 167 | // until AFTER this function is finished). |
| 168 | objdir := filepath.Dir(objf.Name()) |
| 169 | mtllibPath = filepath.Join(objdir, dec.Matlib) |
| 170 | dec.mtlDir = objdir // NOTE (quillaja): should this be set? |
| 171 | } |
| 172 | mtlf, errMTL := os.Open(mtllibPath) |
| 173 | defer mtlf.Close() |
| 174 | if errMTL == nil { |
| 175 | err = dec.parse(mtlf, dec.parseMtlLine) // will set err to nil if successful |
| 176 | } |
| 177 | } |
| 178 | |
| 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) |
no test coverage detected