www.pudn.com > 林海血原源代码.zip > Ms3dLoader.cpp
// CMs3dLoader.cpp: implementation of the CMs3dLoader2 class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Ms3dLoader.h"
#include "stdio.h"
#include "texture.h"
#include "cmath.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// File header
/////////////////////////////////////////////
CMs3dLoader::CMs3dLoader()
{
m_numMeshes = 0;
m_pMeshes = NULL;
m_numTriangles = 0;
m_pTriangles = NULL;
m_numVertices = 0;
m_pVertices = NULL;
m_numTexture=0;
m_pTexture=NULL;
}
CMs3dLoader::~CMs3dLoader()
{
unsigned short i;
for ( i = 0; i < m_numMeshes; i++ )
delete[] m_pMeshes[i].m_pTriangleIndices;
m_numMeshes = 0;
if ( m_pMeshes != NULL )
{
delete[] m_pMeshes;
m_pMeshes = NULL;
}
m_numTriangles = 0;
if ( m_pTriangles != NULL )
{
delete [] m_pTriangles;
m_pTriangles = NULL;
}
m_numVertices = 0;
if ( m_pVertices != NULL )
{
delete [] m_pVertices;
m_pVertices = NULL;
}
m_numTexture=0;
if(m_pTexture != NULL)
{
delete [] m_pTexture;
m_pTexture = NULL;
}
}
bool CMs3dLoader::Load( const char *filename )
{
FILE* file;
//Open the .ms3d model file
if((file= fopen(filename, "rb"))==NULL)
return false;
MS3DHeader Header ;
fread(&Header.m_ID, sizeof(char), 10, file);
fread(&Header.m_version, sizeof(int), 1, file);
if ( strncmp( Header.m_ID, "MS3D000000", 10 ) != 0 )
{
MessageBox(NULL,"id error","ERROR",MB_OK|MB_ICONEXCLAMATION);
fclose(file);
return false; // "Not a valid Milkshape3D model file."
}
if ( Header.m_version < 3 ||Header.m_version > 4 )
{
MessageBox(NULL,"version error","ERROR",MB_OK|MB_ICONEXCLAMATION);
fclose(file);
return false; // "Unhandled file version. Only Milkshape3D Version 1.3 and 1.4 is supported." );
}
fread(&m_numVertices, sizeof(unsigned short), 1, file);
m_pVertices = new Vertex[m_numVertices];
MS3DVertex ms3dVertex;
for (unsigned short i = 0; i < m_numVertices; i++ )
{
fread( &ms3dVertex.m_flags, sizeof(unsigned char),1,file);
fread( ms3dVertex.m_vertex, sizeof(float), 3,file);
fread( &ms3dVertex.m_refCount, sizeof(unsigned char),1,file);
fread( &ms3dVertex.m_boneID, sizeof(char), 1,file);
/////////////data copy
memcpy( m_pVertices[i].m_location, ms3dVertex.m_vertex, sizeof( float )*3 );
///////////////zoom
m_pVertices[i].m_location[0] *=1.5f;
m_pVertices[i].m_location[1] *=1.5f;
m_pVertices[i].m_location[2] *=1.5f;
if(m_pVertices[i].m_location[0]>m_boundary.maxx)m_boundary.maxx=m_pVertices[i].m_location[0];
if(m_pVertices[i].m_location[0]m_boundary.maxy)m_boundary.maxy=m_pVertices[i].m_location[1];
if(m_pVertices[i].m_location[1]m_boundary.maxz)m_boundary.maxz=m_pVertices[i].m_location[2];
if(m_pVertices[i].m_location[2]0)return false;
return true;
}
void CMs3dLoader::DrawBoundary()
{
glDisable(GL_TEXTURE_2D);
glColor3f(1,1,1);
glBegin(GL_LINE_STRIP);
glVertex3f(m_boundary.minx,m_boundary.miny,m_boundary.minz);
glVertex3f(m_boundary.maxx,m_boundary.miny,m_boundary.minz);
glVertex3f(m_boundary.maxx,m_boundary.miny,m_boundary.maxz);
glVertex3f(m_boundary.minx,m_boundary.miny,m_boundary.maxz);
glVertex3f(m_boundary.minx,m_boundary.miny,m_boundary.minz);
glVertex3f(m_boundary.minx,m_boundary.maxy,m_boundary.minz);
glVertex3f(m_boundary.maxx,m_boundary.maxy,m_boundary.minz);
glVertex3f(m_boundary.maxx,m_boundary.maxy,m_boundary.maxz);
glVertex3f(m_boundary.minx,m_boundary.maxy,m_boundary.maxz);
glVertex3f(m_boundary.minx,m_boundary.maxy,m_boundary.minz);
glEnd();
glBegin(GL_LINES);
glVertex3f(m_boundary.maxx,m_boundary.miny,m_boundary.minz);
glVertex3f(m_boundary.maxx,m_boundary.maxy,m_boundary.minz);
glVertex3f(m_boundary.maxx,m_boundary.miny,m_boundary.maxz);
glVertex3f(m_boundary.maxx,m_boundary.maxy,m_boundary.maxz);
glVertex3f(m_boundary.minx,m_boundary.miny,m_boundary.maxz);
glVertex3f(m_boundary.minx,m_boundary.maxy,m_boundary.maxz);
glEnd();
glColor3f(1,1,1);
}