MeshDeformUndo.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando / G 3D engine *
  23. * *
  24. * File Name : MeshDeformUndo.h *
  25. * *
  26. * Programmer : Patrick Smith *
  27. * *
  28. * Start Date : 06/08/99 *
  29. * *
  30. * Last Update :
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "MeshDeformUndo.H"
  36. #include "Util.H"
  37. #include "MeshDeformData.H"
  38. #include "MeshDeformSet.H"
  39. #include "MeshDeform.H"
  40. ///////////////////////////////////////////////////////////////////////////
  41. //
  42. // VertexRestoreClass
  43. //
  44. ///////////////////////////////////////////////////////////////////////////
  45. VertexRestoreClass::VertexRestoreClass
  46. (
  47. Mesh * mesh,
  48. MeshDeformClass * modifier,
  49. MeshDeformModData * mod_data
  50. )
  51. : m_pModifier (modifier),
  52. m_pModData (mod_data),
  53. m_pMesh (mesh),
  54. m_SetIndex (0),
  55. m_KeyframeIndex (0)
  56. {
  57. assert (mesh != NULL);
  58. //
  59. // Remember the deformer's current settings
  60. //
  61. m_SetIndex = m_pModData->Get_Current_Set ();
  62. m_KeyframeIndex = m_pModData->Peek_Set (m_SetIndex).Get_Current_Key_Frame ();
  63. return ;
  64. }
  65. ///////////////////////////////////////////////////////////////////////////
  66. //
  67. // Free_Vertex_Array
  68. //
  69. ///////////////////////////////////////////////////////////////////////////
  70. void
  71. VertexRestoreClass::Free_Vertex_Array (void)
  72. {
  73. m_VertexList.Delete_All ();
  74. m_RedoVertexList.Delete_All ();
  75. return ;
  76. }
  77. ///////////////////////////////////////////////////////////////////////////
  78. //
  79. // Restore
  80. //
  81. ///////////////////////////////////////////////////////////////////////////
  82. void
  83. VertexRestoreClass::Restore (int is_undo)
  84. {
  85. assert (m_pMesh != NULL);
  86. assert (m_pModData != NULL);
  87. assert (m_pModifier != NULL);
  88. // Is this being called as part of an undo operation?
  89. if (is_undo != 0) {
  90. //
  91. // Ensure the modifier is in the state it was when
  92. // the undo operation was recorded
  93. //
  94. m_pModData->Set_Current_Set (m_SetIndex);
  95. m_pModData->Peek_Set (m_SetIndex).Set_Current_Key_Frame (m_KeyframeIndex);
  96. //
  97. // Apply the original vertex positions to the mesh
  98. //
  99. Apply_Vertex_Data (m_VertexList);
  100. //
  101. // Notify the mesh of geometry changes
  102. //
  103. m_pModifier->NotifyDependents (FOREVER, PART_GEOM | PART_VERTCOLOR, REFMSG_CHANGE);
  104. m_pModifier->Update_UI (m_pModData);
  105. }
  106. return ;
  107. }
  108. ///////////////////////////////////////////////////////////////////////////
  109. //
  110. // Redo
  111. //
  112. ///////////////////////////////////////////////////////////////////////////
  113. void
  114. VertexRestoreClass::Redo (void)
  115. {
  116. assert (m_pMesh != NULL);
  117. assert (m_pModData != NULL);
  118. assert (m_pModifier != NULL);
  119. //
  120. // Ensure the modifier is in the state it was when
  121. // the undo operation was recorded
  122. //
  123. m_pModData->Set_Current_Set (m_SetIndex);
  124. m_pModData->Peek_Set (m_SetIndex).Set_Current_Key_Frame (m_KeyframeIndex);
  125. //
  126. // Apply the original vertex positions to the mesh
  127. //
  128. Apply_Vertex_Data (m_RedoVertexList);
  129. //
  130. // Notify the mesh of geometry changes
  131. //
  132. m_pModifier->NotifyDependents (FOREVER, PART_GEOM | PART_VERTCOLOR, REFMSG_CHANGE);
  133. m_pModifier->Update_UI (m_pModData);
  134. return ;
  135. }
  136. ///////////////////////////////////////////////////////////////////////////
  137. //
  138. // EndHold
  139. //
  140. ///////////////////////////////////////////////////////////////////////////
  141. void
  142. VertexRestoreClass::EndHold (void)
  143. {
  144. //
  145. // Record the position of all the verts we are about to change
  146. // (to support redo).
  147. //
  148. Copy_Vertex_State (m_RedoVertexList);
  149. m_pModifier->ClearAFlag (A_HELD);
  150. return ;
  151. }
  152. /***************************************************************************************/
  153. /*
  154. /* End VertexRestoreClass
  155. /*
  156. /***************************************************************************************/
  157. /***************************************************************************************/
  158. /*
  159. /* Start VertexPositionRestoreClass
  160. /*
  161. /***************************************************************************************/
  162. ///////////////////////////////////////////////////////////////////////////
  163. //
  164. // VertexPositionRestoreClass
  165. //
  166. ///////////////////////////////////////////////////////////////////////////
  167. VertexPositionRestoreClass::VertexPositionRestoreClass
  168. (
  169. Mesh * mesh,
  170. MeshDeformClass * modifier,
  171. MeshDeformModData * mod_data
  172. )
  173. : VertexRestoreClass (mesh, modifier, mod_data)
  174. {
  175. //
  176. // Make a copy of the vertex positions
  177. //
  178. Copy_Vertex_State (m_VertexList);
  179. return ;
  180. }
  181. ///////////////////////////////////////////////////////////////////////////
  182. //
  183. // Copy_Vertex_State
  184. //
  185. ///////////////////////////////////////////////////////////////////////////
  186. void
  187. VertexPositionRestoreClass::Copy_Vertex_State (DEFORM_LIST &list)
  188. {
  189. //
  190. // Make a copy of each vertex in the current keyframe
  191. //
  192. list.Delete_All ();
  193. MeshDeformSetClass &set_obj = m_pModData->Peek_Set (m_SetIndex);
  194. int count = set_obj.Get_Vertex_Count (m_KeyframeIndex);
  195. for (int index = 0; index < count; index ++) {
  196. const VERT_INFO &data = set_obj.Get_Vertex_Data (m_KeyframeIndex, index);
  197. list.Add (VERT_INFO (data.index, data.value));
  198. }
  199. return ;
  200. }
  201. ///////////////////////////////////////////////////////////////////////////
  202. //
  203. // Apply_Vertex_Data
  204. //
  205. ///////////////////////////////////////////////////////////////////////////
  206. void
  207. VertexPositionRestoreClass::Apply_Vertex_Data (DEFORM_LIST &list)
  208. {
  209. m_pModData->Peek_Set (m_SetIndex).Reset_Key_Frame_Verts (m_KeyframeIndex);
  210. //
  211. // Apply each vertex in our list
  212. //
  213. for (int index = 0; index < list.Count (); index ++) {
  214. VERT_INFO &info = list[index];
  215. m_pModData->Set_Vertex_Position (info.index, info.value);
  216. }
  217. return ;
  218. }
  219. /***************************************************************************************/
  220. /*
  221. /* End VertexPositionRestoreClass
  222. /*
  223. /***************************************************************************************/
  224. /***************************************************************************************/
  225. /*
  226. /* Start VertexColorRestoreClass
  227. /*
  228. /***************************************************************************************/
  229. ///////////////////////////////////////////////////////////////////////////
  230. //
  231. // VertexColorRestoreClass
  232. //
  233. ///////////////////////////////////////////////////////////////////////////
  234. VertexColorRestoreClass::VertexColorRestoreClass
  235. (
  236. Mesh * mesh,
  237. MeshDeformClass * modifier,
  238. MeshDeformModData * mod_data
  239. )
  240. : VertexRestoreClass (mesh, modifier, mod_data)
  241. {
  242. //
  243. // Make a copy of the vertex positions
  244. //
  245. Copy_Vertex_State (m_VertexList);
  246. return ;
  247. }
  248. ///////////////////////////////////////////////////////////////////////////
  249. //
  250. // Copy_Vertex_State
  251. //
  252. ///////////////////////////////////////////////////////////////////////////
  253. void
  254. VertexColorRestoreClass::Copy_Vertex_State (DEFORM_LIST &list)
  255. {
  256. //
  257. // Make a copy of each vertex color in the current keyframe
  258. //
  259. list.Delete_All ();
  260. MeshDeformSetClass &set_obj = m_pModData->Peek_Set (m_SetIndex);
  261. int count = set_obj.Get_Color_Count (m_KeyframeIndex);
  262. for (int index = 0; index < count; index ++) {
  263. const VERT_INFO &data = set_obj.Get_Color_Data (m_KeyframeIndex, index);
  264. list.Add (VERT_INFO (data.index, data.value, data.color_index));
  265. }
  266. return ;
  267. }
  268. ///////////////////////////////////////////////////////////////////////////
  269. //
  270. // Apply_Vertex_Data
  271. //
  272. ///////////////////////////////////////////////////////////////////////////
  273. void
  274. VertexColorRestoreClass::Apply_Vertex_Data (DEFORM_LIST &list)
  275. {
  276. m_pModData->Peek_Set (m_SetIndex).Reset_Key_Frame_Colors (m_KeyframeIndex);
  277. //
  278. // Apply each vertex in our list
  279. //
  280. for (int index = 0; index < list.Count (); index ++) {
  281. VERT_INFO &info = list[index];
  282. m_pModData->Set_Vertex_Color (info.index, info.color_index, info.value);
  283. }
  284. return ;
  285. }