-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathglmodel.cpp
More file actions
1793 lines (1606 loc) · 52.2 KB
/
glmodel.cpp
File metadata and controls
1793 lines (1606 loc) · 52.2 KB
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
#include <cmath>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <glmodel.h>
#include <gleasymath.h>
#include<QImage>
#include<QFileInfo>
#ifndef GL_PI
#define GL_PI 3.14159265358979323846
#endif
/* defines */
#define T(x) model->triangles[(x)]
/* 枚举值代表坐标 */
enum { X, Y, Z, W }; //节点元素
//通用节点
typedef struct _GLMnode {
int index;
bool averaged;
struct _GLMnode* next;
} GLMnode;
/* 为字符串分配内存 */
static char* stralloc(const char *string)
{
char *copy;
copy = (char*)malloc(strlen(string) + 1);
if (copy == NULL)
return NULL;
strcpy(copy, string);
return copy;
}
/* _glmWeldVectors: eliminate (weld) vectors that are within an
* epsilon of each other.//在阈值内消除或者合并????
*
* vectors - array of GLfloat[3]'s to be welded
* numvectors - number of GLfloat[3]'s in vectors
* epsilon - maximum difference between vectors
*
*/
GLfloat* _glmWeldVectors(GLfloat* vectors, GLuint* numvectors, GLfloat epsilon)
{
GLfloat* copies;
GLuint copied;
GLuint i, j;
copies = (GLfloat*)malloc(sizeof(GLfloat) * 3 * (*numvectors + 1));
memcpy(copies, vectors, (sizeof(GLfloat) * 3 * (*numvectors + 1)));
copied = 1;
for (i = 1; i <= *numvectors; i++) {
for (j = 1; j <= copied; j++) {
if (_glmEqual(&vectors[3 * i], &copies[3 * j], epsilon)) {
goto duplicate;
}
}
/* must not be any duplicates -- add to the copies array */
copies[3 * copied + 0] = vectors[3 * i + 0];
copies[3 * copied + 1] = vectors[3 * i + 1];
copies[3 * copied + 2] = vectors[3 * i + 2];
j = copied; /* pass this along for below */
copied++;
duplicate:
/* set the first component of this vector to point at the correct
index into the new copies array */
vectors[3 * i + 0] = (GLfloat)j;
}
*numvectors = copied - 1;
return copies;
}
/* glmWeld: eliminate (weld) vectors that are within an epsilon of
* each other.
*
* model - initialized GLMmodel structure
* epsilon - maximum difference between vertices
* ( 0.00001 is a good start for a unitized model)
*
*/
GLvoid glmWeld(GLMmodel* model, GLfloat epsilon)
{
GLfloat* vectors;
GLfloat* copies;
GLuint numvectors;
GLuint i;
/* vertices */
numvectors = model->numvertices;
vectors = model->vertices;
copies = _glmWeldVectors(vectors, &numvectors, epsilon);
printf("glmWeld(): %d redundant vertices.\n",
model->numvertices - numvectors - 1);
for (i = 0; i < model->numtriangles; i++) {
T(i).vindices[0] = (int)vectors[3 * T(i).vindices[0] + 0];
T(i).vindices[1] = (int)vectors[3 * T(i).vindices[1] + 0];
T(i).vindices[2] = (int)vectors[3 * T(i).vindices[2] + 0];
}
/* free space for old vertices */
free(vectors);
/* allocate space for the new vertices */
model->numvertices = numvectors;
model->vertices = (GLfloat*)malloc(sizeof(GLfloat) *
3 * (model->numvertices + 1));
/* copy the optimized vertices into the actual vertex list */
for (i = 1; i <= model->numvertices; i++) {
model->vertices[3 * i + 0] = copies[3 * i + 0];
model->vertices[3 * i + 1] = copies[3 * i + 1];
model->vertices[3 * i + 2] = copies[3 * i + 2];
}
free(copies);
}
//找到模型中的组
GLMgroup* _glmFindGroup(GLMmodel* model, char* name)
{
GLMgroup* group;
assert(model);
group = model->groups;
while (group) {
if (!strcmp(name, group->name))//strcmp相等返回0,不等返回1
break;
group = group->next;
}
return group;
}
//模型中添加一个组,如果为null则新建一个组
GLMgroup* _glmAddGroup(GLMmodel* model, char* name)
{
GLMgroup* group;
group = _glmFindGroup(model, name);
if (!group) {
group = (GLMgroup*)malloc(sizeof(GLMgroup));
group->name = stralloc(name);
group->material = 0;
group->numtriangles = 0;
group->triangles = NULL;
group->next = model->groups;//将模型的当前组索引赋给该组下一个组(实际是上一个)
model->groups = group;//该组赋给模型的当前组索引
model->numgroups++;
}
return group;
}
//在模型中找到材质
int _glmFindMaterial(GLMmodel* model, char* name)
{
GLuint i;
for (i = 0; i < model->nummaterials; i++) {
if (!strcmp(model->materials[i].name, name))
goto found;
}
/* didn't find the name, so set it as the default material */
printf("_glmFindMaterial(): can't find material \"%s\".\n", name);
i = 0;
found:
return i;
}
//返回指定路径的文件夹
static char* _glmDirName(char* path)
{
char* dir;
char* s;
dir = stralloc(path);
s = strrchr(dir, '/');//查找字符在指定字符串中从左面开始的最后一次出现的位置,如果成功,返回该字符以及其后面的字符
if (s)
s[1] = '\0';
else
dir[0] = '\0';
return dir;
}
/* //ppm格式纹理文件
返回值:是否正确读取ppm
map_Kd Image\001.png
*/
int _glmReadPPM(GLuint textureArray[], char* filename, int textureID)
{
int h, w, i;
//int no_read;
char buf[200], temp[80];
FILE *fp;
static int _teximageWidth, _teximageHeight;
GLubyte *_teximage;
if ((fp = fopen(filename, "r")) == NULL) {
printf("File cannot open!!\n");
return 0;
}
else {
fscanf(fp, "%s\n", buf); /* GET "P6" */
fscanf(fp, "%s", temp);
while (temp[0] == '#') {
while (fgetc(fp) != '\n');
fscanf(fp, "%s", temp);
}
w = atoi(temp);//是把字符串转换成整型数的一个函数
fscanf(fp, " %d\n", &h);
fscanf(fp, "%s\n", temp);
_teximage = (GLubyte*)malloc(sizeof(GLubyte)*w*h * 3);
for (i = 0; i < w*h * 3; i++) {
_teximage[i] = (GLubyte)fgetc(fp);
}
_teximageWidth = w;
_teximageHeight = h;
fclose(fp);
/*
* Generate a texture with the associative texture ID stored in
* the array, sets the alignment requirements for the start of each pixel
* row in memory. Bind the texture to the texture arrays index and
* init the texture
*/
glGenTextures(1, &textureArray[textureID]);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, textureArray[textureID]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, w, h, GL_RGB,
GL_UNSIGNED_BYTE, (GLvoid*)_teximage);
printf("texture %d : %s is loaded\n", textureArray[textureID], filename);
/*
glTexImage2D(GL_TEXTURE_2D, 0, 3, w, h, 0, GL_RGB,
GL_UNSIGNED_BYTE, (GLvoid*)_teximage);
*/
free(_teximage);
}
return 1;
}
// 装化为最接近的2的阶乘数
int RoundUpToTheNextHighestPowerOf2(unsigned int v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
//重写读取格式文件的函数,将读取到的有效路径保存到一个集合中去
void _glmReadPNG(GLMmodel* model, GLuint textureArray[], char* filename, int textureID)
{
FILE *fp;
if ((fp = fopen(filename, "r")) == NULL) {
printf("File cannot open!!\n");
}
else
{
glEnable(GL_TEXTURE_2D);
GLint MaxTextureSize;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &MaxTextureSize);
QImage img, imgScaled, imgGL;
QFileInfo fi(QString::fromLocal8Bit(filename));
QString filaPath = fi.absoluteFilePath();
bool res = img.load(fi.absoluteFilePath());
if (res)
{
int bestW = RoundUpToTheNextHighestPowerOf2(img.width());
int bestH = RoundUpToTheNextHighestPowerOf2(img.height());
while (bestW > MaxTextureSize) bestW /= 2;
while (bestH > MaxTextureSize) bestH /= 2;
imgScaled = img.scaled(bestW, bestH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
//imgGL = convertToGLFormat(imgScaled);
imgGL = imgScaled;
glGenTextures(1, (GLuint*)&(textureArray[textureID]));
glBindTexture(GL_TEXTURE_2D, (GLuint)textureArray[textureID]);
//glTexImage2D(GL_TEXTURE_2D, 0, 3, imgGL.width(), imgGL.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, imgGL.bits());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imgGL.width(), imgGL.height(), GL_RGBA, GL_UNSIGNED_BYTE, imgGL.bits());
}
glDisable(GL_TEXTURE_2D);
fclose(fp);
}
}
//读取mtl贴图格式文件
static void _glmReadMTL(GLMmodel* model, char* name)
{
FILE* file;
char* dir;
char* filename;
char* picturefilename;
char buf[128];
GLuint nummaterials, i;
dir = _glmDirName(model->pathname);
filename = (char*)malloc(sizeof(char) * (strlen(dir) + strlen(name) + 1));
strcpy(filename, dir);//复制
strcat(filename, name);//连接字符串获得贴图文件路径
//free(dir);//释放分配的内存空间
/* 只读方式打开文件 */
file = fopen(filename, "r");//r+是读写方式打开
if (!file) {
fprintf(stderr, "_glmReadMTL() failed: can't open material file \"%s\".\n",
filename);
exit(1);
}
free(filename);//释放文件名路径资源
/* 计算材质的个数 */
nummaterials = 1;//从1开始方便后面分配内存
while (fscanf(file, "%s", buf) != EOF) {
switch (buf[0]) {
case '#':
fgets(buf, sizeof(buf), file);
break;
case 'n': /* newmtl */
fgets(buf, sizeof(buf), file);
nummaterials++;
sscanf(buf, "%s %s", buf, buf);
break;
default:
fgets(buf, sizeof(buf), file);
break;
}
}
rewind(file);//重置指针到文件流的头部
/* 为材质对象分配内存 */
model->materials = (GLMmaterial*)malloc(sizeof(GLMmaterial) * nummaterials);
model->nummaterials = nummaterials;
/* 设置默认材质 */
for (i = 0; i < nummaterials; i++) {
model->materials[i].name = NULL;
model->materials[i].shininess = 0;
model->materials[i].diffuse[0] = 0.8;
model->materials[i].diffuse[1] = 0.8;
model->materials[i].diffuse[2] = 0.8;
model->materials[i].diffuse[3] = 1.0;
model->materials[i].ambient[0] = 0.2;
model->materials[i].ambient[1] = 0.2;
model->materials[i].ambient[2] = 0.2;
model->materials[i].ambient[3] = 1.0;
model->materials[i].specular[0] = 0.0;
model->materials[i].specular[1] = 0.0;
model->materials[i].specular[2] = 0.0;
model->materials[i].specular[3] = 1.0;
model->materials[i].map_file = NULL;
}
model->materials[0].name = stralloc("default");
/* 读取数据 */
nummaterials = 0;
while (fscanf(file, "%s", buf) != EOF) {
switch (buf[0]) {
case '#':
fgets(buf, sizeof(buf), file);
break;
case 'n':
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s %s", buf, buf);
nummaterials++;
model->materials[nummaterials].name = stralloc(buf);
break;
case 'N':
fscanf(file, "%f", &model->materials[nummaterials].shininess);
/*Ns 10.000 # 范围从0到1000 wavefront shininess is from [0, 1000], so scale for OpenGL */
model->materials[nummaterials].shininess /= 1000.0;
model->materials[nummaterials].shininess *= 128.0;
break;
case 'm': /* texture map */
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s %s", buf, buf);
picturefilename = (char*)malloc(sizeof(char) * (strlen(dir) + strlen(buf) + 1));
strcpy(picturefilename, dir);
strcat(picturefilename, buf);
model->materials[nummaterials].map_file = picturefilename;
//_glmReadPNG(model, textureArray, picturefilename, nummaterials);
break;
case 'K':
switch (buf[1]) {
case 'd':
fscanf(file, "%f %f %f",
&model->materials[nummaterials].diffuse[0],
&model->materials[nummaterials].diffuse[1],
&model->materials[nummaterials].diffuse[2]);
break;
case 's':
fscanf(file, "%f %f %f",
&model->materials[nummaterials].specular[0],
&model->materials[nummaterials].specular[1],
&model->materials[nummaterials].specular[2]);
break;
case 'a':
fscanf(file, "%f %f %f",
&model->materials[nummaterials].ambient[0],
&model->materials[nummaterials].ambient[1],
&model->materials[nummaterials].ambient[2]);
break;
default:
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
}
break;
default:
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
}
}
free(dir);
}
/* _glmWriteMTL: write a wavefront material library file//写入mtl文件
*
* model - properly initialized GLMmodel structure
* modelpath - pathname of the model being written
* mtllibname - name of the material library to be written
*/
static GLvoid _glmWriteMTL(GLMmodel* model, char* modelpath, char* mtllibname)
{
FILE* file;
char* dir;
char* filename;
GLMmaterial* material;
GLuint i;
dir = _glmDirName(modelpath);
filename = (char*)malloc(sizeof(char) * (strlen(dir) + strlen(mtllibname)));
strcpy(filename, dir);
strcat(filename, mtllibname);
free(dir);
/* open the file */
file = fopen(filename, "w");
if (!file) {
fprintf(stderr, "_glmWriteMTL() failed: can't open file \"%s\".\n",
filename);
exit(1);
}
free(filename);
/* spit out a header */
fprintf(file, "# \n");
fprintf(file, "# Wavefront MTL generated by GLM library\n");
fprintf(file, "# \n");
fprintf(file, "# GLM library copyright (C) 1997 by Nate Robins\n");
fprintf(file, "# email: ndr@pobox.com\n");
fprintf(file, "# www: http://www.pobox.com/~ndr\n");
fprintf(file, "# \n\n");
for (i = 0; i < model->nummaterials; i++) {
material = &model->materials[i];
fprintf(file, "newmtl %s\n", material->name);
fprintf(file, "Ka %f %f %f\n",
material->ambient[0], material->ambient[1], material->ambient[2]);
fprintf(file, "Kd %f %f %f\n",
material->diffuse[0], material->diffuse[1], material->diffuse[2]);
fprintf(file, "Ks %f %f %f\n",
material->specular[0], material->specular[1], material->specular[2]);
fprintf(file, "Ns %f\n", material->shininess);
fprintf(file, "\n");
}
}
//获取模型的相关信息,值读取了数量信息
static GLvoid _glmFirstPass(GLMmodel* model, FILE* file)
{
GLuint numvertices; /* 节点个数 */
GLuint numnormals; /* 向量个数 */
GLuint numtexcoords; /* 纹理坐标 */
GLuint numtriangles; /* 三角形个数 */
GLMgroup* group; /* 当前组 */
unsigned v, n, t;
char buf[128];
/* 默认组 */
group = _glmAddGroup(model, "default");
numvertices = numnormals = numtexcoords = numtriangles = 0;
while (fscanf(file, "%s", buf) != EOF) {/*从一个流中执行格式化输入,fscanf遇到空格和换行时结束,注意空格时也结束。这与fgets有区别,fgets遇到空格不结束。*/
switch (buf[0]) {
case '#': /* #代表注释 */
/* 如果文件中的该行,不足bufsize个字符,则读完该行就结束。如若该行(包括最后一个换行符)的字符数超过bufsize-1,则fgets只返回一个不完整的行,但是,缓冲区总是以NULL字符结尾,对fgets的下一次调用会继续读该行。 */
fgets(buf, sizeof(buf), file);
break;
case 'v': /* 节点,法向量,纹理坐标 */
switch (buf[1]) {
case '\0': /* 是c/c++语言中的字符串结束符 */
/* fgets函数读取完整行数据 */
fgets(buf, sizeof(buf), file);
numvertices++;
break;
case 'n':
fgets(buf, sizeof(buf), file);
numnormals++;
break;
case 't':
fgets(buf, sizeof(buf), file);
numtexcoords++;
break;
default:
printf("_glmFirstPass(): Unknown token \"%s\".\n", buf);
exit(1);
break;
}
break;
case 'm':
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s %s", buf, buf);
model->mtllibname = stralloc(buf);
_glmReadMTL(model, buf);
break;
case 'u':
fgets(buf, sizeof(buf), file);
break;
case 'g': //组
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s", buf);
group = _glmAddGroup(model, buf);
break;
case 'f':
v = n = t = 0;
fscanf(file, "%s", buf);
/* %d, %d//%d, %d/%d, %d/%d/%d %d//%d */
// fscanf从文件输入,sscanf//从字符串输入;scanf从控制台输入
if (strstr(buf, "//")) {
sscanf(buf, "%d//%d", &v, &n);
fscanf(file, "%d//%d", &v, &n);
fscanf(file, "%d//%d", &v, &n);
numtriangles++;
group->numtriangles++;
while (fscanf(file, "%d//%d", &v, &n) > 0) {
numtriangles++;
group->numtriangles++;
}
}
else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3) {
/* v/t/n */
fscanf(file, "%d/%d/%d", &v, &t, &n);
fscanf(file, "%d/%d/%d", &v, &t, &n);
numtriangles++;
group->numtriangles++;
while (fscanf(file, "%d/%d/%d", &v, &t, &n) > 0) {
numtriangles++;
group->numtriangles++;
}
}
else if (sscanf(buf, "%d/%d", &v, &t) == 2) {
/* v/t */
fscanf(file, "%d/%d", &v, &t);
fscanf(file, "%d/%d", &v, &t);
numtriangles++;
group->numtriangles++;
while (fscanf(file, "%d/%d", &v, &t) > 0) {
numtriangles++;
group->numtriangles++;
}
}
else {
/* v */
fscanf(file, "%d", &v);
fscanf(file, "%d", &v);
numtriangles++;
group->numtriangles++;
while (fscanf(file, "%d", &v) > 0) {
numtriangles++;
group->numtriangles++;
}
}
break;
default:
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
}
}
#if 0
/* announce the model statistics */
printf(" Vertices: %d\n", numvertices);
printf(" Normals: %d\n", numnormals);
printf(" Texcoords: %d\n", numtexcoords);
printf(" Triangles: %d\n", numtriangles);
printf(" Groups: %d\n", model->numgroups);
#endif
/* set the stats in the model structure */
model->numvertices = numvertices;
model->numnormals = numnormals;
model->numtexcoords = numtexcoords;
model->numtriangles = numtriangles;
/* 为组分配内存*/
group = model->groups;
while (group) {
group->triangles = (GLuint*)malloc(sizeof(GLuint) * group->numtriangles);
group->numtriangles = 0;
group = group->next;
}
}
//获取所有的实际数据
static GLvoid _glmSecondPass(GLMmodel* model, FILE* file)
{
GLuint numvertices; /* number of vertices in model */
GLuint numnormals; /* number of normals in model */
GLuint numtexcoords; /* number of texcoords in model */
GLuint numtriangles; /* number of triangles in model */
GLfloat* vertices; /* array of vertices */
GLfloat* normals; /* array of normals */
GLfloat* texcoords; /* array of texture coordinates */
GLMgroup* group; /* current group pointer */
GLuint material; /* current material */
GLuint v, n, t;
char buf[128];
GLfloat tmpYcoord;
/* set the pointer shortcuts */
vertices = model->vertices;
normals = model->normals;
texcoords = model->texcoords;
group = model->groups;
/* 将数据读入到分配好的内存当中 */
numvertices = numnormals = numtexcoords = 1;
numtriangles = 0;
material = 0;
while (fscanf(file, "%s", buf) != EOF) {
switch (buf[0]) {
case '#':/* 注释 */
fgets(buf, sizeof(buf), file);
break;
case 'v': /* v, vn, vt */
switch (buf[1]) {
case '\0': /* vertex */
fscanf(file, "%f %f %f",
&vertices[3 * numvertices + X],
&vertices[3 * numvertices + Y],
&vertices[3 * numvertices + Z]);
numvertices++;
break;
case 'n': /* normal */
fscanf(file, "%f %f %f",
&normals[3 * numnormals + X],
&normals[3 * numnormals + Y],
&normals[3 * numnormals + Z]);
numnormals++;
break;
case 't': /* texcoord */
fscanf(file, "%f %f",
&texcoords[2 * numtexcoords + X],
&tmpYcoord);
texcoords[2 * numtexcoords + Y] = -tmpYcoord;//??为什么是负值
numtexcoords++;
break;
}
break;
case 'u':
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s %s", buf, buf);
group->material = material = _glmFindMaterial(model, buf);
break;
case 'g': /* group */
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s", buf);
group = _glmFindGroup(model, buf);
group->material = material;
break;
case 'f': /* face */
v = n = t = 0;
fscanf(file, "%s", buf);
/* can be one of %d, %d//%d, %d/%d, %d/%d/%d %d//%d */
if (strstr(buf, "//")) {
/* v//n */
sscanf(buf, "%d//%d", &v, &n);
T(numtriangles).vindices[0] = v;
T(numtriangles).nindices[0] = n;
fscanf(file, "%d//%d", &v, &n);
T(numtriangles).vindices[1] = v;
T(numtriangles).nindices[1] = n;
fscanf(file, "%d//%d", &v, &n);
T(numtriangles).vindices[2] = v;
T(numtriangles).nindices[2] = n;
group->triangles[group->numtriangles++] = numtriangles;
numtriangles++;
while (fscanf(file, "%d//%d", &v, &n) > 0) {
T(numtriangles).vindices[0] = T(numtriangles - 1).vindices[0];
T(numtriangles).nindices[0] = T(numtriangles - 1).nindices[0];
T(numtriangles).vindices[1] = T(numtriangles - 1).vindices[2];
T(numtriangles).nindices[1] = T(numtriangles - 1).nindices[2];
T(numtriangles).vindices[2] = v;
T(numtriangles).nindices[2] = n;
group->triangles[group->numtriangles++] = numtriangles;
numtriangles++;
}
}
else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3) {
/* v/t/n */
T(numtriangles).vindices[0] = v;
T(numtriangles).tindices[0] = t;
T(numtriangles).nindices[0] = n;
fscanf(file, "%d/%d/%d", &v, &t, &n);
T(numtriangles).vindices[1] = v;
T(numtriangles).tindices[1] = t;
T(numtriangles).nindices[1] = n;
fscanf(file, "%d/%d/%d", &v, &t, &n);
T(numtriangles).vindices[2] = v;
T(numtriangles).tindices[2] = t;
T(numtriangles).nindices[2] = n;
group->triangles[group->numtriangles++] = numtriangles;
numtriangles++;
while (fscanf(file, "%d/%d/%d", &v, &t, &n) > 0) {//如果多于三个点,那么构建新的三角形
T(numtriangles).vindices[0] = T(numtriangles - 1).vindices[0];
T(numtriangles).tindices[0] = T(numtriangles - 1).tindices[0];
T(numtriangles).nindices[0] = T(numtriangles - 1).nindices[0];
T(numtriangles).vindices[1] = T(numtriangles - 1).vindices[2];
T(numtriangles).tindices[1] = T(numtriangles - 1).tindices[2];
T(numtriangles).nindices[1] = T(numtriangles - 1).nindices[2];
T(numtriangles).vindices[2] = v;
T(numtriangles).tindices[2] = t;
T(numtriangles).nindices[2] = n;
group->triangles[group->numtriangles++] = numtriangles;
numtriangles++;
}
}
else if (sscanf(buf, "%d/%d", &v, &t) == 2) {
/* v/t */
T(numtriangles).vindices[0] = v;
T(numtriangles).tindices[0] = t;
fscanf(file, "%d/%d", &v, &t);
T(numtriangles).vindices[1] = v;
T(numtriangles).tindices[1] = t;
fscanf(file, "%d/%d", &v, &t);
T(numtriangles).vindices[2] = v;
T(numtriangles).tindices[2] = t;
group->triangles[group->numtriangles++] = numtriangles;
numtriangles++;
while (fscanf(file, "%d/%d", &v, &t) > 0) {
T(numtriangles).vindices[0] = T(numtriangles - 1).vindices[0];
T(numtriangles).tindices[0] = T(numtriangles - 1).tindices[0];
T(numtriangles).vindices[1] = T(numtriangles - 1).vindices[2];
T(numtriangles).tindices[1] = T(numtriangles - 1).tindices[2];
T(numtriangles).vindices[2] = v;
T(numtriangles).tindices[2] = t;
group->triangles[group->numtriangles++] = numtriangles;
numtriangles++;
}
}
else {
/* v */
sscanf(buf, "%d", &v);
T(numtriangles).vindices[0] = v;
fscanf(file, "%d", &v);
T(numtriangles).vindices[1] = v;
fscanf(file, "%d", &v);
T(numtriangles).vindices[2] = v;
group->triangles[group->numtriangles++] = numtriangles;
numtriangles++;
while (fscanf(file, "%d", &v) > 0) {
T(numtriangles).vindices[0] = T(numtriangles - 1).vindices[0];
T(numtriangles).vindices[1] = T(numtriangles - 1).vindices[2];
T(numtriangles).vindices[2] = v;
group->triangles[group->numtriangles++] = numtriangles;
numtriangles++;
}
}
break;
default:
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
}
}
#if 0
/* announce the memory requirements */
printf(" Memory: %d bytes\n",
numvertices * 3 * sizeof(GLfloat) +
numnormals * 3 * sizeof(GLfloat) * (numnormals ? 1 : 0) +
numtexcoords * 3 * sizeof(GLfloat) * (numtexcoords ? 1 : 0) +
numtriangles * sizeof(GLMtriangle));
#endif
}
/* public functions */
/* glmUnitize: "unitize" a model by translating it to the origin and
* scaling it to fit in a unit cube around the origin. Returns the
* scalefactor used.//将模型在指定的范围内显示
*
* model - properly initialized GLMmodel structure
*/
GLfloat glmUnitize(GLMmodel* model, GLfloat center[3])
{
GLuint i;
GLfloat maxx, minx, maxy, miny, maxz, minz;
GLfloat cx, cy, cz, w, h, d;
GLfloat scale;
assert(model);
assert(model->vertices);
/* get the max/mins */
maxx = minx = model->vertices[3 + X];
maxy = miny = model->vertices[3 + Y];
maxz = minz = model->vertices[3 + Z];
for (i = 1; i <= model->numvertices; i++) {
if (maxx < model->vertices[3 * i + X])
maxx = model->vertices[3 * i + X];
if (minx > model->vertices[3 * i + X])
minx = model->vertices[3 * i + X];
if (maxy < model->vertices[3 * i + Y])
maxy = model->vertices[3 * i + Y];
if (miny > model->vertices[3 * i + Y])
miny = model->vertices[3 * i + Y];
if (maxz < model->vertices[3 * i + Z])
maxz = model->vertices[3 * i + Z];
if (minz > model->vertices[3 * i + Z])
minz = model->vertices[3 * i + Z];
}
/* 计算宽高深度*/
w = _glmAbs(maxx) + _glmAbs(minx);
h = _glmAbs(maxy) + _glmAbs(miny);
d = _glmAbs(maxz) + _glmAbs(minz);
/* calculate center of the model */
cx = (maxx + minx) / 2.0;
cy = (maxy + miny) / 2.0;
cz = (maxz + minz) / 2.0;
/* calculate unitizing scale factor */
scale = 2.0 / _glmMax(_glmMax(w, h), d);
/* translate around center then scale */
for (i = 1; i <= model->numvertices; i++) {
model->vertices[3 * i + X] -= cx;
model->vertices[3 * i + Y] -= cy;
model->vertices[3 * i + Z] -= cz;
model->vertices[3 * i + X] *= scale;
model->vertices[3 * i + Y] *= scale;
model->vertices[3 * i + Z] *= scale;
}
center[0] = cx;
center[1] = cy;
center[2] = cz;
return scale;
}
/* glmDimensions: Calculates the dimensions (width, height, depth) of
* a model.计算当前数据的维度,
*
* model - initialized GLMmodel structure
* dimensions - array of 3 GLfloats (GLfloat dimensions[3])
*/
GLvoid glmDimensions(GLMmodel* model, GLfloat* dimensions)
{
GLuint i;
GLfloat maxx, minx, maxy, miny, maxz, minz;
assert(model);
assert(model->vertices);
assert(dimensions);
/* get the max/mins */
maxx = minx = model->vertices[3 + X];
maxy = miny = model->vertices[3 + Y];
maxz = minz = model->vertices[3 + Z];
for (i = 1; i <= model->numvertices; i++) {
if (maxx < model->vertices[3 * i + X])
maxx = model->vertices[3 * i + X];
if (minx > model->vertices[3 * i + X])
minx = model->vertices[3 * i + X];
if (maxy < model->vertices[3 * i + Y])
maxy = model->vertices[3 * i + Y];
if (miny > model->vertices[3 * i + Y])
miny = model->vertices[3 * i + Y];
if (maxz < model->vertices[3 * i + Z])
maxz = model->vertices[3 * i + Z];
if (minz > model->vertices[3 * i + Z])
minz = model->vertices[3 * i + Z];
}
/* calculate model width, height, and depth */
dimensions[X] = _glmAbs(maxx) + _glmAbs(minx);
dimensions[Y] = _glmAbs(maxy) + _glmAbs(miny);
dimensions[Z] = _glmAbs(maxz) + _glmAbs(minz);
}
/* glmScale: Scales a model by a given amount.缩放全图
*
* model - properly initialized GLMmodel structure
* scale - scalefactor (0.5 = half as large, 2.0 = twice as large)
*/
GLvoid glmScale(GLMmodel* model, GLfloat scale)
{
GLuint i;
for (i = 1; i <= model->numvertices; i++) {
model->vertices[3 * i + X] *= scale;
model->vertices[3 * i + Y] *= scale;
model->vertices[3 * i + Z] *= scale;
}
}
/* glmReverseWinding: Reverse the polygon winding for all polygons in
* this model. Default winding is counter-clockwise. Also changes
* the direction of the normals.
*
* model - properly initialized GLMmodel structure
*/
GLvoid glmReverseWinding(GLMmodel* model)
{
GLuint i, swap;
assert(model);
for (i = 0; i < model->numtriangles; i++) {
swap = T(i).vindices[0];
T(i).vindices[0] = T(i).vindices[2];
T(i).vindices[2] = swap;
if (model->numnormals) {
swap = T(i).nindices[0];
T(i).nindices[0] = T(i).nindices[2];
T(i).nindices[2] = swap;
}
if (model->numtexcoords) {
swap = T(i).tindices[0];
T(i).tindices[0] = T(i).tindices[2];
T(i).tindices[2] = swap;
}
}
/* reverse facet normals */
for (i = 1; i <= model->numfacetnorms; i++) {
model->facetnorms[3 * i + X] = -model->facetnorms[3 * i + X];
model->facetnorms[3 * i + Y] = -model->facetnorms[3 * i + Y];
model->facetnorms[3 * i + Z] = -model->facetnorms[3 * i + Z];
}
/* reverse vertex normals */
for (i = 1; i <= model->numnormals; i++) {
model->normals[3 * i + X] = -model->normals[3 * i + X];
model->normals[3 * i + Y] = -model->normals[3 * i + Y];