hmdldef.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program 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 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/hmdldef.cpp $*
  25. * *
  26. * Author:: Greg_h *
  27. * *
  28. * $Modtime:: 1/08/01 10:04a $*
  29. * *
  30. * $Revision:: 1 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * HModelDefClass::HModelDefClass -- Constructor *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #include "hmdldef.h"
  37. #include <assert.h>
  38. #include <string.h>
  39. #include "w3d_file.h"
  40. #include "chunkio.h"
  41. #include "snappts.h"
  42. /***********************************************************************************************
  43. * HModelDefClass::HModelDefClass -- Constructor *
  44. * *
  45. * INPUT: *
  46. * *
  47. * OUTPUT: *
  48. * *
  49. * WARNINGS: *
  50. * *
  51. * HISTORY: *
  52. * 12/15/97 GTH : Created. *
  53. *=============================================================================================*/
  54. HModelDefClass::HModelDefClass(void) :
  55. SubObjectCount(0),
  56. SubObjects(NULL),
  57. SnapPoints(NULL)
  58. {
  59. }
  60. /***********************************************************************************************
  61. * HModelDefClass::~HModelDefClass -- destructor *
  62. * *
  63. * INPUT: *
  64. * *
  65. * OUTPUT: *
  66. * *
  67. * WARNINGS: *
  68. * *
  69. * HISTORY: *
  70. * 08/11/1997 GH : Created. *
  71. *=============================================================================================*/
  72. HModelDefClass::~HModelDefClass(void)
  73. {
  74. Free();
  75. }
  76. /***********************************************************************************************
  77. * HModelDefClass::Free -- de-allocate all memory in use *
  78. * *
  79. * INPUT: *
  80. * *
  81. * OUTPUT: *
  82. * *
  83. * WARNINGS: *
  84. * *
  85. * HISTORY: *
  86. * 08/11/1997 GH : Created. *
  87. *=============================================================================================*/
  88. void HModelDefClass::Free(void)
  89. {
  90. if (SubObjects != NULL) {
  91. delete[] SubObjects;
  92. SubObjects = NULL;
  93. }
  94. SubObjectCount = 0;
  95. if (SnapPoints != NULL) {
  96. SnapPoints->Release_Ref();
  97. SnapPoints = NULL;
  98. }
  99. }
  100. /***********************************************************************************************
  101. * HModelDefClass::Load -- load a set of mesh connections from a file *
  102. * *
  103. * INPUT: *
  104. * *
  105. * OUTPUT: *
  106. * *
  107. * WARNINGS: *
  108. * *
  109. * HISTORY: *
  110. * 08/11/1997 GH : Created. *
  111. *=============================================================================================*/
  112. int HModelDefClass::Load_W3D(ChunkLoadClass & cload)
  113. {
  114. bool pre30 = false;
  115. int subobjcounter = 0;
  116. Free();
  117. /*
  118. ** Read the first chunk, it should be the header
  119. */
  120. if (!cload.Open_Chunk()) {
  121. return false;
  122. }
  123. if (cload.Cur_Chunk_ID() != W3D_CHUNK_HMODEL_HEADER) {
  124. goto Error;
  125. }
  126. /*
  127. ** read in the header
  128. */
  129. W3dHModelHeaderStruct header;
  130. if (cload.Read(&header,sizeof(W3dHModelHeaderStruct)) != sizeof(W3dHModelHeaderStruct)) {
  131. goto Error;
  132. }
  133. cload.Close_Chunk();
  134. /*
  135. ** process the header info
  136. */
  137. strncpy(ModelName,header.Name,W3D_NAME_LEN);
  138. ModelName[W3D_NAME_LEN - 1] = 0;
  139. strncpy(BasePoseName,header.HierarchyName,W3D_NAME_LEN);
  140. BasePoseName[W3D_NAME_LEN-1] = 0;
  141. strcpy(Name,ModelName);
  142. /*
  143. ** Just allocate a node for the number of sub objects we're expecting
  144. */
  145. SubObjectCount = header.NumConnections;
  146. SubObjects = new HmdlNodeDefStruct[SubObjectCount];
  147. if (SubObjects == NULL) {
  148. goto Error;
  149. }
  150. /*
  151. ** If this is pre-3.0 set a flag so that each render object's
  152. ** bone id will be incremented by one to account for the new
  153. ** root node added with version3.0 of the file format. Basically,
  154. ** I'm making all of the code assume that node 0 is the root and
  155. ** therefore, pre-3.0 files have to have it added and all of
  156. ** the indices adjusted
  157. */
  158. if (header.Version < W3D_MAKE_VERSION(3,0)) {
  159. pre30 = true;
  160. }
  161. /*
  162. ** Process the rest of the chunks
  163. */
  164. subobjcounter = 0;
  165. while (cload.Open_Chunk()) {
  166. switch (cload.Cur_Chunk_ID()) {
  167. case W3D_CHUNK_NODE:
  168. case W3D_CHUNK_COLLISION_NODE:
  169. case W3D_CHUNK_SKIN_NODE:
  170. if (!read_connection(cload,&(SubObjects[subobjcounter]),pre30)) {
  171. goto Error;
  172. }
  173. subobjcounter++;
  174. break;
  175. case W3D_CHUNK_POINTS:
  176. SnapPoints = new SnapPointsClass;
  177. SnapPoints->Load_W3D(cload);
  178. break;
  179. default:
  180. break;
  181. }
  182. cload.Close_Chunk();
  183. }
  184. return OK;
  185. Error:
  186. return LOAD_ERROR;
  187. }
  188. /***********************************************************************************************
  189. * HModelDefClass::read_connection -- read a single connection from the file *
  190. * *
  191. * INPUT: *
  192. * *
  193. * OUTPUT: *
  194. * *
  195. * WARNINGS: *
  196. * *
  197. * HISTORY: *
  198. * 08/11/1997 GH : Created. *
  199. * 10/22/97 GH : Check for mesh connections with PivotID=-1 *
  200. *=============================================================================================*/
  201. bool HModelDefClass::read_connection(ChunkLoadClass & cload,HmdlNodeDefStruct * node,bool pre30)
  202. {
  203. W3dHModelNodeStruct con;
  204. if (cload.Read(&con,sizeof(W3dHModelNodeStruct)) != sizeof(W3dHModelNodeStruct)) {
  205. return false;
  206. }
  207. strcpy(node->RenderObjName,ModelName);
  208. strcat(node->RenderObjName,".");
  209. strcat(node->RenderObjName,con.RenderObjName);
  210. if (pre30) {
  211. if (con.PivotIdx == 65535) {
  212. node->PivotID = 0;
  213. } else {
  214. node->PivotID = con.PivotIdx + 1;
  215. }
  216. } else {
  217. assert(con.PivotIdx != 65535);
  218. node->PivotID = con.PivotIdx;
  219. }
  220. return true;
  221. }