-
Notifications
You must be signed in to change notification settings - Fork 1
/
AccessObj.cpp
1962 lines (1769 loc) · 57.6 KB
/
AccessObj.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
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <vector>
#include "io.h"
#include <fcntl.h>
//////////////////////////////////////////////////////////////////////
#include <GL\glaux.h>
#include "AccessObj.h"
#define UV_SCALE 1
#define _T6k (6 * m_pModel->nVisibRes * m_pModel->nVisibRes)
#define _G6k (6 * m_pModel->pRes * m_pModel->pRes)
#define _GSumNum (6 * ((m_pModel->pRes * m_pModel->pRes - 1) / 3))
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAccessObj::CAccessObj()
{
m_pModel = NULL;
}
CAccessObj::~CAccessObj()
{
Destory();
}
//////////////////////////////////////////////////////////////////////
// Equal: 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 (float u[3])
// v - array of 3 GLfloats (float v[3])
//////////////////////////////////////////////////////////////////////
bool CAccessObj::Equal(Vec3 * u, Vec3 * v, float epsilon)
{
if (objAbs(u->x - v->x) < epsilon &&
objAbs(u->y - v->y) < epsilon &&
objAbs(u->z - v->z) < epsilon)
{
return GL_TRUE;
}
return GL_FALSE;
}
//////////////////////////////////////////////////////////////////////
// FindGroup: Find a group in the model
//////////////////////////////////////////////////////////////////////
/*COBJgroup * CAccessObj::FindGroup(char* name)
{
COBJgroup * group;
assert(m_pModel);
group = m_pModel->pGroups;
while(group)
{
if (!strcmp(name, group->name))
break;
group = group->next;
}
return group;
}
*/
//////////////////////////////////////////////////////////////////////
// AddGroup: Add a group to the model
//////////////////////////////////////////////////////////////////////
/*COBJgroup * CAccessObj::AddGroup(char* name)
{
COBJgroup* group;
group = FindGroup(name);
if (!group)
{
group = new COBJgroup;
sprintf(group->name, "%s", name);
group->material = 0;
group->nTriangles = 0;
group->pTriangles = NULL;
group->next = m_pModel->pGroups;
m_pModel->pGroups = group;
m_pModel->nGroups++;
}
return group;
}
*/
//////////////////////////////////////////////////////////////////////
// FindGroup: Find a material in the model
//////////////////////////////////////////////////////////////////////
unsigned int CAccessObj::FindMaterial(char* name)
{
unsigned int i;
bool bFound = false;
// XXX doing a linear search on a string key'd list is pretty lame, but it works and is fast enough for now.
for (i = 0; i < m_pModel->nMaterials; i++)
{
if (!strcmp(m_pModel->pMaterials[i].name, name))
{
bFound = true;
break;
}
}
// didn't find the name, so print a warning and return the default material (0).
if (!bFound)
{
printf("FindMaterial(): can't find material \"%s\".\n", name);
i = 0;
}
return i;
}
//////////////////////////////////////////////////////////////////////
// DirName: return the directory given a path
//
// path - filesystem path
//
// NOTE: the return value should be free'd.
//////////////////////////////////////////////////////////////////////
char * CAccessObj::DirName(char* path)
{
static char dir[256];
char *s;
sprintf(dir, "%s", path);
s = strrchr(dir, '\\');
if (s) s[1] = '\0';
else dir[0] = '\0';
return dir;
}
//////////////////////////////////////////////////////////////////////
// ReadMTL: read a wavefront material library file
//
// model - properly initialized COBJmodel structure
// name - name of the material library
//////////////////////////////////////////////////////////////////////
void CAccessObj::ReadMTL(char* name)
{
FILE* file;
char dir[256];
char filename[256];
char buf[128];
unsigned int nMaterials, i;
sprintf(dir, "%s", DirName(m_pModel->pathname));
strcpy(filename, dir);
strcat(filename, name);
file = fopen(filename, "r");
if (!file)
{
fprintf(stderr, "ReadMTL() failed: can't open material file \"%s\".\n", filename);
return;//exit(1);
}
// count the number of materials in the file
nMaterials = 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);
nMaterials++;
sscanf(buf, "%s %s", buf, buf);
break;
default:
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
}
}
rewind(file);
m_pModel->pMaterials = new COBJmaterial [nMaterials];
m_pModel->nMaterials = nMaterials;
// set the default material
for (i = 0; i < nMaterials; i++) {
m_pModel->pMaterials[i].name[0] = '\0';
m_pModel->pMaterials[i].shininess[0] = 32.0f;
m_pModel->pMaterials[i].diffuse[0] = 0.8f;
m_pModel->pMaterials[i].diffuse[1] = 0.8f;
m_pModel->pMaterials[i].diffuse[2] = 0.8f;
m_pModel->pMaterials[i].diffuse[3] = 1.0f;
m_pModel->pMaterials[i].ambient[0] = 0.2f;
m_pModel->pMaterials[i].ambient[1] = 0.2f;
m_pModel->pMaterials[i].ambient[2] = 0.2f;
m_pModel->pMaterials[i].ambient[3] = 1.0f;
m_pModel->pMaterials[i].specular[0] = 0.0f;
m_pModel->pMaterials[i].specular[1] = 0.0f;
m_pModel->pMaterials[i].specular[2] = 0.0f;
m_pModel->pMaterials[i].specular[3] = 1.0f;
m_pModel->pMaterials[i].emissive[0] = 0.0f;
m_pModel->pMaterials[i].emissive[1] = 0.0f;
m_pModel->pMaterials[i].emissive[2] = 0.0f;
m_pModel->pMaterials[i].emissive[3] = 1.0f;
}
sprintf(m_pModel->pMaterials[0].name, "default");
// now, read in the data
nMaterials = 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);
nMaterials++;
sprintf(m_pModel->pMaterials[nMaterials].name, "%s", buf);
break;
case 'N':
fscanf(file, "%f", &m_pModel->pMaterials[nMaterials].shininess);
/* wavefront shininess is from [0, 1000], so scale for OpenGL */
m_pModel->pMaterials[nMaterials].shininess[0] /= 1000.0f;
m_pModel->pMaterials[nMaterials].shininess[0] *= 128.0f;
break;
case 'K':
switch(buf[1])
{
case 'd':
fscanf(file, "%f %f %f",
&m_pModel->pMaterials[nMaterials].diffuse[0],
&m_pModel->pMaterials[nMaterials].diffuse[1],
&m_pModel->pMaterials[nMaterials].diffuse[2]);
break;
case 's':
fscanf(file, "%f %f %f",
&m_pModel->pMaterials[nMaterials].specular[0],
&m_pModel->pMaterials[nMaterials].specular[1],
&m_pModel->pMaterials[nMaterials].specular[2]);
break;
case 'a':
fscanf(file, "%f %f %f",
&m_pModel->pMaterials[nMaterials].ambient[0],
&m_pModel->pMaterials[nMaterials].ambient[1],
&m_pModel->pMaterials[nMaterials].ambient[2]);
break;
default:
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
}
break;
case 'm':
fscanf(file, "%s", filename);
sprintf(m_pModel->pMaterials[nMaterials].sTexture, "%s%s", dir, filename);
break;
default:
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
}
}
fclose (file);
}
//////////////////////////////////////////////////////////////////////
// WriteMTL: write a wavefront material library file
//
// model - properly initialized COBJmodel structure
// modelpath - pathname of the model being written
// mtllibname - name of the material library to be written
//////////////////////////////////////////////////////////////////////
void CAccessObj::WriteMTL(char* modelpath, char* mtllibname)
{
FILE* file;
char* dir;
char* filename;
COBJmaterial* material;
unsigned int i;
dir = DirName(modelpath);
filename = new char [(strlen(dir)+strlen(mtllibname))];
strcpy(filename, dir);
strcat(filename, mtllibname);
free(dir);
/* open the file */
file = fopen(filename, "w");
if (!file)
{
fprintf(stderr, "WriteMTL() failed: can't open file \"%s\".\n", filename);
exit(1);
}
delete [] filename;
/* spit out a header */
fprintf(file, "# \n");
fprintf(file, "# Wavefront MTL generated by OBJ library\n");
fprintf(file, "# \n");
fprintf(file, "# OBJ library\n");
fprintf(file, "# Nate Robins\n");
fprintf(file, "# [email protected]\n");
fprintf(file, "# http://www.pobox.com/~ndr\n");
fprintf(file, "# \n\n");
for (i = 0; i < m_pModel->nMaterials; i++)
{
material = &m_pModel->pMaterials[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[0] / 128.0 * 1000.0);
fprintf(file, "\n");
}
}
//////////////////////////////////////////////////////////////////////
// FirstPass: first pass at a Wavefront OBJ file that gets all the
// statistics of the model (such as #vertices, #normals, etc)
//
// model - properly initialized COBJmodel structure
// file - (fopen'd) file descriptor
//////////////////////////////////////////////////////////////////////
void CAccessObj::FirstPass(FILE* file)
{
unsigned int nVertices; /* number of vertices in m_pModel */ //NOTE: can only store 65k vertices
unsigned int nNormals; /* number of normals in m_pModel */
unsigned int nTexCoords; /* number of texcoords in m_pModel */
unsigned int nTriangles; /* number of triangles in m_pModel */
COBJgroup* group; /* current group */
unsigned v, n, t;
char buf[128];
/* make a default group */
group = AddGroup("default");
nVertices = nNormals = nTexCoords = nTriangles = 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);
nVertices++;
break;
case 'n': /* normal */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
nNormals++;
break;
case 't': /* texcoord */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
nTexCoords++;
break;
default:
printf("FirstPass(): Unknown token \"%s\".\n", buf);
exit(1);
break;
}
break;
case 'm':
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s %s", buf, buf);
sprintf(m_pModel->mtllibname, "%s", buf);
ReadMTL(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);
buf[strlen(buf)-1] = '\0'; /* nuke '\n' */
group = AddGroup(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);
nTriangles++;
group->nTriangles++;
while(fscanf(file, "%d//%d", &v, &n) > 0)
{
nTriangles++;
group->nTriangles++;
}
}
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);
nTriangles++;
group->nTriangles++;
while(fscanf(file, "%d/%d/%d", &v, &t, &n) > 0)
{
nTriangles++;
group->nTriangles++;
}
}
else if (sscanf(buf, "%d/%d", &v, &t) == 2)
{
/* v/t */
fscanf(file, "%d/%d", &v, &t);
fscanf(file, "%d/%d", &v, &t);
nTriangles++;
group->nTriangles++;
while(fscanf(file, "%d/%d", &v, &t) > 0)
{
nTriangles++;
group->nTriangles++;
}
}
else
{
/* v */
fscanf(file, "%d", &v);
fscanf(file, "%d", &v);
nTriangles++;
group->nTriangles++;
while(fscanf(file, "%d", &v) > 0)
{
nTriangles++;
group->nTriangles++;
}
}
break;
default:
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
}
}
/* set the stats in the m_pModel structure */
m_pModel->nVertices = nVertices;
m_pModel->nNormals = nNormals;
m_pModel->nTexCoords = nTexCoords;
m_pModel->nTriangles = nTriangles;
/* allocate memory for the triangles in each group */
group = m_pModel->pGroups;
while(group)
{
group->pTriangles = new unsigned int [group->nTriangles];
group->nTriangles = 0;
group = group->next;
}
}
//////////////////////////////////////////////////////////////////////
// SecondPass: second pass at a Wavefront OBJ file that gets all
// the data.
//
// model - properly initialized COBJmodel structure
// file - (fopen'd) file descriptor
//////////////////////////////////////////////////////////////////////
void CAccessObj::SecondPass(FILE* file)
{
unsigned int nVertices; /* number of vertices in m_pModel */
unsigned int nNormals; /* number of normals in m_pModel */
unsigned int nTexCoords; /* number of texcoords in m_pModel */
unsigned int nTriangles; /* number of triangles in m_pModel */
Vec3 * vertices; /* array of vertices */
Vec3 * normals; /* array of normals */
Vec3 * texcoords; /* array of texture coordinates */
COBJgroup * group; /* current group pointer */
unsigned int material; /* current material */
unsigned int v, n, t;
char buf[128];
/* set the pointer shortcuts */
vertices = m_pModel->vpVertices;
normals = m_pModel->vpNormals;
texcoords = m_pModel->vpTexCoords;
group = m_pModel->pGroups;
/* on the second pass through the file, read all the data into the
allocated arrays */
nVertices = nNormals = nTexCoords = 1;
nTriangles = 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[nVertices].x,
&vertices[nVertices].y,
&vertices[nVertices].z);
nVertices++;
break;
case 'n': /* normal */
fscanf(file, "%f %f %f",
&normals[nNormals].x,
&normals[nNormals].y,
&normals[nNormals].z);
nNormals++;
break;
case 't': /* texcoord */
fscanf(file, "%f %f",
&texcoords[nTexCoords].x,
&texcoords[nTexCoords].y);
if (UV_SCALE>1) {
texcoords[nTexCoords].x = float(texcoords[nTexCoords].x * UV_SCALE - floor(texcoords[nTexCoords].x*UV_SCALE));
texcoords[nTexCoords].y = float(texcoords[nTexCoords].y * UV_SCALE - floor(texcoords[nTexCoords].y*UV_SCALE));
}
nTexCoords++;
break;
}
break;
case 'u':
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s %s", buf, buf);
group->material = material = FindMaterial(buf);
break;
case 'g': /* group */
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
buf[strlen(buf)-1] = '\0'; /* nuke '\n' */
group = FindGroup(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 */
Tri(nTriangles).mindex = material;
if (strstr(buf, "//"))
{
/* v//n */
sscanf(buf, "%d//%d", &v, &n);
Tri(nTriangles).vindices[0] = v;
Tri(nTriangles).nindices[0] = n;
fscanf(file, "%d//%d", &v, &n);
Tri(nTriangles).vindices[1] = v;
Tri(nTriangles).nindices[1] = n;
fscanf(file, "%d//%d", &v, &n);
Tri(nTriangles).vindices[2] = v;
Tri(nTriangles).nindices[2] = n;
group->pTriangles[group->nTriangles++] = nTriangles;
nTriangles++;
while(fscanf(file, "%d//%d", &v, &n) > 0)
{
Tri(nTriangles).vindices[0] = Tri(nTriangles-1).vindices[0];
Tri(nTriangles).nindices[0] = Tri(nTriangles-1).nindices[0];
Tri(nTriangles).vindices[1] = Tri(nTriangles-1).vindices[2];
Tri(nTriangles).nindices[1] = Tri(nTriangles-1).nindices[2];
Tri(nTriangles).vindices[2] = v;
Tri(nTriangles).nindices[2] = n;
Tri(nTriangles).mindex = material;
group->pTriangles[group->nTriangles++] = nTriangles;
nTriangles++;
}
}
else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3)
{
/* v/t/n */
Tri(nTriangles).vindices[0] = v;
Tri(nTriangles).tindices[0] = t;
Tri(nTriangles).nindices[0] = n;
fscanf(file, "%d/%d/%d", &v, &t, &n);
Tri(nTriangles).vindices[1] = v;
Tri(nTriangles).tindices[1] = t;
Tri(nTriangles).nindices[1] = n;
fscanf(file, "%d/%d/%d", &v, &t, &n);
Tri(nTriangles).vindices[2] = v;
Tri(nTriangles).tindices[2] = t;
Tri(nTriangles).nindices[2] = n;
group->pTriangles[group->nTriangles++] = nTriangles;
nTriangles++;
while(fscanf(file, "%d/%d/%d", &v, &t, &n) > 0)
{
Tri(nTriangles).vindices[0] = Tri(nTriangles-1).vindices[0];
Tri(nTriangles).tindices[0] = Tri(nTriangles-1).tindices[0];
Tri(nTriangles).nindices[0] = Tri(nTriangles-1).nindices[0];
Tri(nTriangles).vindices[1] = Tri(nTriangles-1).vindices[2];
Tri(nTriangles).tindices[1] = Tri(nTriangles-1).tindices[2];
Tri(nTriangles).nindices[1] = Tri(nTriangles-1).nindices[2];
Tri(nTriangles).vindices[2] = v;
Tri(nTriangles).tindices[2] = t;
Tri(nTriangles).nindices[2] = n;
Tri(nTriangles).mindex = material;
group->pTriangles[group->nTriangles++] = nTriangles;
nTriangles++;
}
}
else if (sscanf(buf, "%d/%d", &v, &t) == 2)
{
/* v/t */
Tri(nTriangles).vindices[0] = v;
Tri(nTriangles).tindices[0] = t;
fscanf(file, "%d/%d", &v, &t);
Tri(nTriangles).vindices[1] = v;
Tri(nTriangles).tindices[1] = t;
fscanf(file, "%d/%d", &v, &t);
Tri(nTriangles).vindices[2] = v;
Tri(nTriangles).tindices[2] = t;
group->pTriangles[group->nTriangles++] = nTriangles;
nTriangles++;
while(fscanf(file, "%d/%d", &v, &t) > 0)
{
Tri(nTriangles).vindices[0] = Tri(nTriangles-1).vindices[0];
Tri(nTriangles).tindices[0] = Tri(nTriangles-1).tindices[0];
Tri(nTriangles).vindices[1] = Tri(nTriangles-1).vindices[2];
Tri(nTriangles).tindices[1] = Tri(nTriangles-1).tindices[2];
Tri(nTriangles).vindices[2] = v;
Tri(nTriangles).tindices[2] = t;
Tri(nTriangles).mindex = material;
group->pTriangles[group->nTriangles++] = nTriangles;
nTriangles++;
}
}
else
{
/* v */
sscanf(buf, "%d", &v);
Tri(nTriangles).vindices[0] = v;
fscanf(file, "%d", &v);
Tri(nTriangles).vindices[1] = v;
fscanf(file, "%d", &v);
Tri(nTriangles).vindices[2] = v;
group->pTriangles[group->nTriangles++] = nTriangles;
nTriangles++;
while(fscanf(file, "%d", &v) > 0)
{
Tri(nTriangles).vindices[0] = Tri(nTriangles-1).vindices[0];
Tri(nTriangles).vindices[1] = Tri(nTriangles-1).vindices[2];
Tri(nTriangles).vindices[2] = v;
group->pTriangles[group->nTriangles++] = nTriangles;
Tri(nTriangles).mindex = material;
nTriangles++;
}
}
break;
default:
/* eat up rest of line */
fgets(buf, sizeof(buf), file);
break;
}
}
}
/* public functions */
//////////////////////////////////////////////////////////////////////
// objUnitize: "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 COBJmodel structure
//////////////////////////////////////////////////////////////////////
//DEL float CAccessObj::objUnitize()
//DEL {
//DEL unsigned int i;
//DEL float maxx, minx, maxy, miny, maxz, minz;
//DEL float cx, cy, cz, w, h, d;
//DEL float scale;
//DEL
//DEL assert(m_pModel);
//DEL assert(m_pModel->vpVertices);
//DEL
//DEL /* get the max/mins */
//DEL maxx = minx = m_pModel->vpVertices[3 + 0];
//DEL maxy = miny = m_pModel->vpVertices[3 + 1];
//DEL maxz = minz = m_pModel->vpVertices[3 + 2];
//DEL for (i = 1; i <= m_pModel->nVertices; i++)
//DEL {
//DEL if (maxx < m_pModel->vpVertices[3 * i + 0])
//DEL maxx = m_pModel->vpVertices[3 * i + 0];
//DEL if (minx > m_pModel->vpVertices[3 * i + 0])
//DEL minx = m_pModel->vpVertices[3 * i + 0];
//DEL
//DEL if (maxy < m_pModel->vpVertices[3 * i + 1])
//DEL maxy = m_pModel->vpVertices[3 * i + 1];
//DEL if (miny > m_pModel->vpVertices[3 * i + 1])
//DEL miny = m_pModel->vpVertices[3 * i + 1];
//DEL
//DEL if (maxz < m_pModel->vpVertices[3 * i + 2])
//DEL maxz = m_pModel->vpVertices[3 * i + 2];
//DEL if (minz > m_pModel->vpVertices[3 * i + 2])
//DEL minz = m_pModel->vpVertices[3 * i + 2];
//DEL }
//DEL
//DEL /* calculate m_pModel width, height, and depth */
//DEL w = maxx - minx;
//DEL h = maxy-miny;
//DEL d = maxz-minz;
//DEL
//DEL /* calculate center of the m_pModel */
//DEL cx = (maxx + minx) / 2.0;
//DEL cy = (maxy + miny) / 2.0;
//DEL cz = (maxz + minz) / 2.0;
//DEL
//DEL /* calculate unitizing scale factor */
//DEL scale = 2.0 / objMax(objMax(w, h), d);
//DEL
//DEL /* translate around center then scale */
//DEL for (i = 1; i <= m_pModel->nVertices; i++)
//DEL {
//DEL m_pModel->vpVertices[3 * i + 0] -= cx;
//DEL m_pModel->vpVertices[3 * i + 1] -= cy;
//DEL m_pModel->vpVertices[3 * i + 2] -= cz;
//DEL m_pModel->vpVertices[3 * i + 0] *= scale;
//DEL m_pModel->vpVertices[3 * i + 1] *= scale;
//DEL m_pModel->vpVertices[3 * i + 2] *= scale;
//DEL }
//DEL
//DEL return scale;
//DEL }
//////////////////////////////////////////////////////////////////////
// Scale: Scales a model by a given amount.
//
// model - properly initialized COBJmodel structure
// scale - scalefactor (0.5 = half as large, 2.0 = twice as large)
//////////////////////////////////////////////////////////////////////
void CAccessObj::Scale(float scale)
{
unsigned int i;
for (i = 1; i <= m_pModel->nVertices; i++)
m_pModel->vpVertices[i] = m_pModel->vpVertices[i] * scale;
m_vMax = m_vMax * scale;
m_vMin = m_vMin * scale;
}
void CAccessObj::Translate(const Vec3& trans)
{
for (unsigned int i = 1; i <= m_pModel->nVertices; i++)
m_pModel->vpVertices[i] = m_pModel->vpVertices[i] + trans;
m_vMax = m_vMax + trans;
m_vMin = m_vMin + trans;
}
//////////////////////////////////////////////////////////////////////
// ReverseWinding: 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 COBJmodel structure
//////////////////////////////////////////////////////////////////////
void CAccessObj::ReverseWinding()
{
unsigned int i, swap;
assert(m_pModel);
for (i = 0; i < m_pModel->nTriangles; i++)
{
swap = Tri(i).vindices[0];
Tri(i).vindices[0] = Tri(i).vindices[2];
Tri(i).vindices[2] = swap;
if (m_pModel->nNormals)
{
swap = Tri(i).nindices[0];
Tri(i).nindices[0] = Tri(i).nindices[2];
Tri(i).nindices[2] = swap;
}
if (m_pModel->nTexCoords)
{
swap = Tri(i).tindices[0];
Tri(i).tindices[0] = Tri(i).tindices[2];
Tri(i).tindices[2] = swap;
}
}
/* reverse facet normals */
for (i = 1; i <= m_pModel->nFacetnorms; i++)
m_pModel->vpFacetNorms[i] = m_pModel->vpFacetNorms[i]*(-1);
/* reverse vertex normals */
for (i = 1; i <= m_pModel->nNormals; i++)
m_pModel->vpNormals[i] = m_pModel->vpNormals[i]*(-1);
}
//////////////////////////////////////////////////////////////////////
// FacetNormals: Generates facet normals for a model (by taking the
// cross product of the two vectors derived from the sides of each
// triangle). Assumes a counter-clockwise winding.
//
// model - initialized COBJmodel structure
//////////////////////////////////////////////////////////////////////
void CAccessObj::FacetNormals()
{
unsigned int i;
Vec3 u, v;
assert(m_pModel);
assert(m_pModel->vpVertices);
/* clobber any old facetnormals */
if (m_pModel->vpFacetNorms)
free(m_pModel->vpFacetNorms);
/* allocate memory for the new facet normals */
m_pModel->nFacetnorms = m_pModel->nTriangles;
m_pModel->vpFacetNorms = new Vec3 [m_pModel->nFacetnorms + 1];
for (i = 0; i < m_pModel->nTriangles; i++)
{
m_pModel->pTriangles[i].findex = i+1;
u = m_pModel->vpVertices[Tri(i).vindices[1]] - m_pModel->vpVertices[Tri(i).vindices[0]];
v = m_pModel->vpVertices[Tri(i).vindices[2]] - m_pModel->vpVertices[Tri(i).vindices[0]];
m_pModel->vpFacetNorms[i+1] = Cross(u, v);
m_pModel->vpFacetNorms[i+1].Normalize();
}
}
//////////////////////////////////////////////////////////////////////
// VertexNormals: Generates smooth vertex normals for a model.
// First builds a list of all the triangles each vertex is in. Then
// loops through each vertex in the the list averaging all the facet
// normals of the triangles each vertex is in. Finally, sets the
// normal index in the triangle for the vertex to the generated smooth
// normal. If the dot product of a facet normal and the facet normal
// associated with the first triangle in the list of triangles the
// current vertex is in is greater than the cosine of the angle
// parameter to the function, that facet normal is not added into the
// average normal calculation and the corresponding vertex is given
// the facet normal. This tends to preserve hard edges. The angle to
// use depends on the model, but 90 degrees is usually a good start.
//
// model - initialized COBJmodel structure
// angle - maximum angle (in degrees) to smooth across
//////////////////////////////////////////////////////////////////////
void CAccessObj::VertexNormals(float angle)
{
OBJnode* node;
OBJnode* tail;
OBJnode** members;
unsigned int nNormals;
Vec3 average;
float dot, cos_angle;
unsigned int i, avg;
assert(m_pModel);
assert(m_pModel->vpFacetNorms);
/* calculate the cosine of the angle (in degrees) */
cos_angle = (float)cos(angle * 3.14159265 / 180.0);
/* nuke any previous normals */
if (m_pModel->vpNormals)
delete[] m_pModel->vpNormals;
/* allocate space for new normals */
m_pModel->nNormals = m_pModel->nTriangles * 3; /* 3 normals per triangle */
m_pModel->vpNormals = new Vec3 [m_pModel->nNormals+1];
//yk
m_pModel->vpVertexNormals.clear();
m_pModel->vpVertexNormals.resize(m_pModel->nVertices +1);
/* allocate a structure that will hold a linked list of triangle
indices for each vertex */
members = new OBJnode * [m_pModel->nVertices + 1];
for (i = 1; i <= m_pModel->nVertices; i++)
members[i] = NULL;
/* for every triangle, create a node for each vertex in it */
for (i = 0; i < m_pModel->nTriangles; i++)
{
node = new OBJnode;
node->index = i;
node->next = members[Tri(i).vindices[0]];
members[Tri(i).vindices[0]] = node;
node = new OBJnode;
node->index = i;
node->next = members[Tri(i).vindices[1]];
members[Tri(i).vindices[1]] = node;
node = new OBJnode;
node->index = i;
node->next = members[Tri(i).vindices[2]];
members[Tri(i).vindices[2]] = node;
}
/* calculate the average normal for each vertex */
nNormals = 1;
for (i = 1; i <= m_pModel->nVertices; i++)
{
// calculate an average normal for this vertex by averaging the
// facet normal of every triangle this vertex is in
node = members[i];
if (!node)
fprintf(stderr, "VertexNormals(): vertex w/o a triangle\n");
average = Vec3(0, 0, 0);
avg = 0;
//yk
Vec3 average1 = Vec3(0, 0, 0);
while (node)
{
//yk
average1 = average1 + m_pModel->vpFacetNorms[(m_pModel->pTriangles[(node->index)]).findex];
/* only average if the dot product of the angle between the two
facet normals is greater than the cosine of the threshold
angle -- or, said another way, the angle between the two
facet normals is less than (or equal to) the threshold angle */
dot = Dot(m_pModel->vpFacetNorms[Tri(node->index).findex], m_pModel->vpFacetNorms[Tri(members[i]->index).findex]);
if (dot > cos_angle)
{
node->averaged = GL_TRUE;
average = average + m_pModel->vpFacetNorms[Tri(node->index).findex];
avg = 1; /* we averaged at least one normal! */
}
else
{
node->averaged = GL_FALSE;
}
node = node->next;
}
average1.Normalize();
m_pModel->vpVertexNormals[i] = average1;
if (avg)
{
/* normalize the averaged normal */
average.Normalize();
/* add the normal to the vertex normals list */
m_pModel->vpNormals[nNormals] = average;
avg = nNormals;
nNormals++;
}
/* set the normal of this vertex in each triangle it is in */
node = members[i];
while (node)
{
if (node->averaged)
{
/* if this node was averaged, use the average normal */
if (Tri(node->index).vindices[0] == i)
Tri(node->index).nindices[0] = avg;
else if (Tri(node->index).vindices[1] == i)
Tri(node->index).nindices[1] = avg;
else if (Tri(node->index).vindices[2] == i)
Tri(node->index).nindices[2] = avg;