📄 md2.cpp
字号:
//***********************************************************************//
// //
// - "Talk to me like I'm a 3 year old!" Programming Lessons - //
// //
// $Author: DigiBen digiben@gametutorials.com //
// //
// $Program: MD2 Loader //
// //
// $Description: Demonstrates how to load a Quake2 MD2 Model //
// //
// $Date: 2/6/02 //
// //
//***********************************************************************//
#include "main.h"
#include "Md2.h"
/////////////////////////////////////////////////////////////////////////
//
// This file holds the code to load the Quake2 models from a .Md2 format.
// The .Md2 file is usually stored in a .zip file (don't let the extension
// fool you, just rename it to .zip), depending on where you get the models
// from. The CLoadMD2 class handles the loading, but we draw the model
// externally on our own in main.cpp. I created a converter function
// to convert to our already used model and object structures. This way
// eventually we can create a model library that can load any type of
// model that we support, as well as use inheritance to create a new class
// for each file format for the small things that each model format needs differently.
// Like the other loading tutorials, we calculate our own vertex normals.
// The .Md2 format is REALLY simple to load. That is why I chose it. The
// next tutorial will show how to load and animate Md2 files. Next, we
// will move from key frame animation to skeletal animation with the Quake3
// .Md3 files. This is also a wonderfuly easy format to load and use.
//
//
///////////////////////////////// CLOAD MD2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This constructor initializes the md2 structures
/////
///////////////////////////////// CLOAD MD2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
CLoadMD2::CLoadMD2()
{
// Here we initialize our structures to 0
memset(&m_Header, 0, sizeof(tMd2Header));
// Set the pointers to null
m_pSkins=NULL;
m_pTexCoords=NULL;
m_pTriangles=NULL;
m_pFrames=NULL;
}
///////////////////////////////// IMPORT MD2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This is called by the client to open the .Md2 file, read it, then clean up
/////
///////////////////////////////// IMPORT MD2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
bool CLoadMD2::ImportMD2(t3DModel *pModel, char *strFileName, char *strTexture)
{
char strMessage[255] = {0};
// Open the MD2 file in binary
m_FilePointer = fopen(strFileName, "rb");
// Make sure we have a valid file pointer (we found the file)
if(!m_FilePointer)
{
// Display an error message and don't load anything if no file was found
sprintf(strMessage, "Unable to find the file: %s!", strFileName);
MessageBox(NULL, strMessage, "Error", MB_OK);
return false;
}
// Just like most file formats, there is a header that needs to be read
// from the .Md2 format. If you look at the tMd2Header structure you will
// find all the data that will be read in. It's nice to know up front about
// the data that we will be reading. This makes it easy to just to large
// binary reads using fread, instead of counting and reading chunks.
// Read the header data and store it in our m_Header member variable
fread(&m_Header, 1, sizeof(tMd2Header), m_FilePointer);
// For some reason, .Md2 files MUST have a version of 8. I am not sure why,
// but if it doesn't there is something wrong and the header was read in
// incorrectly, or perhaps the file format is bad.
if(m_Header.version != 8)
{
// Display an error message for bad file format, then stop loading
sprintf(strMessage, "Invalid file format (Version not 8): %s!", strFileName);
MessageBox(NULL, strMessage, "Error", MB_OK);
return false;
}
// Now that we made sure the header had correct data, we want to read in the
// rest of the data. Once the data is read in, we need to convert it to our structures.
// Since we are only reading in the first frame of animation, there will only
// be ONE object in our t3DObject structure, held within our pModel variable.
ReadMD2Data();
// Here we pass in our model structure to it can store the read Quake data
// in our own model and object structure data
ConvertDataStructures(pModel);
// After we have read the whole MD2 file, we want to calculate our own vertex normals.
ComputeNormals(pModel);
// If there is a valid texture name passed in, we want to set the texture data
if(strTexture)
{
// Create a local material info structure
tMaterialInfo texture;
// Copy the name of the file into our texture file name variable
strcpy(texture.strFile, strTexture);
// Since there is only one texture for a .Md2 file, the ID is always 0
texture.texureId = 0;
// The tile or scale for the UV's is 1 to 1 (but Quake saves off a 0-256 ratio)
texture.uTile = texture.uTile = 1;
// We only have 1 material for a model
pModel->numOfMaterials = 1;
// Add the local material info to our model's material list
pModel->pMaterials.push_back(texture);
}
// Clean up after everything
CleanUp();
// Return a success
return true;
}
///////////////////////////////// READ MD2 DATA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This function reads in all of the model's data, except the animation frames
/////
///////////////////////////////// READ MD2 DATA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void CLoadMD2::ReadMD2Data()
{
// Create a larger buffer for the frames of animation (not fully used yet)
unsigned char buffer[MD2_MAX_FRAMESIZE];
int j = 0;
// Here we allocate all of our memory from the header's information
m_pSkins = new tMd2Skin [m_Header.numSkins];
m_pTexCoords = new tMd2TexCoord [m_Header.numTexCoords];
m_pTriangles = new tMd2Face [m_Header.numTriangles];
m_pFrames = new tMd2Frame [m_Header.numFrames];
// Next, we start reading in the data by seeking to our skin names offset
fseek(m_FilePointer, m_Header.offsetSkins, SEEK_SET);
// Depending on the skin count, we read in each skin for this model
fread(m_pSkins, sizeof(tMd2Skin), m_Header.numSkins, m_FilePointer);
// Move the file pointer to the position in the file for texture coordinates
fseek(m_FilePointer, m_Header.offsetTexCoords, SEEK_SET);
// Read in all the texture coordinates in one fell swoop
fread(m_pTexCoords, sizeof(tMd2TexCoord), m_Header.numTexCoords, m_FilePointer);
// Move the file pointer to the triangles/face data offset
fseek(m_FilePointer, m_Header.offsetTriangles, SEEK_SET);
// Read in the face data for each triangle (vertex and texCoord indices)
fread(m_pTriangles, sizeof(tMd2Face), m_Header.numTriangles, m_FilePointer);
// Move the file pointer to the vertices (frames)
fseek(m_FilePointer, m_Header.offsetFrames, SEEK_SET);
// Assign our alias frame to our buffer memory
tMd2AliasFrame *pFrame = (tMd2AliasFrame *) buffer;
// Allocate the memory for the first frame of animation's vertices
m_pFrames[0].pVertices = new tMd2Triangle [m_Header.numVertices];
// Read in the first frame of animation
fread(pFrame, 1, m_Header.frameSize, m_FilePointer);
// Copy the name of the animation to our frames array
strcpy(m_pFrames[0].strName, pFrame->name);
// After we have read in the data for the model, since there is animation,
// This means that there are scale and translation values to be dealt with.
// To apply the scale and translation values, we simply multiply the scale (x, y, z)
// by the current vertex (x, y, z). Also notice that we switch the Y and Z values
// so that Y is faces up, NOT Z.
// Store off a vertex array pointer to cut down large lines of code
tMd2Triangle *pVertices = m_pFrames[0].pVertices;
// Go through all of the number of vertices and assign the scale and translations.
// Store the vertices in our current frame's vertex list array, while swapping Y and Z.
// Notice we also negate the Z axis as well to make the swap correctly.
for (j=0; j < m_Header.numVertices; j++)
{
pVertices[j].vertex[0] = pFrame->aliasVertices[j].vertex[0] * pFrame->scale[0] + pFrame->translate[0];
pVertices[j].vertex[2] = -1 * (pFrame->aliasVertices[j].vertex[1] * pFrame->scale[1] + pFrame->translate[1]);
pVertices[j].vertex[1] = pFrame->aliasVertices[j].vertex[2] * pFrame->scale[2] + pFrame->translate[2];
}
}
///////////////////////////////// CONVERT DATA STRUCTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This function converts the .md2 structures to our own model and object structures
/////
///////////////////////////////// CONVERT DATA STRUCTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void CLoadMD2::ConvertDataStructures(t3DModel *pModel)
{
int j = 0, i = 0;
// Assign the number of objects, which is 1 since we only want 1 frame
// of animation. In the next tutorial each object will be a key frame
// to interpolate between.
pModel->numOfObjects = 1;
// Create a local object to store the first frame of animation's data
t3DObject currentFrame = {0};
// Assign the vertex, texture coord and face count to our new structure
currentFrame.numOfVerts = m_Header.numVertices;
currentFrame.numTexVertex = m_Header.numTexCoords;
currentFrame.numOfFaces = m_Header.numTriangles;
// Allocate memory for the vertices, texture coordinates and face data.
currentFrame.pVerts = new CVector3 [currentFrame.numOfVerts];
currentFrame.pTexVerts = new CVector2 [currentFrame.numTexVertex];
currentFrame.pFaces = new tFace [currentFrame.numOfFaces];
// Go through all of the vertices and assign them over to our structure
for (j=0; j < currentFrame.numOfVerts; j++)
{
currentFrame.pVerts[j].x = m_pFrames[0].pVertices[j].vertex[0];
currentFrame.pVerts[j].y = m_pFrames[0].pVertices[j].vertex[1];
currentFrame.pVerts[j].z = m_pFrames[0].pVertices[j].vertex[2];
}
// We can now free the old vertices stored in this frame of animation
delete m_pFrames[0].pVertices;
// Go through all of the uv coordinates and assign them over to our structure.
// The UV coordinates are not normal uv coordinates, they have a pixel ratio of
// 0 to 256. We want it to be a 0 to 1 ratio, so we divide the u value by the
// skin width and the v value by the skin height. This gives us our 0 to 1 ratio.
// For some reason also, the v coodinate is flipped upside down. We just subtract
// the v coordinate from 1 to remedy this problem.
for (j=0; j < currentFrame.numTexVertex; j++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -