www.pudn.com > 林海血原源代码.zip > MD2Loader.cpp
// MD2Loader.cpp: implementation of the CMD2Loader class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include#include "MD2Loader.h" #include "Texture.h" #include "structDef.h" ////////////////////////////////////////////// ///////////////////////////////////////// bool CMD2Loader::LoadModel(char* filename) { FILE* file; MD2_HEADER header; if((file= fopen(filename, "rb"))==NULL) return false; fread(&header, sizeof(MD2_HEADER), 1, file); if(header.magic!= 844121161) return false; if(header.version!=8) return false; numVertices=header.numVertices; numFrames = header.numFrames; numGlCommands= header.numGlCommands; frameSize = header.frameSize; numTriangles = header.numTriangles; unsigned char *frames; frames= new unsigned char[header.frameSize*header.numFrames]; //Check to see if it allocated correctly if(frames==NULL) return false; //Zoom to the correct spot in the file fseek(file, header.offsetFrames, SEEK_SET); fread(frames, header.frameSize*header.numFrames*sizeof(char),1, file); pVertexData=new MD2_MODELVERTEX[numVertices*numFrames]; if(pVertexData ==NULL)return false; pTempFrame =new MD2_TEMPVERTEX [numVertices]; if(pTempFrame ==NULL)return false; for(int i=0;i vertices[j].vertex[0]* currentFrame->scale[0]+ currentFrame->translate[0]; pVertexData[i*numVertices+j].z=currentFrame->vertices[j].vertex[1]* currentFrame->scale[1]+ currentFrame->translate[1]; pVertexData[i*numVertices+j].y=currentFrame->vertices[j].vertex[2]* currentFrame->scale[2]+ currentFrame->translate[2]; pVertexData[i*numVertices+j].x=-pVertexData[i*numVertices+j].x*scale; pVertexData[i*numVertices+j].y=(pVertexData[i*numVertices+j].y)*scale; pVertexData[i*numVertices+j].z=pVertexData[i*numVertices+j].z*scale; pVertexData[i*numVertices+j].normalIndex=currentFrame->vertices[j].lightNormalIndex; } } delete [] frames; //Create space for the GL command information (whether or not to use tri strips, or tri fans) glCommands= new long [header.numGlCommands]; //Check to see if it allocated correctly if(glCommands==NULL) return false; //Zoom to the correct spot in the file fseek(file, header.offsetGlCommands, SEEK_SET); fread(glCommands, header.numGlCommands, sizeof(long), file); //Move the important information from the header, to the permanent class info. //Close 'da friggin file mon fclose(file); return true; } bool CMD2Loader::LoadTexture(char *filename) { CTexture cTexture; if(!cTexture.MakeTextureBind(filename,&textureID))return false; return true; } bool CMD2Loader::InitMd2Loader(char* Model,char* Texture,float nscale) { scale=nscale; if(Model==NULL || Texture==NULL)return false; if(!LoadModel(Model))return false; if(!LoadTexture(Texture))return false; PreProcess(); return true; } void CMD2Loader::PreProcess() { for(int i=0;i m_boundary.maxx)m_boundary.maxx=pVertexData[i].x; if(pVertexData[i].x m_boundary.maxy)m_boundary.maxy=pVertexData[i].y; if(pVertexData[i].y m_boundary.maxz)m_boundary.maxz=pVertexData[i].z; if(pVertexData[i].z (numFrames-1))numFrame=0; MD2_MODELVERTEX *pCurFrame=pVertexData+numFrame*numVertices; long* command; int vertIndex; int type; int numVertex; //Get the current frame and gl command information command = glCommands; ////////////////////////////////////////////// glBindTexture(GL_TEXTURE_2D, textureID); glEnable(GL_TEXTURE_2D); glCullFace(GL_FRONT); //Make sure that the command doesn't equal 0, and if it doesn't lets start rendering! while((*command)!=0) { if(*command>0) //This is a triangle strip { numVertex= *command; command++; type= 0; } else //This is a triangle fan { numVertex= - *command; command++; type= 1; } if(numVertex<0) numVertex= -numVertex; //Fill the vertex list information for(int loop=0; loop 0) //This is a triangle strip { numVertex= *command; command++; type= 0; } else //This is a triangle fan { numVertex= - *command; command++; type= 1; } if(numVertex<0) numVertex= -numVertex; //Fill the vertex list information for(int loop=0; loop