📄 md2.cpp
字号:
#include <windows.h>
#include <cstdio>
#include <iostream>
#include <fstream>
#include "md2.h"
#include "gmdformat.h"
#include "../math/point.h"
#include "../math/math.h"
using namespace std;
////////////////////////////////////////////////////////////////////////////////////////////
GcMD2::GcMD2()
{
triIndex = NULL;
texCrds = NULL;
vertexList = NULL;
texture = NULL;
}
////////////////////////////////////////////////////////////////////////////////////////////
GcMD2::~GcMD2()
{
// Clear up the memory
Destroy();
}
////////////////////////////////////////////////////////////////////////////////////////////
bool GcMD2::Load(char *fileName, char *texName)
{
int fileLen;
byte *buffer;
MD2Header *header;
MD2Frame *frame;
int i,j;
// Open the model file
ifstream file(fileName, ios::in | ios::binary);
// Check for success
if(!file) {
return false;
}
// Get file lenght
file.seekg(0, ios::end);
fileLen = file.tellg();
file.seekg(0, ios::beg);
// Allocate memory for the buffer
buffer = new byte[fileLen+1];
// Read the file into the buffer
file.read((char*)buffer, fileLen);
// Extract the header
header = (MD2Header*)buffer;
/* Calculate and save the vertex data */
// Save important model data
numVertices = header->numVertices;
numFrames = header->numFrames;
frameSize = header->frameSize;
// Allocate memory for the vertex list
vertexList = new GcPoint3[numVertices * numFrames];
GcPoint3 *vertexStart;
for(i = 0; i < numFrames; i++)
{
// Extract the fram form the buffer
frame = (MD2Frame*)&buffer[header->offsetFrames + frameSize * i];
vertexStart = (GcPoint3*)&vertexList[numVertices * i];
for(j = 0; j < numVertices; j++)
{
// Calculate the vertex
vertexStart[j].x = frame->scale.x * frame->fp[j].v[0] + frame->translate.x;
vertexStart[j].y = frame->scale.y * frame->fp[j].v[1] + frame->translate.y;
vertexStart[j].z = frame->scale.z * frame->fp[j].v[2] + frame->translate.z;
}
}
/* Calculate and save the texture data */
// Create the texture
texture = new GcTexture[1];
// Loade the texture
texture[0].Create(texName);
// Save the number of texture coordinates
numTexCrds = header->numTexCoords;
// Allocate memory for the texture coordinates
texCrds = new GcPoint2[numTexCrds];
// Point to the begining of the texture coords
MD2TexInd *texStart = (MD2TexInd*)&buffer[header->offsetTexCoords];
// Calcualte the texutre coords
for(i = 0; i < numTexCrds; i++)
{
texCrds[i].x = (float)texStart[i].x / (float)texture[0].Width();
texCrds[i].y = (float)texStart[i].y / (float)texture[0].Height();
}
/* Calculate and save the triangle data */
// Get the number of trinagles
numTris = header->numTris;
// Allocate memory for the trinagles (well, the indicec for the tringles
triIndex = new MD2Tri[numTris];
// Point to the start of the triangle indices
MD2Tri *triStart = (MD2Tri*)&buffer[header->offsetTris];
// Save the trinagle indeces
for(i = 0; i < numTris; i++)
{
// Save the triangle indices
triIndex[i].triInd[0] = triStart[i].triInd[0];
triIndex[i].triInd[1] = triStart[i].triInd[1];
triIndex[i].triInd[2] = triStart[i].triInd[2];
// Save the texture coordinates indices of the trinagle
triIndex[i].texCrdInd[0] = triStart[i].texCrdInd[0];
triIndex[i].texCrdInd[1] = triStart[i].texCrdInd[1];
triIndex[i].texCrdInd[2] = triStart[i].texCrdInd[2];
}
/* Cleen up temporary files */
// Close the file
file.close();
// Free upp memory
delete [] buffer;
/* Set model vars */
currentFrame = 0;
nextFrame = 1;
interpol = 0.0f;
activeSkin = 0;
numSkins = 1;
/* Find the extreme points for the bounding box */
GcVector3 extreme(0, 0, 0);
GcPoint3 currPoint;
for(i = 0; i < numTris; i++)
{
// Get the first points from this fram and the next
currPoint.x = vertexList[triIndex[i].triInd[0]].x;
currPoint.y = vertexList[triIndex[i].triInd[0]].y;
currPoint.z = vertexList[triIndex[i].triInd[0]].z;
if(currPoint.x > extreme.x) {
extreme.x = currPoint.x;
}
if(currPoint.y > extreme.y) {
extreme.y = currPoint.y;
}
if(currPoint.z > extreme.z) {
extreme.z = currPoint.z;
}
}
g_Debug->Log("Extreme x: %f - y: %f - z: %f\n", extreme.x, extreme.y, extreme.z );
SetExtents(extreme);
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////
bool GcMD2::Load(char *fileName)
{
int fileLen;
char *buffer;
MD2Header *header;
MD2Frame *frame;
int i,j;
// Open the model file
ifstream file(fileName, ios::in | ios::binary);
// Check for success
if(!file) {
return false;
}
// Get file lenght
file.seekg(0, ios::end);
fileLen = file.tellg();
file.seekg(0, ios::beg);
// Allocate memory for the buffer
buffer = new char[fileLen+1];
// Read the file into the buffer
//file.read((char *)buffer, fileLen);
file.read(buffer, fileLen);
// Extract the header
header = (MD2Header*)buffer;
/* Calculate and save the vertex data */
// Save important model data
numVertices = header->numVertices;
numFrames = header->numFrames;
frameSize = header->frameSize;
// Allocate memory for the vertex list
vertexList = new GcPoint3[numVertices * numFrames];
GcPoint3 *vertexStart;
for(i = 0; i < numFrames; i++)
{
// Extract the fram form the buffer
frame = (MD2Frame*)&buffer[header->offsetFrames + frameSize * i];
vertexStart = (GcPoint3*)&vertexList[numVertices * i];
for(j = 0; j < numVertices; j++)
{
// Calculate the vertex
vertexStart[j].x = frame->scale.x * frame->fp[j].v[0] + frame->translate.x;
vertexStart[j].y = frame->scale.y * frame->fp[j].v[1] + frame->translate.y;
vertexStart[j].z = frame->scale.z * frame->fp[j].v[2] + frame->translate.z;
}
}
/* Calculate and save the texture data (load it now, to use when the other skins */
/* are loaded) */
// Save the number of texture coordinates
numTexCrds = header->numTexCoords;
// Allocate memory for the texture coordinates
texCrds = new GcPoint2[numTexCrds];
// Point to the begining of the texture coords
MD2TexInd *texStart = (MD2TexInd*)&buffer[header->offsetTexCoords];
// Calcualte the texutre coords
for(i = 0; i < numTexCrds; i++)
{
texCrds[i].x = (float)texStart[i].x;
texCrds[i].y = (float)texStart[i].y;
}
/* Calculate and save the triangle data */
// Get the number of trinagles
numTris = header->numTris;
// Allocate memory for the trinagles (well, the indicec for the tringles
triIndex = new MD2Tri[numTris];
// Point to the start of the triangle indices
MD2Tri *triStart = (MD2Tri*)&buffer[header->offsetTris];
// Save the trinagle indeces
for(i = 0; i < numTris; i++)
{
// Save the triangle indices
triIndex[i].triInd[0] = triStart[i].triInd[0];
triIndex[i].triInd[1] = triStart[i].triInd[1];
triIndex[i].triInd[2] = triStart[i].triInd[2];
// Save the texture coordinates indices of the trinagle
triIndex[i].texCrdInd[0] = triStart[i].texCrdInd[0];
triIndex[i].texCrdInd[1] = triStart[i].texCrdInd[1];
triIndex[i].texCrdInd[2] = triStart[i].texCrdInd[2];
}
/* Cleen up temporary files */
// Close the file
file.close();
// Free upp memory
delete [] buffer;
/* Set model vars */
currentFrame = 0;
nextFrame = 1;
interpol = 0.0f;
numSkins = 0;
/* Find the extreme points for the bounding box */
GcVector3 extreme(0, 0, 0);
GcPoint3 currPoint;
for(i = 0; i < numTris; i++)
{
// Get the first points from this fram and the next
currPoint.x = vertexList[triIndex[i].triInd[0]].x;
currPoint.y = vertexList[triIndex[i].triInd[0]].y;
currPoint.z = vertexList[triIndex[i].triInd[0]].z;
if(currPoint.x > extreme.x) {
extreme.x = currPoint.x;
}
if(currPoint.y > extreme.y) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -