-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwavefront.go
54 lines (49 loc) · 1.45 KB
/
wavefront.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
package graphile
import (
"strconv"
"strings"
)
func (g *GeometryFile) parseLineOBJ(line string) {
newLine := strings.Replace(strings.TrimSpace(line), "/", " ", -1)
larray := strings.Split(newLine, " ")
switch larray[0] {
case "v":
x, _ := strconv.ParseFloat(larray[1], 32)
y, _ := strconv.ParseFloat(larray[2], 32)
z, _ := strconv.ParseFloat(larray[3], 32)
g.vertex = append(g.vertex, []float32{float32(x), float32(y), float32(z)})
case "vt":
x, _ := strconv.ParseFloat(larray[1], 32)
y, _ := strconv.ParseFloat(larray[2], 32)
g.vertexTexture = append(g.vertexTexture, []float32{float32(x), float32(y)})
g.hasTextures = g.hasTextures || true
case "vn":
x, _ := strconv.ParseFloat(larray[1], 32)
y, _ := strconv.ParseFloat(larray[2], 32)
z, _ := strconv.ParseFloat(larray[3], 32)
g.vertexNormal = append(g.vertexNormal, []float32{float32(x), float32(y), float32(z)})
g.hasNormals = g.hasNormals || true
case "f":
buffer := []int32{}
for _, faceIdx := range larray[1:] {
v, err := strconv.Atoi(faceIdx)
if err == nil {
buffer = append(buffer, int32(v))
}
}
offset := g.offset()
pivot := offset * 3
switch len(buffer) / offset {
case 3:
g.triangles = append(g.triangles, buffer)
case 4:
buffer = append(buffer, buffer[0:offset]...)
g.triangles = append(g.triangles, buffer[:pivot]) // T1
g.triangles = append(g.triangles, buffer[pivot-offset:]) // T2
default:
return
}
default:
return
}
}