-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.fs
executable file
·304 lines (239 loc) · 9.86 KB
/
Program.fs
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
(*
Copyright 2017, Sjors van Gelderen
Converts Wavefront .obj files to C structs
*)
open System
open System.IO
// Version of the program
let version = 1.0
// All message types
type Message =
| HelpMessage
| UnrecognizedHeader of string
| MalformedVertex of string
| MalformedFace of string
| FileNotFound of string
| FailedRead of string
| FailedWrite of string
// Display a helpful message or an error
let show (which: Message) = fun () ->
match which with
| HelpMessage ->
printfn "obj_to_c version %A\nUsage: obj_to_c structname" version
| UnrecognizedHeader header ->
printfn "Header %A not recognized" header
| MalformedVertex line ->
printfn "Malformed vertex: %A" line
| MalformedFace face ->
printfn "Malformed face: %A" face
| FileNotFound fileName ->
printfn "File %A not found" fileName
| FailedRead fileName ->
printfn "Failed to read file %A" fileName
| FailedWrite fileName ->
printfn "Failed to write file %A" fileName
// Vertex data
type Vertex =
{
x : double
y : double
z : double
}
// Index data
type Index = int
// Texture coordinate data
type TexCoord = double
// Used to collect information from the file
type Accumulator =
{
vertices : Vertex List
verIndices : Index List
texCoords : TexCoord List
texIndices : Index List
normals : Index List
}
let accumulatorZero =
{
vertices = []
verIndices = []
texCoords = []
texIndices = []
normals = []
}
// Reads vertex data from a supplied line
let parseVertex = fun (acc: Accumulator) (line: string) ->
let split = line.Split ' '
if split.Length = 4 then
Some { acc with vertices = { x = double split.[1]
y = double split.[2]
z = double split.[3] } :: acc.vertices }
else
show <| MalformedVertex line <| ()
None
// Reads texture coordinate data from a supplied line
let parseTexCoord = fun (acc: Accumulator) (line: string) ->
let split = line.Split ' '
if split.Length = 3 then
Some { acc with texCoords = acc.texCoords @ [ double split.[1]; double split.[2] ] }
else
None
// Reads index data from a supplied line
let parseFace = fun (acc : Accumulator) (line: string) ->
let split = line.Split ' '
if split.Length = 4 then
let folder = fun (acc: Index List * Index List * Index List) (elem: string) ->
let contents = elem.Split '/'
if contents.Length = 3 then
match acc with
| (verIndices, texIndices, normals) ->
(int contents.[0] :: verIndices,
int contents.[1] :: texIndices,
int contents.[2] :: normals)
else
show <| MalformedFace line <| ()
acc
let verIndices, texIndices, normals =
List.fold folder ([], [], []) <| Array.toList split.[1..]
Some { vertices = acc.vertices
verIndices = acc.verIndices @ List.rev verIndices
texCoords = acc.texCoords
texIndices = acc.texIndices @ List.rev texIndices
normals = acc.normals @ List.rev normals }
else
show <| MalformedFace line <| ()
None
// Checks whether the correct arguments were supplied
let argsCheck = fun args ->
if Array.isEmpty args || Array.length args > 1 then
show HelpMessage ()
false
else
true
// Opens and reads a file, collects data from its contents
let scanFile = fun (structName: string) ->
let fileName = structName + ".obj"
if File.Exists fileName then
try
let lines = File.ReadAllLines fileName
let parseSkip = fun acc _ -> Some acc
let folder = fun (acc: Accumulator) (line: string) ->
if line.Length > 0 then
let parser =
match line.[0] with
| '#' -> parseSkip
| 'v' -> if line.[1] = 't' then parseTexCoord else parseVertex
| 'f' -> parseFace
| _ ->
show <| UnrecognizedHeader (string line.[0]) <| ()
parseSkip
match parser acc line with
| Some result -> result
| None -> acc
else
// Empty line
acc
let data = Seq.fold folder accumulatorZero lines
Some { data with vertices = List.rev data.vertices }
with
| _ ->
show <| FailedRead fileName <| ()
None
else
show <| FileNotFound fileName <| ()
None
let processVertices (structName: string) (model: Accumulator) =
let verticesAmount = model.vertices.Length
let verticesStart =
sprintf "f32 %sVertices[] ATTRIBUTE_ALIGN(32) = {\n" structName
let verticesFolder = fun (acc: string) (v: Vertex) ->
acc + (sprintf " %5.2fF, %5.2fF, %5.2fF,\n" v.x v.y v.z)
let verticesString = List.fold verticesFolder verticesStart model.vertices
verticesString.[..verticesString.Length - 3] + "\n};\n", verticesAmount
let processVerIndices (structName: string) (model: Accumulator) =
let indicesAmount = model.verIndices.Length
let indicesStart =
sprintf "u16 %sIndices[] ATTRIBUTE_ALIGN(32) = {\n" structName
let indicesFolder = fun (acc: string * int) (i: Index) ->
match acc with
| (text, count) ->
let indent = if count % 3 = 0 then " " else ""
let text' = text + indent
let count' = count + 1
if count' % 3 = 0 then
(text' + (sprintf "%5d, \n" <| i - 1), count')
else
(text' + (sprintf "%5d, " <| i - 1), count')
let indicesFolded = List.fold indicesFolder (indicesStart, 0) model.verIndices
let indicesString = match indicesFolded with (text, _) -> text
indicesString.[..indicesString.Length - 4] + "\n};\n", indicesAmount
let processTexCoords (structName: string) (model: Accumulator) =
let texCoordsAmount = model.texCoords.Length
let texCoordsStart =
sprintf "f32 %sTexCoords[] ATTRIBUTE_ALIGN(32) = {\n" structName
let texCoordsFolder = fun (acc: string * int) (vt: TexCoord) ->
match acc with
| (text, count) ->
let indent = if count % 2 = 0 then " " else ""
let text' = text + indent
let count' = count + 1
if count' % 2 = 0 then
(text' + (sprintf "%2.5fF, \n" <| vt), count')
else
(text' + (sprintf "%2.5fF, " <| vt), count')
let texCoordsFolded = List.fold texCoordsFolder (texCoordsStart, 0) model.texCoords
let texCoordsString = match texCoordsFolded with (text, _) -> text
texCoordsString.[..texCoordsString.Length - 4] + "\n};\n", texCoordsAmount
let processTexIndices (structName: string) (model: Accumulator) =
let texIndicesAmount = model.texIndices.Length
let texIndicesStart =
sprintf "u16 %sTexIndices[] ATTRIBUTE_ALIGN(32) = {\n" structName
let texIndicesFolder = fun (acc: string * int) (i: Index) ->
match acc with
| (text, count) ->
let indent = if count % 3 = 0 then " " else ""
let text' = text + indent
let count' = count + 1
if count' % 3 = 0 then
(text' + (sprintf "%5d, \n" <| i - 1), count')
else
(text' + (sprintf "%5d, " <| i - 1), count')
let texIndicesFolded = List.fold texIndicesFolder (texIndicesStart, 0) model.texIndices
let texIndicesString = match texIndicesFolded with (text, _) -> text
texIndicesString.[..texIndicesString.Length - 4] + "\n};\n", texIndicesAmount
// Writes the struct into a C file
let writeStruct = fun (structName: string) (model: Accumulator) ->
let fileName = structName + ".obj"
try
let verticesString, verticesAmount = processVertices structName model
let verIndicesString, verIndicesAmount = processVerIndices structName model
let texCoordsString, texCoordsAmount = processTexCoords structName model
let texIndicesString, texIndicesAmount = processTexIndices structName model
let text =
[
verticesString
verIndicesString
texCoordsString
texIndicesString
sprintf "struct Model %s = {" structName
sprintf " .vertices = %sVertices," structName
sprintf " .verticesAmount = %d," verticesAmount
sprintf " .indices = %sIndices," structName
sprintf " .indicesAmount = %d," verIndicesAmount
sprintf " .texCoords = %sTexCoords," structName
sprintf " .texCoordsAmount = %d," texCoordsAmount
sprintf " .texIndices = %sTexIndices," structName
sprintf " .texIndicesAmount = %d\n};" texIndicesAmount
] |> String.concat "\n"
File.WriteAllText (structName + ".c", text)
printfn "All done!"
with
_ -> show <| FailedWrite fileName <| ()
[<EntryPoint>]
let main = fun args ->
if argsCheck args then
let structName = args.[0]
match scanFile structName with
| Some data ->
data |> writeStruct structName
| None -> ()
0