Ms3dLoader.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:9k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. // CMs3dLoader.cpp: implementation of the CMs3dLoader2 class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Ms3dLoader.h"
  6. #include "stdio.h"
  7. #include "cmath.h"
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. // File header
  12. /////////////////////////////////////////////
  13. CMs3dLoader::CMs3dLoader()
  14. {
  15. m_numMeshes = 0;
  16. m_pMeshes = NULL;
  17. m_numTriangles = 0;
  18. m_pTriangles = NULL;
  19. m_numVertices = 0;
  20. m_pVertices = NULL;
  21. }
  22. CMs3dLoader::~CMs3dLoader()
  23. {
  24. unsigned short i;
  25. for ( i = 0; i < m_numMeshes; i++ )
  26. delete[] m_pMeshes[i].m_pTriangleIndices;
  27. m_numMeshes = 0;
  28. if ( m_pMeshes != NULL )
  29. {
  30. delete[] m_pMeshes;
  31. m_pMeshes = NULL;
  32. }
  33. m_numTriangles = 0;
  34. if ( m_pTriangles != NULL )
  35. {
  36. delete [] m_pTriangles;
  37. m_pTriangles = NULL;
  38. }
  39. m_numVertices = 0;
  40. if ( m_pVertices != NULL )
  41. {
  42. delete [] m_pVertices;
  43. m_pVertices = NULL;
  44. }
  45. }
  46. bool CMs3dLoader::Load( const char *filename,float scale )
  47. {
  48. FILE* file;
  49. //Open the .ms3d model file
  50. if((file= fopen(filename, "rb"))==NULL)
  51. return false;
  52. MS3DHeader Header ;
  53. fread(&Header.m_ID, sizeof(char), 10, file);
  54. fread(&Header.m_version, sizeof(int), 1, file);
  55. if ( strncmp( Header.m_ID, "MS3D000000", 10 ) != 0 )
  56. MessageBox(NULL,"id error","ERROR",MB_OK|MB_ICONEXCLAMATION);
  57. fclose(file);
  58. return false; // "Not a valid Milkshape3D model file."
  59. }
  60. if ( Header.m_version < 3 ||Header.m_version > 4 )
  61. {
  62. MessageBox(NULL,"version error","ERROR",MB_OK|MB_ICONEXCLAMATION);
  63. fclose(file);
  64. return false; // "Unhandled file version. Only Milkshape3D Version 1.3 and 1.4 is supported." );
  65. }
  66. fread(&m_numVertices, sizeof(unsigned short), 1, file);
  67. m_pVertices = new Vertex[m_numVertices];
  68. MS3DVertex ms3dVertex;
  69. for (unsigned short i = 0; i < m_numVertices; i++ )
  70. {
  71.         fread( &ms3dVertex.m_flags,    sizeof(unsigned char),1,file);
  72.         fread(  ms3dVertex.m_vertex,   sizeof(float),        3,file);
  73.         fread( &ms3dVertex.m_refCount, sizeof(unsigned char),1,file);
  74.         fread( &ms3dVertex.m_boneID,   sizeof(char),         1,file);
  75.         /////////////data copy
  76. m_pVertices[i].m_location[0]=ms3dVertex.m_vertex[0];
  77. m_pVertices[i].m_location[1]=ms3dVertex.m_vertex[1];
  78. m_pVertices[i].m_location[2]=ms3dVertex.m_vertex[2];
  79. ///////////////zoom
  80. m_pVertices[i].m_location[0] *=scale;
  81. m_pVertices[i].m_location[1] *=scale;
  82. m_pVertices[i].m_location[2] *=scale;
  83. /*
  84.   if(m_pVertices[i].m_location[0]>m_boundary.maxx)m_boundary.maxx=m_pVertices[i].m_location[0];
  85. if(m_pVertices[i].m_location[0]<m_boundary.minx)m_boundary.minx=m_pVertices[i].m_location[0];
  86. if(m_pVertices[i].m_location[1]>m_boundary.maxy)m_boundary.maxy=m_pVertices[i].m_location[1];
  87. if(m_pVertices[i].m_location[1]<m_boundary.miny)m_boundary.miny=m_pVertices[i].m_location[1];
  88. if(m_pVertices[i].m_location[2]>m_boundary.maxz)m_boundary.maxz=m_pVertices[i].m_location[2];
  89. if(m_pVertices[i].m_location[2]<m_boundary.minz)m_boundary.minz=m_pVertices[i].m_location[2];
  90. */
  91. }
  92.     ///////////read Triangles///////////////////////////
  93.     fread(&m_numTriangles, sizeof(unsigned short), 1, file);
  94. m_pTriangles = new Triangle[m_numTriangles];
  95.     MS3DTriangle ms3dTriangle;
  96. CMath  cmath;
  97. for ( i = 0; i < m_numTriangles; i++ )
  98. {
  99.         fread(&ms3dTriangle.m_flags,            sizeof(unsigned short),   1, file);
  100.         fread( ms3dTriangle.m_vertexIndices,    sizeof(unsigned short),   3, file);
  101.         fread( ms3dTriangle.m_vertexNormals[0], sizeof(float), 3, file);
  102.         fread( ms3dTriangle.m_vertexNormals[1], sizeof(float), 3, file);
  103.         fread( ms3dTriangle.m_vertexNormals[2], sizeof(float), 3, file);
  104.         fread( ms3dTriangle.m_u,                sizeof(float), 3, file);
  105.         fread( ms3dTriangle.m_v,                sizeof(float), 3, file);
  106.   
  107.         fread(&ms3dTriangle.m_smoothingGroup,   sizeof(unsigned char), 1, file);
  108.         fread(&ms3dTriangle.m_groupIndex  ,     sizeof(unsigned char), 1, file);
  109.         //////////////////////////////////////////////////////////////////////
  110. int v1,v2,v3;
  111. v1=ms3dTriangle.m_vertexIndices[0];
  112. v2=ms3dTriangle.m_vertexIndices[1];
  113. v3=ms3dTriangle.m_vertexIndices[2];
  114.         NORMAL normal=cmath.GetNormal(m_pVertices[v1].m_location,
  115.                           m_pVertices[v2].m_location,
  116.               m_pVertices[v3].m_location);
  117. m_pTriangles[i].m_normal[0]=normal.nx;
  118. m_pTriangles[i].m_normal[1]=normal.ny;
  119. m_pTriangles[i].m_normal[2]=normal.nz;
  120. memcpy( m_pTriangles[i].m_u, ms3dTriangle.m_u, sizeof( float )*3 );
  121. memcpy( m_pTriangles[i].m_v, ms3dTriangle.m_v, sizeof( float )*3 );
  122. memcpy( m_pTriangles[i].m_vertexIndices, ms3dTriangle.m_vertexIndices, sizeof(unsigned short )*3 );
  123. }
  124.     //////////////read Meshes/////////////
  125.     fread(&m_numMeshes, sizeof(unsigned short), 1, file);
  126. m_pMeshes = new Mesh[m_numMeshes];
  127. MS3DMesh ms3dMesh ;
  128. ////////////allocate maximum memory : m_numTriangles
  129. ms3dMesh.m_TriangleIndices = new unsigned short [m_numTriangles];
  130. for ( i = 0; i < m_numMeshes; i++ )
  131. {
  132. fread(&ms3dMesh.m_flags,        sizeof(unsigned char), 1,  file);
  133. fread( ms3dMesh.m_name,         sizeof(char),          32, file);
  134. fread(&ms3dMesh.m_numTriangles, sizeof(unsigned short ),           1,  file);
  135. fread( ms3dMesh.m_TriangleIndices, sizeof(unsigned short), ms3dMesh.m_numTriangles, file);
  136. fread(&ms3dMesh.m_MaterialIndex, sizeof(char),  1, file);
  137.         //////////////////////copy data
  138. m_pMeshes[i].m_pTriangleIndices=new unsigned short [ms3dMesh.m_numTriangles];
  139. memcpy( m_pMeshes[i].m_pTriangleIndices,ms3dMesh.m_TriangleIndices, sizeof( unsigned short )*ms3dMesh.m_numTriangles );
  140.     m_pMeshes[i].m_textureIndex=ms3dMesh.m_MaterialIndex;
  141.     m_pMeshes[i].m_numTriangles=ms3dMesh.m_numTriangles;
  142. }
  143. delete [] ms3dMesh.m_TriangleIndices;
  144.     ////////////////read texture/////////////
  145.     fclose(file);
  146. Process();
  147.     
  148. return true;
  149. }
  150. void CMs3dLoader::Process()
  151. {
  152. float bias[3]={m_pVertices[51].m_location[0],
  153.            m_pVertices[51].m_location[1],
  154.                    m_pVertices[51].m_location[2]};
  155. for (unsigned short i = 0; i < m_numVertices; i++ )
  156. {
  157. m_pVertices[i].m_location[0] -= bias[0];
  158. m_pVertices[i].m_location[1] -= bias[1];
  159. m_pVertices[i].m_location[2] -= bias[2];
  160. }
  161. for ( i = 0; i < m_numVertices; i++ )
  162. {
  163.   if(m_pVertices[i].m_location[0]>m_boundary.maxx)m_boundary.maxx=m_pVertices[i].m_location[0];
  164. if(m_pVertices[i].m_location[0]<m_boundary.minx)m_boundary.minx=m_pVertices[i].m_location[0];
  165. if(m_pVertices[i].m_location[1]>m_boundary.maxy)m_boundary.maxy=m_pVertices[i].m_location[1];
  166. if(m_pVertices[i].m_location[1]<m_boundary.miny)m_boundary.miny=m_pVertices[i].m_location[1];
  167. if(m_pVertices[i].m_location[2]>m_boundary.maxz)m_boundary.maxz=m_pVertices[i].m_location[2];
  168. if(m_pVertices[i].m_location[2]<m_boundary.minz)m_boundary.minz=m_pVertices[i].m_location[2];
  169. }
  170. /*
  171.   m_boundary.maxx=m_pVertices[65].m_location[0];
  172. m_boundary.minx=m_pVertices[51].m_location[0];
  173. m_boundary.maxy=m_pVertices[48].m_location[1];
  174. m_boundary.miny=m_pVertices[51].m_location[1];
  175. m_boundary.maxz=m_pVertices[71].m_location[2];
  176. m_boundary.minz=m_pVertices[51].m_location[2];
  177. */
  178. }
  179. VERTEX CMs3dLoader::GetVertex(int number)
  180. {
  181. if(number<0 || number>(m_numVertices-1) )number=0;
  182. return VERTEX(m_pVertices[number].m_location[0],
  183.           m_pVertices[number].m_location[1],
  184.   m_pVertices[number].m_location[2]);
  185. }
  186. void CMs3dLoader::Render() 
  187. {
  188. for ( int i = 0; i < m_numMeshes; i++ )
  189. {
  190. glBegin( GL_TRIANGLES );
  191. {
  192. for ( int j = 0; j < m_pMeshes[i].m_numTriangles; j++ )
  193. {
  194. int triangleIndex = m_pMeshes[i].m_pTriangleIndices[j];
  195.     for ( int k = 0; k < 3; k++ )
  196. {
  197.      int index = m_pTriangles[triangleIndex].m_vertexIndices[k];
  198.     glNormal3fv( m_pTriangles[triangleIndex].m_normal );
  199.     glTexCoord2f( m_pTriangles[triangleIndex].m_u[k], m_pTriangles[triangleIndex].m_v[k] );
  200.     glVertex3fv( m_pVertices[index].m_location );
  201. }
  202. }
  203. }
  204. glEnd();
  205. }
  206. //  DrawBoundary();
  207. }
  208. void CMs3dLoader::DrawBoundary()
  209. {
  210. glDisable(GL_TEXTURE_2D);
  211. glColor3f(1,1,1);
  212. glBegin(GL_LINE_STRIP);
  213.     glVertex3f(m_boundary.minx,m_boundary.miny,m_boundary.minz);
  214.     glVertex3f(m_boundary.maxx,m_boundary.miny,m_boundary.minz);
  215.     glVertex3f(m_boundary.maxx,m_boundary.miny,m_boundary.maxz);
  216.     glVertex3f(m_boundary.minx,m_boundary.miny,m_boundary.maxz);
  217.     glVertex3f(m_boundary.minx,m_boundary.miny,m_boundary.minz);
  218.     glVertex3f(m_boundary.minx,m_boundary.maxy,m_boundary.minz);
  219.     glVertex3f(m_boundary.maxx,m_boundary.maxy,m_boundary.minz);
  220.     glVertex3f(m_boundary.maxx,m_boundary.maxy,m_boundary.maxz);
  221.     glVertex3f(m_boundary.minx,m_boundary.maxy,m_boundary.maxz);
  222.     glVertex3f(m_boundary.minx,m_boundary.maxy,m_boundary.minz);
  223. glEnd();
  224. glBegin(GL_LINES);
  225.     glVertex3f(m_boundary.maxx,m_boundary.miny,m_boundary.minz);
  226.     glVertex3f(m_boundary.maxx,m_boundary.maxy,m_boundary.minz);
  227.     glVertex3f(m_boundary.maxx,m_boundary.miny,m_boundary.maxz);
  228.     glVertex3f(m_boundary.maxx,m_boundary.maxy,m_boundary.maxz);
  229.     glVertex3f(m_boundary.minx,m_boundary.miny,m_boundary.maxz);
  230.     glVertex3f(m_boundary.minx,m_boundary.maxy,m_boundary.maxz);
  231. glEnd();
  232. glColor3f(1,1,1);
  233. }