msModel.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ///////////////////////////////////////////////////////////////////////
  2. // title: MilkShape 3D Model Viewer Sample
  3. //
  4. // copyright: The programs and associated files contained in this
  5. // distribution were developed by Mete Ciragan. The
  6. // programs are not in the public domain, but they are
  7. // freely distributable without licensing fees. These
  8. // programs are provided without guarantee or warrantee
  9. // expressed or implied. Use at your own risk!
  10. //
  11. // email: [email protected]
  12. // web: http://www.milkshape3d.com
  13. ///////////////////////////////////////////////////////////////////////
  14. #ifndef _MSMODEL_H_
  15. #define _MSMODEL_H_
  16. #define MAX_VERTICES 65534
  17. #define MAX_TRIANGLES 65534
  18. #define MAX_GROUPS 255
  19. #define MAX_MATERIALS 128
  20. #define MAX_JOINTS 128
  21. #define MAX_TEXTURE_FILENAME_SIZE 128
  22. #define SELECTED 1
  23. #define HIDDEN 2
  24. #define SELECTED2 4
  25. #define DIRTY 8
  26. #define ISKEY 16
  27. #define NEWLYCREATED 32
  28. #define MARKED 64
  29. #define SPHEREMAP 0x80
  30. #define HASALPHA 0x40
  31. #define COMBINEALPHA 0x20
  32. #define TRANSPARENCY_MODE_SIMPLE 0
  33. #define TRANSPARENCY_MODE_DEPTHSORTEDTRIANGLES 1
  34. #define TRANSPARENCY_MODE_ALPHAREF 2
  35. struct ms3d_vertex_t
  36. {
  37. unsigned char flags;
  38. float vertex[3];
  39. char boneId;
  40. unsigned char referenceCount;
  41. char boneIds[3];
  42. unsigned char weights[3];
  43. unsigned int extra;
  44. float renderColor[3];
  45. };
  46. struct ms3d_triangle_t
  47. {
  48. unsigned short flags;
  49. unsigned short vertexIndices[3];
  50. float vertexNormals[3][3];
  51. float s[3];
  52. float t[3];
  53. float normal[3];
  54. unsigned char smoothingGroup;
  55. unsigned char groupIndex;
  56. };
  57. struct ms3d_group_t
  58. {
  59. unsigned char flags;
  60. char name[32];
  61. std__vector<unsigned short> triangleIndices;
  62. char materialIndex;
  63. std__vector<char> comment;
  64. };
  65. struct ms3d_material_t
  66. {
  67. char name[32];
  68. float ambient[4];
  69. float diffuse[4];
  70. float specular[4];
  71. float emissive[4];
  72. float shininess;
  73. float transparency;
  74. unsigned char mode;
  75. char texture[MAX_TEXTURE_FILENAME_SIZE];
  76. char alphamap[MAX_TEXTURE_FILENAME_SIZE];
  77. unsigned char id;
  78. std__vector<char> comment;
  79. };
  80. struct ms3d_keyframe_t
  81. {
  82. float time;
  83. float key[3];
  84. };
  85. struct ms3d_tangent_t
  86. {
  87. float tangentIn[3];
  88. float tangentOut[3];
  89. };
  90. struct ms3d_joint_t
  91. {
  92. unsigned char flags;
  93. char name[32];
  94. char parentName[32];
  95. float rot[3];
  96. float pos[3];
  97. std__vector<ms3d_keyframe_t> rotationKeys;
  98. std__vector<ms3d_keyframe_t> positionKeys;
  99. std__vector<ms3d_tangent_t> tangents;
  100. std__vector<char> comment;
  101. float color[3];
  102. // used for rendering
  103. int parentIndex;
  104. float matLocalSkeleton[3][4];
  105. float matGlobalSkeleton[3][4];
  106. float matLocal[3][4];
  107. float matGlobal[3][4];
  108. };
  109. class msModel
  110. {
  111. public:
  112. msModel();
  113. virtual ~msModel();
  114. public:
  115. bool Load(C Str &filename);
  116. void Clear();
  117. int GetNumGroups() const;
  118. ms3d_group_t *GetGroup(int index);
  119. int GetNumTriangles() const;
  120. ms3d_triangle_t *GetTriangle(int index);
  121. int GetNumVertices() const;
  122. ms3d_vertex_t *GetVertex(int index);
  123. int GetNumMaterials() const;
  124. ms3d_material_t *GetMaterial(int index);
  125. int GetNumJoints() const;
  126. ms3d_joint_t *GetJoint(int index);
  127. float GetJointSize() const;
  128. int GetTransparencyMode() const;
  129. float GetAlphaRef() const;
  130. int FindJointByName(const char *name);
  131. void SetupJoints(bool set_tangents);
  132. void SetupTangents();
  133. void SetFrame(float frame);
  134. void EvaluateJoint(int index, float frame);
  135. float GetAnimationFps() const;
  136. float GetCurrentFrame() const;
  137. int GetTotalFrames() const;
  138. void TransformVertex(const ms3d_vertex_t *vertex, float out[3]) const;
  139. void TransformNormal(const ms3d_vertex_t *vertex, const float normal[3], float out[3]) const;
  140. void FillJointIndicesAndWeights(const ms3d_vertex_t *vertex, int jointIndices[4], int jointWeights[4]) const;
  141. private:
  142. std__vector<ms3d_vertex_t> m_vertices;
  143. std__vector<ms3d_triangle_t> m_triangles;
  144. std__vector<ms3d_group_t> m_groups;
  145. std__vector<ms3d_material_t> m_materials;
  146. float m_animationFps;
  147. float m_currentTime;
  148. int m_totalFrames;
  149. std__vector<ms3d_joint_t> m_joints;
  150. std__vector<char> m_comment;
  151. float m_jointSize;
  152. int m_transparencyMode;
  153. float m_alphaRef;
  154. private:
  155. msModel(const msModel& rhs);
  156. msModel& operator=(const msModel& rhs);
  157. };
  158. #endif // _MSMODEL_H_