-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj.go
158 lines (126 loc) · 3.14 KB
/
obj.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
package main
import (
"os"
"strings"
"bufio"
"strconv"
"fmt"
)
type OBJMesh struct {
v []Vec4
vt []Vec4
// v/vt/vn v/vt/vn v/vt/vn
f [][3][3]int
}
func OBJVHash( v [3]int ) int {
res := 17
res = 31 * res + v[0]
res = 31 * res + v[1]
res = 31 * res + v[2]
return res
}
// what even
type Vinf struct {
vi int
vert *Vertex
}
// load an obj file and convert to indexed Mesh
func LoadOBJMesh( file string ) *Mesh {
var om *OBJMesh = &OBJMesh{}
om.v = make( []Vec4, 0 )
om.vt = make( []Vec4, 0 )
om.f = make( [][3][3]int, 0 )
objFile, err := os.Open( file )
if err != nil { panic( err ) }
defer objFile.Close()
objReader := bufio.NewReader( objFile )
// load objmesh structure
// assumptions:
// len of om.f should be multiple of 3
// no mtl, multitexture, NO QUADS
for true {
var objLine string
objLine, err := objReader.ReadString( '\n' )
if err != nil { break }
objFlds := strings.Fields( objLine )
if len( objFlds ) == 0 { continue }
if objFlds[0] == "v" {
var v Vec4
var ffloat float64
ffloat, _ = strconv.ParseFloat( objFlds[1], 32 )
v.X = float32(ffloat)
ffloat, _ = strconv.ParseFloat( objFlds[2], 32 )
v.Y = float32(ffloat)
ffloat, _ = strconv.ParseFloat( objFlds[3], 32 )
v.Z = float32(ffloat)
v.W = 1.0
om.v = append( om.v, v )
}
if objFlds[0] == "vt" {
var vtx, vty float64
vtx, _ = strconv.ParseFloat( objFlds[1], 32 )
vty, _ = strconv.ParseFloat( objFlds[2], 32 )
/*for vtx < 0 { vtx += 1 }
for vtx > 1 { vtx -= 1 }
for vty < 0 { vty += 1 }
for vty > 1 { vty -= 1 }*/
vty = 1 - vty
//vtx = 1 - vtx
om.vt = append( om.vt, Vec4{ float32(vtx), float32(vty), 0, 1 } )
}
// 1 based index
if objFlds[0] == "f" {
var f [3][3]int
fsplit := strings.Split( objFlds[1], "/" )
f[0][0], _ = strconv.Atoi( fsplit[0] )
f[0][1], f[0][2] = f[0][0], f[0][0]
if len( fsplit ) > 1 {
f[0][1], _ = strconv.Atoi( fsplit[1] )
}
if len( fsplit ) > 2 {
f[0][2], _ = strconv.Atoi( fsplit[2] )
}
fsplit = strings.Split( objFlds[2], "/" )
f[1][0], _ = strconv.Atoi( fsplit[0] )
f[1][1], f[1][2] = f[1][0], f[1][0]
if len( fsplit ) > 1 {
f[1][1], _ = strconv.Atoi( fsplit[1] )
}
if len( fsplit ) > 2 {
f[1][2], _ = strconv.Atoi( fsplit[2] )
}
fsplit = strings.Split( objFlds[3], "/" )
f[2][0], _ = strconv.Atoi( fsplit[0] )
f[2][1], f[2][2] = f[2][0], f[2][0]
if len( fsplit ) > 1 {
f[2][1], _ = strconv.Atoi( fsplit[1] )
}
if len( fsplit ) > 2 {
f[2][2], _ = strconv.Atoi( fsplit[2] )
}
om.f = append( om.f, f )
}
}
// convert to indexed Mesh
m := &Mesh{}
m.Vertices = make( []Vertex, 0 )
m.Indices = make( []int, 0 )
vi := 0
objVMap := make( map[int]*Vinf )
for _, f := range om.f {
for _, v := range f {
hash := OBJVHash( v )
if objVMap[hash] == nil {
vert := Vertex{ om.v[ v[0] - 1 ], om.vt[ v[1] - 1 ] }
m.Vertices = append( m.Vertices, vert )
objVMap[hash] = &Vinf{ vi, &vert }
m.Indices = append( m.Indices, vi )
vi += 1
} else {
m.Indices = append( m.Indices, objVMap[hash].vi )
}
}
}
fmt.Printf( "%v\n", m.Vertices[m.Indices[0]] )
return m
}