-
Notifications
You must be signed in to change notification settings - Fork 0
/
glm.cpp
2186 lines (1957 loc) · 58.5 KB
/
glm.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
/*
* GLM library. Wavefront .obj file format reader/writer/manipulator.
*
* Written by Nate Robins, 1997.
* email: [email protected]
* www: http://www.pobox.com/~ndr
*/
/* includes */
#include "ss.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "glm.h"
/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/* defines */
#define T(x) model->triangles[(x)]
/* enums */
enum { X, Y, Z, W }; /* elements of a vertex */
/* typedefs */
/* _GLMnode: general purpose node
*/
typedef struct _GLMnode {
GLuint index;
GLboolean averaged;
struct _GLMnode *next;
} GLMnode;
/* strdup is actually not a standard ANSI C or POSIX routine
so implement a private one. OpenVMS does not have a strdup; Linux's
standard libc doesn't declare strdup by default (unless BSD or SVID
interfaces are requested). */
static char *stralloc(const char *string)
{
char *copy;
copy = (char *) malloc(strlen(string) + 1);
if (copy == NULL)
return NULL;
strcpy(copy, string);
copy[strlen(string)] = 0;
return copy;
}
/* private functions */
/* _glmMax: returns the maximum of two floats */
static GLfloat _glmMax(GLfloat a, GLfloat b)
{
if (a > b)
return a;
return b;
}
/* _glmAbs: returns the absolute value of a float */
static GLfloat _glmAbs(GLfloat f)
{
if (f < 0)
return -f;
return f;
}
/* _glmDot: compute the dot product of two vectors
*
* u - array of 3 GLfloats (GLfloat u[3])
* v - array of 3 GLfloats (GLfloat v[3])
*/
static GLfloat _glmDot(GLfloat * u, GLfloat * v)
{
assert(u);
assert(v);
/* compute the dot product */
return u[X] * v[X] + u[Y] * v[Y] + u[Z] * v[Z];
}
/* _glmCross: compute the cross product of two vectors
*
* u - array of 3 GLfloats (GLfloat u[3])
* v - array of 3 GLfloats (GLfloat v[3])
* n - array of 3 GLfloats (GLfloat n[3]) to return the cross product in
*/
static GLvoid _glmCross(GLfloat * u, GLfloat * v, GLfloat * n)
{
assert(u);
assert(v);
assert(n);
/* compute the cross product (u x v for right-handed [ccw]) */
n[X] = u[Y] * v[Z] - u[Z] * v[Y];
n[Y] = u[Z] * v[X] - u[X] * v[Z];
n[Z] = u[X] * v[Y] - u[Y] * v[X];
}
/* _glmNormalize: normalize a vector
*
* n - array of 3 GLfloats (GLfloat n[3]) to be normalized
*/
static GLvoid _glmNormalize(GLfloat * n)
{
GLfloat l;
assert(n);
/* normalize */
l = (GLfloat) sqrt(n[X] * n[X] + n[Y] * n[Y] + n[Z] * n[Z]);
n[0] /= l;
n[1] /= l;
n[2] /= l;
}
/* _glmEqual: compares two vectors and returns GL_TRUE if they are
* equal (within a certain threshold) or GL_FALSE if not. An epsilon
* that works fairly well is 0.000001.
*
* u - array of 3 GLfloats (GLfloat u[3])
* v - array of 3 GLfloats (GLfloat v[3])
*/
static GLboolean _glmEqual(GLfloat * u, GLfloat * v, GLfloat epsilon)
{
if (_glmAbs(u[0] - v[0]) < epsilon &&
_glmAbs(u[1] - v[1]) < epsilon &&
_glmAbs(u[2] - v[2]) < epsilon) {
return GL_TRUE;
}
return GL_FALSE;
}
/* _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;
}
/* _glmFindGroup: Find a group in the model
*/
GLMgroup *_glmFindGroup(GLMmodel * model, const char *name)
{
GLMgroup *group;
assert(model);
group = model->groups;
while (group) {
if (!strcmp(name, group->name))
break;
group = group->next;
}
return group;
}
/* _glmAddGroup: Add a group to the model
*/
GLMgroup *_glmAddGroup(GLMmodel * model, const char *name)
{
GLMgroup *group;
printf("Adding group %s\n", name);
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;
}
/* _glmFindMaterial: Find a material in the model
*/
GLuint _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;
}
/* _glmDirName: return the directory given a path
*
* path - filesystem path
*
* The return value should be free'd.
*/
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;
}
/* _glmReadMTL: read a wavefront material library file
*
* model - properly initialized GLMmodel structure
* name - name of the material library
*/
static GLvoid _glmReadMTL(GLMmodel * model, char *name)
{
FILE *file;
char *dir;
char *filename;
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);
/* open the file */
file = fopen(filename, "r");
if (!file) {
fprintf(stderr,
"_glmReadMTL() failed: can't open material file \"%s\".\n",
filename);
exit(1);
}
free(filename);
/* count the number of materials in the file */
nummaterials = 1;
while (fscanf(file, "%s", buf) != EOF) {
switch (buf[0]) {
case '#': /* comment */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
case 'n': /* newmtl */
fgets(buf, sizeof(buf), file);
nummaterials++;
sscanf(buf, "%s %s", buf, buf);
break;
default:
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
}
}
rewind(file);
/* allocate memory for the materials */
model->materials =
(GLMmaterial *) malloc(sizeof(GLMmaterial) * nummaterials);
model->nummaterials = nummaterials;
/* set the default material */
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[0].name = stralloc("default");
/* now, read in the data */
nummaterials = 0;
while (fscanf(file, "%s", buf) != EOF) {
switch (buf[0]) {
case '#': /* comment */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
case 'n': /* newmtl */
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);
/* wavefront shininess is from [0, 1000], so scale for OpenGL */
model->materials[nummaterials].shininess /= 1000.0;
model->materials[nummaterials].shininess *= 128.0;
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;
}
}
}
/* _glmWriteMTL: write a wavefront material library file
*
* 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: [email protected]\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");
}
}
/* _glmFirstPass: first pass at a Wavefront OBJ file that gets all the
* statistics of the model (such as #vertices, #normals, etc)
*
* model - properly initialized GLMmodel structure
* file - (fopen'd) file descriptor
*/
static GLvoid _glmFirstPass(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 */
GLMgroup *group; /* current group */
unsigned v, n, t;
char buf[128];
/* make a default group */
group = _glmAddGroup(model, "default");
numvertices = numnormals = numtexcoords = numtriangles = 0;
while (fscanf(file, "%s", buf) != EOF) {
switch (buf[0]) {
case '#': /* comment */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
case 'v': /* v, vn, vt */
switch (buf[1]) {
case '\0': /* vertex */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
numvertices++;
break;
case 'n': /* normal */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
numnormals++;
break;
case 't': /* texcoord */
/* eat up rest of line */
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':
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
case 'g': /* group */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s", buf);
group = _glmAddGroup(model, buf);
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);
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;
/* allocate memory for the triangles in each group */
group = model->groups;
while (group) {
group->triangles =
(GLuint *) malloc(sizeof(GLuint) *
group->numtriangles);
group->numtriangles = 0;
group = group->next;
}
}
/* _glmSecondPass: second pass at a Wavefront OBJ file that gets all
* the data.
*
* model - properly initialized GLMmodel structure
* file - (fopen'd) file descriptor
*/
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];
/* set the pointer shortcuts */
vertices = model->vertices;
normals = model->normals;
texcoords = model->texcoords;
group = model->groups;
/* on the second pass through the file, read all the data into the
allocated arrays */
numvertices = numnormals = numtexcoords = 1;
numtriangles = 0;
material = 0;
while (fscanf(file, "%s", buf) != EOF) {
switch (buf[0]) {
case '#': /* comment */
/* eat up rest of line */
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],
&texcoords[2 * numtexcoords + Y]);
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 */
/* eat up rest of line */
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
}
/* _glmInterpolate: Perform a linear interpolation of the first model to the second model,
* using the supplied step value. Place the result into dest.
*/
void _glmInterp(GLMmodel *source, GLMmodel *dest, float step)
{
unsigned int i;
for (i=0;i < (dest->numvertices + 1) * 3;i++){
if (dest->vertices[i] != source->vertices[i]){
dest->vertices[i] = source->vertices[i] + ((dest->vertices[i] - source->vertices[i]) * step);
}
}
return;
}
/* 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)
{
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];
}
/* calculate model width, height, and depth */
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;
}
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
*/