mdl.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. Copyright (C) 2001-2006, William Joseph.
  3. All Rights Reserved.
  4. This file is part of GtkRadiant.
  5. GtkRadiant is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #include "mdl.h"
  18. #include "ifilesystem.h"
  19. #include "imodel.h"
  20. #include "imagelib.h"
  21. #include "bytestreamutils.h"
  22. #include "model.h"
  23. #include "ident.h"
  24. #include "mdlnormals.h"
  25. #include "mdlformat.h"
  26. void istream_read_mdlHeader(PointerInputStream& inputStream, mdlHeader_t& header)
  27. {
  28. inputStream.read(header.ident, 4);
  29. header.version = istream_read_int32_le(inputStream);
  30. header.scale[0] = istream_read_float32_le(inputStream);
  31. header.scale[1] = istream_read_float32_le(inputStream);
  32. header.scale[2] = istream_read_float32_le(inputStream);
  33. header.scale_origin[0] = istream_read_float32_le(inputStream);
  34. header.scale_origin[1] = istream_read_float32_le(inputStream);
  35. header.scale_origin[2] = istream_read_float32_le(inputStream);
  36. header.boundingradius = istream_read_float32_le(inputStream);
  37. header.eyeposition[0] = istream_read_float32_le(inputStream);
  38. header.eyeposition[1] = istream_read_float32_le(inputStream);
  39. header.eyeposition[2] = istream_read_float32_le(inputStream);
  40. header.numskins = istream_read_int32_le(inputStream);
  41. header.skinwidth = istream_read_int32_le(inputStream);
  42. header.skinheight = istream_read_int32_le(inputStream);
  43. header.numverts = istream_read_int32_le(inputStream);
  44. header.numtris = istream_read_int32_le(inputStream);
  45. header.numframes = istream_read_int32_le(inputStream);
  46. header.synctype = istream_read_int32_le(inputStream);
  47. header.flags = istream_read_int32_le(inputStream);
  48. header.size = istream_read_float32_le(inputStream);
  49. }
  50. inline ArbitraryMeshVertex MDLVertex_construct(const mdlHeader_t& header, const mdlXyzNormal_t& xyz, const mdlSt_t& st, bool facesfront)
  51. {
  52. return ArbitraryMeshVertex(
  53. Vertex3f(
  54. xyz.v[0] * header.scale[0] + header.scale_origin[0],
  55. xyz.v[1] * header.scale[1] + header.scale_origin[1],
  56. xyz.v[2] * header.scale[2] + header.scale_origin[2]
  57. ),
  58. Normal3f(
  59. g_mdl_normals[xyz.lightnormalindex][0],
  60. g_mdl_normals[xyz.lightnormalindex][1],
  61. g_mdl_normals[xyz.lightnormalindex][2]
  62. ),
  63. TexCoord2f(
  64. ((float)st.s / header.skinwidth) + ((st.onseam == MDL_ONSEAM && !facesfront) ? 0.5f : 0.0f),
  65. (float)st.t / header.skinheight
  66. )
  67. );
  68. }
  69. class mdlVertex_t
  70. {
  71. public:
  72. inline mdlVertex_t(int vertindex, int facesfront)
  73. : m_vertindex(vertindex), m_facesfront(facesfront)
  74. {}
  75. inline bool operator<(const mdlVertex_t& other) const
  76. {
  77. if(m_facesfront < other.m_facesfront)
  78. return true;
  79. if(other.m_facesfront < m_facesfront)
  80. return false;
  81. if(m_vertindex < other.m_vertindex)
  82. return true;
  83. if(other.m_vertindex < m_vertindex)
  84. return false;
  85. return false;
  86. }
  87. inline bool operator==(const mdlVertex_t& other) const
  88. {
  89. return m_vertindex == other.m_vertindex
  90. && m_facesfront == other.m_facesfront;
  91. }
  92. int m_vertindex;
  93. int m_facesfront;
  94. };
  95. typedef const mdlTriangle_t* mdlTriangleIterator;
  96. void MDLSurface_read(Surface& surface, const byte* buffer, const char* name)
  97. {
  98. mdlHeader_t header;
  99. PointerInputStream inputStream(buffer);
  100. istream_read_mdlHeader(inputStream, header);
  101. for(int i = 0; i < header.numskins; ++i)
  102. {
  103. switch(istream_read_int32_le(inputStream))
  104. {
  105. case MDL_SKIN_SINGLE:
  106. inputStream.seek(header.skinwidth * header.skinheight);
  107. break;
  108. case MDL_SKIN_GROUP:
  109. int numskins = istream_read_int32_le(inputStream);
  110. inputStream.seek(numskins * (4 + (header.skinwidth * header.skinheight)));
  111. break;
  112. }
  113. }
  114. Array<mdlSt_t> mdlSts(header.numverts);
  115. for(Array<mdlSt_t>::iterator i = mdlSts.begin(); i != mdlSts.end(); ++i)
  116. {
  117. (*i).onseam = istream_read_int32_le(inputStream);
  118. (*i).s = istream_read_int32_le(inputStream);
  119. (*i).t = istream_read_int32_le(inputStream);
  120. }
  121. Array<mdlTriangle_t> mdlTriangles(header.numtris);
  122. for(Array<mdlTriangle_t>::iterator i = mdlTriangles.begin(); i != mdlTriangles.end(); ++i)
  123. {
  124. (*i).facesfront = istream_read_int32_le(inputStream);
  125. (*i).vertindex[0] = istream_read_int32_le(inputStream);
  126. (*i).vertindex[1] = istream_read_int32_le(inputStream);
  127. (*i).vertindex[2] = istream_read_int32_le(inputStream);
  128. }
  129. {
  130. bool found = false;
  131. for(int i = 0; i < header.numframes && found == false; i++)
  132. {
  133. switch(istream_read_int32_le(inputStream))
  134. {
  135. case MDL_FRAME_SINGLE:
  136. inputStream.seek(MDL_FRAME_SIZE);
  137. found = true;
  138. break;
  139. case MDL_FRAME_GROUP:
  140. int numframes = istream_read_int32_le(inputStream);
  141. inputStream.seek((MDL_XYZNORMAL_SIZE * 2) + (numframes * 4));
  142. found = true;
  143. break;
  144. }
  145. }
  146. }
  147. Array<mdlXyzNormal_t> mdlXyzNormals(header.numtris);
  148. for(Array<mdlXyzNormal_t>::iterator i = mdlXyzNormals.begin(); i != mdlXyzNormals.end(); ++i)
  149. {
  150. inputStream.read((*i).v, 3);
  151. inputStream.read(&(*i).lightnormalindex, 1);
  152. }
  153. {
  154. VertexBuffer<mdlVertex_t> mdl_vertices;
  155. {
  156. UniqueVertexBuffer<mdlVertex_t> inserter(mdl_vertices);
  157. for(Array<mdlTriangle_t>::iterator i = mdlTriangles.begin(); i != mdlTriangles.end(); ++i)
  158. {
  159. surface.indices().insert(inserter.insert(mdlVertex_t((*i).vertindex[0], (*i).facesfront)));
  160. surface.indices().insert(inserter.insert(mdlVertex_t((*i).vertindex[1], (*i).facesfront)));
  161. surface.indices().insert(inserter.insert(mdlVertex_t((*i).vertindex[2], (*i).facesfront)));
  162. }
  163. }
  164. {
  165. surface.vertices().reserve(mdl_vertices.size());
  166. for(VertexBuffer<mdlVertex_t>::iterator i = mdl_vertices.begin(); i != mdl_vertices.end(); ++i)
  167. {
  168. surface.vertices().push_back(MDLVertex_construct(header, mdlXyzNormals[(*i).m_vertindex], mdlSts[(*i).m_vertindex], (*i).m_facesfront == MDL_FACES_FRONT));
  169. }
  170. }
  171. }
  172. surface.setShader(name);
  173. surface.updateAABB();
  174. }
  175. void MDLModel_read(Model& model, const byte* buffer, const char* name)
  176. {
  177. MDLSurface_read(model.newSurface(), buffer, name);
  178. model.updateAABB();
  179. }
  180. scene::Node& MDLModel_new(const byte* buffer, const char* name)
  181. {
  182. ModelNode* modelNode = new ModelNode();
  183. MDLModel_read(modelNode->model(), buffer, name);
  184. return modelNode->node();
  185. }
  186. scene::Node& MDLModel_default()
  187. {
  188. ModelNode* modelNode = new ModelNode();
  189. Model_constructNull(modelNode->model());
  190. return modelNode->node();
  191. }
  192. scene::Node& MDLModel_fromBuffer(unsigned char* buffer, const char* name)
  193. {
  194. if (!ident_equal(buffer, MDL_IDENT))
  195. {
  196. globalErrorStream() << "MDL read error: incorrect ident\n";
  197. return MDLModel_default();
  198. }
  199. else
  200. {
  201. return MDLModel_new(buffer, name);
  202. }
  203. }
  204. scene::Node& loadMDLModel(ArchiveFile& file)
  205. {
  206. ScopedArchiveBuffer buffer(file);
  207. return MDLModel_fromBuffer(buffer.buffer, file.getName());
  208. }