util.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. /* $Header: /Commando/Code/Tools/max2w3d/util.cpp 29 3/14/02 4:20p Greg_h $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando Tools - W3D export *
  24. * *
  25. * $Archive:: /Commando/Code/Tools/max2w3d/util.cpp $*
  26. * *
  27. * $Author:: Greg_h $*
  28. * *
  29. * $Modtime:: 2/04/02 4:55p $*
  30. * *
  31. * $Revision:: 29 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * Cleanup_Orthogonal_Matrix -- removes very small numbers from the matrix *
  36. * Set_W3D_Name -- set a W3D name *
  37. * Split_Node_Name -- break a node name into the base and extension *
  38. * Is_Max_Tri_Mesh -- Is this node a triangle mesh? *
  39. * -- checks if the node is the origin of a model *
  40. * -- Checks if the node is the origin for the base obect (non-LOD'd). *
  41. * Compute_3x3_Determinant -- computes the determinant of the 3x3 sub-matrix *
  42. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  43. #include "util.h"
  44. #include "w3dutil.h"
  45. #include "skin.h"
  46. #include "skindata.h"
  47. #include "modstack.h"
  48. #define MAX_NODE_NAME_LEN 256 // max name size we can handle
  49. const float EPSILON = 0.00001f;
  50. static char _string[256];
  51. static int get_geometry_type(INode * node)
  52. {
  53. assert(node != NULL);
  54. return W3DAppData2Struct::Get_App_Data(node)->Get_Geometry_Type();
  55. //return (get_w3d_bits(node) & GEO_TYPE_MASK);
  56. }
  57. /***********************************************************************************************
  58. * Cleanup_Orthogonal_Matrix -- removes very small numbers from the matrix *
  59. * *
  60. * INPUT: *
  61. * *
  62. * OUTPUT: *
  63. * *
  64. * WARNINGS: *
  65. * *
  66. * HISTORY: *
  67. * 10/26/1997 GH : Created. *
  68. *=============================================================================================*/
  69. Matrix3 Cleanup_Orthogonal_Matrix(Matrix3 & mat)
  70. {
  71. Matrix3 newmat = mat;
  72. for (int j=0; j<3; j++) {
  73. Point3 row = newmat.GetRow(j);
  74. if (fabs(row.x) < EPSILON) row.x = 0.0f;
  75. if (fabs(row.y) < EPSILON) row.y = 0.0f;
  76. if (fabs(row.z) < EPSILON) row.z = 0.0f;
  77. row = Normalize(row);
  78. newmat.SetRow(j,row);
  79. }
  80. return newmat;
  81. }
  82. /***********************************************************************************************
  83. * Compute_3x3_Determinant -- computes the determinant of the 3x3 sub-matrix *
  84. * *
  85. * INPUT: *
  86. * *
  87. * OUTPUT: *
  88. * *
  89. * WARNINGS: *
  90. * *
  91. * HISTORY: *
  92. * 2/4/2002 gth : Created. *
  93. *=============================================================================================*/
  94. float Compute_3x3_Determinant(const Matrix3 & tm)
  95. {
  96. float det = tm[0][0] * (tm[1][1]*tm[2][2] - tm[1][2]*tm[2][1]);
  97. det -= tm[0][1] * (tm[1][0]*tm[2][2] - tm[1][2]*tm[2][0]);
  98. det += tm[0][2] * (tm[1][0]*tm[2][1] - tm[1][1]*tm[2][0]);
  99. return det;
  100. }
  101. /***********************************************************************************************
  102. * Set_W3D_Name -- set a W3D name *
  103. * *
  104. * INPUT: *
  105. * *
  106. * OUTPUT: *
  107. * *
  108. * WARNINGS: *
  109. * *
  110. * HISTORY: *
  111. * 10/26/1997 GH : Created. *
  112. * 9/13/1999 AJA : Strip off the trailing ".digits" since this is a convention we've set in *
  113. * MAX to help artists manage LODs. *
  114. *=============================================================================================*/
  115. void Set_W3D_Name(char * set_name,const char * src)
  116. {
  117. memset(set_name,0,W3D_NAME_LEN);
  118. strncpy(set_name,src,W3D_NAME_LEN-1);
  119. char *dot = strrchr(set_name, '.');
  120. if (dot)
  121. {
  122. // If a number comes after the dot, strip it off
  123. int value;
  124. if (sscanf(dot+1, "%d", &value) == 1)
  125. *dot = 0;
  126. // If nothing comes after the dot, strip it off
  127. else if (*(dot+1) == 0)
  128. *dot = 0;
  129. }
  130. strupr(set_name);
  131. }
  132. /***********************************************************************************************
  133. * Split_Node_Name -- break a node name into the base and extension *
  134. * *
  135. * INPUT: *
  136. * *
  137. * OUTPUT: *
  138. * *
  139. * WARNINGS: *
  140. * *
  141. * HISTORY: *
  142. * 10/26/1997 GH : Created. *
  143. *=============================================================================================*/
  144. void Split_Node_Name(const char * name,char * set_base,char * set_exten,int * set_exten_index)
  145. {
  146. // Nodes are assumed to be named in the following way:
  147. // <name>.<exten character><exten digits>
  148. // for example: mesh.d1
  149. char buf[MAX_NODE_NAME_LEN];
  150. char * ptr;
  151. assert(strlen(name) < MAX_NODE_NAME_LEN);
  152. // Initialize
  153. if (set_base != NULL) set_base[0] = 0;
  154. if (set_exten != NULL) set_exten[0] = 0;
  155. if (set_exten_index != NULL) *set_exten_index = 0;
  156. // Get the base name
  157. strncpy(buf,name,MAX_NODE_NAME_LEN);
  158. ptr = buf;
  159. while ((*ptr != 0) && (*ptr != '.')) {
  160. ptr++;
  161. }
  162. if (*ptr == '.') {
  163. // copy what we have so far into set_base
  164. *ptr = 0;
  165. if (set_base != NULL) strncpy(set_base,buf,MAX_NODE_NAME_LEN);
  166. // copy the rest back into the extension
  167. ptr++;
  168. if (set_exten != NULL) strncpy(set_exten,ptr,MAX_NODE_NAME_LEN);
  169. // now get the extension index
  170. ptr++;
  171. if (set_exten_index != NULL) *set_exten_index = atoi(ptr);
  172. } else {
  173. // no extension, just copy the base name
  174. if (set_base != NULL) strncpy(set_base,buf,MAX_NODE_NAME_LEN);
  175. return;
  176. }
  177. }
  178. bool Append_Lod_Character (char *meshname, int lod_level, INodeListClass *origin_list)
  179. {
  180. if (meshname == NULL || lod_level < 0)
  181. return false;
  182. if (!origin_list)
  183. return false;
  184. int num_lods = origin_list->Num_Nodes();
  185. /*
  186. ** Search the other LODs to see if there is a mesh with the same name.
  187. ** If there is, we will append the current LOD level digit to the name.
  188. ** If there is not, the name will not be modified.
  189. */
  190. INode *conflict = NULL, *cur_origin = NULL;
  191. int i, lod;
  192. for (i = 0; i < num_lods; i++)
  193. {
  194. // Don't bother searching the current LOD.
  195. lod = Get_Lod_Level((*origin_list)[i]);
  196. if (lod == lod_level)
  197. continue;
  198. // Search this lod for a node of the same name.
  199. conflict = Find_Named_Node(meshname, (*origin_list)[i]);
  200. if (conflict)
  201. {
  202. // Name length is a worry here, because the name plus the number
  203. // must be less than W3D_NAME_LEN (which is fairly small!).
  204. int length = strlen(meshname);
  205. if ( (lod_level < 10) && (length < W3D_NAME_LEN - 1) )
  206. {
  207. // Append a number corresponding to the LOD level to the mesh name.
  208. // Highest-detail LOD is '0' (convention).
  209. char *insert = meshname + length;
  210. *insert++ = '0' + lod_level;
  211. *insert = '\0';
  212. }
  213. else if ( (lod_level < 100) && (length < W3D_NAME_LEN - 2) )
  214. {
  215. // Append a number corresponding to the LOD level to the mesh name.
  216. // Highest-detail LOD is '0' (convention).
  217. char buf[3];
  218. sprintf(buf, "%d", lod_level);
  219. strcat(meshname, buf);
  220. }
  221. else
  222. {
  223. // Replace the last character of the mesh name with the lod character (as above).
  224. meshname[W3D_NAME_LEN-2] = '0' + lod_level;
  225. }
  226. // Name mangling finished (conflict with other LODs is solved on their pass).
  227. break;
  228. }
  229. }
  230. return true;
  231. }
  232. void Create_Full_Path(char *full_path, const char *curr, const char *rel_path)
  233. {
  234. // Copy current dir to full path. If it doesn't end with a slash, add one.
  235. strcpy(full_path, curr);
  236. int curr_len = strlen(curr);
  237. char *full_p = full_path + curr_len; // Point at the terminating NULL
  238. if (curr_len == 0 ||(*(full_p - 1) != '/' && *(full_p - 1) != '\\')) {
  239. *full_p = '\\';
  240. *(++full_p) = '\000'; // Point at the terminating NULL
  241. }
  242. // Scan "..\"s at the beginning of the rel path, scan backwards on the
  243. // full path (current dir):
  244. const char *rel_p;
  245. for ( rel_p = rel_path;
  246. *rel_p == '.' && *(rel_p+1) == '.' && ( *(rel_p+2) == '/' || *(rel_p+2) == '\\' );
  247. rel_p += 3)
  248. {
  249. full_p--;
  250. for (; full_p > full_path && *(full_p-1) != '/' && *(full_p-1) != '\\'; full_p--);
  251. *full_p = '\000';
  252. }
  253. // Copy the remainder of the relative path to the full path:
  254. strcpy(full_p, rel_p);
  255. }
  256. // This enum is used inside Create_Relative_Path:
  257. enum PathCharType {
  258. NULL_CHAR,
  259. SLASH_CHAR,
  260. PLAIN_CHAR
  261. };
  262. void Create_Relative_Path(char *rel_path, const char *curr, const char *full_path)
  263. {
  264. // Copy both constant strings and convert them to uppercase:
  265. int curr_len = strlen(curr);
  266. char *up_curr = (char *)malloc(curr_len + 1);
  267. strcpy(up_curr, curr);
  268. _strupr(up_curr);
  269. int full_len = strlen(full_path);
  270. char *up_full = (char *)malloc(full_len + 1);
  271. strcpy(up_full, full_path);
  272. _strupr(up_full);
  273. char *rel_p = rel_path;
  274. // Find shared prefix of curr and full path
  275. const char *full_p = up_full;
  276. const char *curr_p = up_curr;
  277. for ( ;
  278. *full_p && *full_p == *curr_p || (*full_p == '/' && *curr_p == '\\') || (*full_p == '\\' && *curr_p == '/');
  279. full_p++, curr_p++
  280. );
  281. // If no shared prefix at this point set the relative path to 0
  282. // This will force the code to use the absolute path.
  283. if (full_p == up_full) {
  284. rel_path[0] = 0;
  285. goto end;
  286. }
  287. // The first different character for each string can be: a NULL, a slash,
  288. // or an ordinary character.
  289. PathCharType full_type, curr_type;
  290. if (*full_p == '\000') {
  291. full_type = NULL_CHAR;
  292. } else {
  293. if (*full_p == '/' || *full_p == '\\') {
  294. full_type = SLASH_CHAR;
  295. } else {
  296. full_type = PLAIN_CHAR;
  297. }
  298. }
  299. if (*curr_p == '\000') {
  300. curr_type = NULL_CHAR;
  301. } else {
  302. if (*curr_p == '/' || *curr_p == '\\') {
  303. curr_type = SLASH_CHAR;
  304. } else {
  305. curr_type = PLAIN_CHAR;
  306. }
  307. }
  308. // If the last fullpath char is a NULL or both are slashes, we have an
  309. // error - return full path
  310. if (full_type == NULL_CHAR || (full_type == SLASH_CHAR && curr_type == SLASH_CHAR)) {
  311. strcpy(rel_path, up_full);
  312. goto end;
  313. }
  314. // If the current path has ended (last char is a NULL) and the full path's
  315. // last char is a slash, then just copy the remainder of the full path
  316. // (w/o the slash) to the relative path, and exit.
  317. if (curr_type == NULL_CHAR && full_type == SLASH_CHAR) {
  318. full_p++; // skip slash
  319. strcpy(rel_path, full_p);
  320. goto end;
  321. }
  322. // If one of following holds:
  323. // 1) One of the last chars is a slash and the other is a plain char
  324. // 2) The current path has ended (last char is NULL) and the last char
  325. // of the full path is a plain char
  326. // 3) The last char of both are plain chars and the previous char is not a
  327. // slash
  328. // Then we must backtrack both pointers until the characters before them
  329. // are slashes. If there are no previous slashes, we have an error.
  330. if ( (full_type == SLASH_CHAR && curr_type == PLAIN_CHAR) ||
  331. (curr_type == SLASH_CHAR && full_type == PLAIN_CHAR) ||
  332. (curr_type == NULL_CHAR && full_type == PLAIN_CHAR) ||
  333. (curr_type == PLAIN_CHAR && full_type == PLAIN_CHAR &&
  334. *(full_p-1) != '/' && *(full_p-1) != '\\') )
  335. {
  336. for (;
  337. full_p > up_full &&
  338. ( (*(full_p - 1) != '/' && *(full_p - 1) != '\\') ||
  339. (*(curr_p - 1) != '/' && *(curr_p - 1) != '\\') );
  340. full_p--, curr_p--);
  341. }
  342. // If no shared prefix at this point (not even a drive letter) return the
  343. // full path
  344. if (full_p == up_full) {
  345. strcpy(rel_path, up_full);
  346. goto end;
  347. }
  348. // Scan all directories levels in current path from shared point to end -
  349. // for each one add a "../" to the relative path. Note that at this point
  350. // we know we have to add at least one.
  351. *rel_p++ = '.';
  352. *rel_p++ = '.';
  353. *rel_p++ = '\\';
  354. // Go over remaining current path, for each slash we find add one "../" to
  355. // the relative path
  356. for (; *curr_p; curr_p++) {
  357. if (*curr_p == '/' || *curr_p == '\\') {
  358. *rel_p++ = '.';
  359. *rel_p++ = '.';
  360. *rel_p++ = '\\';
  361. }
  362. }
  363. // If the last char of the current path is a slash remove a "../" from the
  364. // relative path.
  365. if (*(curr_p - 1) == '/' || *(curr_p - 1) == '\\') {
  366. rel_p -= 3;
  367. }
  368. // Copy remaining full path (from shared point to end) to relative path
  369. strcpy(rel_p, full_p);
  370. end:
  371. free(up_curr);
  372. free(up_full);
  373. }
  374. bool Is_Full_Path(char * path)
  375. {
  376. // first scan for a drive letter (scan for a colon)
  377. if (strchr(path,':') != NULL) {
  378. return true;
  379. }
  380. // now scan for a "network" path (starts with "//")
  381. if ((path[0] == '/') && (path[1] == '/')) {
  382. return true;
  383. }
  384. if ((path[0] == '\\') && (path[1] == '\\')) {
  385. return true;
  386. }
  387. return false;
  388. }
  389. /***********************************************************************************************
  390. * Is_Max_Tri_Mesh -- Is this node a triangle mesh? *
  391. * *
  392. * INPUT: *
  393. * *
  394. * OUTPUT: *
  395. * *
  396. * WARNINGS: *
  397. * *
  398. * HISTORY: *
  399. * 2/2/98 GTH : Created. *
  400. *=============================================================================================*/
  401. bool Is_Max_Tri_Mesh(INode * node)
  402. {
  403. Object *obj = node->EvalWorldState(0).obj;
  404. if (obj && obj->CanConvertToType(Class_ID(TRIOBJ_CLASS_ID, 0))) {
  405. return true;
  406. }
  407. return false;
  408. }
  409. bool Is_Damage_Root(INode *node)
  410. {
  411. if (node == NULL)
  412. return false;
  413. // Is the node's parent the scene root?
  414. INode *parent = node->GetParentNode();
  415. if (!parent || !parent->IsRootNode())
  416. return false;
  417. // Is the node's name in the form "damage.*"?
  418. char *name = node->GetName();
  419. if (strnicmp(name, "damage.", strlen("damage.")) != 0)
  420. return false;
  421. /* This won't pick up references to a dummy object for some reason.
  422. // Does the node point to a dummy object?
  423. Object *obj = node->GetObjectRef();
  424. if (!obj || !obj->CanConvertToType(Class_ID(DUMMY_CLASS_ID, 0)))
  425. return false;
  426. */
  427. return true;
  428. }
  429. /***********************************************************************************************
  430. * Is_Origin -- checks if the node is the origin of a model *
  431. * *
  432. * A node is an origin if its parent is the scene root, it is a dummy object, and its name *
  433. * is of the form "origin.*" (case insensitive). All descendants of the origin will be *
  434. * expressed in coordinates relative to the origin object. *
  435. * *
  436. * INPUT: *
  437. * *
  438. * OUTPUT: *
  439. * *
  440. * WARNINGS: *
  441. * *
  442. * HISTORY: *
  443. * 9/13/99 AJA : Created.
  444. *=============================================================================================*/
  445. bool Is_Origin(INode * node)
  446. {
  447. if (!node) return false;
  448. if (node->IsRootNode()) return true;
  449. if (node->IsHidden()) return false;
  450. // Is the node's parent the scene root?
  451. INode *parent = node->GetParentNode();
  452. if (!parent || !parent->IsRootNode())
  453. return false;
  454. // Is the node's name in the form "origin.*"?
  455. char *name = node->GetName();
  456. if (strnicmp(name, "origin.", strlen("origin.")) != 0)
  457. return false;
  458. /* This won't pick up references to a dummy object for some reason.
  459. // Does the node point to a dummy object?
  460. Object *obj = node->GetObjectRef();
  461. if (!obj || !obj->CanConvertToType(Class_ID(DUMMY_CLASS_ID, 0)))
  462. return false;
  463. */
  464. // This is an origin.
  465. return true;
  466. }
  467. /***********************************************************************************************
  468. * Is_Base_Origin -- Checks if the node is the origin for the base obect (non-LOD'd). *
  469. * *
  470. * INPUT: *
  471. * *
  472. * OUTPUT: *
  473. * *
  474. * WARNINGS: *
  475. * *
  476. * HISTORY: *
  477. * 9/13/1999 AJA : Created. *
  478. *=============================================================================================*/
  479. bool Is_Base_Origin(INode * node)
  480. {
  481. if (!node) return false;
  482. if (node->IsRootNode()) return true;
  483. if (!Is_Origin(node)) return false;
  484. // An origin is the base object origin if it's name is "origin." or
  485. // "origin.0" (a numeric value evaluating to zero as scanned by sscanf
  486. // which would include "origin.00" "origin.000", etc.).
  487. bool is_base_origin = false;
  488. char *name = node->GetName();
  489. if (stricmp(name, "origin.") == 0)
  490. is_base_origin = true;
  491. else if (strlen(name) > strlen("origin."))
  492. {
  493. // We know the first 7 characters are "origin." because that
  494. // was tested in Is_Origin(). Is it "origin.0"?
  495. int idx;
  496. if ((sscanf(name+strlen("origin."), "%d", &idx) == 1) && (idx == 0))
  497. is_base_origin = true;
  498. }
  499. return is_base_origin;
  500. }
  501. int Get_Lod_Level(INode *node)
  502. {
  503. if (node->IsRootNode()) return 0;
  504. if (!Is_Origin(node)) return -1;
  505. char *name = node->GetName();
  506. char *dot = strrchr(name, '.');
  507. assert(dot);
  508. return atoi(dot+1);
  509. }
  510. int Get_Damage_State(INode *node)
  511. {
  512. if (!Is_Damage_Root(node)) return -1;
  513. char *name = node->GetName();
  514. char *dot = strrchr(name, '.');
  515. assert(dot);
  516. return atoi(dot+1);
  517. }
  518. INode *Find_Named_Node(char *nodename, INode *root)
  519. {
  520. if (!root || !nodename)
  521. return NULL;
  522. // Perform a breadth-first search of the tree for a node
  523. // of the given name.
  524. INode *child = NULL;
  525. int i;
  526. char cur_name[W3D_NAME_LEN];
  527. // Is this the node we're looking for?
  528. Set_W3D_Name(cur_name, root->GetName());
  529. if (strcmp(cur_name, nodename) == 0)
  530. return root;
  531. // Check the children against the given name.
  532. for (i = 0; i < root->NumChildren(); i++)
  533. {
  534. // Is it this child?
  535. child = root->GetChildNode(i);
  536. Set_W3D_Name(cur_name, child->GetName());
  537. if (strcmp(nodename, cur_name) == 0)
  538. return child;
  539. }
  540. // Wasn't any children. Check each child's descendants.
  541. for (i = 0; i < root->NumChildren(); i++)
  542. {
  543. child = root->GetChildNode(i);
  544. INode *found = Find_Named_Node(nodename, child);
  545. if (found)
  546. return found;
  547. }
  548. // Didn't find the node anywhere.
  549. return NULL;
  550. }