Original Post
Hello,
I have been studying the MD2 file format for the past couple hours and have tried writing a class to load and render one.
I was hoping I could get some feedback on my code. It isn''t working properly and at this point, I just want to be able to load and see it. No animation or even textures yet. (I''ve tried commenting out where I read in the texture coordinates and stuff but it still doesn''t work). Anyway I appreciate any feedback or explanations of the format or how to best read in the data and render it. Thanks!
void ModelMD2::Load(char *pFilename)
{
FILE *pFile;
char *pTextureName;
char *buffer;
int fileLength;
//open rb to read in as binary
pFile = fopen(pFilename, "rb");
//make sure it exists
if(!pFile) return;
//get length of file
fseek(pFile,0,SEEK_END);
fileLength = ftell(pFile);
fseek(pFile,0,SEEK_SET);
//read file into a buffer
buffer = new char[fileLength];
fread(buffer,sizeof(char),fileLength,pFile);
fclose(pFile);
//get the header from the buffer
m_pHeader = (HEADER*)buffer;
//allocate the frames and vertices
m_pFrames = new FRAME[m_pHeader->numFrames];
m_pVertices = new CUSTOMVERTEX3[m_pHeader->numVertices*m_pHeader->numFrames];
FRAME *frame;
for(int j=0;j<m_pHeader->numFrames;j++)
{
//get the frame
frame = (FRAME*)&buffer[m_pHeader->offsetFrames+(m_pHeader->framesize*j)];
//get the vertices for this frame
CUSTOMVERTEX3 *pVertices = (CUSTOMVERTEX3*)&m_pVertices[m_pHeader->numVertices*j];
//copy data from frame
for(int i=0;i<m_pHeader->numVertices;i++)
{
pVertices[i].position.x = frame->scale.x*frame->framePoint[i].vertex.x+
frame->translate.x;
pVertices[i].position.y = frame->scale.y*frame->framePoint[i].vertex.y+
frame->translate.y;
pVertices[i].position.z = frame->scale.y*frame->framePoint[i].vertex.z+
frame->translate.z;
}
}
//read the texture name and load texture
pTextureName = (char*)&buffer[m_pHeader->offsetSkins+m_pHeader->numSkins];
if(FAILED(D3DXCreateTextureFromFile(m_pd3dDevice,pTextureName,&m_d3dTexture)))
return;
//read texture coordinates
m_pTextureCoordinates = new TEXCOORD[m_pHeader->numTextureCoordinates];
TEXCOORDINDEX *tcIndex = (TEXCOORDINDEX*)&buffer[m_pHeader->offsetTextureCoords];
float maxTu, maxTv;
for(j=0;j<m_pHeader->numTextureCoordinates;j++)
{
//get texture coordinates
m_pTextureCoordinates[j].tu = (float)tcIndex[j].tu;
m_pTextureCoordinates[j].tv = (float)tcIndex[j].tv;
//find the max texture coordinate size
maxTu = max(maxTu, m_pTextureCoordinates[j].tu);
maxTv = max(maxTv, m_pTextureCoordinates[j].tv);
}
//fix the texture coordinates
for(j=0;j<m_pHeader->numTextureCoordinates;j++)
{
m_pTextureCoordinates[j].tu /= maxTu;
m_pTextureCoordinates[j].tv /= maxTv;
}
//read triangle index data
m_pTriangleIndices = new TRIANGLE[m_pHeader->numTriangles];
TRIANGLE *triangleIndex = (TRIANGLE*)&buffer[m_pHeader->offsetTriangles];
for(int i=0;i<m_pHeader->numTriangles;i++)
{
m_pTriangleIndices[i].indices[0] = triangleIndex[i].indices[0];
m_pTriangleIndices[i].indices[1] = triangleIndex[i].indices[1];
m_pTriangleIndices[i].indices[2] = triangleIndex[i].indices[2];
m_pTriangleIndices[i].textureCoordinates[0] = triangleIndex[i].textureCoordinates[0];
m_pTriangleIndices[i].textureCoordinates[1] = triangleIndex[i].textureCoordinates[1];
m_pTriangleIndices[i].textureCoordinates[2] = triangleIndex[i].textureCoordinates[2];
}
delete [] buffer;
}
/**
* Render the Model
*
* @param an integer that specifies the frame to render
*/
void ModelMD2::Render(int frameNumber)
{
if(frameNumber > m_pHeader->numFrames) return;
m_pd3dDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX3);
CUSTOMVERTEX3 *pVertices = &m_pVertices[m_pHeader->numVertices*frameNumber];
for(int j=0;j<m_pHeader->numTriangles;j++)
m_pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST,1,(BYTE**)&pVertices[j],
sizeof(D3DFVF_CUSTOMVERTEX3));
}