#### Archived

This topic is now archived and is closed to further replies.

# Need some help with .md3 file loading

This topic is 5797 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts


#include "Md3.h"
#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;
Mesh		pMesh;
Tag			*pTag		= NULL;
Bone		*pBone		= NULL;
................................................

pBone = new Bone[pHeader.numFrames];
delete[] pBone;

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

##### Share on other sites
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.

##### Share on other sites
quote:

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

Mesh.push_back(new Mesh);
it would be
Mesh.push_back(Mesh); ?
I have only seen vectors used with pointers

##### Share on other sites
something along the lines of

int number_of_meshes_in_model = whatever;
meshes.resize(number_of_meshes_in_model);
fseek(to_meshes);

##### Share on other sites
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]

##### Share on other sites
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.

##### Share on other sites
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]

##### Share on other sites
that''s why you should always pass vectors by reference.

##### Share on other sites
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]

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632959
• Total Posts
3009458
• ### Who's Online (See full list)

There are no registered users currently online

×