Need some help with .md3 file loading

Started by
7 comments, last by Shadow1234567890 21 years, 4 months ago
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 www.gametutorials.com 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 gametutorials.com. 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);
getch();

//THIS IS WHERE WE ASSIGN THE OBJECTS TO GLOBAL VARIABLES SO WE CAN THEN CONFIRM REAL DATA

//strncpy(Skins[VectorIndices]->strName, pSkin->strName, 68);	//STRNAME = 68 BYTES


delete[] pSkin;
delete[] pTriangle;
delete[] pTexCoord;
delete[] pVertices;
 
VectorIndices++;
offset += pMesh.meshSize;
}
  
I''ve probably made tons of stupid errors, but if you could please help me out I would be very happy. thx
What do you mean ''Someday become a programmer?'' I'm a programmer right now!
Advertisement
you''re doing pointer assignments. try reading data directly into vectors instead.

and why on earth would you want to have vectors of pointers? use vectors of frames, vertices, etc. instead.
quote:
and why on earth would you want to have vectors of pointers? use vectors of frames, vertices, etc. instead.


so instead of
Mesh.push_back(new Mesh);
it would be
Mesh.push_back(Mesh); ?
I have only seen vectors used with pointers
What do you mean ''Someday become a programmer?'' I'm a programmer right now!
something along the lines of

int number_of_meshes_in_model = whatever;
meshes.resize(number_of_meshes_in_model);
fseek(to_meshes);
fread(&meshes[0]);
fread(&meshes[1]);
I don't see how any of these things could be the cause of my bogus data. The only thing I was having problems with was the triangle data. I have thought a little bit about your suggestions, and I have decided to have it read directly to the vectors, but I do not see any point in making any of the other changes because they seem more like preferences and not the cause of my problem. You don't think using pointers in the vectors could be causing the bogus data do you?? It seems it should be working just fine.

EDIT:
Interestingly making it read directly to the vectors did change what the app was producing for values, but these values (which are supposed to be coordinate values for the vertices of the triangles) go up into the thousands (i.e values well about 27,000) and I'm not sure how that's even possible.



[edited by - Shadow1234567890 on November 30, 2002 11:05:03 PM]
What do you mean ''Someday become a programmer?'' I'm a programmer right now!
quote:Original post by Shadow1234567890
You don''t think using pointers in the vectors could be causing the bogus data do you??

you''re putting address of a local variable into vector, presumably for future use. that''s garbage right there.

you''re copying pointers to i.e. triangle data to the vector, leaking the memory block that vector had before and accessing garbage data in the rest of your program, plus possibly deleting those pointers twice.

i suggest you read up on pointers before going any further, as you have little understanding of what you''re doing.
Wouldn't this have fixed that problem? Silly mistake on my part
Skins[VectorIndices] = *pSkin;
Faces[VectorIndices] = *pTriangle;
Textures[VectorIndices] = *pTexCoord;
Triangles[VectorIndices] = *pVertices;

And anyway like I said in my post I made it so it reads directly into the vectors, so I don't have to worry about that part anyway. I was questioning the necessity of the other changes. The whole vector thing was an error, but do I have to have resize or were you just suggesting it?
thanks

EDIT:
I assume the whole resize thing allows you to use vectors without pointers so you are allocating the memory similarly to how malloc does as opposed to using the new keyword. however this situation seems ideal to use pointers because there are structures that end up taking quite a bit of memory and will need to be passed into functions.







[edited by - Shadow1234567890 on November 30, 2002 12:18:48 AM]
What do you mean ''Someday become a programmer?'' I'm a programmer right now!
that''s why you should always pass vectors by reference.
I guess I am not very good with this stuff. I have a model structure, that in turn has a vector of objects (it is not a vector of pointers). Each object(mesh) has several attributes including an array of vertices (of type CVector3, technically it isn't a vector but seeing as how a CVector3 holys X, Y, and Z floats it is legal to use it as a vertex). Anyway I am resizing the vector of the model to equal the number of meshes in the model ( as you suggested ). My problem is allocating space for the vertices. Whenever I do the following it screws up what is being read from the file, instead of giving me values of under 50, it gives me some that are under 50, some that are between 50-100, and some up to 400!
m1.theObjects[location].pVertices = new CVector3[pMesh.numVertices];

So far I am not assigning anything to any of the vertices, just trying to allocate memory for them. But when I do this it screws up the values of the temporary vertices that are being read in.

If you don't know what I am talking about I'll post everything I have, but I'm looking for a generic solution to this stupid problem.



[edited by - Shadow1234567890 on December 5, 2002 3:18:43 PM]
What do you mean ''Someday become a programmer?'' I'm a programmer right now!

This topic is closed to new replies.

Advertisement