-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectFileParser.kt
185 lines (141 loc) · 6.76 KB
/
ObjectFileParser.kt
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
package longevity.software.vrgamedemo
import android.content.Context
import java.io.BufferedReader
import java.io.InputStreamReader
class ObjectFileParser(context: Context, file: String) {
private val mFinalVertices = mutableListOf<Float>()
private val mFinalNormals = mutableListOf<Float>()
private val mFinalUVs = mutableListOf<Float>()
private val mFinalIndices = mutableListOf<Short>()
/**
* Init - parses the file in assets that matches the file string passed.
*/
init {
val inStream = context.assets.open(file)
val reader = BufferedReader(InputStreamReader(inStream))
val vertices = ArrayList<Triple<Float, Float, Float>>()
val normals = ArrayList<Triple<Float, Float, Float>>()
val uvs = ArrayList<Pair<Float, Float>>()
val faceMap = HashMap<Triple<Int, Int?, Int?>, Short>()
var nextIndex: Short = 0
reader.forEachLine {
if ( it.startsWith("v " ) ) {
val split = it.split( " " )
// split[0] is the prefix
val vertex = Triple(split[1].toFloat(), split[2].toFloat(), split[3].toFloat())
vertices.add(vertex)
}
else if ( it.startsWith("vn" ) ) {
val split = it.split( " " )
// split[0] is the prefix
val normal = Triple(split[1].toFloat(), split[2].toFloat(), split[3].toFloat())
normals.add(normal)
}
else if ( it.startsWith( "vt" ) ) {
val split = it.split( " " )
// split[0] is the prefix
val uv = Pair(split[1].toFloat(), split[2].toFloat())
uvs.add(uv)
}
else if ( it.startsWith("f " ) ) {
// perform the first split
val split = it.split( " " )
// get the first vertex, uv and normal
val vun1 = split[1].split("/")
val point1 = Triple(vun1[0].toInt(), vun1[1].toIntOrNull(), vun1[2].toIntOrNull())
val pointInFaceMap1 = faceMap.get( point1 )
if ( null != pointInFaceMap1 ) {
mFinalIndices.add(pointInFaceMap1)
} else {
// add this sequence to the map
faceMap.put(point1, nextIndex)
// add vertex data
mFinalVertices.add(vertices[point1.first - 1].first)
mFinalVertices.add(vertices[point1.first - 1].second)
mFinalVertices.add(vertices[point1.first - 1].third)
// add uvs if present
if ( null != point1.second ) {
mFinalUVs.add(uvs[point1.second!! - 1].first)
mFinalUVs.add(uvs[point1.second!! - 1].second)
}
// add normals if present
if ( null != point1.third ) {
mFinalNormals.add(normals[point1.third!! - 1].first)
mFinalNormals.add(normals[point1.third!! - 1].second)
mFinalNormals.add(normals[point1.third!! - 1].third)
}
// and finally add the index
mFinalIndices.add(nextIndex++)
}
// get the second vertex, uv and normal
val vun2 = split[2].split("/")
val point2 = Triple(vun2[0].toInt(), vun2[1].toIntOrNull(), vun2[2].toIntOrNull())
val pointInFaceMap2 = faceMap.get( point2 )
if ( null != pointInFaceMap2 ) {
mFinalIndices.add(pointInFaceMap2)
} else {
// add this sequence to the map
faceMap.put(point2, nextIndex)
// add vertex data
mFinalVertices.add(vertices[point2.first - 1].first)
mFinalVertices.add(vertices[point2.first - 1].second)
mFinalVertices.add(vertices[point2.first - 1].third)
// add uvs if present
if ( null != point2.second ) {
mFinalUVs.add(uvs[point2.second!! - 1].first)
mFinalUVs.add(uvs[point2.second!! - 1].second)
}
// add normals if present
if ( null != point2.third ) {
mFinalNormals.add(normals[point2.third!! - 1].first)
mFinalNormals.add(normals[point2.third!! - 1].second)
mFinalNormals.add(normals[point2.third!! - 1].third)
}
// and finally add the index
mFinalIndices.add(nextIndex++)
}
// get the third vertex, uv and normal
val vun3 = split[3].split("/")
val point3 = Triple(vun3[0].toInt(), vun3[1].toIntOrNull(), vun3[2].toIntOrNull())
val pointInFaceMap3 = faceMap.get( point3 )
if ( null != pointInFaceMap3 ) {
mFinalIndices.add(pointInFaceMap3)
} else {
// add this sequence to the map
faceMap.put(point3, nextIndex)
// add vertex data
mFinalVertices.add(vertices[point3.first - 1].first)
mFinalVertices.add(vertices[point3.first - 1].second)
mFinalVertices.add(vertices[point3.first - 1].third)
// add uvs if present
if ( null != point3.second ) {
mFinalUVs.add(uvs[point3.second!! - 1].first)
mFinalUVs.add(uvs[point3.second!! - 1].second)
}
// add normals if present
if ( null != point3.third ) {
mFinalNormals.add(normals[point3.third!! - 1].first)
mFinalNormals.add(normals[point3.third!! - 1].second)
mFinalNormals.add(normals[point3.third!! - 1].third)
}
// and finally add the index
mFinalIndices.add(nextIndex++)
}
}
}
}
/**
* returns the Model data which has been parsed
*/
fun getModelData(): ModelData {
return ModelData(mFinalVertices.toFloatArray(),
mFinalNormals.toFloatArray(),
mFinalUVs.toFloatArray(),
mFinalIndices.toShortArray(),
emptyArray<Float>().toFloatArray(),
emptyArray<Float>().toFloatArray(),
emptyArray<Float>().toFloatArray(),
emptyArray<Float>().toFloatArray(),
emptyArray<Float>().toFloatArray())
}
}