obj.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package obj
  5. import (
  6. "bufio"
  7. "errors"
  8. "fmt"
  9. "github.com/g3n/engine/core"
  10. "github.com/g3n/engine/geometry"
  11. "github.com/g3n/engine/gls"
  12. "github.com/g3n/engine/graphic"
  13. "github.com/g3n/engine/material"
  14. "github.com/g3n/engine/math32"
  15. "io"
  16. "math"
  17. "os"
  18. "path/filepath"
  19. "strconv"
  20. "strings"
  21. )
  22. // Decoder contains all decoded data from the obj and mtl files
  23. type Decoder struct {
  24. Objects []Object // decoded objects
  25. Matlib string // name of the material lib
  26. Materials map[string]*Material // maps material name to object
  27. Vertices math32.ArrayF32 // vertices positions array
  28. Normals math32.ArrayF32 // vertices normals
  29. Uvs math32.ArrayF32 // vertices texture coordinates
  30. Warnings []string // warning messages
  31. line uint // current line number
  32. objCurrent *Object // current object
  33. matCurrent *Material // current material
  34. smoothCurrent bool // current smooth state
  35. }
  36. // Object contains all information about one decoded object
  37. type Object struct {
  38. Name string // Object name
  39. Faces []Face // Faces
  40. materials []string // Materials used in this object
  41. }
  42. // Face contains all information about an object face
  43. type Face struct {
  44. Vertices []int // Indices to the face vertices
  45. Uvs []int // Indices to the face UV coordinates
  46. Normals []int // Indices to the face normals
  47. Material string // Material name
  48. Smooth bool // Smooth face
  49. }
  50. // Material contains all information about an object material
  51. type Material struct {
  52. Name string // Material name
  53. Illum int // Illumination model
  54. Opacity float32 // Opacity factor
  55. Refraction float32 // Refraction factor
  56. Shininess float32 // Shininess (specular exponent)
  57. Ambient math32.Color // Ambient color reflectivity
  58. Diffuse math32.Color // Diffuse color reflectivity
  59. Specular math32.Color // Specular color reflectivity
  60. Emissive math32.Color // Emissive color
  61. MapKd string // Texture file linked to diffuse color
  62. }
  63. // Local constants
  64. const (
  65. blanks = "\r\n\t "
  66. invINDEX = math.MaxUint32
  67. objType = "obj"
  68. mtlType = "mtl"
  69. )
  70. // Decode decodes the specified obj and mtl files returning a decoder
  71. // object and an error.
  72. func Decode(objpath string, mtlpath string) (*Decoder, error) {
  73. // Opens obj file
  74. fobj, err := os.Open(objpath)
  75. if err != nil {
  76. return nil, err
  77. }
  78. defer fobj.Close()
  79. // If path of material file not supplied,
  80. // try to use the base name of the obj file
  81. if len(mtlpath) == 0 {
  82. dir, objfile := filepath.Split(objpath)
  83. ext := filepath.Ext(objfile)
  84. mtlpath = dir + objfile[:len(objfile)-len(ext)] + ".mtl"
  85. }
  86. // Opens mtl file
  87. fmtl, err := os.Open(mtlpath)
  88. if err != nil {
  89. return nil, err
  90. }
  91. defer fmtl.Close()
  92. return DecodeReader(fobj, fmtl)
  93. }
  94. // DecodeReader decodes the specified obj and mtl readers returning a decoder
  95. // object and an error.
  96. func DecodeReader(objreader, mtlreader io.Reader) (*Decoder, error) {
  97. dec := new(Decoder)
  98. dec.Objects = make([]Object, 0)
  99. dec.Warnings = make([]string, 0)
  100. dec.Materials = make(map[string]*Material)
  101. dec.Vertices = math32.NewArrayF32(0, 0)
  102. dec.Normals = math32.NewArrayF32(0, 0)
  103. dec.Uvs = math32.NewArrayF32(0, 0)
  104. dec.line = 1
  105. // Parses obj lines
  106. err := dec.parse(objreader, dec.parseObjLine)
  107. if err != nil {
  108. return nil, err
  109. }
  110. // Parses mtl lines
  111. dec.matCurrent = nil
  112. dec.line = 1
  113. err = dec.parse(mtlreader, dec.parseMtlLine)
  114. if err != nil {
  115. return nil, err
  116. }
  117. return dec, nil
  118. }
  119. // NewGroup creates and returns a group containing as children meshes
  120. // with all the decoded objects.
  121. // A group is returned even if there is only one object decoded.
  122. func (dec *Decoder) NewGroup() (*core.Node, error) {
  123. group := core.NewNode()
  124. for i := 0; i < len(dec.Objects); i++ {
  125. mesh, err := dec.NewMesh(&dec.Objects[i])
  126. if err != nil {
  127. return nil, err
  128. }
  129. group.Add(mesh)
  130. }
  131. return group, nil
  132. }
  133. // NewMesh creates and returns a mesh from an specified decoded object.
  134. func (dec *Decoder) NewMesh(obj *Object) (*graphic.Mesh, error) {
  135. // Creates object geometry
  136. geom, err := dec.NewGeometry(obj)
  137. if err != nil {
  138. return nil, err
  139. }
  140. // Single material
  141. if geom.GroupCount() == 1 {
  142. matName := obj.materials[0]
  143. matDesc := dec.Materials[matName]
  144. // Creates material
  145. mat := material.NewPhong(&matDesc.Diffuse)
  146. ambientColor := mat.AmbientColor()
  147. mat.SetAmbientColor(ambientColor.Multiply(&matDesc.Ambient))
  148. mat.SetSpecularColor(&matDesc.Specular)
  149. mat.SetShininess(matDesc.Shininess)
  150. return graphic.NewMesh(geom, mat), nil
  151. }
  152. // Multi material
  153. mesh := graphic.NewMesh(geom, nil)
  154. for idx := 0; idx < geom.GroupCount(); idx++ {
  155. group := geom.GroupAt(idx)
  156. matName := obj.materials[group.Matindex]
  157. matDesc := dec.Materials[matName]
  158. // Creates material
  159. matGroup := material.NewPhong(&matDesc.Diffuse)
  160. ambientColor := matGroup.AmbientColor()
  161. matGroup.SetAmbientColor(ambientColor.Multiply(&matDesc.Ambient))
  162. matGroup.SetSpecularColor(&matDesc.Specular)
  163. matGroup.SetShininess(matDesc.Shininess)
  164. mesh.AddGroupMaterial(matGroup, idx)
  165. }
  166. return mesh, nil
  167. }
  168. // NewGeometry generates and returns a geometry from the specified object
  169. func (dec *Decoder) NewGeometry(obj *Object) (*geometry.Geometry, error) {
  170. geom := geometry.NewGeometry()
  171. // Create buffers
  172. positions := math32.NewArrayF32(0, 0)
  173. normals := math32.NewArrayF32(0, 0)
  174. uvs := math32.NewArrayF32(0, 0)
  175. indices := math32.NewArrayU32(0, 0)
  176. // copy all vertex info from the decoded Object, face and index to the geometry
  177. copyVertex := func(face *Face, idx int) {
  178. var vec3 math32.Vector3
  179. var vec2 math32.Vector2
  180. pos := positions.Size() / 3
  181. // Copy vertex position and append to geometry
  182. dec.Vertices.GetVector3(3*face.Vertices[idx], &vec3)
  183. positions.AppendVector3(&vec3)
  184. // Copy vertex normal and append to geometry
  185. if face.Normals[idx] != invINDEX {
  186. dec.Normals.GetVector3(3*face.Normals[idx], &vec3)
  187. normals.AppendVector3(&vec3)
  188. }
  189. // Copy vertex uv and append to geometry
  190. if face.Uvs[idx] != invINDEX {
  191. dec.Uvs.GetVector2(2*face.Uvs[idx], &vec2)
  192. uvs.AppendVector2(&vec2)
  193. }
  194. indices.Append(uint32(pos))
  195. }
  196. var group *geometry.Group
  197. matName := ""
  198. matIndex := 0
  199. for _, face := range obj.Faces {
  200. // If face material changed, starts a new group
  201. if face.Material != matName {
  202. group = geom.AddGroup(indices.Size(), 0, matIndex)
  203. matName = face.Material
  204. matIndex++
  205. }
  206. // Copy face vertices to geometry
  207. for idx := 1; idx < len(face.Vertices)-1; idx++ {
  208. copyVertex(&face, 0)
  209. copyVertex(&face, idx)
  210. copyVertex(&face, idx+1)
  211. group.Count += 3
  212. }
  213. }
  214. geom.SetIndices(indices)
  215. geom.AddVBO(gls.NewVBO().AddAttrib("VertexPosition", 3).SetBuffer(positions))
  216. geom.AddVBO(gls.NewVBO().AddAttrib("VertexNormal", 3).SetBuffer(normals))
  217. geom.AddVBO(gls.NewVBO().AddAttrib("VertexTexcoord", 2).SetBuffer(uvs))
  218. return geom, nil
  219. }
  220. // parse reads the lines from the specified reader and dispatch them
  221. // to the specified line parser.
  222. func (dec *Decoder) parse(reader io.Reader, parseLine func(string) error) error {
  223. bufin := bufio.NewReader(reader)
  224. dec.line = 1
  225. for {
  226. // Reads next line and abort on errors (not EOF)
  227. line, err := bufin.ReadString('\n')
  228. if err != nil && err != io.EOF {
  229. return err
  230. }
  231. // Parses the line
  232. line = strings.Trim(line, blanks)
  233. perr := parseLine(line)
  234. if perr != nil {
  235. return perr
  236. }
  237. // If EOF ends of parsing.
  238. if err == io.EOF {
  239. break
  240. }
  241. dec.line++
  242. }
  243. return nil
  244. }
  245. // Parses obj file line, dispatching to specific parsers
  246. func (dec *Decoder) parseObjLine(line string) error {
  247. // Ignore empty lines
  248. fields := strings.Split(line, " ")
  249. if len(fields) == 0 {
  250. return nil
  251. }
  252. // Ignore comment lines
  253. ltype := fields[0]
  254. if strings.HasPrefix(ltype, "#") {
  255. return nil
  256. }
  257. switch ltype {
  258. // Material library
  259. case "mtllib":
  260. return dec.parseMatlib(fields[1:])
  261. // Object name
  262. case "o":
  263. return dec.parseObject(fields[1:])
  264. // Group names. We are considering "group" the same as "object"
  265. // This may not be right
  266. case "g":
  267. return dec.parseObject(fields[1:])
  268. // Vertex coordinate
  269. case "v":
  270. return dec.parseVertex(fields[1:])
  271. // Vertex normal coordinate
  272. case "vn":
  273. return dec.parseNormal(fields[1:])
  274. // Vertex texture coordinate
  275. case "vt":
  276. return dec.parseTex(fields[1:])
  277. // Face vertex
  278. case "f":
  279. return dec.parseFace(fields[1:])
  280. // Use material
  281. case "usemtl":
  282. return dec.parseUsemtl(fields[1:])
  283. // Smooth
  284. case "s":
  285. return dec.parseSmooth(fields[1:])
  286. default:
  287. dec.appendWarn(objType, "field not supported: "+ltype)
  288. }
  289. return nil
  290. }
  291. // Parses a mtllib line:
  292. // mtllib <name>
  293. func (dec *Decoder) parseMatlib(fields []string) error {
  294. if len(fields) < 1 {
  295. return errors.New("Object line (o) with less than 2 fields")
  296. }
  297. dec.Matlib = fields[0]
  298. return nil
  299. }
  300. // Parses an object line:
  301. // o <name>
  302. func (dec *Decoder) parseObject(fields []string) error {
  303. if len(fields) < 1 {
  304. return errors.New("Object line (o) with less than 2 fields")
  305. }
  306. var ob Object
  307. ob.Name = fields[0]
  308. ob.Faces = make([]Face, 0)
  309. ob.materials = make([]string, 0)
  310. dec.Objects = append(dec.Objects, ob)
  311. dec.objCurrent = &dec.Objects[len(dec.Objects)-1]
  312. return nil
  313. }
  314. // Parses a vertex position line
  315. // v <x> <y> <z> [w]
  316. func (dec *Decoder) parseVertex(fields []string) error {
  317. if len(fields) < 3 {
  318. return errors.New("Less than 3 vertices in 'v' line")
  319. }
  320. for _, f := range fields[:3] {
  321. val, err := strconv.ParseFloat(f, 32)
  322. if err != nil {
  323. return err
  324. }
  325. dec.Vertices.Append(float32(val))
  326. }
  327. return nil
  328. }
  329. // Parses a vertex normal line
  330. // vn <x> <y> <z>
  331. func (dec *Decoder) parseNormal(fields []string) error {
  332. if len(fields) < 3 {
  333. return errors.New("Less than 3 normals in 'vn' line")
  334. }
  335. for _, f := range fields[:3] {
  336. val, err := strconv.ParseFloat(f, 32)
  337. if err != nil {
  338. return err
  339. }
  340. dec.Normals.Append(float32(val))
  341. }
  342. return nil
  343. }
  344. // Parses a vertex texture coordinate line:
  345. // vt <u> <v> <w>
  346. func (dec *Decoder) parseTex(fields []string) error {
  347. if len(fields) < 2 {
  348. return errors.New("Less than 2 texture coords. in 'vt' line")
  349. }
  350. for _, f := range fields[:2] {
  351. val, err := strconv.ParseFloat(f, 32)
  352. if err != nil {
  353. return err
  354. }
  355. dec.Uvs.Append(float32(val))
  356. }
  357. return nil
  358. }
  359. // parseFace parses a face decription line:
  360. // f v1[/vt1][/vn1] v2[/vt2][/vn2] v3[/vt3][/vn3] ...
  361. func (dec *Decoder) parseFace(fields []string) error {
  362. // If current object has no material, appends last material if defined
  363. if len(dec.objCurrent.materials) == 0 && dec.matCurrent != nil {
  364. dec.objCurrent.materials = append(dec.objCurrent.materials, dec.matCurrent.Name)
  365. }
  366. if len(fields) < 3 {
  367. return dec.formatError("Face line with less 3 fields")
  368. }
  369. if dec.matCurrent == nil {
  370. return dec.formatError("No material defined")
  371. }
  372. var face Face
  373. face.Vertices = make([]int, len(fields))
  374. face.Uvs = make([]int, len(fields))
  375. face.Normals = make([]int, len(fields))
  376. face.Material = dec.matCurrent.Name
  377. face.Smooth = dec.smoothCurrent
  378. for pos, f := range fields {
  379. // Separate the current field in its components: v vt vn
  380. vfields := strings.Split(f, "/")
  381. if len(vfields) < 1 {
  382. return dec.formatError("Face field with no parts")
  383. }
  384. // Get the index of this vertex position (must always exist)
  385. val, err := strconv.ParseInt(vfields[0], 10, 32)
  386. if err != nil {
  387. return err
  388. }
  389. // Positive index is an absolute vertex index
  390. if val > 0 {
  391. face.Vertices[pos] = int(val - 1)
  392. // Negative vertex index is relative to the last parsed vertex
  393. } else if val < 0 {
  394. current := (len(dec.Vertices) / 3) - 1
  395. face.Vertices[pos] = current + int(val) + 1
  396. // Vertex index could never be 0
  397. } else {
  398. return dec.formatError("Face vertex index value equal to 0")
  399. }
  400. // Get the index of this vertex UV coordinate (optional)
  401. if len(vfields) > 1 && len(vfields[1]) > 0 {
  402. val, err := strconv.ParseInt(vfields[1], 10, 32)
  403. if err != nil {
  404. return err
  405. }
  406. // Positive index is an absolute UV index
  407. if val > 0 {
  408. face.Uvs[pos] = int(val - 1)
  409. // Negative vertex index is relative to the last parsed uv
  410. } else if val < 0 {
  411. current := (len(dec.Uvs) / 2) - 1
  412. face.Uvs[pos] = current + int(val) + 1
  413. // UV index could never be 0
  414. } else {
  415. return dec.formatError("Face uv index value equal to 0")
  416. }
  417. } else {
  418. face.Uvs[pos] = invINDEX
  419. }
  420. // Get the index of this vertex normal (optional)
  421. if len(vfields) >= 3 {
  422. val, err = strconv.ParseInt(vfields[2], 10, 32)
  423. if err != nil {
  424. return err
  425. }
  426. // Positive index is an absolute normal index
  427. if val > 0 {
  428. face.Normals[pos] = int(val - 1)
  429. // Negative vertex index is relative to the last parsed normal
  430. } else if val < 0 {
  431. current := (len(dec.Normals) / 3) - 1
  432. face.Normals[pos] = current + int(val) + 1
  433. // Normal index could never be 0
  434. } else {
  435. return dec.formatError("Face normal index value equal to 0")
  436. }
  437. } else {
  438. face.Normals[pos] = invINDEX
  439. }
  440. }
  441. // Appends this face to the current object
  442. dec.objCurrent.Faces = append(dec.objCurrent.Faces, face)
  443. return nil
  444. }
  445. // parseUsemtl parses a "usemtl" decription line:
  446. // usemtl <name>
  447. func (dec *Decoder) parseUsemtl(fields []string) error {
  448. if len(fields) < 1 {
  449. return dec.formatError("Usemtl with no fields")
  450. }
  451. // Checks if this material has already been parsed
  452. name := fields[0]
  453. mat := dec.Materials[name]
  454. // Creates material descriptor
  455. if mat == nil {
  456. mat = new(Material)
  457. mat.Name = name
  458. dec.Materials[name] = mat
  459. }
  460. dec.objCurrent.materials = append(dec.objCurrent.materials, name)
  461. // Set this as the current material
  462. dec.matCurrent = mat
  463. return nil
  464. }
  465. // parseSmooth parses a "s" decription line:
  466. // s <0|1>
  467. func (dec *Decoder) parseSmooth(fields []string) error {
  468. if len(fields) < 1 {
  469. return dec.formatError("'s' with no fields")
  470. }
  471. if fields[0] == "0" || fields[0] == "off" {
  472. dec.smoothCurrent = false
  473. return nil
  474. }
  475. if fields[0] == "1" || fields[0] == "on" {
  476. dec.smoothCurrent = true
  477. return nil
  478. }
  479. return dec.formatError("'s' with invalid value")
  480. }
  481. /******************************************************************************
  482. mtl parse functions
  483. */
  484. // Parses material file line, dispatching to specific parsers
  485. func (dec *Decoder) parseMtlLine(line string) error {
  486. // Ignore empty lines
  487. fields := strings.Split(line, " ")
  488. if len(fields) == 0 {
  489. return nil
  490. }
  491. // Ignore comment lines
  492. ltype := fields[0]
  493. if strings.HasPrefix(ltype, "#") {
  494. return nil
  495. }
  496. switch ltype {
  497. case "newmtl":
  498. return dec.parseNewmtl(fields[1:])
  499. case "d":
  500. return dec.parseDissolve(fields[1:])
  501. case "Ka":
  502. return dec.parseKa(fields[1:])
  503. case "Kd":
  504. return dec.parseKd(fields[1:])
  505. case "Ke":
  506. return dec.parseKe(fields[1:])
  507. case "Ks":
  508. return dec.parseKs(fields[1:])
  509. case "Ni":
  510. return dec.parseNi(fields[1:])
  511. case "Ns":
  512. return dec.parseNs(fields[1:])
  513. case "illum":
  514. return dec.parseIllum(fields[1:])
  515. case "map_Kd":
  516. return dec.parseMapKd(fields[1:])
  517. default:
  518. dec.appendWarn(mtlType, "field not supported: "+ltype)
  519. }
  520. return nil
  521. }
  522. // Parses new material definition
  523. // newmtl <mat_name>
  524. func (dec *Decoder) parseNewmtl(fields []string) error {
  525. if len(fields) < 1 {
  526. return dec.formatError("newmtl with no fields")
  527. }
  528. // Checks if material has already been seen
  529. name := fields[0]
  530. mat := dec.Materials[name]
  531. // Creates material descriptor
  532. if mat == nil {
  533. mat = new(Material)
  534. mat.Name = name
  535. dec.Materials[name] = mat
  536. }
  537. dec.matCurrent = mat
  538. return nil
  539. }
  540. // Parses the dissolve factor (opacity)
  541. // d <factor>
  542. func (dec *Decoder) parseDissolve(fields []string) error {
  543. if len(fields) < 1 {
  544. return dec.formatError("'d' with no fields")
  545. }
  546. val, err := strconv.ParseFloat(fields[0], 32)
  547. if err != nil {
  548. return dec.formatError("'d' parse float error")
  549. }
  550. dec.matCurrent.Opacity = float32(val)
  551. return nil
  552. }
  553. // Parses ambient reflectivity:
  554. // Ka r g b
  555. func (dec *Decoder) parseKa(fields []string) error {
  556. if len(fields) < 3 {
  557. return dec.formatError("'Ka' with less than 3 fields")
  558. }
  559. var colors [3]float32
  560. for pos, f := range fields[:3] {
  561. val, err := strconv.ParseFloat(f, 32)
  562. if err != nil {
  563. return err
  564. }
  565. colors[pos] = float32(val)
  566. }
  567. dec.matCurrent.Ambient.Set(colors[0], colors[1], colors[2])
  568. return nil
  569. }
  570. // Parses diffuse reflectivity:
  571. // Kd r g b
  572. func (dec *Decoder) parseKd(fields []string) error {
  573. if len(fields) < 3 {
  574. return dec.formatError("'Kd' with less than 3 fields")
  575. }
  576. var colors [3]float32
  577. for pos, f := range fields[:3] {
  578. val, err := strconv.ParseFloat(f, 32)
  579. if err != nil {
  580. return err
  581. }
  582. colors[pos] = float32(val)
  583. }
  584. dec.matCurrent.Diffuse.Set(colors[0], colors[1], colors[2])
  585. return nil
  586. }
  587. // Parses emissive color:
  588. // Ke r g b
  589. func (dec *Decoder) parseKe(fields []string) error {
  590. if len(fields) < 3 {
  591. return dec.formatError("'Ke' with less than 3 fields")
  592. }
  593. var colors [3]float32
  594. for pos, f := range fields[:3] {
  595. val, err := strconv.ParseFloat(f, 32)
  596. if err != nil {
  597. return err
  598. }
  599. colors[pos] = float32(val)
  600. }
  601. dec.matCurrent.Emissive.Set(colors[0], colors[1], colors[2])
  602. return nil
  603. }
  604. // Parses specular reflectivity:
  605. // Ks r g b
  606. func (dec *Decoder) parseKs(fields []string) error {
  607. if len(fields) < 3 {
  608. return dec.formatError("'Ks' with less than 3 fields")
  609. }
  610. var colors [3]float32
  611. for pos, f := range fields[:3] {
  612. val, err := strconv.ParseFloat(f, 32)
  613. if err != nil {
  614. return err
  615. }
  616. colors[pos] = float32(val)
  617. }
  618. dec.matCurrent.Specular.Set(colors[0], colors[1], colors[2])
  619. return nil
  620. }
  621. // Parses optical density, also known as index of refraction
  622. // Ni <optical_density>
  623. func (dec *Decoder) parseNi(fields []string) error {
  624. if len(fields) < 1 {
  625. return dec.formatError("'Ni' with no fields")
  626. }
  627. val, err := strconv.ParseFloat(fields[0], 32)
  628. if err != nil {
  629. return dec.formatError("'d' parse float error")
  630. }
  631. dec.matCurrent.Refraction = float32(val)
  632. return nil
  633. }
  634. // Parses specular exponent
  635. // Ns <specular_exponent>
  636. func (dec *Decoder) parseNs(fields []string) error {
  637. if len(fields) < 1 {
  638. return dec.formatError("'Ns' with no fields")
  639. }
  640. val, err := strconv.ParseFloat(fields[0], 32)
  641. if err != nil {
  642. return dec.formatError("'d' parse float error")
  643. }
  644. dec.matCurrent.Shininess = float32(val)
  645. return nil
  646. }
  647. // Parses illumination model (0 to 10)
  648. // illum <ilum_#>
  649. func (dec *Decoder) parseIllum(fields []string) error {
  650. if len(fields) < 1 {
  651. return dec.formatError("'illum' with no fields")
  652. }
  653. val, err := strconv.ParseUint(fields[0], 10, 32)
  654. if err != nil {
  655. return dec.formatError("'d' parse int error")
  656. }
  657. dec.matCurrent.Illum = int(val)
  658. return nil
  659. }
  660. // Parses color texture linked to the diffuse reflectivity of the material
  661. // map_Kd [-options] <filename>
  662. func (dec *Decoder) parseMapKd(fields []string) error {
  663. if len(fields) < 1 {
  664. return dec.formatError("No fields")
  665. }
  666. dec.matCurrent.MapKd = fields[0]
  667. return nil
  668. }
  669. func (dec *Decoder) formatError(msg string) error {
  670. return fmt.Errorf("%s in line:%d", msg, dec.line)
  671. }
  672. func (dec *Decoder) appendWarn(ftype string, msg string) {
  673. wline := fmt.Sprintf("%s(%d): %s", ftype, dec.line, msg)
  674. dec.Warnings = append(dec.Warnings, wline)
  675. }