-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel_3DS.cpp
1273 lines (1058 loc) · 34.9 KB
/
Model_3DS.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
//////////////////////////////////////////////////////////////////////
//
// 3D Studio Model Class
// by: Matthew Fairfax
//
// Model_3DS.cpp: implementation of the Model_3DS class.
// This is a simple class for loading and viewing
// 3D Studio model files (.3ds). It supports models
// with multiple objects. It also supports multiple
// textures per object. It does not support the animation
// for 3D Studio models b/c there are simply too many
// ways for an artist to animate a 3D Studio model and
// I didn't want to impose huge limitations on the artists.
// However, I have imposed a limitation on how the models are
// textured:
// 1) Every faces must be assigned a material
// 2) If you want the face to be textured assign the
// texture to the Diffuse Color map
// 3) The texture must be supported by the GLTexture class
// which only supports bitmap and targa right now
// 4) The texture must be located in the same directory as
// the model
//
// Support for non-textured faces is done by reading the color
// from the material's diffuse color.
//
// Some models have problems loading even if you follow all of
// the restrictions I have stated and I don't know why. If you
// can import the 3D Studio file into Milkshape 3D
// (http://www.swissquake.ch/chumbalum-soft) and then export it
// to a new 3D Studio file. This seems to fix many of the problems
// but there is a limit on the number of faces and vertices Milkshape 3D
// can read.
//
// Usage:
// Model_3DS m;
//
// m.Load("model.3ds"); // Load the model
// m.Draw(); // Renders the model to the screen
//
// // If you want to show the model's normals
// m.shownormals = true;
//
// // If the model is not going to be lit then set the lit
// // variable to false. It defaults to true.
// m.lit = false;
//
// // You can disable the rendering of the model
// m.visible = false;
//
// // You can move and rotate the model like this:
// m.rot.x = 90.0f;
// m.rot.y = 30.0f;
// m.rot.z = 0.0f;
//
// m.pos.x = 10.0f;
// m.pos.y = 0.0f;
// m.pos.z = 0.0f;
//
// // If you want to move or rotate individual objects
// m.Objects[0].rot.x = 90.0f;
// m.Objects[0].rot.y = 30.0f;
// m.Objects[0].rot.z = 0.0f;
//
// m.Objects[0].pos.x = 10.0f;
// m.Objects[0].pos.y = 0.0f;
// m.Objects[0].pos.z = 0.0f;
//
//////////////////////////////////////////////////////////////////////
// This is used to generate a warning from the compiler
#define _QUOTE(x) # x
#define QUOTE(x) _QUOTE(x)
#define __FILE__LINE__ __FILE__ "(" QUOTE(__LINE__) ") : "
#define warn( x ) message( __FILE__LINE__ #x "\n" )
// You need to uncomment this if you are using MFC
//#pragma warn( You need to uncomment this if you are using MFC )
//#include "stdafx.h"
#include "Model_3DS.h"
#include <math.h> // Header file for the math library
#include <gl.h> // Header file for the OpenGL32 library
// The chunk's id numbers
#define MAIN3DS 0x4D4D
#define MAIN_VERS 0x0002
#define EDIT3DS 0x3D3D
#define MESH_VERS 0x3D3E
#define OBJECT 0x4000
#define TRIG_MESH 0x4100
#define VERT_LIST 0x4110
#define FACE_DESC 0x4120
#define FACE_MAT 0x4130
#define TEX_VERTS 0x4140
#define SMOOTH_GROUP 0x4150
#define LOCAL_COORDS 0x4160
#define MATERIAL 0xAFFF
#define MAT_NAME 0xA000
#define MAT_AMBIENT 0xA010
#define MAT_DIFFUSE 0xA020
#define MAT_SPECULAR 0xA030
#define SHINY_PERC 0xA040
#define SHINY_STR_PERC 0xA041
#define TRANS_PERC 0xA050
#define TRANS_FOFF_PERC 0xA052
#define REF_BLUR_PERC 0xA053
#define RENDER_TYPE 0xA100
#define SELF_ILLUM 0xA084
#define MAT_SELF_ILPCT 0xA08A
#define WIRE_THICKNESS 0xA087
#define MAT_TEXMAP 0xA200
#define MAT_MAPNAME 0xA300
#define ONE_UNIT 0x0100
#define KEYF3DS 0xB000
#define FRAMES 0xB008
#define MESH_INFO 0xB002
#define HIER_POS 0xB030
#define HIER_FATHER 0xB010
#define PIVOT_PT 0xB013
#define TRACK00 0xB020
#define TRACK01 0xB021
#define TRACK02 0xB022
#define COLOR_RGB 0x0010
#define COLOR_TRU 0x0011
#define COLOR_TRUG 0x0012
#define COLOR_RGBG 0x0013
#define PERC_INT 0x0030
#define PERC_FLOAT 0x0031
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Model_3DS::Model_3DS()
{
// Initialization
// Don't show the normals by default
shownormals = false;
// The model is lit by default
lit = true;
// The model is visible by default
visible = true;
// Set up the default position
pos.x = 0.0f;
pos.y = 0.0f;
pos.z = 0.0f;
// Set up the default rotation
rot.x = 0.0f;
rot.y = 0.0f;
rot.z = 0.0f;
// Set up the path
path = new char[80];
sprintf(path, "");
// Zero out our counters for MFC
numObjects = 0;
numMaterials = 0;
// Set the scale to one
scale = 1.0f;
}
Model_3DS::~Model_3DS()
{
}
void Model_3DS::Load(char *name)
{
// holds the main chunk header
ChunkHeader main1;
// strip "'s
if (strstr(name, "\""))
name = strtok(name, "\"");
// Find the path
if (strstr(name, "/") || strstr(name, "\\"))
{
// Holds the name of the model minus the path
char *temp;
// Find the name without the path
if (strstr(name, "/"))
temp = strrchr(name, '/');
else
temp = strrchr(name, '\\');
// Allocate space for the path
path = new char[strlen(name)-strlen(temp)+1];
// Get a pointer to the end of the path and name
char *src = name + strlen(name) - 1;
// Back up until a \ or the start
while (src != path && !((*(src-1)) == '\\' || (*(src-1)) == '/'))
src--;
// Copy the path into path
memcpy (path, name, src-name);
path[src-name] = 0;
}
// Load the file
bin3ds = fopen(name,"rb");
// Make sure we are at the beginning
fseek(bin3ds, 0, SEEK_SET);
// Load the Main Chunk's header
fread(&main1.id,sizeof(main1.id),1,bin3ds);
fread(&main1.len,sizeof(main1.len),1,bin3ds);
// Start Processing
MainChunkProcessor(main1.len, ftell(bin3ds));
// Don't need the file anymore so close it
fclose(bin3ds);
// Calculate the vertex normals
CalculateNormals();
// For future reference
modelname = name;
// Find the total number of faces and vertices
totalFaces = 0;
totalVerts = 0;
for (int i = 0; i < numObjects; i ++)
{
totalFaces += Objects[i].numFaces/3;
totalVerts += Objects[i].numVerts;
}
// If the object doesn't have any texcoords generate some
/* for (int k = 0; k < numObjects; k++)
{
if (Objects[k].numTexCoords == 0)
{
// Set the number of texture coords
Objects[k].numTexCoords = Objects[k].numVerts;
// Allocate an array to hold the texture coordinates
Objects[k].TexCoords = new GLfloat[Objects[k].numTexCoords * 2];
// Make some texture coords
for (int m = 0; m < Objects[k].numTexCoords; m++)
{
Objects[k].TexCoords[2*m] = Objects[k].Vertexes[3*m];
Objects[k].TexCoords[2*m+1] = Objects[k].Vertexes[3*m+1];
}
}
} */
// Let's build simple colored textures for the materials w/o a texture
/* for (int j = 0; j < numMaterials; j++)
{
if (Materials[j].textured == false)
{
unsigned char r = Materials[j].color.r;
unsigned char g = Materials[j].color.g;
unsigned char b = Materials[j].color.b;
Materials[j].tex.BuildColorTexture(r, g, b);
Materials[j].textured = true;
}
} */
}
void Model_3DS::Draw()
{
if (visible)
{
glPushMatrix();
// Move the model
glTranslatef(pos.x, pos.y, pos.z);
// Rotate the model
glRotatef(rot.x, 1.0f, 0.0f, 0.0f);
glRotatef(rot.y, 0.0f, 1.0f, 0.0f);
glRotatef(rot.z, 0.0f, 0.0f, 1.0f);
glScalef(scale, scale, scale);
// Loop through the objects
for (int i = 0; i < numObjects; i++)
{
// Enable texture coordiantes, normals, and vertices arrays
if (Objects[i].textured)
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
if (lit)
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
// Point them to the objects arrays
if (Objects[i].textured)
glTexCoordPointer(2, GL_FLOAT, 0, Objects[i].TexCoords);
if (lit)
glNormalPointer(GL_FLOAT, 0, Objects[i].Normals);
glVertexPointer(3, GL_FLOAT, 0, Objects[i].Vertexes);
// Loop through the faces as sorted by material and draw them
for (int j = 0; j < Objects[i].numMatFaces; j ++)
{
// Use the material's texture
Materials[Objects[i].MatFaces[j].MatIndex].tex.Use();
glPushMatrix();
// Move the model
glTranslatef(Objects[i].pos.x, Objects[i].pos.y, Objects[i].pos.z);
// Rotate the model
//glRotatef(Objects[i].rot.x, 1.0f, 0.0f, 0.0f);
//glRotatef(Objects[i].rot.y, 0.0f, 1.0f, 0.0f);
//glRotatef(Objects[i].rot.z, 0.0f, 0.0f, 1.0f);
glRotatef(Objects[i].rot.z, 0.0f, 0.0f, 1.0f);
glRotatef(Objects[i].rot.y, 0.0f, 1.0f, 0.0f);
glRotatef(Objects[i].rot.x, 1.0f, 0.0f, 0.0f);
/*glBegin(GL_LINES);
GLfloat mat_emission0[] = {1,0,0,1};
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission0);
glVertex3f(-100,0,0);
glVertex3f(+100,0,0);
GLfloat mat_emission1[] = {0,1,0,1};
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission1);
glVertex3f(0,-100,0);
glVertex3f(0,+100,0);
GLfloat mat_emission2[] = {0,0,1,1};
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission2);
glVertex3f(0,0,-100);
glVertex3f(0,0,+100);
glEnd(); */
//if(!textured){
//GLfloat mat_ambient[] = {Materials[Objects[i].MatFaces[j].MatIndex].ambient.r,Materials[Objects[i].MatFaces[j].MatIndex].ambient.g,Materials[Objects[i].MatFaces[j].MatIndex].ambient.b,1};
//GLfloat mat_diffuse[] = {Materials[Objects[i].MatFaces[j].MatIndex].diffuse.r,Materials[Objects[i].MatFaces[j].MatIndex].diffuse.g,Materials[Objects[i].MatFaces[j].MatIndex].diffuse.b,1};
//GLfloat mat_specular[] = {Materials[Objects[i].MatFaces[j].MatIndex].specular.r,Materials[Objects[i].MatFaces[j].MatIndex].specular.g,Materials[Objects[i].MatFaces[j].MatIndex].specular.b,1};
//GLfloat mat_emission[] = {0,0,0,1};
//glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
//glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
//glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
////glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission);
// }
glDrawElements(GL_TRIANGLES, Objects[i].MatFaces[j].numSubFaces, GL_UNSIGNED_SHORT, Objects[i].MatFaces[j].subFaces);
glPopMatrix();
}
// Show the normals?
if (shownormals)
{
// Loop through the vertices and normals and draw the normal
for (int k = 0; k < Objects[i].numVerts * 3; k += 3)
{
// Disable texturing
glDisable(GL_TEXTURE_2D);
// Disbale lighting if the model is lit
if (lit)
glDisable(GL_LIGHTING);
// Draw the normals blue
glColor3f(0.0f, 0.0f, 1.0f);
// Draw a line between the vertex and the end of the normal
glBegin(GL_LINES);
glVertex3f(Objects[i].Vertexes[k], Objects[i].Vertexes[k+1], Objects[i].Vertexes[k+2]);
glVertex3f(Objects[i].Vertexes[k]+Objects[i].Normals[k], Objects[i].Vertexes[k+1]+Objects[i].Normals[k+1], Objects[i].Vertexes[k+2]+Objects[i].Normals[k+2]);
glEnd();
// Reset the color to white
glColor3f(1.0f, 1.0f, 1.0f);
// If the model is lit then renable lighting
if (lit)
glEnable(GL_LIGHTING);
}
}
}
glPopMatrix();
}
}
void Model_3DS::CalculateNormals()
{
// Let's build some normals
for (int i = 0; i < numObjects; i++)
{
for (int g = 0; g < Objects[i].numVerts; g++)
{
// Reduce each vert's normal to unit
float length;
Vector unit;
unit.x = Objects[i].Normals[g*3];
unit.y = Objects[i].Normals[g*3+1];
unit.z = Objects[i].Normals[g*3+2];
length = (float)sqrt((unit.x*unit.x) + (unit.y*unit.y) + (unit.z*unit.z));
if (length == 0.0f)
length = 1.0f;
unit.x /= length;
unit.y /= length;
unit.z /= length;
Objects[i].Normals[g*3] = unit.x;
Objects[i].Normals[g*3+1] = unit.y;
Objects[i].Normals[g*3+2] = unit.z;
}
}
}
void Model_3DS::MainChunkProcessor(long length, long findex)
{
ChunkHeader h;
// move the file pointer to the beginning of the main
// chunk's data findex + the size of the header
fseek(bin3ds, findex, SEEK_SET);
while (ftell(bin3ds) < (findex + length - 6))
{
fread(&h.id,sizeof(h.id),1,bin3ds);
fread(&h.len,sizeof(h.len),1,bin3ds);
switch (h.id)
{
// This is the mesh information like vertices, faces, and materials
case EDIT3DS :
EditChunkProcessor(h.len, ftell(bin3ds));
break;
// I left this in case anyone gets very ambitious
case KEYF3DS :
//KeyFrameChunkProcessor(h.len, ftell(bin3ds));
break;
default :
break;
}
fseek(bin3ds, (h.len - 6), SEEK_CUR);
}
// move the file pointer back to where we got it so
// that the ProcessChunk() which we interrupted will read
// from the right place
fseek(bin3ds, findex, SEEK_SET);
}
void Model_3DS::EditChunkProcessor(long length, long findex)
{
ChunkHeader h;
// move the file pointer to the beginning of the main
// chunk's data findex + the size of the header
fseek(bin3ds, findex, SEEK_SET);
// First count the number of Objects and Materials
while (ftell(bin3ds) < (findex + length - 6))
{
fread(&h.id,sizeof(h.id),1,bin3ds);
fread(&h.len,sizeof(h.len),1,bin3ds);
switch (h.id)
{
case OBJECT :
numObjects++;
break;
case MATERIAL :
numMaterials++;
break;
default :
break;
}
fseek(bin3ds, (h.len - 6), SEEK_CUR);
}
// Now load the materials
if (numMaterials > 0)
{
Materials = new Material[numMaterials];
// Material is set to untextured until we find otherwise
for (int d = 0; d < numMaterials; d++)
Materials[d].textured = false;
fseek(bin3ds, findex, SEEK_SET);
int i = 0;
while (ftell(bin3ds) < (findex + length - 6))
{
fread(&h.id,sizeof(h.id),1,bin3ds);
fread(&h.len,sizeof(h.len),1,bin3ds);
switch (h.id)
{
case MATERIAL :
MaterialChunkProcessor(h.len, ftell(bin3ds), i);
i++;
break;
default :
break;
}
fseek(bin3ds, (h.len - 6), SEEK_CUR);
}
}
// Load the Objects (individual meshes in the whole model)
if (numObjects > 0)
{
Objects = new Object[numObjects];
// Set the textured variable to false until we find a texture
for (int k = 0; k < numObjects; k++)
Objects[k].textured = false;
// Zero the objects position and rotation
for (int m = 0; m < numObjects; m++)
{
Objects[m].pos.x = 0.0f;
Objects[m].pos.y = 0.0f;
Objects[m].pos.z = 0.0f;
Objects[m].rot.x = 0.0f;
Objects[m].rot.y = 0.0f;
Objects[m].rot.z = 0.0f;
}
// Zero out the number of texture coords
for (int n = 0; n < numObjects; n++)
Objects[n].numTexCoords = 0;
fseek(bin3ds, findex, SEEK_SET);
int j = 0;
while (ftell(bin3ds) < (findex + length - 6))
{
fread(&h.id,sizeof(h.id),1,bin3ds);
fread(&h.len,sizeof(h.len),1,bin3ds);
switch (h.id)
{
case OBJECT :
ObjectChunkProcessor(h.len, ftell(bin3ds), j);
j++;
break;
default :
break;
}
fseek(bin3ds, (h.len - 6), SEEK_CUR);
}
}
// move the file pointer back to where we got it so
// that the ProcessChunk() which we interrupted will read
// from the right place
fseek(bin3ds, findex, SEEK_SET);
}
void Model_3DS::MaterialChunkProcessor(long length, long findex, int matindex)
{
ChunkHeader h;
// move the file pointer to the beginning of the main
// chunk's data findex + the size of the header
fseek(bin3ds, findex, SEEK_SET);
while (ftell(bin3ds) < (findex + length - 6))
{
fread(&h.id,sizeof(h.id),1,bin3ds);
fread(&h.len,sizeof(h.len),1,bin3ds);
switch (h.id)
{
case MAT_NAME :
// Loads the material's names
MaterialNameChunkProcessor(h.len, ftell(bin3ds), matindex);
break;
case MAT_AMBIENT :
AmbientColorChunkProcessor(h.len, ftell(bin3ds), matindex);
break;
case MAT_DIFFUSE :
DiffuseColorChunkProcessor(h.len, ftell(bin3ds), matindex);
break;
case MAT_SPECULAR :
SpecularColorChunkProcessor(h.len, ftell(bin3ds), matindex);
case MAT_TEXMAP :
// Finds the names of the textures of the material and loads them
TextureMapChunkProcessor(h.len, ftell(bin3ds), matindex);
break;
default :
break;
}
fseek(bin3ds, (h.len - 6), SEEK_CUR);
}
// move the file pointer back to where we got it so
// that the ProcessChunk() which we interrupted will read
// from the right place
fseek(bin3ds, findex, SEEK_SET);
}
void Model_3DS::MaterialNameChunkProcessor(long length, long findex, int matindex)
{
// move the file pointer to the beginning of the main
// chunk's data findex + the size of the header
fseek(bin3ds, findex, SEEK_SET);
// Read the material's name
for (int i = 0; i < 80; i++)
{
Materials[matindex].name[i] = fgetc(bin3ds);
if (Materials[matindex].name[i] == 0)
{
Materials[matindex].name[i] = NULL;
break;
}
}
// move the file pointer back to where we got it so
// that the ProcessChunk() which we interrupted will read
// from the right place
fseek(bin3ds, findex, SEEK_SET);
}
void Model_3DS::AmbientColorChunkProcessor(long length, long findex, int matindex)
{
ChunkHeader h;
// move the file pointer to the beginning of the main
// chunk's data findex + the size of the header
fseek(bin3ds, findex, SEEK_SET);
while (ftell(bin3ds) < (findex + length - 6))
{
fread(&h.id,sizeof(h.id),1,bin3ds);
fread(&h.len,sizeof(h.len),1,bin3ds);
// Determine the format of the color and load it
switch (h.id)
{
case COLOR_RGB :
// A rgb float color chunk
FloatColorChunkProcessor(h.len, ftell(bin3ds), this->Materials[matindex].ambient);
break;
case COLOR_TRU :
// A rgb int color chunk
IntColorChunkProcessor(h.len, ftell(bin3ds), this->Materials[matindex].ambient);
break;
case COLOR_RGBG :
// A rgb gamma corrected float color chunk
FloatColorChunkProcessor(h.len, ftell(bin3ds), this->Materials[matindex].ambient);
break;
case COLOR_TRUG :
// A rgb gamma corrected int color chunk
IntColorChunkProcessor(h.len, ftell(bin3ds), this->Materials[matindex].ambient);
break;
default :
break;
}
fseek(bin3ds, (h.len - 6), SEEK_CUR);
}
// move the file pointer back to where we got it so
// that the ProcessChunk() which we interrupted will read
// from the right place
fseek(bin3ds, findex, SEEK_SET);
}
void Model_3DS::DiffuseColorChunkProcessor(long length, long findex, int matindex)
{
ChunkHeader h;
// move the file pointer to the beginning of the main
// chunk's data findex + the size of the header
fseek(bin3ds, findex, SEEK_SET);
while (ftell(bin3ds) < (findex + length - 6))
{
fread(&h.id,sizeof(h.id),1,bin3ds);
fread(&h.len,sizeof(h.len),1,bin3ds);
// Determine the format of the color and load it
switch (h.id)
{
case COLOR_RGB :
// A rgb float color chunk
FloatColorChunkProcessor(h.len, ftell(bin3ds), this->Materials[matindex].diffuse);
break;
case COLOR_TRU :
// A rgb int color chunk
IntColorChunkProcessor(h.len, ftell(bin3ds), this->Materials[matindex].diffuse);
break;
case COLOR_RGBG :
// A rgb gamma corrected float color chunk
FloatColorChunkProcessor(h.len, ftell(bin3ds), this->Materials[matindex].diffuse);
break;
case COLOR_TRUG :
// A rgb gamma corrected int color chunk
IntColorChunkProcessor(h.len, ftell(bin3ds), this->Materials[matindex].diffuse);
break;
default :
break;
}
fseek(bin3ds, (h.len - 6), SEEK_CUR);
}
// move the file pointer back to where we got it so
// that the ProcessChunk() which we interrupted will read
// from the right place
fseek(bin3ds, findex, SEEK_SET);
}
void Model_3DS::SpecularColorChunkProcessor(long length, long findex, int matindex)
{
ChunkHeader h;
// move the file pointer to the beginning of the main
// chunk's data findex + the size of the header
fseek(bin3ds, findex, SEEK_SET);
while (ftell(bin3ds) < (findex + length - 6))
{
fread(&h.id,sizeof(h.id),1,bin3ds);
fread(&h.len,sizeof(h.len),1,bin3ds);
// Determine the format of the color and load it
switch (h.id)
{
case COLOR_RGB :
// A rgb float color chunk
FloatColorChunkProcessor(h.len, ftell(bin3ds), this->Materials[matindex].specular);
break;
case COLOR_TRU :
// A rgb int color chunk
IntColorChunkProcessor(h.len, ftell(bin3ds), this->Materials[matindex].specular);
break;
case COLOR_RGBG :
// A rgb gamma corrected float color chunk
FloatColorChunkProcessor(h.len, ftell(bin3ds), this->Materials[matindex].specular);
break;
case COLOR_TRUG :
// A rgb gamma corrected int color chunk
IntColorChunkProcessor(h.len, ftell(bin3ds), this->Materials[matindex].specular);
break;
default :
break;
}
fseek(bin3ds, (h.len - 6), SEEK_CUR);
}
// move the file pointer back to where we got it so
// that the ProcessChunk() which we interrupted will read
// from the right place
fseek(bin3ds, findex, SEEK_SET);
}
void Model_3DS::FloatColorChunkProcessor(long length, long findex, Color& color)
{
float r;
float g;
float b;
// move the file pointer to the beginning of the main
// chunk's data findex + the size of the header
fseek(bin3ds, findex, SEEK_SET);
fread(&r,sizeof(r),1,bin3ds);
fread(&g,sizeof(g),1,bin3ds);
fread(&b,sizeof(b),1,bin3ds);
color.r = r;
color.g = g;
color.b = b;
color.a = 1;
// move the file pointer back to where we got it so
// that the ProcessChunk() which we interrupted will read
// from the right place
fseek(bin3ds, findex, SEEK_SET);
}
void Model_3DS::IntColorChunkProcessor(long length, long findex, Color& color)
{
unsigned char r;
unsigned char g;
unsigned char b;
// move the file pointer to the beginning of the main
// chunk's data findex + the size of the header
fseek(bin3ds, findex, SEEK_SET);
fread(&r,sizeof(r),1,bin3ds);
fread(&g,sizeof(g),1,bin3ds);
fread(&b,sizeof(b),1,bin3ds);
color.r = float(r)/float(255);
color.g = float(g)/float(255);
color.b = float(b)/float(255);
color.a = 1;
// move the file pointer back to where we got it so
// that the ProcessChunk() which we interrupted will read
// from the right place
fseek(bin3ds, findex, SEEK_SET);
}
void Model_3DS::TextureMapChunkProcessor(long length, long findex, int matindex)
{
ChunkHeader h;
// move the file pointer to the beginning of the main
// chunk's data findex + the size of the header
fseek(bin3ds, findex, SEEK_SET);
while (ftell(bin3ds) < (findex + length - 6))
{
fread(&h.id,sizeof(h.id),1,bin3ds);
fread(&h.len,sizeof(h.len),1,bin3ds);
switch (h.id)
{
case MAT_MAPNAME:
// Read the name of texture in the Diffuse Color map
MapNameChunkProcessor(h.len, ftell(bin3ds), matindex);
break;
default :
break;
}
fseek(bin3ds, (h.len - 6), SEEK_CUR);
}
// move the file pointer back to where we got it so
// that the ProcessChunk() which we interrupted will read
// from the right place
fseek(bin3ds, findex, SEEK_SET);
}
void Model_3DS::MapNameChunkProcessor(long length, long findex, int matindex)
{
char name[80];
// move the file pointer to the beginning of the main
// chunk's data findex + the size of the header
fseek(bin3ds, findex, SEEK_SET);
// Read the name of the texture
for (int i = 0; i < 80; i++)
{
name[i] = fgetc(bin3ds);
if (name[i] == 0)
{
name[i] = NULL;
break;
}
}
// Load the name and indicate that the material has a texture
char fullname[80];
sprintf(fullname, "%s%s", path, name);
Materials[matindex].tex.Load(fullname);
Materials[matindex].textured = true;
// move the file pointer back to where we got it so
// that the ProcessChunk() which we interrupted will read
// from the right place
fseek(bin3ds, findex, SEEK_SET);
}
void Model_3DS::ObjectChunkProcessor(long length, long findex, int objindex)
{
ChunkHeader h;
// move the file pointer to the beginning of the main
// chunk's data findex + the size of the header
fseek(bin3ds, findex, SEEK_SET);
// Load the object's name
for (int i = 0; i < 80; i++)
{
Objects[objindex].name[i] = fgetc(bin3ds);
if (Objects[objindex].name[i] == 0)
{
Objects[objindex].name[i] = NULL;
break;
}
}
while (ftell(bin3ds) < (findex + length - 6))
{
fread(&h.id,sizeof(h.id),1,bin3ds);
fread(&h.len,sizeof(h.len),1,bin3ds);
switch (h.id)
{
case TRIG_MESH :
// Process the triangles of the object
TriangularMeshChunkProcessor(h.len, ftell(bin3ds), objindex);
break;
default :
break;
}
fseek(bin3ds, (h.len - 6), SEEK_CUR);
}
// move the file pointer back to where we got it so
// that the ProcessChunk() which we interrupted will read
// from the right place
fseek(bin3ds, findex, SEEK_SET);
}
void Model_3DS::TriangularMeshChunkProcessor(long length, long findex, int objindex)
{
ChunkHeader h;
// move the file pointer to the beginning of the main
// chunk's data findex + the size of the header
fseek(bin3ds, findex, SEEK_SET);
while (ftell(bin3ds) < (findex + length - 6))
{
fread(&h.id,sizeof(h.id),1,bin3ds);
fread(&h.len,sizeof(h.len),1,bin3ds);
switch (h.id)
{
case VERT_LIST :
// Load the vertices of the onject
VertexListChunkProcessor(h.len, ftell(bin3ds), objindex);
break;
case LOCAL_COORDS :
//LocalCoordinatesChunkProcessor(h.len, ftell(bin3ds));
break;
case TEX_VERTS :
// Load the texture coordinates for the vertices
TexCoordsChunkProcessor(h.len, ftell(bin3ds), objindex);
Objects[objindex].textured = true;
break;
default :
break;
}
fseek(bin3ds, (h.len - 6), SEEK_CUR);
}
// After we have loaded the vertices we can load the faces
fseek(bin3ds, findex, SEEK_SET);
while (ftell(bin3ds) < (findex + length - 6))
{
fread(&h.id,sizeof(h.id),1,bin3ds);
fread(&h.len,sizeof(h.len),1,bin3ds);
switch (h.id)
{
case FACE_DESC :
// Load the faces of the object
FacesDescriptionChunkProcessor(h.len, ftell(bin3ds), objindex);
break;
default :
break;
}
fseek(bin3ds, (h.len - 6), SEEK_CUR);
}
// move the file pointer back to where we got it so
// that the ProcessChunk() which we interrupted will read