Original Post
Hi, I've been working on this MD2 loader for the past 2 days, so far, I can load the vertices and normals. However, whenever I try to load the texture, it becomes very distorted. So, I decided to create as many vertices as there are indices, creating a total of (number of triangles * 3) vertices. The problem came when, 10-15 seconds into program execution, the entire computer freezes, restarts, and overwrites the current source file I'm working on with some sort of data dump!, which has forced me to rewrite at least 2 member function files over the day. Anyway, here's the source for the loader function: I'm almost positive that the error lies in the number of vertices. However, I have no clue how to fix it! Thanks for any help. Asterisk Man PS: How the heck to I make the above code snippet into a code snippet on a post? Edited by Coder: Used source tags. Please check the GDNet Forums FAQ [Edited by - Coder on December 30, 2004 6:08:12 AM]
UINT GAnimatedMeshMD2::LoadMD2File(char* filename)
{
FILE* model_file = 0;
model_t model_header;
triangle_t model_triangles[MD2_MAX_TRIANGLES];
frame_t model_frame;
VERTEX* processed_vertices = 0; // Where we convert compressed data into regular coordinates.
VERTEX* final_vertices = 0; // Where we put the final mesh.
UINT model_indices[MD2_MAX_TRIANGLES * 3]; // Indices for the mesh;
textureCoordinate_t model_tcoords[MD2_MAX_TCOORDS];
// Try to open the file.
if((model_file = fopen(filename, "rb")) == 0)
{
return GERR_OTHER;
}
// Read the header.
fread(&model_header, sizeof(model_t), 1, model_file);
// Check if this is a valid MD2 file.
if(model_header.magic != 844121161)
{
return GERR_OTHER;
}
// Allocate memory for the vertex buffers we will be using.
processed_vertices = new VERTEX[model_header.numVertices];
final_vertices = new VERTEX[model_header.numTriangles * 3];
// Read triangle data.
fseek(model_file, model_header.offsetTriangles, 0);
fread(model_triangles, sizeof(triangle_t), model_header.numTriangles, model_file);
// Read texture coordinate data
fseek(model_file, model_header.numTexCoords, 0);
fread(model_tcoords, sizeof(textureCoordinate_t), model_header.numTexCoords, model_file);
// Prepare the frame structure for holding the vertex data.
model_frame.vertices = new triangleVertex_t[model_header.numVertices];
// Load 1 frame and its vertices, since it is not a fixed size,
// we need to load each field individually.
fseek(model_file, model_header.offsetFrames, 0);
fread(&model_frame.scale, sizeof(float), 3, model_file); // Scale data
fread(&model_frame.translate, sizeof(float), 3, model_file); // Translation data.
fread(&model_frame.name, sizeof(char), 16, model_file); // Frame name
fread(model_frame.vertices, sizeof(triangleVertex_t), model_header.numVertices, model_file); // Vertex data.
// Now, we begin processing the data, this is the fun part!
for(int i = 0; i < model_header.numVertices; i++)
{
// Expand XYZ byte coordinates into real coordinates.
processed_vertices.x = (model_frame.vertices.vertex[0] * model_frame.scale[0]) + model_frame.translate[0];
processed_vertices.z = (model_frame.vertices.vertex[1] * model_frame.scale[1]) + model_frame.translate[1];
processed_vertices.y = (model_frame.vertices.vertex[2] * model_frame.scale[2]) + model_frame.translate[2];
// Get the normals from the table in the .h file and invert them.
processed_vertices.nx = Q2_VERTEX_NORMAL_TABLE[model_frame.vertices.lightNormalIndex][0] * -1;
processed_vertices.nz = Q2_VERTEX_NORMAL_TABLE[model_frame.vertices.lightNormalIndex][1] * -1;
processed_vertices.ny = Q2_VERTEX_NORMAL_TABLE[model_frame.vertices.lightNormalIndex][2] * -1;
}
#ifdef MD2_DEBUG
// Create indices for the frame now.
for(i = 0; i < model_header.numTriangles; i++)
{
model_indices[(3 * i) + 0] = model_triangles.vertexIndices[0];
model_indices[(3 * i) + 1] = model_triangles.vertexIndices[1];
model_indices[(3 * i) + 2] = model_triangles.vertexIndices[2];
}
// Get the vertex count and index count.
VertexCount = model_header.numVertices;
IndexCount = model_header.numTriangles * 3;
// Now we create the vertex and the index buffer.
VertexBuffer = parent->getVideoDriver()->CreateVB(processed_vertices, VertexCount);
IndexBuffer = parent->getVideoDriver()->CreateIB(model_indices, IndexCount);
#endif
#ifndef MD2_DEBUG
// Create separate vertices for each triangle, thus allowing
// correct texturing.
for(i = 0; i < model_header.numTriangles; i++)
{
final_vertices[(3 * i) + 0] = processed_vertices[model_triangles.vertexIndices[0]];
final_vertices[(3 * i) + 1] = processed_vertices[model_triangles.vertexIndices[1]];
final_vertices[(3 * i) + 2] = processed_vertices[model_triangles.vertexIndices[2]];
final_vertices[(3 * i) + 0].tu = model_tcoords[model_triangles.textureIndices[0]].s/(float) model_header.skinWidth;
final_vertices[(3 * i) + 0].tv = model_tcoords[model_triangles.textureIndices[0]].t/(float) model_header.skinHeight;
final_vertices[(3 * i) + 1].tu = model_tcoords[model_triangles.textureIndices[1]].s/(float) model_header.skinWidth;
final_vertices[(3 * i) + 1].tv = model_tcoords[model_triangles.textureIndices[1]].t/(float) model_header.skinHeight;
final_vertices[(3 * i) + 2].tu = model_tcoords[model_triangles.textureIndices[2]].s/(float) model_header.skinWidth;
final_vertices[(3 * i) + 2].tv = model_tcoords[model_triangles.textureIndices[2]].t/(float) model_header.skinHeight;
model_indices[(3 * i) + 0] = (3 * i) + 0;
model_indices[(3 * i) + 1] = (3 * i) + 1;
model_indices[(3 * i) + 2] = (3 * i) + 2;
}
VertexCount = (model_header.numTriangles * 3);
IndexCount = model_header.numTriangles * 3;
VertexBuffer = parent->getVideoDriver()->CreateVB(final_vertices, VertexCount);
IndexBuffer = parent->getVideoDriver()->CreateIB(model_indices, IndexCount);
#endif
// Since we are done, we really should clean up, and leave.
delete processed_vertices;
delete final_vertices;
fclose(model_file);
return G_OK;
}