-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloader.go
184 lines (163 loc) · 3.61 KB
/
loader.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
package wfobj
import (
"fmt"
"strconv"
)
type meshLoader struct {
mesh *Mesh
vertices VertexList
normals VertexList
tokens []*Token
pos int
}
type MeshLoadError string
func NewMeshLoadError(val interface{}) MeshLoadError {
return MeshLoadError(fmt.Sprintf("%v", val))
}
func (m MeshLoadError) Error() string {
return "MeshLoadError: " + string(m)
}
// Read a new token from the parser
func (m *meshLoader) next() (ok bool) {
m.pos++
if m.pos >= len(m.tokens) {
return
}
ok = true
return
}
func (m *meshLoader) peek(k Kind) (t *Token, ok bool) {
npos := m.pos + 1
if npos >= len(m.tokens) {
return
}
t = m.tokens[npos]
if k != AnyKind {
ok = t.Kind == k
}
return
}
// Read the current token
func (m *meshLoader) token() *Token {
if m.pos < 0 {
panic("Invalid position. Must be non zero")
}
if m.pos >= len(m.tokens) {
panic("Invalid position. Must be less then length")
}
return m.tokens[m.pos]
}
func (m *meshLoader) ensureKind(k Kind) {
if m.token().Kind != k {
panic(fmt.Sprintf("Invalid token %v. Expecting %v", m.token(), k))
}
}
func (m *meshLoader) pushBack() {
m.pos--
}
// Read a number from the token stream and return the number
// panic if it's not a number
//
// If t is not nil, instead of consuming a token from the stream
// just ensure that t is a valid number literal
func (m *meshLoader) readNumberLit() (num float64) {
m.next()
t := m.token()
m.ensureKind(NumberLit)
num, err := strconv.ParseFloat(t.Val, 64)
if err != nil {
panic(err)
}
return num
}
// Read the face declaration with the number/number/number format
func (m *meshLoader) readFaceDecl(f *Face) {
for m.next() {
t := m.token()
if t.Kind == NumberLit {
m.pushBack()
idx := int32(m.readNumberLit())
f.Vertices = append(f.Vertices, m.vertices[idx-1])
// texture information
m.next()
t = m.token()
if t.Kind == SlashLit {
// TODO handle textures
} else {
m.pushBack()
continue
}
// normal information
m.next()
t = m.token()
if t.Kind == SlashLit {
idx := int32(m.readNumberLit())
f.Normals = append(f.Normals, m.normals[idx-1])
} else {
m.pushBack()
continue
}
} else {
m.pushBack()
break
}
}
}
func (m *meshLoader) Load() (err error) {
defer func() {
if p := recover(); p != nil {
err = NewMeshLoadError(p)
}
}()
m.vertices = make(VertexList, 0)
m.normals = make(VertexList, 0)
m.mesh = &Mesh{}
m.mesh.Faces = make([]Face, 0)
for m.next() {
switch m.token().Kind {
case VertexDecl:
v := Vertex{}
v.X = float32(m.readNumberLit())
v.Y = float32(m.readNumberLit())
v.Z = float32(m.readNumberLit())
m.vertices = append(m.vertices, v)
case NormalDecl:
n := Vertex{}
n.X = float32(m.readNumberLit())
n.Y = float32(m.readNumberLit())
n.Z = float32(m.readNumberLit())
m.normals = append(m.normals, n)
case FaceDecl:
f := Face{}
f.Vertices = make(VertexList, 0)
f.Normals = make(VertexList, 0)
m.readFaceDecl(&f)
m.mesh.Faces = append(m.mesh.Faces, f)
case Eof:
break
default:
panic(fmt.Sprintf("Unexpected token (%v) expecting: %v", m.token(), fmt.Sprintf("[%v]", []Kind{VertexDecl, FaceDecl, Eof})))
}
}
return
}
// Load a new mesh
func LoadMesh(tokens <-chan *Token) (m *Mesh, err error) {
ml := &meshLoader{nil, nil, nil, make([]*Token, 0), -1}
for t := range tokens {
ml.tokens = append(ml.tokens, t)
}
err = ml.Load()
m = ml.mesh
return
}
// Load a new mesh from the given .obj file
func LoadMeshFromFile(file string) (m *Mesh, err error) {
p, err := NewParserFromFile(file)
if err != nil {
return
}
go p.Parse()
m, err = LoadMesh(p.Tokens)
return
}