-
Notifications
You must be signed in to change notification settings - Fork 3
/
objLoader.go
305 lines (268 loc) · 7.71 KB
/
objLoader.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
package in3d
import (
"bufio"
"errors"
"os"
"strconv"
"strings"
"github.com/go-gl/mathgl/mgl32"
)
// Mesh :
type Mesh struct {
MaterialGroups map[string]*MaterialGroup
}
// MaterialGroup :
type MaterialGroup struct {
Material *Material
Faces []*Face
VAO uint32
VertCount int32
}
// Material represents a material
type Material struct {
Name string
Ambient []float32
Diffuse []float32
Specular []float32
Shininess float32
DiffuseTex uint32
NormalTex uint32
}
// Face :
type Face struct {
VertID []int
UVID []int
NormID []int
}
func buildVAOforMatGroup(group *MaterialGroup, vertexs, uvs, normals [][]float32, program uint32) {
var (
vao []float32
)
for _, f := range group.Faces { // use face data to construct GL VAO: XYZ UV [3]normal [3]tangent
// This is UGLY!!
vec0 := mgl32.NewVecNFromData(vertexs[f.VertID[0]-1])
vec1 := mgl32.NewVecNFromData(vertexs[f.VertID[1]-1])
vec2 := mgl32.NewVecNFromData(vertexs[f.VertID[2]-1])
normal0 := normals[f.NormID[0]-1]
normal1 := normals[f.NormID[1]-1]
normal2 := normals[f.NormID[2]-1]
uv0 := mgl32.NewVecNFromData([]float32{0, 0})
uv1 := mgl32.NewVecNFromData([]float32{0, 0})
uv2 := mgl32.NewVecNFromData([]float32{0, 0})
tangent := mgl32.NewVecNFromData([]float32{0, 0, 0})
if f.UVID[0] >= 0 {
// if we have UV mappings, calculate tangentent and bitangent for normal map
uv0 = mgl32.NewVecNFromData(uvs[f.UVID[0]-1])
uv1 = mgl32.NewVecNFromData(uvs[f.UVID[1]-1])
uv2 = mgl32.NewVecNFromData(uvs[f.UVID[2]-1])
e1 := vec1.Sub(nil, vec0)
e2 := vec2.Sub(nil, vec0)
dUV1 := uv1.Sub(nil, uv0)
dUV2 := uv2.Sub(nil, uv0)
x, y, z := 0, 1, 2
f := 1.0 / (dUV1.Get(x)*dUV2.Get(y) - dUV2.Get(x)*dUV1.Get(y))
// print(f)
tangent.Set(x, f*(dUV2.Get(y)*e1.Get(x)-dUV1.Get(y)*e2.Get(x)))
tangent.Set(y, f*(dUV2.Get(y)*e1.Get(y)-dUV1.Get(y)*e2.Get(y)))
tangent.Set(z, f*(dUV2.Get(y)*e1.Get(z)-dUV1.Get(y)*e2.Get(z)))
tangent = tangent.Normalize(nil)
// println(tangent)
}
// This is UGLY!!
vao = append(vao, vec0.Raw()...)
vao = append(vao, uv0.Raw()...)
vao = append(vao, normal0...)
vao = append(vao, tangent.Raw()...)
vao = append(vao, vec1.Raw()...)
vao = append(vao, uv1.Raw()...)
vao = append(vao, normal1...)
vao = append(vao, tangent.Raw()...)
vao = append(vao, vec2.Raw()...)
vao = append(vao, uv2.Raw()...)
vao = append(vao, normal2...)
vao = append(vao, tangent.Raw()...)
}
group.VAO = MakeVAO(vao, program)
group.VertCount = int32(len(vao))
}
// LoadObject : opens a wavefront file and parses it into Material Groups
// TODO: Fix UV coords, they are upside down...
func LoadObject(filename string, program uint32) *Mesh {
file, ferr := os.Open(filename)
EoE("Error Opening File", ferr)
defer file.Close()
vertexs := [][]float32{}
normals := [][]float32{}
uvs := [][]float32{}
var materialGroups map[string]*MaterialGroup
currentGroup := "string"
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") || strings.HasPrefix(line, " ") {
continue
}
fields := strings.Fields(line)
if len(fields) == 0 {
continue
}
switch fields[0] {
case "mtllib":
materialGroups = LoadMaterials(fields[1])
case "usemtl":
currentGroup = fields[1]
case "v":
if len(fields) != 4 {
EoE("Error Parsing Vertex too few feilds ", errors.New(filename))
}
var v []float32
for i := 1; i < 4; i++ {
f, err := strconv.ParseFloat(fields[i], 32)
EoE("Failed to parse float", err)
v = append(v, float32(f))
}
vertexs = append(vertexs, v)
case "vt":
if len(fields) != 3 {
EoE("Error Parsing UV coords", errors.New(filename))
}
var uv []float32
for i := 1; i < 3; i++ {
f, err := strconv.ParseFloat(fields[i], 32)
EoE("Failed to parse float", err)
uv = append(uv, float32(f))
}
uvs = append(uvs, uv)
case "vn":
if len(fields) != 4 {
EoE("unsupported vertex normal line", errors.New(filename+" "+line))
}
var n []float32
for i := 1; i < 4; i++ {
f, err := strconv.ParseFloat(fields[i], 32)
EoE("cannot parse float", err)
n = append(n, float32(f))
}
normals = append(normals, n)
case "f":
if len(fields) != 4 {
EoE("unsupported face:"+string(rune(len(fields)))+" "+line, errors.New(filename))
}
var (
vi, ui, ni []int
)
for i := 1; i < 4; i++ {
faceStr := strings.Split(fields[i], "/")
svi, err := strconv.Atoi(faceStr[0])
vi = append(vi, svi)
EoE("unsupported face vertex index", err)
sni, err := strconv.Atoi(faceStr[2])
ni = append(ni, sni)
EoE("unsupported face normal index", err)
if faceStr[1] == "" {
// set negative value as placeholder for .obj with no UV mapping
faceStr[1] = "-1"
}
sui, err := strconv.Atoi(faceStr[1])
ui = append(ui, sui)
EoE("unsupported face uv index", err)
}
materialGroups[currentGroup].Faces = append(materialGroups[currentGroup].Faces, &Face{vi, ui, ni})
}
}
for _, g := range materialGroups {
buildVAOforMatGroup(g, vertexs, uvs, normals, program)
}
return &Mesh{materialGroups}
}
// LoadMaterials : create material groups from wavefront data
func LoadMaterials(filename string) map[string]*MaterialGroup {
file, ferr := os.Open(filename)
EoE("Error Opening Material File", ferr)
defer file.Close()
line := ""
scanner := bufio.NewScanner(file)
currentMat := ""
materialGroups := make(map[string]*MaterialGroup)
for scanner.Scan() {
line = scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
fields := strings.Fields(line)
if len(fields) == 0 {
continue
}
if fields[0] == "newmtl" {
if len(fields) != 2 {
EoE("unsupported material definition", errors.New(filename))
}
currentMat = fields[1]
material := &Material{
currentMat,
[]float32{0.1, 0.1, 0.1},
[]float32{1, 1, 1},
[]float32{0.8, 0.8, 0.8},
1,
NoTexture,
NoTexture,
}
materialGroups[currentMat] = &MaterialGroup{}
materialGroups[currentMat].Material = material
continue
}
switch fields[0] {
case "Ka":
if len(fields) != 4 {
EoE("unsupported ambient color line", errors.New(filename))
}
for i := 0; i < 3; i++ {
f, err := strconv.ParseFloat(fields[i+1], 32)
EoE("Error parsing float", err)
materialGroups[currentMat].Material.Ambient[i] = float32(f)
}
case "Kd":
if len(fields) != 4 {
EoE("Error Diffuse Parse", errors.New(filename))
}
for i := 0; i < 3; i++ {
f, err := strconv.ParseFloat(fields[i+1], 32)
EoE("Error parsing float", err)
materialGroups[currentMat].Material.Diffuse[i] = float32(f)
}
case "Ks":
if len(fields) != 4 {
EoE("Error KS Parse", errors.New(filename))
}
for i := 0; i < 3; i++ {
f, err := strconv.ParseFloat(fields[i+1], 32)
EoE("Error parsing float", err)
materialGroups[currentMat].Material.Specular[i] = float32(f)
}
case "Ns":
if len(fields) != 2 {
EoE("Error NS Parse", errors.New(filename))
}
f, err := strconv.ParseFloat(fields[1], 32)
EoE("Error parsing float", err)
materialGroups[currentMat].Material.Shininess = float32(f / 1000 * 128)
// case "d":
// if len(fields) != 2 {
// EoE("Error d Parse", errors.New(filename))
// }
// f, err := strconv.ParseFloat(fields[1], 32)
// EoE("Error parsing float", err)
// materialGroups[currentMat].Material.Shininess = float32(f)
case "map_Kd":
DiffuseTexFile := fields[1]
DiffuseTex := NewTexture(DiffuseTexFile)
materialGroups[currentMat].Material.DiffuseTex = DiffuseTex
case "map_Bump":
NormalTexFile := fields[1]
NormalTex := NewTexture(NormalTexFile)
materialGroups[currentMat].Material.NormalTex = NormalTex
}
}
EoE("Scann Error", scanner.Err())
return materialGroups
}