-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjLoader.cpp
1978 lines (1655 loc) · 57.6 KB
/
ObjLoader.cpp
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
/*===================================================================================================
**
** Author : Robert Bateman
** E-Mail : [email protected]
** Brief : This Sourcefile is part of a series explaining how to load and render Alias Wavefront
** Files somewhat efficently. If you are simkply after a reliable Obj Loader, then I would
** Recommend version8; or possibly version9 and the supplied loader for extreme efficency.
**
** Note : This Source Code is provided as is. No responsibility is accepted by myself for any
** damage to hardware or software caused as a result of using this code. You are free to
** make any alterations you see fit to this code for your own purposes, and distribute
** that code either as part of a source code or binary executable package. All I ask is
** for a little credit somewhere for my work!
**
** Any source improvements or bug fixes should be e-mailed to myself so I can update the
** code for the greater good of the community at large. Credit will be given to the
** relevant people as always....
**
**
** Copyright (c) Robert Bateman, www.robthebloke.org, 2004
**
**
** National Centre for Computer Animation,
** Bournemouth University,
** Talbot Campus,
** Bournemouth,
** BH3 72F,
** United Kingdom
** ncca.bournemouth.ac.uk
**
**
===================================================================================================*/
/*===================================================================================================
**
** Includes
**
**=================================================================================================*/
/*
** Need the windows headers to be included for openGL when coding on WIN32
*/
#include <windows.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <malloc.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ObjLoader.h"
#ifndef APIENTRY
# if defined(__CYGWIN__) || defined(__MINGW32__)
# define APIENTRY __stdcall
# elif (_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED)
# define APIENTRY __stdcall
# else
# define APIENTRY
# endif
#endif
#ifndef GLAPIENTRY
#define GLAPIENTRY APIENTRY
#endif
/*===================================================================================================
**
** Vertex Buffer Object Extensions
**
**=================================================================================================*/
#ifndef GL_ARB_vertex_buffer_object
#define GL_ARB_vertex_buffer_object 1
#define GL_BUFFER_SIZE_ARB 0x8764
#define GL_BUFFER_USAGE_ARB 0x8765
#define GL_ARRAY_BUFFER_ARB 0x8892
#define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
#define GL_ARRAY_BUFFER_BINDING_ARB 0x8894
#define GL_ELEMENT_ARRAY_BUFFER_BINDING_ARB 0x8895
#define GL_VERTEX_ARRAY_BUFFER_BINDING_ARB 0x8896
#define GL_NORMAL_ARRAY_BUFFER_BINDING_ARB 0x8897
#define GL_COLOR_ARRAY_BUFFER_BINDING_ARB 0x8898
#define GL_INDEX_ARRAY_BUFFER_BINDING_ARB 0x8899
#define GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING_ARB 0x889A
#define GL_EDGE_FLAG_ARRAY_BUFFER_BINDING_ARB 0x889B
#define GL_SECONDARY_COLOR_ARRAY_BUFFER_BINDING_ARB 0x889C
#define GL_FOG_COORDINATE_ARRAY_BUFFER_BINDING_ARB 0x889D
#define GL_WEIGHT_ARRAY_BUFFER_BINDING_ARB 0x889E
#define GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB 0x889F
#define GL_READ_ONLY_ARB 0x88B8
#define GL_WRITE_ONLY_ARB 0x88B9
#define GL_READ_WRITE_ARB 0x88BA
#define GL_BUFFER_ACCESS_ARB 0x88BB
#define GL_BUFFER_MAPPED_ARB 0x88BC
#define GL_BUFFER_MAP_POINTER_ARB 0x88BD
#define GL_STREAM_DRAW_ARB 0x88E0
#define GL_STREAM_READ_ARB 0x88E1
#define GL_STREAM_COPY_ARB 0x88E2
#define GL_STATIC_DRAW_ARB 0x88E4
#define GL_STATIC_READ_ARB 0x88E5
#define GL_STATIC_COPY_ARB 0x88E6
#define GL_DYNAMIC_DRAW_ARB 0x88E8
#define GL_DYNAMIC_READ_ARB 0x88E9
#define GL_DYNAMIC_COPY_ARB 0x88EA
typedef int GLsizeiptrARB;
typedef int GLintptrARB;
typedef void (GLAPIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
typedef void (GLAPIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, GLsizeiptrARB size, const void* data, GLenum usage);
typedef void (GLAPIENTRY * PFNGLBUFFERSUBDATAARBPROC) (GLenum target, GLintptrARB offset, GLsizeiptrARB size, const void* data);
typedef void (GLAPIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint* buffers);
typedef void (GLAPIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint* buffers);
typedef void (GLAPIENTRY * PFNGLGETBUFFERPARAMETERIVARBPROC) (GLenum target, GLenum pname, GLint* params);
typedef void (GLAPIENTRY * PFNGLGETBUFFERPOINTERVARBPROC) (GLenum target, GLenum pname, GLvoid ** params);
typedef void (GLAPIENTRY * PFNGLGETBUFFERSUBDATAARBPROC) (GLenum target, GLintptrARB offset, GLsizeiptrARB size, void* data);
typedef GLboolean (GLAPIENTRY * PFNGLISBUFFERARBPROC) (GLuint buffer);
typedef GLvoid * (GLAPIENTRY * PFNGLMAPBUFFERARBPROC) (GLenum target, GLenum access);
typedef GLboolean (GLAPIENTRY * PFNGLUNMAPBUFFERARBPROC) (GLenum target);
PFNGLBINDBUFFERARBPROC glBindBufferARB =NULL;
PFNGLBUFFERDATAARBPROC glBufferDataARB =NULL;
PFNGLBUFFERSUBDATAARBPROC glBufferSubDataARB =NULL;
PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB =NULL;
PFNGLGENBUFFERSARBPROC glGenBuffersARB =NULL;
PFNGLGETBUFFERPARAMETERIVARBPROC glGetBufferParameterivARB =NULL;
PFNGLGETBUFFERPOINTERVARBPROC glGetBufferPointervARB =NULL;
PFNGLGETBUFFERSUBDATAARBPROC glGetBufferSubDataARB =NULL;
PFNGLISBUFFERARBPROC glIsBufferARB =NULL;
PFNGLMAPBUFFERARBPROC glMapBufferARB =NULL;
PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB =NULL;
#endif
/*===================================================================================================
**
** Multi-Texturing Extensions
**
**=================================================================================================*/
#ifndef GL_ARB_multitexture
#define GL_ARB_multitexture 1
#define GL_ACTIVE_TEXTURE_ARB 0x84E0
#define GL_CLIENT_ACTIVE_TEXTURE_ARB 0x84E1
#define GL_MAX_ACTIVE_TEXTURES_ARB 0x84E2
#define GL_TEXTURE0_ARB 0x84C0
#define GL_TEXTURE1_ARB 0x84C1
#define GL_TEXTURE2_ARB 0x84C2
#define GL_TEXTURE3_ARB 0x84C3
#define GL_TEXTURE4_ARB 0x84C4
#define GL_TEXTURE5_ARB 0x84C5
#define GL_TEXTURE6_ARB 0x84C6
#define GL_TEXTURE7_ARB 0x84C7
#define GL_TEXTURE8_ARB 0x84C8
#define GL_TEXTURE9_ARB 0x84C9
#define GL_TEXTURE10_ARB 0x84CA
#define GL_TEXTURE11_ARB 0x84CB
#define GL_TEXTURE12_ARB 0x84CC
#define GL_TEXTURE13_ARB 0x84CD
#define GL_TEXTURE14_ARB 0x84CE
#define GL_TEXTURE15_ARB 0x84CF
#define GL_TEXTURE16_ARB 0x84D0
#define GL_TEXTURE17_ARB 0x84D1
#define GL_TEXTURE18_ARB 0x84D2
#define GL_TEXTURE19_ARB 0x84D3
#define GL_TEXTURE20_ARB 0x84D4
#define GL_TEXTURE21_ARB 0x84D5
#define GL_TEXTURE22_ARB 0x84D6
#define GL_TEXTURE23_ARB 0x84D7
#define GL_TEXTURE24_ARB 0x84D8
#define GL_TEXTURE25_ARB 0x84D9
#define GL_TEXTURE26_ARB 0x84DA
#define GL_TEXTURE27_ARB 0x84DB
#define GL_TEXTURE28_ARB 0x84DC
#define GL_TEXTURE29_ARB 0x84DD
#define GL_TEXTURE30_ARB 0x84DE
#define GL_TEXTURE31_ARB 0x84DF
typedef void (APIENTRY * PFNGLACTIVETEXTUREARBPROC) (GLenum texture);
typedef void (APIENTRY * PFNGLCLIENTACTIVETEXTUREARBPROC) (GLenum texture);
typedef void (APIENTRY * PFNGLMULTITEXCOORD1DARBPROC) (GLenum texture, GLdouble s);
typedef void (APIENTRY * PFNGLMULTITEXCOORD1DVARBPROC) (GLenum texture, const GLdouble *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD1FARBPROC) (GLenum texture, GLfloat s);
typedef void (APIENTRY * PFNGLMULTITEXCOORD1FVARBPROC) (GLenum texture, const GLfloat *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD1IARBPROC) (GLenum texture, GLint s);
typedef void (APIENTRY * PFNGLMULTITEXCOORD1IVARBPROC) (GLenum texture, const GLint *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD1SARBPROC) (GLenum texture, GLshort s);
typedef void (APIENTRY * PFNGLMULTITEXCOORD1SVARBPROC) (GLenum texture, const GLshort *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD2DARBPROC) (GLenum texture, GLdouble s, GLdouble t);
typedef void (APIENTRY * PFNGLMULTITEXCOORD2DVARBPROC) (GLenum texture, const GLdouble *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD2FARBPROC) (GLenum texture, GLfloat s, GLfloat t);
typedef void (APIENTRY * PFNGLMULTITEXCOORD2FVARBPROC) (GLenum texture, const GLfloat *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD2IARBPROC) (GLenum texture, GLint s, GLint t);
typedef void (APIENTRY * PFNGLMULTITEXCOORD2IVARBPROC) (GLenum texture, const GLint *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD2SARBPROC) (GLenum texture, GLshort s, GLshort t);
typedef void (APIENTRY * PFNGLMULTITEXCOORD2SVARBPROC) (GLenum texture, const GLshort *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD3DARBPROC) (GLenum texture, GLdouble s, GLdouble t, GLdouble r);
typedef void (APIENTRY * PFNGLMULTITEXCOORD3DVARBPROC) (GLenum texture, const GLdouble *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD3FARBPROC) (GLenum texture, GLfloat s, GLfloat t, GLfloat r);
typedef void (APIENTRY * PFNGLMULTITEXCOORD3FVARBPROC) (GLenum texture, const GLfloat *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD3IARBPROC) (GLenum texture, GLint s, GLint t, GLint r);
typedef void (APIENTRY * PFNGLMULTITEXCOORD3IVARBPROC) (GLenum texture, const GLint *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD3SARBPROC) (GLenum texture, GLshort s, GLshort t, GLshort r);
typedef void (APIENTRY * PFNGLMULTITEXCOORD3SVARBPROC) (GLenum texture, const GLshort *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD4DARBPROC) (GLenum texture, GLdouble s, GLdouble t, GLdouble r, GLdouble q);
typedef void (APIENTRY * PFNGLMULTITEXCOORD4DVARBPROC) (GLenum texture, const GLdouble *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD4FARBPROC) (GLenum texture, GLfloat s, GLfloat t, GLfloat r, GLfloat q);
typedef void (APIENTRY * PFNGLMULTITEXCOORD4FVARBPROC) (GLenum texture, const GLfloat *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD4IARBPROC) (GLenum texture, GLint s, GLint t, GLint r, GLint q);
typedef void (APIENTRY * PFNGLMULTITEXCOORD4IVARBPROC) (GLenum texture, const GLint *v);
typedef void (APIENTRY * PFNGLMULTITEXCOORD4SARBPROC) (GLenum texture, GLshort s, GLshort t, GLshort r, GLshort q);
typedef void (APIENTRY * PFNGLMULTITEXCOORD4SVARBPROC) (GLenum texture, const GLshort *v);
PFNGLACTIVETEXTUREARBPROC glActiveTextureARB = NULL;
PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB = NULL;
#endif /* GL_ARB_multitexture */
/*===================================================================================================
**
** Cube Map Extensions
**
**=================================================================================================*/
#ifndef GL_ARB_texture_cube_map
#define GL_ARB_texture_cube_map 1
#define GL_NORMAL_MAP_ARB 0x8511
#define GL_REFLECTION_MAP_ARB 0x8512
#define GL_TEXTURE_CUBE_MAP_ARB 0x8513
#define GL_TEXTURE_BINDING_CUBE_MAP_ARB 0x8514
#define GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB 0x8515
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB 0x8516
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB 0x8517
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB 0x8518
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB 0x8519
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB 0x851A
#define GL_PROXY_TEXTURE_CUBE_MAP_ARB 0x851B
#define GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB 0x851C
#endif /* GL_ARB_texture_cube_map */
/*===================================================================================================
**
** Texture Combiner Extensions
**
**=================================================================================================*/
#ifndef GL_EXT_texture_env_combine
#define GL_EXT_texture_env_combine 1
#define GL_COMBINE_EXT 0x8570
#define GL_COMBINE_RGB_EXT 0x8571
#define GL_COMBINE_ALPHA_EXT 0x8572
#define GL_SOURCE0_RGB_EXT 0x8580
#define GL_SOURCE1_RGB_EXT 0x8581
#define GL_SOURCE2_RGB_EXT 0x8582
#define GL_SOURCE0_ALPHA_EXT 0x8588
#define GL_SOURCE1_ALPHA_EXT 0x8589
#define GL_SOURCE2_ALPHA_EXT 0x858A
#define GL_OPERAND0_RGB_EXT 0x8590
#define GL_OPERAND1_RGB_EXT 0x8591
#define GL_OPERAND2_RGB_EXT 0x8592
#define GL_OPERAND0_ALPHA_EXT 0x8598
#define GL_OPERAND1_ALPHA_EXT 0x8599
#define GL_OPERAND2_ALPHA_EXT 0x859A
#define GL_RGB_SCALE_EXT 0x8573
#define GL_ADD_SIGNED_EXT 0x8574
#define GL_INTERPOLATE_EXT 0x8575
#define GL_CONSTANT_EXT 0x8576
#define GL_PRIMARY_COLOR_EXT 0x8577
#define GL_PREVIOUS_EXT 0x8578
#endif /* GL_EXT_texture_env_combine */
/*===================================================================================================
**
** Texture Env Dot3 Extensions
**
**=================================================================================================*/
#ifndef GL_EXT_texture_env_dot3
#define GL_EXT_texture_env_dot3 1
#define GL_DOT3_RGB_EXT 0x8740
#define GL_DOT3_RGBA_EXT 0x8741
#endif
/*===================================================================================================
**
** OpenGL Extension Initialisation
**
**=================================================================================================*/
#ifdef WIN32
void InitOpenGL_Extensions()
{
if (!glBindBufferARB)
{
const char* ext = (const char*) glGetString(GL_EXTENSIONS);
unsigned int i=0;
while (ext[i] != '\0') {
if (ext[i] == ' ') {
putc('\n',stdout);
} else
putc(ext[i],stdout);
++i;
}
glBindBufferARB = (PFNGLBINDBUFFERARBPROC) wglGetProcAddress("glBindBufferARB");
glBufferDataARB = (PFNGLBUFFERDATAARBPROC) wglGetProcAddress("glBufferDataARB");
glBufferSubDataARB = (PFNGLBUFFERSUBDATAARBPROC) wglGetProcAddress("glBufferSubDataARB");
glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC) wglGetProcAddress("glDeleteBuffersARB");
glGenBuffersARB = (PFNGLGENBUFFERSARBPROC) wglGetProcAddress("glGenBuffersARB");
glGetBufferParameterivARB = (PFNGLGETBUFFERPARAMETERIVARBPROC)wglGetProcAddress("glGetBufferParameterivARB");
glGetBufferPointervARB = (PFNGLGETBUFFERPOINTERVARBPROC) wglGetProcAddress("glGetBufferPointervARB");
glGetBufferSubDataARB = (PFNGLGETBUFFERSUBDATAARBPROC) wglGetProcAddress("glGetBufferSubDataARB");
glIsBufferARB = (PFNGLISBUFFERARBPROC) wglGetProcAddress("glIsBufferARB");
glMapBufferARB = (PFNGLMAPBUFFERARBPROC) wglGetProcAddress("glMapBufferARB");
glUnmapBufferARB = (PFNGLUNMAPBUFFERARBPROC) wglGetProcAddress("glUnmapBufferARB");
glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) wglGetProcAddress("glActiveTextureARB");
glClientActiveTextureARB = (PFNGLCLIENTACTIVETEXTUREARBPROC) wglGetProcAddress("glClientActiveTextureARB");
if (!glBindBufferARB)
{
fprintf(stderr,"[ERROR] This hardware does not seem to support Vertex Buffer Objects. You might want to ask for just vertex arrays next time....\n");
exit(0);
}
if (!glActiveTextureARB)
{
fprintf(stderr,"[ERROR] This hardware does not seem to support multi-texturing. Buy a new graphics card!\n");
exit(0);
}
}
}
#else
void InitOpenGL_Extensions()
{
}
#endif
/*===================================================================================================
**
** Internal Data Structures
**
**=================================================================================================*/
/*===================================================================================================
**
** Global Variables
**
**=================================================================================================*/
/*
** The global head of the linked list of meshes. This is a linked list because it is possible that you will be
** loading and deleting meshes during the course of the programs execution.
*/
ObjMesh *g_LinkedListHead = NULL;
/*
** This is used to generate a unique ID for each Obj File
*/
unsigned int g_ObjIdGenerator=0;
/*
** This function will take a pointer to the mesh and the number of extra triangles that
** are required. It will then resize the polygon face array within the mesh and return
** the index of the first new face that was allocated.
*/
unsigned int ResizeTriangleArray(ObjMesh *pMesh,unsigned int iExtraTriangleCount)
{
/*
** Create a temporary pointer to the old face array in the mesh
*/
ObjFace *pTempFaces = pMesh->m_aFaces;
/*
** Keep track of the old face count
*/
unsigned int iTemp = pMesh->m_iNumberOfFaces;
/*
** Increase the number of faces that will be stored in the mesh
*/
pMesh->m_iNumberOfFaces += iExtraTriangleCount;
/*
** Allocate the number of faces now required by this mesh. Note, you could use
** realloc for this, but it's just my personal preference to do it this way.
*/
pMesh->m_aFaces = (ObjFace*)malloc(pMesh->m_iNumberOfFaces*sizeof(ObjFace));
/*
** If we assert here then the memory allocation for the meshes failed
*/
assert(pMesh->m_aFaces);
/*
** Copy across the data that was stored in the old array
*/
memcpy(pMesh->m_aFaces,pTempFaces,iTemp*sizeof(ObjFace));
/*
** Delete the data held in the previous array
*/
free(pTempFaces);
memset(&pMesh->m_aFaces[ iTemp ],0,iExtraTriangleCount*sizeof(ObjFace) );
/*
** return the old size of the array (thats the same as the index of the first new triangle).
*/
return iTemp;
}
/*
** This function is only called from within the *.c file and is used to create an ObjMesh structure and
** initialise its values (adds the mesh to the linked list also).
*/
ObjMesh *MakeOBJ( void )
{
/*
** The pointer we will create the mesh at the end of
*/
ObjMesh *pMesh = NULL;
pMesh = (ObjMesh*) malloc (sizeof(ObjMesh));
/* If the program asserts here, then there was a memory allocation failure */
assert(pMesh);
/*
** Initialise all pointers to NULL
*/
pMesh->m_aFaces = NULL;
pMesh->m_aNormalArray = NULL;
pMesh->m_aTangentArray = NULL;
pMesh->m_aBiNormalArray = NULL;
pMesh->m_aTexCoordArray = NULL;
pMesh->m_aColourArray = NULL;
pMesh->m_aVertexArray = NULL;
pMesh->m_aIndices = NULL;
pMesh->m_iNumberOfFaces = 0;
pMesh->m_iNumberOfNormals = 0;
pMesh->m_iNumberOfTexCoords = 0;
pMesh->m_iNumberOfVertices = 0;
pMesh->m_iMode = 0;
pMesh->m_iMeshID = ++g_ObjIdGenerator;
/*
** Insert the mesh at the beginning of the linked list
*/
pMesh->m_pNext = g_LinkedListHead;
g_LinkedListHead = pMesh;
return pMesh;
}
void GetIndices(char *pString, unsigned int *pVert, unsigned int *pNorm, unsigned int *pUV)
{
/*
** Use 3 pointers to hold references to the bits of the string where the indices start
** for the vertex, normal and texturing co-ordinate respectivly
*/
char *pV = pString,
*pN = NULL,
*pT = NULL;
/*
** Walk through the string a character at a time looking for the '/' character. The first one
** we find will may have the uv - coord index behind it. The '/' character will be removed by
** setting it to NULL, If for example the texturing co-ord is missing, then you'd expect "//" to
** appear. This while loop would set both of those to NULL, that would give you a NULL string
** for the uv coord. This allows us to test the string later to see if there is or isn't a
** value there.
*/
while( *pString != '\0' )
{
if(*pString == '/')
{
*pString = '\0';
if(!pT)
{
pT = pString+1;
}
else
{
pN = pString+1;
}
}
pString++;
}
/*
** Read the vertex index
*/
sscanf(pV,"%d",pVert);
/*
** If a normal is there, then read it's value, otherwise set the default of 1
** (1 is used because we will decrease the value by 1 later thus giving us 0)
*/
if( pN == NULL || *pN == '\0' )
{
*pNorm = 1;
}
else
{
sscanf(pN,"%d",pNorm);
}
/*
** If a texture co-ordinate is there, then read it's value, otherwise set the default of 1
** (1 is used because we will decrease the value by 1 later thus giving us 0)
*/
if( pT == NULL || *pT == '\0' )
{
*pUV = 1;
}
else
{
sscanf(pT,"%d",pUV);
}
}
ObjMesh* LoadOBJ(const char *filename)
{
ObjMesh *pMesh=NULL;
unsigned int vc=0,nc=0,tc=0,fc=0;
char buffer[256];
FILE *fp = NULL;
printf("\nLoading the OBJ %s", filename);
/*
** Open the file for reading
*/
fp = fopen(filename,"r");
/*
** If the program asserted here, then the file could not be found or opened
*/
//assert(fp);
//ADDED BY CHRIS - print out a comment instead
if (fp == NULL)
{
printf("\nError - couldn't find file %s", filename);
return NULL;
}
/*
** Create the mesh structure and add it to the linked list
*/
pMesh = MakeOBJ();
/*
** Run through the whole file looking for the various flags so that we can count
** up how many data elements there are. This is done so that we can make one memory
** allocation for the meshes data and then run through the file once more, this time
** reading in the data. It's purely done to reduce system overhead of memory allocation due
** to otherwise needing to reallocate data everytime we read in a new element.
*/
while(!feof(fp))
{
memset(buffer,0,200);
/* Grab a line at a time */
fgets(buffer,256,fp);
/* look for the 'vn' - vertex normal - flag */
if( strncmp("vn ",buffer,3) == 0 )
{
++pMesh->m_iNumberOfNormals;
}
else
/* look for the 'vt' - texturing co-ordinate - flag */
if( strncmp("vt ",buffer,3) == 0 )
{
++pMesh->m_iNumberOfTexCoords;
}
else
/* look for the 'v ' - vertex co-ordinate - flag */
if( strncmp("v ",buffer,2) == 0 )
{
++pMesh->m_iNumberOfVertices;
}
else
/* look for the 'f ' - face - flag */
if( strncmp("f ",buffer,2) == 0 )
{
++pMesh->m_iNumberOfFaces;
}
}
/*
** close the file for now....
*/
fclose(fp);
/*
** Allocate the memory for the data arrays and check that it allocated ok
*/
pMesh->m_aVertexArray = (ObjVertex* )malloc( pMesh->m_iNumberOfVertices * sizeof(ObjVertex) );
assert(pMesh->m_aVertexArray);
/* there are occasionally times when the obj does not have any normals in it */
if( pMesh->m_iNumberOfNormals > 0 )
{
pMesh->m_aNormalArray = (ObjNormal* )malloc( pMesh->m_iNumberOfNormals * sizeof(ObjNormal) );
assert(pMesh->m_aNormalArray);
}
/* there are occasionally times when the obj does not have any tex coords in it */
if( pMesh->m_iNumberOfTexCoords > 0 )
{
pMesh->m_aTexCoordArray = (ObjTexCoord*)malloc( pMesh->m_iNumberOfTexCoords * sizeof(ObjTexCoord) );
assert(pMesh->m_aTexCoordArray);
}
pMesh->m_aFaces = (ObjFace* )malloc( pMesh->m_iNumberOfFaces * sizeof(ObjFace) );
assert(pMesh->m_aFaces);
/*
** Now we know how much data is contained in the file and we've allocated memory to hold it all.
** What we can therefore do now, is load up all of the data in the file easily.
*/
fp = fopen(filename,"r");
while(!feof(fp))
{
memset(buffer,0,255);
/* Grab a line at a time */
fgets(buffer,256,fp);
/* look for the 'vn' - vertex normal - flag */
if( strncmp("vn ",buffer,3) == 0 )
{
sscanf((buffer+2),"%f%f%f",
&pMesh->m_aNormalArray[ nc ].x,
&pMesh->m_aNormalArray[ nc ].y,
&pMesh->m_aNormalArray[ nc ].z);
++nc;
}
else
/* look for the 'vt' - texturing co-ordinate - flag */
if( strncmp("vt ",buffer,3) == 0 )
{
sscanf((buffer+2),"%f%f",
&pMesh->m_aTexCoordArray[ tc ].u,
&pMesh->m_aTexCoordArray[ tc ].v);
++tc;
}
else
/* look for the 'v ' - vertex co-ordinate - flag */
if( strncmp("v ",buffer,2) == 0 )
{
sscanf((buffer+1),"%f%f%f",
&pMesh->m_aVertexArray[ vc ].x,
&pMesh->m_aVertexArray[ vc ].y,
&pMesh->m_aVertexArray[ vc ].z);
++vc;
}
else
/* look for the 'f ' - face - flag */
if( strncmp("f ",buffer,2) == 0 )
{
/*
** some data for later....
*/
char *pSplitString = NULL;
unsigned int i,ii = 0;
unsigned int iNewTrianglesIndex = 0,
iNumberOfExtraTriangles = 0;
unsigned int aFirstIndices[3],
aSecondIndices[3];
ObjFace *pf = NULL;
/*
** These next few lines are used to figure out how many '/' characters there
** are in the string. This gives us the information we need to find out how
** many vertices are used in this face (by dividing by two)
*/
for(i=0;i<strlen(buffer);i++)
{
if(buffer[i] == '/')
ii++;
}
ii/=2;
/*
** This is where it all gets confusing, What I'm going to do is to convert all polys
** With vertices above 3 to triangles. The number of triangles I require will be (ii-2).
** For example, with a poly that has vertices a,b,c,d,e; there will be 3 triangles comprised
** of verts (a,b,c), (a,c,d) and (a,d,e) respectively. We need to check the amount of triangles
** that will be needed for this polygon and resize the array accordingly.
*/
if( (ii-2) > 1 )
{
iNumberOfExtraTriangles = ii-3;
iNewTrianglesIndex = ResizeTriangleArray(pMesh,iNumberOfExtraTriangles);
}
/*
** Pointer to the face we are currently dealing with. It's only used so that
** the code becomes more readable and I have less to type.
*/
pf = &pMesh->m_aFaces[ fc ];
/*
** tokenise the string using strtok(). Basically this splits the string up
** and removes the spaces from each chunk. This way we only have to deal with
** one set of indices at a time for each of the poly's vertices.
*/
pSplitString = strtok((buffer+2)," \t\n");
i=0;
/*
** Get the vertex structures that we know WILL exists (there has to be a minimum of three)
*/
for(i=0;i<3;i++)
{
GetIndices( pSplitString,
&pf->m_aVertexIndices[i],
&pf->m_aNormalIndices[i],
&pf->m_aTexCoordIndicies[i]);
/* need to reduce the indices by 1 because array indices start at 0, obj starts at 1 */
--pf->m_aTexCoordIndicies[i];
--pf->m_aNormalIndices[i];
--pf->m_aVertexIndices[i];
/*
** In order to start constructing our triangles that are created after we split the poly,
** we need to store some of the indices that are going to be used for the extra additional
** triangles after this. Basically we store the first and last sets of indices from this
** first triangle becasue they will be used as the first & second sets for the next triangle.
*/
switch(i)
{
case 0:
aFirstIndices[0] = pf->m_aVertexIndices[i];
aFirstIndices[1] = pf->m_aNormalIndices[i];
aFirstIndices[2] = pf->m_aTexCoordIndicies[i];
break;
case 2:
aSecondIndices[0] = pf->m_aVertexIndices[i];
aSecondIndices[1] = pf->m_aNormalIndices[i];
aSecondIndices[2] = pf->m_aTexCoordIndicies[i];
break;
default:
break;
}
pSplitString = strtok(NULL," \t\n");
}
++fc;
/*
** Get the additional triangles that may be there
*/
for(i=0;i<iNumberOfExtraTriangles;i++)
{
pf = &pMesh->m_aFaces[ iNewTrianglesIndex + i ];
GetIndices( pSplitString,
&pf->m_aVertexIndices[2],
&pf->m_aNormalIndices[2],
&pf->m_aTexCoordIndicies[2]);
/* need to reduce the indices by 1 because array indices start at 0, obj starts at 1 */
--pf->m_aTexCoordIndicies[2];
--pf->m_aNormalIndices[2];
--pf->m_aVertexIndices[2];
/*
** Now assign the other indices for the triangle
*/
pf->m_aVertexIndices[0] = aFirstIndices[0];
pf->m_aNormalIndices[0] = aFirstIndices[1];
pf->m_aTexCoordIndicies[0] = aFirstIndices[2];
pf->m_aVertexIndices[1] = aSecondIndices[0];
pf->m_aNormalIndices[1] = aSecondIndices[1];
pf->m_aTexCoordIndicies[1] = aSecondIndices[2];
/*
** Copy over the indices for the next triangle.
*/
aSecondIndices[0] = pf->m_aVertexIndices[2];
aSecondIndices[1] = pf->m_aNormalIndices[2];
aSecondIndices[2] = pf->m_aTexCoordIndicies[2];
pSplitString = strtok(NULL," \t\n");
}
}
}
fclose(fp);
return pMesh;
}
/*
** This function generates normals for the specified mesh. The data MUST
** be stored in vertex arrays for this routine to work.
*/
void CalculateNormalArray(ObjMesh* pMesh)
{
{
unsigned int* f = pMesh->m_aIndices;
unsigned int* end = f + (pMesh->m_iNumberOfFaces);
memset(pMesh->m_aNormalArray,0,sizeof(ObjNormal)*pMesh->m_iVBO_Size);
/*
* Loop through each face in the mesh
*/
for( ; f != end; f+=3 )
{
ObjVertex* v1 = pMesh->m_aVertexArray + f[0];
ObjVertex* v2 = pMesh->m_aVertexArray + f[1];
ObjVertex* v3 = pMesh->m_aVertexArray + f[2];
ObjNormal* n1 = pMesh->m_aNormalArray + f[0];
ObjNormal* n2 = pMesh->m_aNormalArray + f[1];
ObjNormal* n3 = pMesh->m_aNormalArray + f[2];
ObjNormal e1 = {
v1->x - v2->x,
v1->y - v2->y,
v1->z - v2->z
};
ObjNormal e2 = {
v3->x - v2->x,
v3->y - v2->y,
v3->z - v2->z
};
ObjNormal e1_cross_e2 = {
e2.y * e1.z - e2.z * e1.y,
e2.z * e1.x - e2.x * e1.z,
e2.x * e1.y - e2.y * e1.x
};
float it = 1.0f/((float)sqrt( e1_cross_e2.x*e1_cross_e2.x +
e1_cross_e2.y*e1_cross_e2.y +
e1_cross_e2.z*e1_cross_e2.z ));
e1_cross_e2.x *= it;
e1_cross_e2.y *= it;
e1_cross_e2.z *= it;
n1->x += e1_cross_e2.x;
n1->y += e1_cross_e2.y;
n1->z += e1_cross_e2.z;
n2->x += e1_cross_e2.x;
n2->y += e1_cross_e2.y;
n2->z += e1_cross_e2.z;
n3->x += e1_cross_e2.x;
n3->y += e1_cross_e2.y;
n3->z += e1_cross_e2.z;
}
}
{
ObjNormal* n = pMesh->m_aNormalArray;
ObjNormal* end = n + pMesh->m_iVBO_Size;
for( ; n != end; ++n )
{
float it = 1.0f/((float)sqrt( n->x * n->x +
n->y * n->y +
n->z * n->z ));
n->x *= it;
n->y *= it;
n->z *= it;
}
}
}
/*
** This function generates tangents for the specified mesh.
** The data MUST be stored in vertex arrays for this routine to work.
*/
void CalculateTangentArray(ObjMesh* pMesh)
{
unsigned int a,count;
/* generate a temporary array for the S & T vectors */
ObjVertex *tan1 = (ObjVertex*)malloc( pMesh->m_iVBO_Size * 2 * sizeof(ObjVertex) );
/* The 2nd array */
ObjVertex *tan2 = tan1 + pMesh->m_iVBO_Size;
/* zero the tangent memory */
memset(tan1, 0, pMesh->m_iVBO_Size * sizeof(ObjVertex) * 2);
/* loop through each triangle to calculate the S & T vectors on that triangle */
for ( a = 0; a < pMesh->m_iNumberOfFaces; a+=3)
{
/*
* get 3 indicies into the vertex array for the triangle
*/
unsigned int i1 = pMesh->m_aIndices[a+0];
unsigned int i2 = pMesh->m_aIndices[a+1];
unsigned int i3 = pMesh->m_aIndices[a+2];
/*
* Get pointers to vertices & texcoords used in the triangle
*/
ObjVertex* v1 = pMesh->m_aVertexArray + i1;
ObjVertex* v2 = pMesh->m_aVertexArray + i2;
ObjVertex* v3 = pMesh->m_aVertexArray + i3;
ObjTexCoord* w1 = pMesh->m_aTexCoordArray + i1;
ObjTexCoord* w2 = pMesh->m_aTexCoordArray + i2;
ObjTexCoord* w3 = pMesh->m_aTexCoordArray + i3;
/*
* Calculate the S & T vectors
*/
float x1 = v2->x - v1->x;
float x2 = v3->x - v1->x;
float y1 = v2->y - v1->y;
float y2 = v3->y - v1->y;
float z1 = v2->z - v1->z;
float z2 = v3->z - v1->z;
float s1 = w2->u - w1->u;
float s2 = w3->u - w1->u;
float t1 = w2->v - w1->v;
float t2 = w3->v - w1->v;
float r = 1.0F / (s1 * t2 - s2 * t1);
ObjVertex sdir = {
(t2 * x1 - t1 * x2) * r,
(t2 * y1 - t1 * y2) * r,
(t2 * z1 - t1 * z2) * r
};
ObjVertex tdir = {
(s1 * x2 - s2 * x1) * r,
(s1 * y2 - s2 * y1) * r,
(s1 * z2 - s2 * z1) * r
};
tan1[i1].x += sdir.x;
tan1[i1].y += sdir.y;
tan1[i1].z += sdir.z;
tan1[i2].x += sdir.x;
tan1[i2].y += sdir.y;
tan1[i2].z += sdir.z;