Thread: need help with .md3 file loading

  1. #1

    need help with .md3 file loading

    Hello there, I am trying to do .md3 file loading so I can load and then convert .md3 models. I have been using the tutorial from on .md3 loading. I have almost successfully made the load work, i.e information about the model is correct (# vertices, #triangles, header, bone, tag information all correct, i opened the model in milkshape to check this information). However the actual values for the x, y, and z coordinates of the triangles are bogus and I'm not sure how I'm readiang this data wrong. I have scrutinized my code and compared it with the gametutorials' code, but I'm not sure where I've gone wrong. I know that when working with file streams doing one thing wrong can throw everything off.

    anyway here is just the code that does the reading of the files. THis is from a console app I did to just read in the file and print to the screen the values (also writes it to another confirmation text file which should exactly mirror the jibberish found in the .md3 file).
    I am posting as little code as I possibly can. The defines are from the md3.h file found on I don't really expect anyone to reply to this post because I have hardly seen conversations on .md3 file loading.

    #include "Md3.h"
    #define Header tMd3Header
    #define Mesh tMd3MeshInfo
    #define Tag tMd3Tag
    #define Bone tMd3Bone
    #define Triangle tMd3Triangle
    #define Face tMd3Face
    #define TexCoord tMd3TexCoord
    #define Skin tMd3Skin
    vector<Mesh*>		Meshes;
    vector<Skin*>		Skins;
    vector<Face*>		Faces;
    vector<TexCoord*>	Textures;
    vector<Triangle*>	Triangles;
    int VectorIndices = 0;
    Header		pHeader;
    Mesh		pMesh;
    Tag			*pTag		= NULL;
    Bone		*pBone		= NULL;
    fread(&pHeader, 1, sizeof(Header), filePtr);
    pBone = new Bone[pHeader.numFrames];
    fread(pBone, sizeof(Bone), pHeader.numFrames, filePtr);
    delete[] pBone;
    pTag = new Tag[pHeader.numTags * pHeader.numFrames];
    fread(pTag, sizeof(Tag), pHeader.numTags * pHeader.numFrames, filePtr);
    long offset = ftell(filePtr);
    for(int index = 0; index < pHeader.numMeshes; index++) {
    	fseek(filePtr, offset, SEEK_SET);
    	fread(&pMesh, sizeof(Mesh), 1, filePtr);	//READ IN ALL OF THE MESH INFORMATION
    Skin		*pSkin		= new Skin[pMesh.numSkins];
    Face		*pTriangle	= new Face[pMesh.numTriangles];
    TexCoord	*pTexCoord	= new TexCoord[pMesh.numVertices];
    Triangle	*pVertices  = new Triangle[pMesh.numVertices * pMesh.numMeshFrames];
    	Meshes.push_back(new Mesh);
    	Skins.push_back(new Skin[pMesh.numSkins]);
    	Faces.push_back(new Face[pMesh.numTriangles]);
    	Textures.push_back(new TexCoord[pMesh.numVertices]);
    	Triangles.push_back(new Triangle[pMesh.numVertices * pMesh.numMeshFrames]);
    fread(pSkin, sizeof(Skin), pMesh.numSkins, filePtr);
    fseek(filePtr, offset + pMesh.triStart, SEEK_SET);
    fread(pTriangle, sizeof(Face), pMesh.numTriangles, filePtr);
    fseek(filePtr, offset + pMesh.uvStart, SEEK_SET);
    fread(pTexCoord, sizeof(TexCoord), pMesh.numVertices, filePtr);
    fseek(filePtr, offset + pMesh.vertexStart, SEEK_SET);
    fread(pVertices, sizeof(Triangle), pMesh.numVertices * pMesh.numMeshFrames, filePtr);
    Meshes[VectorIndices]		=	&pMesh;
    Skins[VectorIndices]		=	pSkin;
    Faces[VectorIndices]		=	pTriangle;
    Textures[VectorIndices]		=	pTexCoord;
    Triangles[VectorIndices]	=	pVertices;
    Confirm(pSkin, pTriangle, pTexCoord, pVertices, pMesh);
    //strncpy(Skins[VectorIndices]->strName, pSkin->strName, 68);	//STRNAME = 68 BYTES
    delete[] pSkin;
    delete[] pTriangle;
    delete[] pTexCoord;
    delete[] pVertices;
    offset += pMesh.meshSize;
    I've probably made tons of stupid errors, but if you could please help me out I would be very happy.


  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    I know this isn't really help, but its the best i can do, i don't know the md3 format and i dont have time to go over the code properly(it would take most of it without knowing the exact error) and learn the format so i'll list the most common "large" bug sources i've found in loading 3d models,

    as you said you read to much or to little somewhere and everything else is screwed, so make a very simple md3 maybe a single triangle or something very simple and go from there, go though each read and verify the data with the debugger, this is tedious sometimes but it works.

    also check your memory allocation carefully, since i've noticed some compilers allocate memory in a massive chunk so if you overwrite the only errors you get are in data validity, sometimes you dont even get a crash when to free the memory, so check you allocations carefully, make sure your allocating enough, triple check it. see if the data is valid at read time then gets invalidated when you read the next chunk of data.

    file integrity, when i was writing a loader i had the file open evey two seconds in notepad reading the contents and sometimes i unwittingly hit a key and close it quickly and accidentally save it like a dummy or something stupid like that and then the file's corrupt.

    and if all else fails stick with IFF compatible formats.

    these are the biggies... i can't think of anything more helpful now...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    that's helpful enough, im glad you have responded. what i was doing wrong was having the vector point to a memory address, and then i was deleting what it pointed to causing memory leaks. i am thinking about just convering the md3 model to .n3d format (linear, very easy to read) and just using that instead. the md3 format sucks. john carmack sucks (despite the fact he is the idol for most of us)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. archive format
    By Nor in forum A Brief History of
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM