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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520 | with
ada.Text_IO,
ada.Integer_Text_IO,
ada.Strings.fixed,
ada.Strings.unbounded;
package body openGL.IO.wavefront
is
package real_Text_IO is new Ada.Text_IO.Float_IO (openGL.Real);
function to_Text (Self : in String) return Text
is
begin
return ada.Strings.unbounded.to_unbounded_String (Self);
end to_Text;
function to_Vector_3 (Self : in String) return Vector_3
is
use real_Text_IO;
X, Y, Z : Real;
Last : Natural;
begin
get (Self, X, Last);
get (Self (Last + 1 .. Self'Last), Y, Last);
get (Self (Last + 1 .. Self'Last), Z, Last);
return (X, Y, Z);
end to_Vector_3;
function to_Coordinate (Self : in String) return Coordinate_2D
is
use real_Text_IO;
U, V : Real;
Last : Natural;
begin
get (Self, U, Last);
get (Self (Last + 1 .. Self'Last), V, Last);
return (U, V);
end to_Coordinate;
function to_Facet (Self : in String) return IO.Face
is
use ada.Integer_Text_IO;
site_Id,
coord_Id,
normal_Id : Integer;
the_Vertices : Vertices (1 .. 5_000);
vertex_Count : long_Index_t := 0;
Last : Natural := Self'First - 1;
begin
loop
get (Self (Last + 1 .. Self'Last),
site_Id,
Last);
if Last = Self'Last
or else Self (Last + 1) = ' '
then -- Both texture coord and normal are absent.
coord_Id := Integer (null_Id);
normal_Id := Integer (null_Id);
elsif Self (Last + 1) = '/'
then
if Self (Last + 2) = '/'
then -- Texture coord is absent.
coord_Id := Integer (null_Id);
get (Self (Last + 3 .. Self'Last),
normal_Id,
Last);
else
get (Self (Last + 2 .. Self'Last),
coord_Id,
Last);
if Last = Self'Last
or else Self (Last + 1) = ' '
then -- Lighting normal is absent.
normal_Id := Integer (null_Id);
elsif Self (Last + 1) = '/'
then
get (Self (Last + 2 .. Self'Last),
normal_Id,
Last);
else
raise Constraint_Error with "Invalid indices: " & Self & ".";
end if;
end if;
else
raise Constraint_Error with "Invalid indices: " & Self & ".";
end if;
if site_Id < 0
or else coord_Id < 0
or else normal_Id < 0
then
raise Constraint_Error with "Negative indices not implemented: " & Self & ".";
end if;
vertex_Count := vertex_Count + 1;
the_Vertices (vertex_Count) := (long_Index_t ( site_Id),
long_Index_t ( coord_Id),
long_Index_t (normal_Id),
null_Id);
exit when Last + 1 >= Self'Last;
end loop;
case vertex_Count
is
when 3 => return (Triangle, the_Vertices (1 .. 3));
when 4 => return (Quad, the_Vertices (1 .. 4));
when others => return (Polygon, new Vertices' (the_Vertices (1 .. vertex_Count)));
end case;
end to_Facet;
function to_Model (model_File : in String) return IO.Model
is
use ada.Strings.fixed,
ada.Text_IO;
the_File : File_Type;
max_Elements : constant := 200_000;
the_Sites : Sites_view := new many_Sites (1 .. max_Elements);
the_Coords : Coords_view := new many_Coordinates_2D (1 .. max_Elements);
the_Normals : Normals_view := new many_Normals (1 .. max_Elements);
the_Faces : IO.Faces_view := new IO.Faces' (1 .. max_Elements => <>);
site_Count : long_Index_t := 0;
coord_Count : long_Index_t := 0;
normal_Count : long_Index_t := 0;
face_Count : long_Index_t := 0;
begin
open (the_File, In_File, model_File);
while not end_of_File (the_File)
loop
declare
the_Line : constant String := get_Line (the_File);
begin
if the_Line'Length = 0 or else the_Line (1) = '#'
then
null;
elsif Head (the_Line, 6) = "mtllib"
then
null; -- TODO
elsif Head (the_Line, 2) = "f "
then
face_Count := face_Count + 1;
the_Faces (face_Count) := to_Facet (the_Line (3 .. the_Line'Last));
elsif Head (the_Line, 2) = "v "
then
site_Count := site_Count + 1;
the_Sites (site_Count) := to_Vector_3 (the_Line (3 .. the_Line'Last));
elsif Head (the_Line, 3) = "vt "
then
coord_Count := coord_Count + 1;
the_Coords (coord_Count) := to_Coordinate (the_Line (4 .. the_Line'Last));
elsif Head (the_Line, 3) = "vn "
then
normal_Count := normal_Count + 1;
the_Normals (normal_Count) := to_Vector_3 (the_Line (4 .. the_Line'Last));
elsif Head (the_Line, 2) = "o "
then
null; -- Currently ignored. TODO
elsif Head (the_Line, 2) = "g "
then
null; -- Currently ignored. TODO
elsif Head (the_Line, 2) = "s "
then
null; -- Currently ignored. TODO
else
null; -- Currently ignored. TODO
end if;
end;
end loop;
close (the_File);
declare
used_Sites : constant IO. Sites_view := new many_Sites' (the_Sites (1 .. site_Count));
used_Coords : constant IO. Coords_view := new many_Coordinates_2D' (the_Coords (1 .. coord_Count));
used_Normals : constant IO.Normals_view := new many_Normals' (the_Normals (1 .. normal_Count));
used_Faces : constant IO. Faces_view := new IO.Faces' (the_Faces (1 .. face_Count));
begin
free (the_Sites);
free (the_Coords);
free (the_Normals);
free (the_Faces);
return (Sites => used_Sites,
Coords => used_Coords,
Normals => used_Normals,
Weights => null,
Faces => used_Faces);
end;
end to_Model;
----------
--- Images
--
function Image (Self : in IO.Face) return String
is
use ada.Strings.unbounded;
the_Vertices : Vertices renames Vertices_of (Self);
the_Image : unbounded_String := to_unbounded_String ("f ");
function id_Image (Self : in long_Index_t) return String
is
use ada.Strings.fixed;
begin
return Trim (long_Index_t'Image (Self),
ada.Strings.left);
end id_Image;
begin
for i in the_Vertices'Range
loop
append (the_Image,
id_Image (the_Vertices (i).site_Id));
if the_Vertices (i).coord_Id = null_Id
then
if the_Vertices (i).normal_Id /= null_Id
then
append (the_Image, "/");
end if;
else
append (the_Image, "/" & id_Image (the_Vertices (i).coord_Id));
end if;
-- if the_Vertices (i).normal_Id /= null_Id
-- then
-- append (the_Image,
-- "/" & id_Image (the_Vertices (i).normal_Id));
-- end if;
append (the_Image, " ");
end loop;
return to_String (the_Image);
end Image;
function Image (Self : in wavefront.Group) return String
is
use ada.Strings.unbounded;
begin
case Self.Kind
is
when object_Name => return "o " & to_String (Self.object_Name);
when group_Name => return "g " & to_String (Self. group_Name);
when smoothing_Group => return "s" & Self.smooth_group_Id'Image;
when merging_Group => return ""; -- TODO
end case;
end Image;
function Image (Self : in wavefront.Face) return String
is
begin
case Self.Kind
is
when a_Group => return Image (Self.Group);
when a_Facet => return Image (Self.Facet);
end case;
end Image;
type wf_Faces_view is access all wavefront.Faces;
function to_Model (model_Path : in String) return wavefront.Model
is
use ada.Strings.fixed,
ada.Text_IO;
the_material_Library : Text;
the_material_Name : Text;
the_object_Name : Text;
the_group_Name : Text;
the_Sites : Sites (1 .. 50_000);
site_Count : Index_t := 0;
the_Coords : Coordinates_2D (1 .. 50_000);
coord_Count : Index_t := 0;
the_Normals : Normals (1 .. 50_000);
normal_Count : Index_t := 0;
the_Faces : wf_Faces_view := new Faces' (1 .. 100_000 => <>);
face_Count : long_Index_t := 0;
the_File : File_Type;
begin
Open (the_File, In_File, model_Path);
while not End_Of_File (the_File)
loop
declare
use ada.Strings.unbounded;
the_Line : constant String := Get_Line (the_File);
begin
if the_Line'Length = 0 or else the_Line (1) = '#' then
null;
elsif Head (the_Line, 6) = "mtllib" then
the_material_Library := to_unbounded_String (the_Line (8 .. the_Line'Last));
elsif Head (the_Line, 6) = "usemtl" then
the_material_Name := to_unbounded_String (the_Line (8 .. the_Line'Last));
elsif Head (the_Line, 2) = "f " then
face_Count := face_Count + 1;
the_Faces (face_Count) := (a_Facet,
to_Facet (the_Line (3 .. the_Line'Last)));
elsif Head (the_Line, 2) = "v " then
site_Count := site_Count + 1;
the_Sites (site_Count) := to_Vector_3 (the_Line (3 .. the_Line'Last));
elsif Head (the_Line, 3) = "vt " then
coord_Count := coord_Count + 1;
the_Coords (coord_Count) := to_Coordinate (the_Line (4 .. the_Line'Last));
elsif Head (the_Line, 3) = "vn " then
normal_Count := normal_Count + 1;
the_Normals (normal_Count) := to_Vector_3 (the_Line (4 .. the_Line'Last));
elsif Head (the_Line, 2) = "o " then
the_object_Name := to_unbounded_String (the_Line (3 .. the_Line'Last));
-- face_Count := face_Count + 1;
-- the_Faces (face_Count) := (a_Group,
-- (object_Name,
-- object_Name => to_Text (the_Line (3 .. the_Line'Last))));
elsif Head (the_Line, 2) = "g " then
the_group_Name := to_unbounded_String (the_Line (3 .. the_Line'Last));
-- face_Count := face_Count + 1;
-- the_Faces (face_Count) := (a_Group,
-- (group_Name,
-- group_Name => to_Text (the_Line (3 .. the_Line'Last))));
elsif Head (the_Line, 2) = "s " then
declare
use Ada.Integer_Text_IO;
the_Id : Natural;
Last : Natural;
begin
if Head (the_Line, 5) = "s off" then
the_Id := 0;
else
Get (the_Line (3 .. the_Line'Last), the_Id, Last);
end if;
face_Count := face_Count + 1;
the_Faces (face_Count) := (a_Group,
(smoothing_Group,
smooth_group_Id => the_Id));
end;
else
put_Line ("openGL.io.wavefront ~ Unhandled line in " & model_Path & ": '" & the_Line & "'");
end if;
end;
end loop;
Close (the_File);
declare
procedure free is new Ada.Unchecked_Deallocation (Faces, wf_Faces_view);
used_Faces : constant wf_Faces_view := new wavefront.Faces'(the_Faces (1 .. face_Count));
begin
free (the_Faces);
return
(material_Library => the_material_Library,
material_Name => the_material_Name,
object_Name => the_object_Name,
group_Name => the_group_Name,
Sites => new openGL.Sites'(the_Sites (1 .. site_Count)),
Coords => new Coordinates_2D'(the_Coords (1 .. coord_Count)),
Normals => new openGL.Normals'(the_Normals (1 .. normal_Count)),
Faces => used_Faces);
end;
end to_Model;
procedure write (the_Model : in wavefront.Model; to_File : in String)
is
use ada.Strings.unbounded,
ada.Text_IO;
the_File : File_type;
use Real_text_IO;
begin
Create (the_File, Out_File, Name => to_File);
if the_Model.material_Library /= ""
then
put_Line (the_File, "mtllib " & to_String (the_Model.material_Library));
new_Line (the_File);
end if;
if the_Model.object_Name /= ""
then
put_Line (the_File, "o " & to_String (the_Model.object_Name));
new_Line (the_File);
end if;
-- Write sites.
--
for Each in the_Model.Sites'Range
loop
Put (the_File, "v ");
Put (the_File, the_Model.Sites (Each) (1), Aft => 19, Exp => 0);
Put (the_File, " ");
Put (the_File, the_Model.Sites (Each) (2), Aft => 19, Exp => 0);
Put (the_File, " ");
Put (the_File, the_Model.Sites (Each) (3), Aft => 19, Exp => 0);
New_Line (the_File);
end loop;
New_Line (the_File);
-- Write texture coords.
--
for Each in the_Model.Coords'Range
loop
Put (the_File, "vt ");
Put (the_File, the_Model.Coords (Each).S, Aft => 19, Exp => 0);
Put (the_File, " ");
Put (the_File, the_Model.Coords (Each).T, Aft => 19, Exp => 0);
New_Line (the_File);
end loop;
-- New_Line (the_File);
-- Write normals.
--
-- for Each in the_Model.Normals'Range
-- loop
-- Put (the_File, "vn ");
-- Put (the_File, the_Model.Normals (Each) (1), Aft => 19, Exp => 0);
-- Put (the_File, " ");
-- Put (the_File, the_Model.Normals (Each) (2), Aft => 19, Exp => 0);
-- Put (the_File, " ");
-- Put (the_File, the_Model.Normals (Each) (3), Aft => 19, Exp => 0);
--
-- New_Line (the_File);
-- end loop;
New_Line (the_File);
-- Write faces.
--
if the_Model.group_Name /= ""
then
put_Line (the_File, "g " & to_String (the_Model.group_Name));
new_Line (the_File);
end if;
if the_Model.material_Name /= ""
then
put_Line (the_File, "usemtl " & to_String (the_Model.material_Name));
new_Line (the_File);
end if;
for Each in the_Model.Faces'Range
loop
Put_Line (the_File, Image (the_Model.Faces (Each)));
end loop;
Close (the_File);
end write;
end openGL.IO.wavefront;
|