This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
miniobj.h
1284 lines (1027 loc) · 40.5 KB
/
miniobj.h
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Public domain. See "unlicense" statement at the end of this file.
// ABOUT
//
// miniobj is a simple library for loading OBJ mesh files. This does not load MTL files, but does keep track of them
// to allow an application to do their own loading. Only a tiny subset of features are currently supported:
// - Only basic triangle/face polygonal meshes are supported.
// - Freeform meshes are not supported (Bazier curves, etc.)
// - Groups are ignored.
//
//
//
// USAGE
//
// This is a single-file library. To use it, do something like the following in one .c file.
// #define MINIOBJ_IMPLEMENTATION
// #include "miniobj.h"
//
// You can then #include this file in other parts of the program as you would with any other header file.
//
//
//
// OPTIONS
// #define these options before including this file.
//
// #define MINIOBJ_NO_STDIO
// Disable miniobj_load_file().
//
//
//
// QUICK NOTES
// - Only triangle polygonal geometry is currently supported.
//
//
//
// TODO
//
#ifndef miniobj_h
#define miniobj_h
#include <stddef.h> /* For size_t. */
/* Sized types. Prefer built-in types. Fall back to stdint. */
#ifdef _MSC_VER
#if defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wlanguage-extension-token"
#pragma GCC diagnostic ignored "-Wc++11-long-long"
#endif
typedef signed __int8 miniobj_int8;
typedef unsigned __int8 miniobj_uint8;
typedef signed __int16 miniobj_int16;
typedef unsigned __int16 miniobj_uint16;
typedef signed __int32 miniobj_int32;
typedef unsigned __int32 miniobj_uint32;
typedef signed __int64 miniobj_int64;
typedef unsigned __int64 miniobj_uint64;
#if defined(__clang__)
#pragma GCC diagnostic pop
#endif
#else
#define MINIOBJ_HAS_STDINT
#include <stdint.h>
typedef int8_t miniobj_int8;
typedef uint8_t miniobj_uint8;
typedef int16_t miniobj_int16;
typedef uint16_t miniobj_uint16;
typedef int32_t miniobj_int32;
typedef uint32_t miniobj_uint32;
typedef int64_t miniobj_int64;
typedef uint64_t miniobj_uint64;
#endif
#ifdef MINIOBJ_HAS_STDINT
typedef uintptr_t miniobj_uintptr;
#else
#if defined(_WIN32)
#if defined(_WIN64)
typedef miniobj_uint64 miniobj_uintptr;
#else
typedef miniobj_uint32 miniobj_uintptr;
#endif
#elif defined(__GNUC__)
#if defined(__LP64__)
typedef miniobj_uint64 miniobj_uintptr;
#else
typedef miniobj_uint32 miniobj_uintptr;
#endif
#else
typedef miniobj_uint64 miniobj_uintptr; /* Fallback. */
#endif
#endif
typedef miniobj_uint8 miniobj_bool8;
typedef miniobj_uint32 miniobj_bool32;
#define MINIOBJ_TRUE 1
#define MINIOBJ_FALSE 0
#ifdef __cplusplus
extern "C" {
#endif
// Callback for when data is read. Return value is the number of bytes actually read.
typedef size_t (* miniobj_read_proc)(void* userData, void* bufferOut, size_t bytesToRead);
// Callback for when the loader needs to be seeked back to the start of the file.
typedef miniobj_bool32 (* miniobj_seek_to_start_proc)(void* userData);
typedef struct
{
// The name.
char* name;
} miniobj_mtllib;
typedef struct
{
// The index of the first face that uses this material.
miniobj_uint32 firstFace;
// The number of faces that use this material (starting from <firstFace>).
miniobj_uint32 faceCount;
// The name of the material.
char* name;
} miniobj_material;
typedef struct
{
float v[3];
} miniobj_vec3;
typedef struct
{
float v[4];
} miniobj_vec4;
typedef struct
{
int positionIndex;
int texcoordIndex;
int normalIndex;
} miniobj_face_vertex;
typedef struct
{
miniobj_face_vertex v[4]; // <-- The 4th component is only used internally. All faces are converted to triangles.
} miniobj_face;
typedef struct
{
// The material library count.
miniobj_uint32 materialLibCount;
// A pointer to the list of material libraries.
miniobj_mtllib* pMaterialLibs;
// The number of materials used by the mesh.
miniobj_uint32 materialCount;
// A pointer to the list of materials used by the mesh.
miniobj_material* pMaterials;
// The number of positions.
miniobj_uint32 positionCount;
// The buffer containing the vertex positions.
miniobj_vec4* pPositions;
// The number of texture coordinates.
miniobj_uint32 texCoordCount;
// The buffer containing the text coordinates.
miniobj_vec3* pTexCoords;
// The number of normals.
miniobj_uint32 normalCount;
// The buffer containing the normal positions.
miniobj_vec3* pNormals;
// The face count.
miniobj_uint32 faceCount;
// A pointer to the face data.
miniobj_face* pFaces;
// A pointer variable length strings that contains the names of materials and whatnot. This is an offset of
// pData and every string is stored immediately after each other and are null terminated.
char* pStrings;
// A pointer to the raw data. This contains the vertex and index data.
char pData[1];
} miniobj;
// Loads an OBJ file using the given callbacks.
miniobj* miniobj_load(miniobj_read_proc onRead, miniobj_seek_to_start_proc onSeek, void* pUserData);
// Deletes the given OBJ file.
void miniobj_delete(miniobj* pOBJ);
#ifndef MINIOBJ_NO_STDIO
// Loads an OBJ file from an actual file.
miniobj* miniobj_load_file(const char* pFile);
#endif
// Helper for loading an OBJ file from a block of memory.
miniobj* miniobj_load_memory(const void* pData, size_t dataSize);
// Frees an internal allocation.
void miniobj_free(void* pData);
// Helper for interleaving the vertex data of the given OBJ object.
//
// Free the returned pointers with miniobj_free().
void miniobj_interleave_p3t2n3(miniobj* pOBJ, miniobj_uint32* pVertexCountOut, float** ppVertexDataOut, miniobj_uint32* pIndexCountOut, miniobj_uint32** ppIndexDataOut);
void miniobj_interleave_p3t2n3_material(miniobj* pOBJ, miniobj_uint32 materialIndex, miniobj_uint32* pVertexCountOut, float** ppVertexDataOut, miniobj_uint32* pIndexCountOut, miniobj_uint32** ppIndexDataOut);
#ifdef __cplusplus
}
#endif
#endif // miniobj_h
///////////////////////////////////////////////////////////////////////////////
//
// IMPLEMENTATION
//
///////////////////////////////////////////////////////////////////////////////
#ifdef MINIOBJ_IMPLEMENTATION
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h> // Use for powf() - can probably be removed later.
#ifndef MINIOBJ_NO_STDIO
#include <stdio.h>
static size_t miniobj__on_read_stdio(void* pUserData, void* bufferOut, size_t bytesToRead)
{
return fread(bufferOut, 1, bytesToRead, (FILE*)pUserData);
}
static miniobj_bool32 miniobj__on_seek_stdio(void* pUserData)
{
return fseek((FILE*)pUserData, 0, SEEK_SET) == 0;
}
miniobj* miniobj_load_file(const char* filename)
{
FILE* pFile;
#ifdef _MSC_VER
if (fopen_s(&pFile, filename, "rb") != 0) {
return NULL;
}
#else
pFile = fopen(filename, "rb");
if (pFile == NULL) {
return NULL;
}
#endif
miniobj* pOBJ = miniobj_load(miniobj__on_read_stdio, miniobj__on_seek_stdio, pFile);
fclose(pFile);
return pOBJ;
}
#endif // MINIOBJ_NO_STDIO
typedef struct
{
// A pointer to the beginning of the data. We use a char as the type here for easy offsetting.
const unsigned char* data;
size_t dataSize;
size_t currentReadPos;
} miniobj_memory;
static size_t miniobj__on_read_memory(void* pUserData, void* bufferOut, size_t bytesToRead)
{
miniobj_memory* memory = (miniobj_memory*)pUserData;
assert(memory != NULL);
assert(memory->dataSize >= memory->currentReadPos);
size_t bytesRemaining = memory->dataSize - memory->currentReadPos;
if (bytesToRead > bytesRemaining) {
bytesToRead = bytesRemaining;
}
if (bytesToRead > 0) {
memcpy(bufferOut, memory->data + memory->currentReadPos, bytesToRead);
memory->currentReadPos += bytesToRead;
}
return bytesToRead;
}
static miniobj_bool32 miniobj__on_seek_memory(void* pUserData)
{
miniobj_memory* memory = (miniobj_memory*)pUserData;
assert(memory != NULL);
memory->currentReadPos = 0;
return 1;
}
miniobj* miniobj_load_memory(const void* data, size_t dataSize)
{
miniobj_memory memory;
memory.data = (const unsigned char*)data;
memory.dataSize = dataSize;
memory.currentReadPos = 0;
return miniobj_load(miniobj__on_read_memory, miniobj__on_seek_memory, &memory);
}
void miniobj_free(void* pData)
{
free(pData);
}
miniobj_bool32 miniobj__find_face_vertex(miniobj_uint32 vertexCount, miniobj_face_vertex* pVertices, miniobj_face_vertex vertex, miniobj_uint32* pIndexOut)
{
for (miniobj_uint32 i = 0; i < vertexCount; ++i) {
if (pVertices[i].positionIndex == vertex.positionIndex && pVertices[i].texcoordIndex == vertex.texcoordIndex && pVertices[i].normalIndex == vertex.normalIndex) {
*pIndexOut = i;
return MINIOBJ_TRUE;
}
}
return MINIOBJ_FALSE;
}
void miniobj_interleave_p3t2n3(miniobj* pOBJ, miniobj_uint32* pVertexCountOut, float** ppVertexDataOut, miniobj_uint32* pIndexCountOut, miniobj_uint32** ppIndexDataOut)
{
// When interleaving we want to ensure we don't copy over duplicate vertices. For example, a quad will be made up of two triangles, with two
// vertices being shared by both faces (along the common edge dividing the two triangles). We don't want to duplicate that data, so when creating
// an index for a face, we first want to check that it hasn't already been added.
// Create output buffers large enough to contain the interleaved data.
miniobj_uint32 indexCount = 0;
miniobj_uint32* pIndexData = (miniobj_uint32*)malloc(sizeof(miniobj_uint32) * pOBJ->faceCount*3);
miniobj_uint32 vertexCount = 0;
float* pVertexData = (float*)malloc((sizeof(float)*(3+2+3)) * pOBJ->faceCount*3);
miniobj_uint32 uniqueVertexCount = 0;
miniobj_face_vertex* pUniqueVertices = (miniobj_face_vertex*)malloc(sizeof(miniobj_face_vertex) * pOBJ->faceCount*3);
for (miniobj_uint32 iFace = 0; iFace < pOBJ->faceCount; ++iFace)
{
for (miniobj_uint32 iFaceVertex = 0; iFaceVertex < 3; ++iFaceVertex)
{
miniobj_uint32 index;
if (!miniobj__find_face_vertex(uniqueVertexCount, pUniqueVertices, pOBJ->pFaces[iFace].v[iFaceVertex], &index))
{
pUniqueVertices[uniqueVertexCount] = pOBJ->pFaces[iFace].v[iFaceVertex];
index = uniqueVertexCount++;
pVertexData[(index*8) + 0] = pOBJ->pPositions[pOBJ->pFaces[iFace].v[iFaceVertex].positionIndex].v[0];
pVertexData[(index*8) + 1] = pOBJ->pPositions[pOBJ->pFaces[iFace].v[iFaceVertex].positionIndex].v[1];
pVertexData[(index*8) + 2] = pOBJ->pPositions[pOBJ->pFaces[iFace].v[iFaceVertex].positionIndex].v[2];
pVertexData[(index*8) + 3] = pOBJ->pTexCoords[pOBJ->pFaces[iFace].v[iFaceVertex].texcoordIndex].v[0];
pVertexData[(index*8) + 4] = pOBJ->pTexCoords[pOBJ->pFaces[iFace].v[iFaceVertex].texcoordIndex].v[1];
pVertexData[(index*8) + 5] = pOBJ->pNormals[pOBJ->pFaces[iFace].v[iFaceVertex].normalIndex].v[0];
pVertexData[(index*8) + 6] = pOBJ->pNormals[pOBJ->pFaces[iFace].v[iFaceVertex].normalIndex].v[1];
pVertexData[(index*8) + 7] = pOBJ->pNormals[pOBJ->pFaces[iFace].v[iFaceVertex].normalIndex].v[2];
vertexCount += 1;
}
pIndexData[indexCount++] = index;
}
}
free(pUniqueVertices);
if (pIndexCountOut) {
*pIndexCountOut = indexCount;
}
if (pVertexCountOut) {
*pVertexCountOut = vertexCount;
}
if (ppIndexDataOut != NULL) {
*ppIndexDataOut = pIndexData;
} else {
free(pIndexData);
}
if (ppVertexDataOut != NULL) {
*ppVertexDataOut = pVertexData;
} else {
free(pIndexData);
}
}
void miniobj_interleave_p3t2n3_material(miniobj* pOBJ, miniobj_uint32 materialIndex, miniobj_uint32* pVertexCountOut, float** ppVertexDataOut, miniobj_uint32* pIndexCountOut, miniobj_uint32** ppIndexDataOut)
{
// When interleaving we want to ensure we don't copy over duplicate vertices. For example, a quad will be made up of two triangles, with two
// vertices being shared by both faces (along the common edge dividing the two triangles). We don't want to duplicate that data, so when creating
// an index for a face, we first want to check that it hasn't already been added.
miniobj_material* pMaterial = &pOBJ->pMaterials[materialIndex];
// Create output buffers large enough to contain the interleaved data.
miniobj_uint32 indexCount = 0;
miniobj_uint32* pIndexData = (miniobj_uint32*)malloc(sizeof(miniobj_uint32) * pMaterial->faceCount*3);
miniobj_uint32 vertexCount = 0;
float* pVertexData = (float*)malloc((sizeof(float)*(3+2+3)) * pMaterial->faceCount*3);
miniobj_uint32 uniqueVertexCount = 0;
miniobj_face_vertex* pUniqueVertices = (miniobj_face_vertex*)malloc(sizeof(miniobj_face_vertex) * pMaterial->faceCount*3);
for (miniobj_uint32 iFace = 0; iFace < pMaterial->faceCount; ++iFace)
{
for (miniobj_uint32 iFaceVertex = 0; iFaceVertex < 3; ++iFaceVertex)
{
miniobj_uint32 index;
if (!miniobj__find_face_vertex(uniqueVertexCount, pUniqueVertices, pOBJ->pFaces[pMaterial->firstFace + iFace].v[iFaceVertex], &index))
{
pUniqueVertices[uniqueVertexCount] = pOBJ->pFaces[pMaterial->firstFace + iFace].v[iFaceVertex];
index = uniqueVertexCount++;
pVertexData[(index*8) + 0] = pOBJ->pPositions[pOBJ->pFaces[pMaterial->firstFace + iFace].v[iFaceVertex].positionIndex].v[0];
pVertexData[(index*8) + 1] = pOBJ->pPositions[pOBJ->pFaces[pMaterial->firstFace + iFace].v[iFaceVertex].positionIndex].v[1];
pVertexData[(index*8) + 2] = pOBJ->pPositions[pOBJ->pFaces[pMaterial->firstFace + iFace].v[iFaceVertex].positionIndex].v[2];
pVertexData[(index*8) + 3] = pOBJ->pTexCoords[pOBJ->pFaces[pMaterial->firstFace + iFace].v[iFaceVertex].texcoordIndex].v[0];
pVertexData[(index*8) + 4] = pOBJ->pTexCoords[pOBJ->pFaces[pMaterial->firstFace + iFace].v[iFaceVertex].texcoordIndex].v[1];
pVertexData[(index*8) + 5] = pOBJ->pNormals[pOBJ->pFaces[pMaterial->firstFace + iFace].v[iFaceVertex].normalIndex].v[0];
pVertexData[(index*8) + 6] = pOBJ->pNormals[pOBJ->pFaces[pMaterial->firstFace + iFace].v[iFaceVertex].normalIndex].v[1];
pVertexData[(index*8) + 7] = pOBJ->pNormals[pOBJ->pFaces[pMaterial->firstFace + iFace].v[iFaceVertex].normalIndex].v[2];
vertexCount += 1;
}
pIndexData[indexCount++] = index;
}
}
free(pUniqueVertices);
if (pIndexCountOut) {
*pIndexCountOut = indexCount;
}
if (pVertexCountOut) {
*pVertexCountOut = vertexCount;
}
if (ppIndexDataOut != NULL) {
*ppIndexDataOut = pIndexData;
} else {
free(pIndexData);
}
if (ppVertexDataOut != NULL) {
*ppVertexDataOut = pVertexData;
} else {
free(pIndexData);
}
}
typedef struct
{
miniobj_read_proc onRead;
miniobj_seek_to_start_proc onSeek;
void* pUserData;
char buffer[4096];
char* pNextBytes;
size_t bytesRemaining;
miniobj_uint32 materialLibCount;
miniobj_uint32 materialCount;
miniobj_uint32 positionCount;
miniobj_uint32 texcoordCount;
miniobj_uint32 normalCount;
miniobj_uint32 faceCount;
size_t totalStringLength; // <-- Includes null terminators.
size_t allocationSize;
} miniobj_load_context;
static inline miniobj_bool32 miniobj__is_whitespace(char c)
{
return c == '\0' || c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r';
}
static inline miniobj_bool32 miniobj__is_valid_digit(char c)
{
return c >= '0' && c <= '9';
}
miniobj_bool32 miniobj__load_next_chunk(miniobj_load_context* pLoadContext)
{
assert(pLoadContext != NULL);
pLoadContext->bytesRemaining = pLoadContext->onRead(pLoadContext->pUserData, pLoadContext->buffer, sizeof(pLoadContext->buffer));
if (pLoadContext->bytesRemaining == 0) {
pLoadContext->pNextBytes = NULL;
return MINIOBJ_FALSE;
}
pLoadContext->pNextBytes = pLoadContext->buffer;
return MINIOBJ_TRUE;
}
miniobj_bool32 miniobj__seek_past_whitespace(miniobj_load_context* pLoadContext)
{
assert(pLoadContext != NULL);
for (;;)
{
while (pLoadContext->bytesRemaining > 0)
{
if (!miniobj__is_whitespace(pLoadContext->pNextBytes[0])) {
return MINIOBJ_TRUE;
}
pLoadContext->pNextBytes += 1;
pLoadContext->bytesRemaining -= 1;
}
if (!miniobj__load_next_chunk(pLoadContext)) {
return MINIOBJ_FALSE;
}
}
}
char* miniobj__find_end_of_line(const miniobj_load_context* pLoadContext)
{
assert(pLoadContext != NULL);
// This function does not load a new chunk. If it runs out of data in the chunk it will return MINIOBJ_FALSE.
char* pLineEnd = pLoadContext->pNextBytes;
size_t bytesRemaining = pLoadContext->bytesRemaining;
while (bytesRemaining > 0)
{
if (pLineEnd[0] == '\0' || pLineEnd[0] == '\n' || (pLineEnd[0] == '\r' && bytesRemaining > 1 && pLineEnd[1] == '\n') || pLineEnd[0] == '#') {
return pLineEnd;
}
pLineEnd += 1;
bytesRemaining -= 1;
}
// If we get here it means we've run out of data in the buffer. We don't want to read the next chunk in this function so
// we return MINIOBJ_FALSE in this case.
return NULL;
}
miniobj_bool32 miniobj__seek_to_next_line(miniobj_load_context* pLoadContext)
{
assert(pLoadContext != NULL);
for (;;)
{
while (pLoadContext->bytesRemaining > 0)
{
if (pLoadContext->pNextBytes[0] == '\n')
{
pLoadContext->pNextBytes += 1;
pLoadContext->bytesRemaining -= 1;
return MINIOBJ_TRUE;
}
else if (pLoadContext->pNextBytes[0] == '\r' && pLoadContext->bytesRemaining > 1 && pLoadContext->pNextBytes[1] == '\n') // Win32 line endings.
{
pLoadContext->pNextBytes += 2;
pLoadContext->bytesRemaining -= 2;
return MINIOBJ_TRUE;
}
else
{
pLoadContext->pNextBytes += 1;
pLoadContext->bytesRemaining -= 1;
}
}
if (!miniobj__load_next_chunk(pLoadContext)) {
return MINIOBJ_FALSE;
}
}
}
miniobj_bool32 miniobj__read_next_line(miniobj_load_context* pLoadContext, char** pLineBeg, char** pLineEnd)
{
assert(pLineBeg != NULL);
assert(pLineEnd != NULL);
// We don't want to include whitespace in the result.
if (!miniobj__seek_past_whitespace(pLoadContext)) {
return MINIOBJ_FALSE;
}
// The entire line must be contained within pLoadContext->buffer.
char* lineBeg = pLoadContext->pNextBytes;
char* lineEnd = miniobj__find_end_of_line(pLoadContext);
if (lineEnd == NULL)
{
// If we get here it means we've either reached the end of the file or ran out of data in the buffer. If we've simply reached the
// end of the file it's not an error. If we've run out of data we need to load the next chunk.
size_t lineLengthSoFar = pLoadContext->bytesRemaining;
memmove(pLoadContext->buffer, lineBeg, lineLengthSoFar);
lineBeg = pLoadContext->buffer;
lineEnd = lineBeg + lineLengthSoFar;
pLoadContext->bytesRemaining = pLoadContext->onRead(pLoadContext->pUserData, pLoadContext->buffer + lineLengthSoFar, sizeof(pLoadContext->buffer) - lineLengthSoFar) + lineLengthSoFar;
if (pLoadContext->bytesRemaining > 0)
{
// At this point we should have the rest of the line in the buffer so we just try finding the end of it again.
pLoadContext->pNextBytes = pLoadContext->buffer;
lineEnd = miniobj__find_end_of_line(pLoadContext);
}
if (lineEnd == NULL)
{
pLoadContext->pNextBytes += pLoadContext->bytesRemaining;
pLoadContext->bytesRemaining = 0;
*pLineBeg = lineBeg;
*pLineEnd = lineBeg + lineLengthSoFar;
return MINIOBJ_TRUE;
}
}
// We don't want to return empty lines, so if the line is empty, just try getting the next one.
if (lineEnd == lineBeg)
{
if (!miniobj__seek_to_next_line(pLoadContext)) {
return MINIOBJ_FALSE;
}
return miniobj__read_next_line(pLoadContext, pLineBeg, pLineEnd);
}
else
{
// At this point we should be good. We need to move to the start of the next line.
size_t lineLength = (lineEnd - lineBeg);
if (lineLength > pLoadContext->bytesRemaining) {
lineLength = pLoadContext->bytesRemaining;
}
pLoadContext->pNextBytes += lineLength;
pLoadContext->bytesRemaining -= lineLength;
miniobj__seek_to_next_line(pLoadContext);
*pLineBeg = lineBeg;
*pLineEnd = lineEnd;
return MINIOBJ_TRUE;
}
}
size_t miniobj__parse_mtl_name(const char* pMtlName, const char* pLineEnd, char* pMtlNameOut)
{
while (pMtlName < pLineEnd && miniobj__is_whitespace(pMtlName[0])) {
pMtlName += 1;
}
const char* pNameBeg = pMtlName;
const char* pLastNonWhitespaceChar = pMtlName;
while (pMtlName[0] != '#' && pMtlName[0] != '\n' && pMtlName < pLineEnd) {
if (!miniobj__is_whitespace(pMtlName[0])) {
pLastNonWhitespaceChar = pMtlName;
}
pMtlName += 1;
}
size_t nameLength = pLastNonWhitespaceChar - pNameBeg + 1;
if (pMtlNameOut != NULL)
{
if (nameLength > 0) {
memcpy(pMtlNameOut, pNameBeg, nameLength);
}
pMtlNameOut[nameLength] = '\0';
}
return nameLength;
}
// Special implementation of atof() for converting a string to a float.
miniobj_bool32 miniobj__atof(const char* str, const char* strEnd, float* pResultOut, const char** strEndOut)
{
// Skip leading whitespace.
while (str < strEnd && miniobj__is_whitespace(*str)) {
str += 1;
}
// Check that we have a string after moving the whitespace.
if (str < strEnd)
{
float sign = 1.0f;
float value = 0.0f;
// Sign.
if (*str == '-')
{
sign = -1.0f;
str += 1;
}
else if (*str == '+')
{
sign = 1.0f;
str += 1;
}
// Digits before the decimal point.
while (str < strEnd && miniobj__is_valid_digit(*str))
{
value = value * 10.0f + (*str - '0');
str += 1;
}
// Digits after the decimal point.
if (*str == '.')
{
float pow10 = 10.0f;
str += 1;
while (str < strEnd && miniobj__is_valid_digit(*str))
{
value += (*str - '0') / pow10;
pow10 *= 10.0f;
str += 1;
}
// Exponential notation.
if (*str == 'e') {
float esign = 1;
float evalue = 0;
str += 1;
if (*str == '-') {
esign = -1;
str += 1;
} else if (*str == '+') {
str += 1;
}
while (str < strEnd && miniobj__is_valid_digit(*str)) {
evalue = evalue * 10.0f + (*str - '0');
str += 1;
}
if (esign < 0) {
value /= powf(10.0f, evalue);
} else {
value *= powf(10.0f, evalue);
}
}
}
if (strEndOut != NULL) {
*strEndOut = str;
}
*pResultOut = sign * value;
return MINIOBJ_TRUE;
}
else
{
// Empty string.
*pResultOut = 0;
return MINIOBJ_FALSE;
}
}
// Parses a single index in a face's p/t/n string.
int miniobj__parse_face_vertex_index(const char* str, const char* strEnd, const char** strEndOut)
{
int sign = 1;
int value = 0;
if (str < strEnd)
{
// Sign.
if (*str == '-')
{
sign = -1;
str += 1;
}
else if (*str == '+')
{
sign = 1;
str += 1;
}
// Value.
while (str < strEnd && miniobj__is_valid_digit(*str))
{
value = value * 10 + (*str - '0');
str += 1;
}
}
if (strEndOut != NULL) {
*strEndOut = str;
}
return sign * value;
}
// Parses a face vertex index string in p/t/n format.
miniobj_bool32 miniobj__parse_face_vertex(const char* str, const char* strEnd, const char** strEndOut, miniobj_face_vertex* pVertexOut)
{
assert(pVertexOut != NULL);
miniobj_face_vertex result;
result.positionIndex = 0;
result.texcoordIndex = 0;
result.normalIndex = 0;
// Skip leading whitespace.
while (str < strEnd && miniobj__is_whitespace(*str)) {
str += 1;
}
// Check that we have a string after moving past the whitespace.
if (str < strEnd)
{
result.positionIndex = miniobj__parse_face_vertex_index(str, strEnd, &str);
if (str[0] == '/') {
str += 1;
result.texcoordIndex = miniobj__parse_face_vertex_index(str, strEnd, &str);
if (str[0] == '/') {
str += 1;
result.normalIndex = miniobj__parse_face_vertex_index(str, strEnd, &str);
}
}
if (strEndOut != NULL) {
*strEndOut = str;
}
*pVertexOut = result;
return MINIOBJ_TRUE;
}
else
{
if (strEndOut != NULL) {
*strEndOut = str;
}
return MINIOBJ_FALSE;
}
}
// Parses a face index string.
miniobj_uint32 miniobj__parse_face(const char* str, const char* strEnd, miniobj_face* pFaceOut)
{
assert(str != NULL);
assert(strEnd != NULL);
assert(pFaceOut != NULL);
miniobj__parse_face_vertex(str, strEnd, &str, pFaceOut->v + 0);
miniobj__parse_face_vertex(str, strEnd, &str, pFaceOut->v + 1);
miniobj__parse_face_vertex(str, strEnd, &str, pFaceOut->v + 2);
miniobj_uint32 faceCount = 3;
if (miniobj__parse_face_vertex(str, strEnd, &str, pFaceOut->v + 3)) {
faceCount += 1;
}
return faceCount;
}
// Parses the value of a "v" element.
void miniobj__parse_v(const char* str, const char* strEnd, miniobj_vec4* pResultOut)
{
assert(str != NULL);
assert(strEnd != NULL);
assert(pResultOut != NULL);
// Format: x y z w
// w is optional.
miniobj__atof(str, strEnd, &pResultOut->v[0], &str);
miniobj__atof(str, strEnd, &pResultOut->v[1], &str);
miniobj__atof(str, strEnd, &pResultOut->v[2], &str);
if (!miniobj__atof(str, strEnd, &pResultOut->v[3], &str)) { // <-- The w component is optional. Defaults to 1.
pResultOut->v[3] = 1;
}
}
// Parses the valid of a "vn" element.
void miniobj__parse_vn(const char* str, const char* strEnd, miniobj_vec3* pResultOut)
{
assert(str != NULL);
assert(strEnd != NULL);
assert(pResultOut != NULL);
// Format: x y z
miniobj__atof(str, strEnd, &pResultOut->v[0], &str);
miniobj__atof(str, strEnd, &pResultOut->v[1], &str);
miniobj__atof(str, strEnd, &pResultOut->v[2], &str);
}
// Parses the valid of a "vt" element.
void miniobj__parse_vt(const char* str, const char* strEnd, miniobj_vec3* pResultOut)
{
assert(str != NULL);
assert(strEnd != NULL);
assert(pResultOut != NULL);
// Format: x y z
// y and z are optional. Both default to zero.
miniobj__atof(str, strEnd, &pResultOut->v[0], &str);
if (!miniobj__atof(str, strEnd, &pResultOut->v[1], &str)) {
pResultOut->v[1] = 0;
pResultOut->v[2] = 0;
return;
}
if (!miniobj__atof(str, strEnd, &pResultOut->v[2], &str)) {
pResultOut->v[2] = 0;
return;
}
}
miniobj_bool32 miniobj__load_stage1(miniobj_load_context* pLoadContext)
{
assert(pLoadContext != NULL);
assert(pLoadContext->onRead != NULL);
char* lineBeg;
char* lineEnd;
while (miniobj__read_next_line(pLoadContext, &lineBeg, &lineEnd)) // <-- This will handle comments and leading whitespace for us.
{
if (lineBeg[0] == 'v' && miniobj__is_whitespace(lineBeg[1]))
{
// Position.
pLoadContext->positionCount += 1;
}
else if (lineBeg[0] == 'v' && lineBeg[1] == 't' && miniobj__is_whitespace(lineBeg[2]))
{
// Texture coordinate.
pLoadContext->texcoordCount += 1;
}