#137587 - yellowstar - Mon Aug 13, 2007 7:42 pm
I am trying to write a 3DS loader.
But it won't work.
I am doing an tutorial,
which this is based on.
Here's my problem:
My loader isn't detecting the EDIT3DS chunk.(This chunk contains
sub-chunks which contains the actual model data.)
Like that tutorial,(at the point I am at)
it only sends output to a text file,
not data structures.
Here's the output from it:(from the text file it writes to)
(Updated. Now it isn't detecting all of the chunks.
It started doing this since I fixed the SkipChunk function.)
Here's how I am doing it:(source code)
Here's the important code:
This is the whole thing,
with some parts removed:
Last edited by yellowstar on Sat Sep 01, 2007 2:37 am; edited 4 times in total
But it won't work.
I am doing an tutorial,
which this is based on.
Here's my problem:
My loader isn't detecting the EDIT3DS chunk.(This chunk contains
sub-chunks which contains the actual model data.)
Like that tutorial,(at the point I am at)
it only sends output to a text file,
not data structures.
Here's the output from it:(from the text file it writes to)
(Updated. Now it isn't detecting all of the chunks.
It started doing this since I fixed the SkipChunk function.)
Code: |
ChunkID: 0x 4d4d Length: 693 ChunkID: 0x 0002 Length: 10 ChunkID: 0x 0000 Length: 671038 |
Here's how I am doing it:(source code)
Here's the important code:
Code: |
//See the below piece of code(the whole thing) for //the structures. void ReadChunk(FILE *f, ChunkHeader *header) { unsigned short ID = 0; unsigned int bytesRead = 0; unsigned int bChunkLength = 0; bytesRead = fread(&ID,2,1,f); bytesRead += fread(&bChunkLength,4,1,f); header->ChunkID = ID; header->Length = bChunkLength; header->bytesRead = bytesRead; } void SkipChunk(FILE *f, ChunkHeader *header) { //Updated. Was wrong. Since //I updated this it won't detect all the chunks. fseek(f, ((long)header->Length - (long)header->bytesRead), SEEK_CUR); } void ReadEDIT3DS(FILE *f, ChunkHeader *header) { fprintf(debug,"\n"); fprintf(debug,"EDIT3DS Chunk\n"); while(header->bytesRead < header->Length) { ChunkHeader temp; ReadChunk(f, &temp); DisplayChunkInfo(&temp); switch(temp.ChunkID) { case NAMED_OBJECT: ReadNAMED_OBJECT(f,&temp); break; default: SkipChunk(f,&temp); } header->bytesRead+= temp.Length; } } //The code below is the while loop which reads in the 3DS file. while(chunk.header.bytesRead < chunk.header.Length) { ChunkHeader temp; ReadChunk(f,&temp); DisplayChunkInfo(&temp); unsigned int bytesRead = 0; switch(temp.ChunkID) { case EDIT3DS: ReadEDIT3DS(f,&temp); bytesRead = temp.Length; break; default: SkipChunk(f,&temp); bytesRead = temp.Length; break; } chunk.header.bytesRead += bytesRead; } |
This is the whole thing,
with some parts removed:
Code: |
#include <stdio.h> #include <string.h> #include "3DSloaderChunkDefs.h" bool Load3DS(char *filename); bool Load3DS(char *filename, unsigned char *ErrorCode); struct ChunkHeader { unsigned short ChunkID; unsigned int Length; unsigned int bytesRead;//NOT a real field in an 3DS Chunk header. //This keeps track of how many bytes were read from this chunk. }; struct Chunk { ChunkHeader header; unsigned char *data; }; FILE *debug = fopen("Debug.txt","w"); unsigned int GetFileLength(FILE *file) { int l_iSavPos, l_iEnd; l_iSavPos = ftell(file); fseek(file, 0, SEEK_END); l_iEnd = ftell(file); fseek(file, l_iSavPos, SEEK_SET); return (unsigned int)l_iEnd; } void DisplayChunkInfo(ChunkHeader *ptr) { fprintf(debug,"ChunkID: 0x %04x Length: %u\n",ptr->ChunkID,ptr->Length); } void ReadChunk(FILE *f, ChunkHeader *header) { unsigned short ID = 0; unsigned int bytesRead = 0; unsigned int bChunkLength = 0; bytesRead = fread(&ID,2,1,f); bytesRead += fread(&bChunkLength,4,1,f); header->ChunkID = ID; header->Length = bChunkLength; header->bytesRead = bytesRead; } void SkipChunk(FILE *f, ChunkHeader *header) { fseek(f, ((long)header->Length - (long)header->bytesRead), SEEK_CUR); } void ReadEDIT3DS(FILE *f, ChunkHeader *header) { fprintf(debug,"\n"); fprintf(debug,"EDIT3DS Chunk\n"); while(header->bytesRead < header->Length) { ChunkHeader temp; ReadChunk(f, &temp); DisplayChunkInfo(&temp); switch(temp.ChunkID) { case NAMED_OBJECT: ReadNAMED_OBJECT(f,&temp); break; default: SkipChunk(f,&temp); } header->bytesRead+= temp.Length; } } bool Load3DS(char *filename) { Load3DS(filename,NULL); } bool Load3DS(char *filename, unsigned char *ErrorCode) FILE *f = fopen(filename,"rb"); Chunk chunk; unsigned int FileSize; bool do_errors = false; if(ErrorCode!=NULL) do_errors = true; if(ErrorCode==NULL) do_errors = false; if(do_errors) { *ErrorCode = 0;//For success. } if(f==NULL) { if(do_errors) *ErrorCode = 1;//Failed to open 3DS file. fprintf(debug,"ERROR: Failed to open 3DS file!\n\n"); return 0; } memset(&chunk,0,sizeof(Chunk)); FileSize = GetFileLength(f); ReadChunk(f,&chunk.header); DisplayChunkInfo(&chunk.header); if(chunk.header.ChunkID != PRIMARY || chunk.header.Length != FileSize) { fprintf(debug,"ERROR: Bad 3DS file!\n\n"); fclose(debug); fclose(f); if(do_errors) *ErrorCode = 2;//Bad 3DS file! return 0; } while(chunk.header.bytesRead < chunk.header.Length) { ChunkHeader temp; ReadChunk(f,&temp); DisplayChunkInfo(&temp); unsigned int bytesRead = 0; switch(temp.ChunkID) { case EDIT3DS: ReadEDIT3DS(f,&temp); bytesRead = temp.Length; break; default: SkipChunk(f,&temp); bytesRead = temp.Length; break; } chunk.header.bytesRead += bytesRead; } fclose(debug); fclose(f); return 1; } |
Last edited by yellowstar on Sat Sep 01, 2007 2:37 am; edited 4 times in total