colboxsave.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 : Max2W3d *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/max2w3d/colboxsave.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 12/06/00 4:06p $*
  29. * *
  30. * $Revision:: 7 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "colboxsave.h"
  36. #include "w3d_file.h"
  37. #include "util.h"
  38. #include "w3dappdata.h"
  39. #include "errclass.h"
  40. CollisionBoxSaveClass::CollisionBoxSaveClass
  41. (
  42. char * mesh_name,
  43. char * container_name,
  44. INode * inode,
  45. Matrix3 & exportspace,
  46. TimeValue curtime,
  47. Progress_Meter_Class & meter
  48. )
  49. {
  50. //////////////////////////////////////////////////////////////////////
  51. // wrestle the mesh out of 3dsMAX
  52. //////////////////////////////////////////////////////////////////////
  53. Object * obj = inode->EvalWorldState(curtime).obj;
  54. TriObject * tri = (TriObject *)obj->ConvertToType(curtime, triObjectClassID);
  55. Mesh mesh = tri->mesh;
  56. DWORD wirecolor = inode->GetWireColor();
  57. if (mesh.getNumVerts() == 0) {
  58. throw ErrorClass("Mesh %s has no vertices!\n",mesh_name);
  59. }
  60. //////////////////////////////////////////////////////////////////////
  61. // Generate the AABox or OBBox data.
  62. //////////////////////////////////////////////////////////////////////
  63. memset(&BoxData,0,sizeof(BoxData));
  64. BoxData.Version = W3D_BOX_CURRENT_VERSION;
  65. if ((container_name != NULL) && (strlen(container_name) > 0)) {
  66. strcpy(BoxData.Name,container_name);
  67. strcat(BoxData.Name,".");
  68. }
  69. strcat(BoxData.Name,mesh_name);
  70. BoxData.Attributes = 0;
  71. if (Is_Collision_AABox(inode)) {
  72. BoxData.Attributes |= W3D_BOX_ATTRIBUTE_ALIGNED;
  73. } else {
  74. BoxData.Attributes |= W3D_BOX_ATTRIBUTE_ORIENTED;
  75. }
  76. if (Is_Physical_Collision(inode)) {
  77. BoxData.Attributes |= W3D_BOX_ATTRIBTUE_COLLISION_TYPE_PHYSICAL;
  78. }
  79. if (Is_Projectile_Collision(inode)) {
  80. BoxData.Attributes |= W3D_BOX_ATTRIBTUE_COLLISION_TYPE_PROJECTILE;
  81. }
  82. if (Is_Vis_Collision(inode)) {
  83. BoxData.Attributes |= W3D_BOX_ATTRIBTUE_COLLISION_TYPE_VIS;
  84. }
  85. if (Is_Camera_Collision(inode)) {
  86. BoxData.Attributes |= W3D_BOX_ATTRIBTUE_COLLISION_TYPE_CAMERA;
  87. }
  88. if (Is_Vehicle_Collision(inode)) {
  89. BoxData.Attributes |= W3D_BOX_ATTRIBTUE_COLLISION_TYPE_VEHICLE;
  90. }
  91. BoxData.Color.R = GetRValue(wirecolor);
  92. BoxData.Color.G = GetGValue(wirecolor);
  93. BoxData.Color.B = GetBValue(wirecolor);
  94. // if this is an axis-aligned box, then use the world coord system
  95. if (Is_Collision_AABox(inode)) {
  96. exportspace.NoRot();
  97. }
  98. // Transform the mesh into the desired coordinate system
  99. Matrix3 node_matrix = inode->GetObjectTM(curtime);
  100. Matrix3 offset_matrix = node_matrix * Inverse(exportspace);
  101. int ivert;
  102. for (ivert = 0; ivert < mesh.getNumVerts (); ++ivert) {
  103. mesh.verts[ivert] = mesh.verts[ivert] * offset_matrix;
  104. }
  105. // Find the center and extent of the box.
  106. Point3 min_point = mesh.verts[0];
  107. Point3 max_point = mesh.verts[1];
  108. for (ivert=0; ivert < mesh.getNumVerts(); ++ivert) {
  109. if (mesh.verts[ivert].x < min_point.x) min_point.x = mesh.verts[ivert].x;
  110. if (mesh.verts[ivert].y < min_point.y) min_point.y = mesh.verts[ivert].y;
  111. if (mesh.verts[ivert].z < min_point.z) min_point.z = mesh.verts[ivert].z;
  112. if (mesh.verts[ivert].x > max_point.x) max_point.x = mesh.verts[ivert].x;
  113. if (mesh.verts[ivert].y > max_point.y) max_point.y = mesh.verts[ivert].y;
  114. if (mesh.verts[ivert].z > max_point.z) max_point.z = mesh.verts[ivert].z;
  115. }
  116. Point3 center = (max_point + min_point) / 2.0f;
  117. Point3 extent = (max_point - min_point) / 2.0f;
  118. BoxData.Center.X = center.x;
  119. BoxData.Center.Y = center.y;
  120. BoxData.Center.Z = center.z;
  121. BoxData.Extent.X = extent.x;
  122. BoxData.Extent.Y = extent.y;
  123. BoxData.Extent.Z = extent.z;
  124. }
  125. int CollisionBoxSaveClass::Write_To_File(ChunkSaveClass & csave)
  126. {
  127. csave.Begin_Chunk(W3D_CHUNK_BOX);
  128. csave.Write(&BoxData,sizeof(BoxData));
  129. csave.End_Chunk();
  130. return 0;
  131. }