rendered paste body#define FILENAME "box.3ds"#include <stdio.h>#include <fstream.h>#include "3dschunks.h"void output(char *string){ FILE *fp; fp = fopen("fileinfo.txt","a+"); fprintf(fp,"%s",string); fclose(fp);}struct stChunk{ unsigned short ID; unsigned int length; unsigned int bytesRead;};void ReadChunk(FILE*fp,stChunk *pChunk){ unsigned short ID = 0; unsigned int bytesRead = 0; unsigned int bChunkLength = 0; bytesRead = fread(&ID,1,2,fp); bytesRead += fread(&bChunkLength,1,4,fp); pChunk->ID = ID; pChunk->length = bChunkLength; pChunk->bytesRead = bytesRead;}void SkipChunk(FILE*fp,stChunk *pChunk){ int buffer[50000] = {0}; fread(buffer,1,pChunk->length - pChunk->bytesRead,fp);}void DisplayChunkInfo(stChunk* pChunk){ char buf[1000] = {0}; sprintf(buf, "Chunk ID: 0x %04x Size of Chunk: %u\n", pChunk->ID, pChunk->length); output(buf);}void ReadKEYF3DS(FILE *fp, stChunk* Chunk){ while(Chunk->bytesRead < Chunk -> length){ stChunk tempChunk = {0}; ReadChunk(fp, &tempChunk); DisplayChunkInfo(&tempChunk); switch(tempChunk.ID){ default: SkipChunk(fp,&tempChunk); } Chunk->bytesRead += tempChunk.length; }}/*read the actual object stuff*/void ReadVerticesMESH_FACES(FILE *fp,stChunk* Chunk){ unsigned int iNumberFaces = 0; //= Chunk->length -6; Chunk->bytesRead += fread(&iNumberFaces,1,2,fp); char buff[100]; sprintf(buff, "Number of Faces %u\n", iNumberFaces); output(buff); struct stFace{unsigned short p1,p2,p3;}; stFace *pFaces = new stFace[iNumberFaces]; Chunk->bytesRead += fread(pFaces,1,iNumberFaces* sizeof(stFace),fp); for(int i=0; i<iNumberFaces;i++) { sprintf(buff, "\t Face: Index1: %d, Index2: %d, Index3 %d\n",pFaces[i].p1, pFaces[i].p2, pFaces[i].p3); output(buff); } delete pFaces; SkipChunk(fp,Chunk);}void ReadVerticesMESH_VERTICES(FILE *fp,stChunk* Chunk){ unsigned int iNumberVertices = 0; Chunk->bytesRead += fread(&iNumberVertices,1,2,fp); //xyz char buff[300]; sprintf(buff, "Number of Vertices %d\n", iNumberVertices); output(buff); struct stVect{float x,y,z;}; stVect *pVerts = new stVect[iNumberVertices]; Chunk->bytesRead += fread( (void*)pVerts, 1, iNumberVertices*sizeof(stVect), fp); for(int i=0; i<iNumberVertices; i++) { sprintf(buff, "\t Vertices Point: x: %.2f, \ty: %.2f, \tz: %.2f\n",pVerts[i].x, pVerts[i].y, pVerts[i].z); output(buff); } delete[] pVerts; SkipChunk(fp,Chunk);}void LoadMeshOBJ_MESH(FILE *fp,stChunk* Chunk){ output("\n"); while(Chunk->bytesRead < Chunk->length){ stChunk tempChunk = {0}; ReadChunk(fp,&tempChunk); DisplayChunkInfo(&tempChunk); switch(tempChunk.ID){ case MESH_VERTICES: ReadVerticesMESH_VERTICES(fp,&tempChunk); break; case MESH_FACES: ReadVerticesMESH_FACES(fp, &tempChunk); break; default: SkipChunk(fp,&tempChunk); } Chunk->bytesRead += tempChunk.length; }}/*get object data*/int GetString(FILE *fp){ int index = 0; char buffer[100] = {0}; fread(buffer,1,1,fp); while(*(buffer + index++) != 0){ fread(buffer+index,1,1,fp); } output(buffer); output("\n"); return(strlen(buffer)+1);}void ReadNAMED_OBJECT(FILE *fp,stChunk* Chunk){ output("\n"); unsigned int characterlen = GetString(fp); Chunk->bytesRead += characterlen; while(Chunk->bytesRead < Chunk->length){ stChunk tempChunk = {0}; ReadChunk(fp,&tempChunk); DisplayChunkInfo(&tempChunk); switch(tempChunk.ID) { case OBJ_MESH: LoadMeshOBJ_MESH(fp,&tempChunk); break; default: SkipChunk(fp,&tempChunk); } Chunk->bytesRead += tempChunk.length; }}void ReadEDIT3DS(FILE *fp, stChunk* Chunk){ output("\n"); while(Chunk->bytesRead < Chunk->length){ stChunk tempChunk = {0}; ReadChunk(fp, &tempChunk); DisplayChunkInfo(&tempChunk); switch(tempChunk.ID){ case NAMED_OBJECT: ReadNAMED_OBJECT(fp, &tempChunk); break; default: SkipChunk(fp,&tempChunk); } Chunk-> bytesRead += tempChunk.length; }}/*read 3ds file*/void read3ds(){ FILE* pFile; pFile = fopen(FILENAME,"rb"); stChunk Chunk = {0}; ReadChunk(pFile,&Chunk); DisplayChunkInfo(&Chunk); while(Chunk.bytesRead< Chunk.length) { stChunk tempChunk = {0}; ReadChunk(pFile, &tempChunk); DisplayChunkInfo(&tempChunk); unsigned int bytesRead=0; switch(tempChunk.ID){ case EDIT3DS: ReadEDIT3DS(pFile,&tempChunk); bytesRead = tempChunk.length; break; case KEYF3DS: ReadKEYF3DS(pFile,&tempChunk); bytesRead = tempChunk.length; break; default: SkipChunk(pFile,&tempChunk); bytesRead = tempChunk.length; } Chunk.bytesRead += bytesRead; } fclose(pFile);}//OUTPUT!//Chunk ID: 0x 4d4d Size of Chunk: 355Chunk ID: 0x 0002 Size of Chunk: 10Chunk ID: 0x 3d3d Size of Chunk: 339Chunk ID: 0x afff Size of Chunk: 66Chunk ID: 0x 4000 Size of Chunk: 2670CubeChunk ID: 0x 4100 Size of Chunk: 255Chunk ID: 0x 4110 Size of Chunk: 104Number of Vertices 8 Vertices Point: x: 1.00, y: 1.00, z: 0.18 Vertices Point: x: 1.00, y: -1.00, z: 0.18 Vertices Point: x: -1.00, y: -1.00, z: 0.18 Vertices Point: x: -1.00, y: 1.00, z: 0.18 Vertices Point: x: 1.00, y: 1.00, z: 2.18 Vertices Point: x: 1.00, y: -1.00, z: 2.18 Vertices Point: x: -1.00, y: -1.00, z: 2.18 Vertices Point: x: -1.00, y: 1.00, z: 2.18Chunk ID: 0x 4120 Size of Chunk: 145Number of Faces 12 Face: Index1: 0, Index2: 1, Index3 2 Face: Index1: 0, Index2: 0, Index3 2 Face: Index1: 3, Index2: 0, Index3 4 Face: Index1: 7, Index2: 6, Index3 0 Face: Index1: 4, Index2: 6, Index3 5 Face: Index1: 0, Index2: 0, Index3 4 Face: Index1: 5, Index2: 0, Index3 0 Face: Index1: 5, Index2: 1, Index3 0 Face: Index1: 1, Index2: 5, Index3 6 Face: Index1: 0, Index2: 1, Index3 6 Face: Index1: 2, Index2: 0, Index3 2 Face: Index1: 6, Index2: 7, Index3 0