MeshDeformSet.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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 : MeshDeformSet.cpp *
  25. * *
  26. * Programmer : Patrick Smith *
  27. * *
  28. * Start Date : 04/26/99 *
  29. * *
  30. * Last Update :
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "MeshDeformSet.H"
  36. #include "Util.H"
  37. #include "MeshDeformSaveSet.H"
  38. #include "MeshBuild.H"
  39. #include "MeshDeformSaveDefs.H"
  40. #include "MeshDeformDefs.H"
  41. ///////////////////////////////////////////////////////////////////////////
  42. //
  43. // Constants
  44. //
  45. ///////////////////////////////////////////////////////////////////////////
  46. const int MAX_DEFORM_KEY_FRAMES = 10;
  47. ///////////////////////////////////////////////////////////////////////////
  48. //
  49. // ~MeshDeformSetClass
  50. //
  51. ///////////////////////////////////////////////////////////////////////////
  52. MeshDeformSetClass::~MeshDeformSetClass (void)
  53. {
  54. SAFE_DELETE (m_pMesh);
  55. SAFE_DELETE (m_pVertexArray);
  56. SAFE_DELETE (m_pVertexOPStartArray);
  57. SAFE_DELETE (m_pVertexColors);
  58. Free_Key_Frames ();
  59. return ;
  60. }
  61. ///////////////////////////////////////////////////////////////////////////
  62. //
  63. // Set_Current_Key_Frame
  64. //
  65. ///////////////////////////////////////////////////////////////////////////
  66. void
  67. MeshDeformSetClass::Set_Current_Key_Frame (int index)
  68. {
  69. if (index >= -1 && index < MAX_DEFORM_KEY_FRAMES) {
  70. m_CurrentKeyFrame = index;
  71. }
  72. return ;
  73. }
  74. ///////////////////////////////////////////////////////////////////////////
  75. //
  76. // Set_Vertex_Position
  77. //
  78. ///////////////////////////////////////////////////////////////////////////
  79. void
  80. MeshDeformSetClass::Set_Vertex_Position
  81. (
  82. int index,
  83. const Point3 & value
  84. )
  85. {
  86. DEFORM_LIST &verticies = m_KeyFrames[m_CurrentKeyFrame]->verticies;
  87. BitArray &affected_verts = m_KeyFrames[m_CurrentKeyFrame]->affected_verts;
  88. //
  89. // Set the vert's position
  90. //
  91. m_pMesh->verts[index] = value;
  92. verticies.Add (VERT_INFO (index, value));
  93. //
  94. // Make sure we remember that this vert is affected
  95. //
  96. affected_verts.Set (index, 1);
  97. m_SetMembers.Set (index, 1);
  98. return ;
  99. }
  100. ///////////////////////////////////////////////////////////////////////////
  101. //
  102. // Set_Vertex_Color
  103. //
  104. ///////////////////////////////////////////////////////////////////////////
  105. void
  106. MeshDeformSetClass::Set_Vertex_Color
  107. (
  108. int index,
  109. int color_index,
  110. const VertColor & value
  111. )
  112. {
  113. DEFORM_LIST &colors = m_KeyFrames[m_CurrentKeyFrame]->colors;
  114. BitArray &affected_colors = m_KeyFrames[m_CurrentKeyFrame]->affected_colors;
  115. //
  116. // Set the vert's color
  117. //
  118. m_pMesh->vertCol[color_index] = value;
  119. colors.Add (VERT_INFO (index, value, color_index));
  120. //
  121. // Make sure we remember that this vert color is affected
  122. //
  123. affected_colors.Set (index, 1);
  124. m_SetMembers.Set (index, 1);
  125. return ;
  126. }
  127. ///////////////////////////////////////////////////////////////////////////
  128. //
  129. // Update_Set_Members
  130. //
  131. ///////////////////////////////////////////////////////////////////////////
  132. void
  133. MeshDeformSetClass::Update_Set_Members (void)
  134. {
  135. //
  136. // Examine each keyframe
  137. //
  138. m_SetMembers.ClearAll ();
  139. for (int index = 0; index < m_KeyFrames.Count (); index ++) {
  140. BitArray &affected_verts = m_KeyFrames[index]->affected_verts;
  141. BitArray &affected_colors = m_KeyFrames[index]->affected_colors;
  142. //
  143. // Mark the verts that are affected by this keyframe
  144. //
  145. for (int vert = 0; vert < m_VertexCount; vert ++) {
  146. if (affected_verts[vert] || affected_colors[vert]) {
  147. m_SetMembers.Set (vert, 1);
  148. }
  149. }
  150. }
  151. return ;
  152. }
  153. ///////////////////////////////////////////////////////////////////////////
  154. //
  155. // Collapse_Keyframe_Data
  156. //
  157. ///////////////////////////////////////////////////////////////////////////
  158. void
  159. MeshDeformSetClass::Collapse_Keyframe_Data (int keyframe)
  160. {
  161. DEFORM_LIST &verticies = m_KeyFrames[keyframe]->verticies;
  162. DEFORM_LIST &colors = m_KeyFrames[keyframe]->colors;
  163. BitArray &affected_verts = m_KeyFrames[keyframe]->affected_verts;
  164. BitArray &affected_colors = m_KeyFrames[keyframe]->affected_colors;
  165. //
  166. // Collapse the vertex position data
  167. //
  168. for (int index = 0; index < verticies.Count (); index ++) {
  169. VERT_INFO &info = verticies[index];
  170. //
  171. // If this vertex is unchanged, then remove it
  172. // from the list.
  173. //
  174. if (m_pVertexArray[index] == info.value) {
  175. verticies.Delete (index);
  176. index --;
  177. } else {
  178. affected_verts.Set (info.index, 1);
  179. m_SetMembers.Set (info.index, 1);
  180. }
  181. }
  182. //
  183. // Collapse the vertex color data
  184. //
  185. for (index = 0; index < colors.Count (); index ++) {
  186. VERT_INFO &info = colors[index];
  187. //
  188. // If this vertex is unchanged, then remove it
  189. // from the list.
  190. //
  191. if (m_pVertexColors[index] == info.value) {
  192. verticies.Delete (index);
  193. index --;
  194. } else {
  195. affected_colors.Set (info.index, 1);
  196. m_SetMembers.Set (info.index, 1);
  197. }
  198. }
  199. return ;
  200. }
  201. ///////////////////////////////////////////////////////////////////////////
  202. //
  203. // Reset_Key_Frame_Verts
  204. //
  205. ///////////////////////////////////////////////////////////////////////////
  206. void
  207. MeshDeformSetClass::Reset_Key_Frame_Verts (int keyframe)
  208. {
  209. DEFORM_LIST &verticies = m_KeyFrames[keyframe]->verticies;
  210. BitArray &affected_verts = m_KeyFrames[keyframe]->affected_verts;
  211. //
  212. // Reset all data for this keyframe
  213. //
  214. affected_verts.ClearAll ();
  215. verticies.Delete_All ();
  216. //
  217. // Regenerate the list of set members
  218. //
  219. Update_Set_Members ();
  220. return ;
  221. }
  222. ///////////////////////////////////////////////////////////////////////////
  223. //
  224. // Reset_Key_Frame_Colors
  225. //
  226. ///////////////////////////////////////////////////////////////////////////
  227. void
  228. MeshDeformSetClass::Reset_Key_Frame_Colors (int keyframe)
  229. {
  230. DEFORM_LIST &colors = m_KeyFrames[keyframe]->colors;
  231. BitArray &affected_colors = m_KeyFrames[keyframe]->affected_colors;
  232. //
  233. // Reset all data for this keyframe
  234. //
  235. affected_colors.ClearAll ();
  236. colors.Delete_All ();
  237. //
  238. // Regenerate the list of set members
  239. //
  240. Update_Set_Members ();
  241. return ;
  242. }
  243. ///////////////////////////////////////////////////////////////////////////
  244. //
  245. // Update_Current_Data
  246. //
  247. ///////////////////////////////////////////////////////////////////////////
  248. void
  249. MeshDeformSetClass::Update_Current_Data (void)
  250. {
  251. DEFORM_LIST &verticies = m_KeyFrames[m_CurrentKeyFrame]->verticies;
  252. DEFORM_LIST &colors = m_KeyFrames[m_CurrentKeyFrame]->colors;
  253. BitArray &affected_verts = m_KeyFrames[m_CurrentKeyFrame]->affected_verts;
  254. BitArray &affected_colors = m_KeyFrames[m_CurrentKeyFrame]->affected_colors;
  255. //
  256. // Assume we have no modifications for this keyframe
  257. //
  258. affected_verts.ClearAll ();
  259. affected_colors.ClearAll ();
  260. verticies.Delete_All ();
  261. colors.Delete_All ();
  262. //
  263. // Record the vertex position data
  264. //
  265. for (int index = 0; index < m_VertexCount; index ++) {
  266. // Is this vertex's position different than the undeformed mesh?
  267. Point3 orig = m_pVertexArray[index];
  268. Point3 current = m_pMesh->verts[index];
  269. //Apply_Position_Changes (index, m_CurrentKeyFrame, orig);
  270. if ( (orig.x != current.x) ||
  271. (orig.y != current.y) ||
  272. (orig.z != current.z)) {
  273. //
  274. // Record this vertex's position in our lists
  275. //
  276. affected_verts.Set (index, 1);
  277. verticies.Add (VERT_INFO (index, m_pMesh->verts[index]));
  278. }
  279. }
  280. // Only do this if the mesh is using vertex coloring
  281. if (m_pMesh->numCVerts >= m_pMesh->numVerts) {
  282. //
  283. // Record the vertex color data
  284. //
  285. for (int face = 0; face < m_pMesh->numFaces; face ++) {
  286. for (int vert = 0; vert < 3; vert ++) {
  287. //
  288. // Has this vertex color changed?
  289. //
  290. int vertex_index = m_pMesh->faces[face].v[vert];
  291. int color_index = m_pMesh->vcFace[face].t[vert];
  292. VertColor orig = m_pVertexColors[color_index];
  293. VertColor current = m_pMesh->vertCol[color_index];
  294. if ( (orig.x != current.x) ||
  295. (orig.y != current.y) ||
  296. (orig.z != current.z)) {
  297. affected_colors.Set (vertex_index, 1);
  298. colors.Add (VERT_INFO (vertex_index, m_pMesh->vertCol[color_index], color_index));
  299. }
  300. }
  301. }
  302. }
  303. //
  304. // Rebuild the list of verticies this 'set' affects
  305. //
  306. Update_Set_Members ();
  307. //
  308. // Collapse all unsused data from the remainder of the keyframes
  309. //
  310. for (index = 0; index < m_KeyFrames.Count (); index ++) {
  311. //Collapse_Keyframe_Data (index);
  312. }
  313. return ;
  314. }
  315. ///////////////////////////////////////////////////////////////////////////
  316. //
  317. // Update_Key_Frame
  318. //
  319. ///////////////////////////////////////////////////////////////////////////
  320. void
  321. MeshDeformSetClass::Update_Key_Frame (int key_frame)
  322. {
  323. DEFORM_LIST &verticies = m_KeyFrames[key_frame]->verticies;
  324. DEFORM_LIST &colors = m_KeyFrames[key_frame]->colors;
  325. BitArray &affected_verts = m_KeyFrames[key_frame]->affected_verts;
  326. BitArray &affected_colors = m_KeyFrames[key_frame]->affected_colors;
  327. if ((key_frame == m_CurrentKeyFrame) ||
  328. (verticies.Count () > 0) ||
  329. (colors.Count () > 0)) {
  330. // Clear all entries from this keyframe
  331. verticies.Delete_All ();
  332. colors.Delete_All ();
  333. //
  334. // Copy the vertex position changes
  335. //
  336. for (int vert = 0; vert < m_pMesh->numVerts; vert ++) {
  337. if (affected_verts[vert]) {
  338. verticies.Add (VERT_INFO (vert, m_pMesh->verts[vert]));
  339. }
  340. }
  341. //
  342. // Copy the vertex color changes
  343. //
  344. // Only do this if the mesh is using vertex coloring
  345. if (m_pMesh->numCVerts >= m_pMesh->numVerts) {
  346. for (int face = 0; face < m_pMesh->numFaces; face ++) {
  347. if (affected_colors[m_pMesh->faces[face].v[0]]) {
  348. int color_index = m_pMesh->vcFace[face].t[0];
  349. colors.Add (VERT_INFO (m_pMesh->faces[face].v[0], m_pMesh->vertCol[color_index], color_index));
  350. }
  351. if (affected_colors[m_pMesh->faces[face].v[1]]) {
  352. int color_index = m_pMesh->vcFace[face].t[1];
  353. colors.Add (VERT_INFO (m_pMesh->faces[face].v[1], m_pMesh->vertCol[color_index], color_index));
  354. }
  355. if (affected_colors[m_pMesh->faces[face].v[2]]) {
  356. int color_index = m_pMesh->vcFace[face].t[2];
  357. colors.Add (VERT_INFO (m_pMesh->faces[face].v[2], m_pMesh->vertCol[color_index], color_index));
  358. }
  359. }
  360. }
  361. }
  362. return ;
  363. }
  364. ///////////////////////////////////////////////////////////////////////////
  365. //
  366. // Init_Key_Frames
  367. //
  368. ///////////////////////////////////////////////////////////////////////////
  369. void
  370. MeshDeformSetClass::Init_Key_Frames (void)
  371. {
  372. //
  373. // For now, add all the key frames upfront
  374. //
  375. for (int index = 0; index < MAX_DEFORM_KEY_FRAMES; index ++) {
  376. KEY_FRAME *key_frame = new KEY_FRAME;
  377. m_KeyFrames.Add (key_frame);
  378. }
  379. return ;
  380. }
  381. ///////////////////////////////////////////////////////////////////////////
  382. //
  383. // Free_Key_Frames
  384. //
  385. ///////////////////////////////////////////////////////////////////////////
  386. void
  387. MeshDeformSetClass::Free_Key_Frames (void)
  388. {
  389. //
  390. // Loop through and free all the key frames
  391. //
  392. for (int index = 0; index < m_KeyFrames.Count (); index ++) {
  393. KEY_FRAME *key_frame = m_KeyFrames[index];
  394. SAFE_DELETE (key_frame);
  395. }
  396. m_KeyFrames.Delete_All ();
  397. return ;
  398. }
  399. ///////////////////////////////////////////////////////////////////////////
  400. //
  401. // Copy_Vertex_Array
  402. //
  403. ///////////////////////////////////////////////////////////////////////////
  404. void
  405. MeshDeformSetClass::Copy_Vertex_Array (Mesh &mesh)
  406. {
  407. Resize_Vertex_Array (mesh.numVerts, mesh.numCVerts);
  408. //
  409. // Copy the vertex positions from the mesh
  410. //
  411. for (int vert = 0; vert < mesh.numVerts; vert ++) {
  412. m_pVertexArray[vert] = mesh.verts[vert];
  413. }
  414. //
  415. // Copy the vertex colors from the mesh
  416. //
  417. for (int index = 0; index < mesh.numCVerts; index ++) {
  418. m_pVertexColors[index] = mesh.vertCol[index];
  419. }
  420. return ;
  421. }
  422. ///////////////////////////////////////////////////////////////////////////
  423. //
  424. // Resize_Vertex_Array
  425. //
  426. ///////////////////////////////////////////////////////////////////////////
  427. void
  428. MeshDeformSetClass::Resize_Vertex_Array (int count, int color_count)
  429. {
  430. if (count != m_VertexCount) {
  431. // Allocate a new array of verticies
  432. Point3 *vertex_array = new Point3[count];
  433. Point3 *opstart_array = new Point3[count];
  434. // Delete the old vertex array and remember the new one
  435. SAFE_DELETE (m_pVertexArray);
  436. SAFE_DELETE (m_pVertexOPStartArray);
  437. m_pVertexArray = vertex_array;
  438. m_pVertexOPStartArray = opstart_array;
  439. m_VertexCount = count;
  440. //
  441. // Reset the bounds of the 'affected verts' per keyframe arrays
  442. //
  443. for (int index = 0; index < m_KeyFrames.Count (); index ++) {
  444. m_KeyFrames[index]->affected_verts.SetSize (count);
  445. m_KeyFrames[index]->affected_colors.SetSize (count);
  446. m_KeyFrames[index]->affected_verts.ClearAll ();
  447. m_KeyFrames[index]->affected_colors.ClearAll ();
  448. }
  449. m_SetMembers.SetSize (count);
  450. m_SetMembers.ClearAll ();
  451. }
  452. if (color_count != m_VertexColorCount) {
  453. // Recreate the color deltas
  454. Point3 *color_array = new VertColor[color_count];
  455. for (int index = 0; index < color_count; index ++) {
  456. color_array[index].x = 0;
  457. color_array[index].y = 0;
  458. color_array[index].z = 0;
  459. }
  460. // Delete the old delta array and remeber the new one
  461. SAFE_DELETE (m_pVertexColors);
  462. m_VertexColorCount = color_count;
  463. m_pVertexColors = color_array;
  464. }
  465. return ;
  466. }
  467. ///////////////////////////////////////////////////////////////////////////
  468. //
  469. // Set_State
  470. //
  471. ///////////////////////////////////////////////////////////////////////////
  472. void
  473. MeshDeformSetClass::Set_State (float state)
  474. {
  475. m_State = state;
  476. int key_frame = (m_State * MAX_DEFORM_KEY_FRAMES) + 0.5F;
  477. Set_Current_Key_Frame ((key_frame >= 0) ? (key_frame - 1) : -1);
  478. return ;
  479. }
  480. ///////////////////////////////////////////////////////////////////////////
  481. //
  482. // Determine_Interpolation_Indicies
  483. //
  484. ///////////////////////////////////////////////////////////////////////////
  485. void
  486. MeshDeformSetClass::Determine_Interpolation_Indicies
  487. (
  488. int key_frame,
  489. bool position,
  490. int & from,
  491. int & to,
  492. float & state
  493. )
  494. {
  495. /*state = m_State;
  496. from = -1;
  497. to = key_frame;
  498. //
  499. // Determine where we should start interpolation
  500. //
  501. for (int index = 0; index <= key_frame; index ++) {
  502. if (position && m_KeyFrames[index]->verticies.Count () > 0) {
  503. from = index;
  504. } else if (!position && m_KeyFrames[index]->colors.Count () > 0) {
  505. from = index;
  506. }
  507. }
  508. //
  509. // Determine where we should end interpolation
  510. //
  511. for (index = to; index < MAX_DEFORM_KEY_FRAMES; index ++) {
  512. if (position && m_KeyFrames[index]->verticies.Count () > 0) {
  513. to = index;
  514. break;
  515. } else if (!position && m_KeyFrames[index]->colors.Count () > 0) {
  516. to = index;
  517. break;
  518. }
  519. }
  520. //
  521. // Determine the state (deformation percent)
  522. //
  523. state = 0;
  524. if (m_State > 0) {
  525. state = 1.0F;
  526. if ((to != from) && (m_CurrentKeyFrame < to)) {
  527. float value = m_CurrentKeyFrame;//key_frame;
  528. state = ((value - ((float)from)) / (float)(to-from));
  529. }
  530. }*/
  531. return ;
  532. }
  533. ///////////////////////////////////////////////////////////////////////////
  534. //
  535. // Apply_Position_Changes
  536. //
  537. ///////////////////////////////////////////////////////////////////////////
  538. void
  539. MeshDeformSetClass::Apply_Position_Changes
  540. (
  541. UINT vert,
  542. int frame_to_check,
  543. Point3 & position,
  544. Matrix3 *transform
  545. )
  546. {
  547. //
  548. // Determine where we should start interpolating this vert
  549. //
  550. int from = -1;
  551. for (int key_frame = frame_to_check; (key_frame >= 0) && (from == -1); key_frame --) {
  552. if (m_KeyFrames[key_frame]->affected_verts[vert]) {
  553. from = key_frame;
  554. }
  555. }
  556. //
  557. // Determine where we should end interpolating this vert
  558. //
  559. int to = -1;
  560. if (frame_to_check >= 0) {
  561. for (key_frame = frame_to_check; (key_frame < m_KeyFrames.Count ()) && (to == -1); key_frame ++) {
  562. if (m_KeyFrames[key_frame]->affected_verts[vert]) {
  563. to = key_frame;
  564. }
  565. }
  566. }
  567. //
  568. // Determine the deformation percent
  569. //
  570. float state = 0;
  571. if (m_State > 0) {
  572. state = 1.0F;
  573. if ((to != from) && (frame_to_check < to)) {
  574. float value = frame_to_check;
  575. state = ((value - ((float)from)) / (float)(to - from));
  576. }
  577. }
  578. if (from != -1) {
  579. //
  580. // Find the vertex value in the 'from' key frame and set the
  581. // triangle object's vertex to be this value (we will interplate from it).
  582. //
  583. DEFORM_LIST &vert_from = m_KeyFrames[from]->verticies;
  584. for (int index = 0; index < vert_from.Count (); index ++) {
  585. VERT_INFO &info = vert_from[index];
  586. if (info.index == vert) {
  587. Point3 new_pos = info.value;
  588. // Transform the new position if necessary
  589. if (transform != NULL) {
  590. new_pos = new_pos * (*transform);
  591. }
  592. position = new_pos;
  593. }
  594. }
  595. }
  596. if (to != -1) {
  597. //
  598. // Find the vertex value in the 'to' key frame and interpolate
  599. // this value from the triangle object's current vertex value.
  600. //
  601. DEFORM_LIST &vert_to = m_KeyFrames[to]->verticies;
  602. for (int index = 0; index < vert_to.Count (); index ++) {
  603. VERT_INFO &info = vert_to[index];
  604. if (info.index == vert) {
  605. Point3 new_pos = info.value;
  606. // Transform the new position if necessary
  607. if (transform != NULL) {
  608. new_pos = new_pos * (*transform);
  609. }
  610. position += state * (new_pos - position);
  611. //m_pMesh->verts[vert] = position;
  612. }
  613. }
  614. }
  615. return ;
  616. }
  617. ///////////////////////////////////////////////////////////////////////////
  618. //
  619. // Apply_Color_Changes
  620. //
  621. ///////////////////////////////////////////////////////////////////////////
  622. void
  623. MeshDeformSetClass::Apply_Color_Changes
  624. (
  625. UINT vert,
  626. int frame_to_check,
  627. Mesh & mesh
  628. //VertColor & color
  629. )
  630. {
  631. //
  632. // Determine where we should start interpolating this vert
  633. //
  634. int from = -1;
  635. for (int key_frame = frame_to_check; (key_frame >= 0) && (from == -1); key_frame --) {
  636. if (m_KeyFrames[key_frame]->affected_colors[vert]) {
  637. from = key_frame;
  638. }
  639. }
  640. //
  641. // Determine where we should end interpolating this vert
  642. //
  643. int to = -1;
  644. if (frame_to_check >= 0) {
  645. for (key_frame = frame_to_check; (key_frame < m_KeyFrames.Count ()) && (to == -1); key_frame ++) {
  646. if (m_KeyFrames[key_frame]->affected_colors[vert]) {
  647. to = key_frame;
  648. }
  649. }
  650. }
  651. //
  652. // Determine the deformation percent
  653. //
  654. float state = 0;
  655. if (m_State > 0) {
  656. state = 1.0F;
  657. if ((to != from) && (frame_to_check < to)) {
  658. float value = frame_to_check;
  659. state = ((value - ((float)from)) / (float)(to - from));
  660. }
  661. }
  662. if (from != -1) {
  663. //
  664. // Find the color value in the 'from' key frame and set the
  665. // triangle object's color to be this value (we will interplate from it).
  666. //
  667. DEFORM_LIST &color_from = m_KeyFrames[from]->colors;
  668. for (int index = 0; index < color_from.Count (); index ++) {
  669. VERT_INFO &info = color_from[index];
  670. if (info.index == vert) {
  671. //color = info.value;
  672. mesh.vertCol[info.color_index] = info.value;
  673. m_pMesh->vertCol[info.color_index] = info.value;
  674. }
  675. }
  676. }
  677. if (to != -1) {
  678. //
  679. // Find the color value in the 'to' key frame and interpolate
  680. // this value from the triangle object's current color value.
  681. //
  682. DEFORM_LIST &color_to = m_KeyFrames[to]->colors;
  683. for (int index = 0; index < color_to.Count (); index ++) {
  684. VERT_INFO &info = color_to[index];
  685. if (info.index == vert) {
  686. mesh.vertCol[info.color_index] = m_pMesh->vertCol[info.color_index] + state * (info.value - m_pMesh->vertCol[info.color_index]);
  687. }
  688. }
  689. }
  690. return ;
  691. }
  692. ///////////////////////////////////////////////////////////////////////////
  693. //
  694. // Apply_Color_Changes
  695. //
  696. ///////////////////////////////////////////////////////////////////////////
  697. void
  698. MeshDeformSetClass::Apply_Color_Changes
  699. (
  700. UINT vert_index,
  701. UINT vert_color_index,
  702. int frame_to_check,
  703. VertColor & color
  704. )
  705. {
  706. //
  707. // Determine where we should start interpolating this vert
  708. //
  709. int from = -1;
  710. for (int key_frame = frame_to_check; (key_frame >= 0) && (from == -1); key_frame --) {
  711. if (m_KeyFrames[key_frame]->affected_colors[vert_index]) {
  712. from = key_frame;
  713. }
  714. }
  715. //
  716. // Determine where we should end interpolating this vert
  717. //
  718. int to = -1;
  719. for (key_frame = frame_to_check; (key_frame < m_KeyFrames.Count ()) && (to == -1); key_frame ++) {
  720. if (m_KeyFrames[key_frame]->affected_colors[vert_index]) {
  721. to = key_frame;
  722. }
  723. }
  724. //
  725. // Determine the deformation percent
  726. //
  727. float state = 0;
  728. if (m_State > 0) {
  729. state = 1.0F;
  730. if ((to != from) && (frame_to_check < to)) {
  731. float value = frame_to_check;
  732. state = ((value - ((float)from)) / (float)(to - from));
  733. }
  734. }
  735. if (from != -1) {
  736. //
  737. // Find the color value in the 'from' key frame and set the
  738. // triangle object's color to be this value (we will interplate from it).
  739. //
  740. DEFORM_LIST &color_from = m_KeyFrames[from]->colors;
  741. for (int index = 0; index < color_from.Count (); index ++) {
  742. VERT_INFO &info = color_from[index];
  743. if (info.color_index == vert_color_index) {
  744. color = info.value;
  745. break;
  746. }
  747. }
  748. }
  749. if (to != -1) {
  750. //
  751. // Find the color value in the 'to' key frame and interpolate
  752. // this value from the triangle object's current color value.
  753. //
  754. DEFORM_LIST &color_to = m_KeyFrames[to]->colors;
  755. for (int index = 0; index < color_to.Count (); index ++) {
  756. VERT_INFO &info = color_to[index];
  757. if (info.color_index == vert_color_index) {
  758. color += state * (info.value - color);
  759. break;
  760. }
  761. }
  762. }
  763. return ;
  764. }
  765. ///////////////////////////////////////////////////////////////////////////
  766. //
  767. // Update_Mesh
  768. //
  769. ///////////////////////////////////////////////////////////////////////////
  770. void
  771. MeshDeformSetClass::Update_Mesh (TriObject &tri_obj)
  772. {
  773. Copy_Vertex_Array (tri_obj.mesh);
  774. // Should we update the mesh or copy it?
  775. if (m_pMesh != NULL) {
  776. //
  777. // Copy the vertex colors from the triangle object
  778. //
  779. for (int vert_color = 0; vert_color < m_pMesh->numCVerts; vert_color ++) {
  780. m_pMesh->vertCol[vert_color] = tri_obj.mesh.vertCol[vert_color];
  781. }
  782. //
  783. // Loop through all the verticies and interpolate their
  784. // positions and colors based on the current 'deformation state'.
  785. //
  786. for (UINT vert = 0; vert < (UINT)m_pMesh->numVerts; vert ++) {
  787. // Is this vertex affected by any keyframe in the set?
  788. if (m_SetMembers[vert]) {
  789. // Interpolate any changes to this vert
  790. Apply_Position_Changes (vert, m_CurrentKeyFrame, tri_obj.mesh.verts[vert]);
  791. m_pMesh->verts[vert] = tri_obj.mesh.verts[vert];
  792. Apply_Color_Changes (vert, m_CurrentKeyFrame, tri_obj.mesh);
  793. }
  794. }
  795. //
  796. // Copy the vertex colors from the triangle object
  797. //
  798. for (vert_color = 0; vert_color < m_pMesh->numCVerts; vert_color ++) {
  799. m_pMesh->vertCol[vert_color] = tri_obj.mesh.vertCol[vert_color];
  800. }
  801. /*for (int index = 0; index < m_pMesh->numCVerts; index ++) {
  802. m_pMesh->vertCol[index] = tri_obj.mesh.vertCol[index];
  803. }
  804. //
  805. for (int key_frame = 0; key_frame < m_KeyFrames.Count (); key_frame ++) {
  806. //
  807. // Update the verticies
  808. //
  809. int from = 0;
  810. int to = 0;
  811. float state = 0;
  812. Determine_Interpolation_Indicies (key_frame, true, from, to, state);
  813. DEFORM_LIST &vert_to = m_KeyFrames[to]->verticies;
  814. if (from <= m_CurrentKeyFrame) {
  815. if (from >= 0) {
  816. DEFORM_LIST &vert_from = m_KeyFrames[from]->verticies;
  817. for (int index = 0; index < vert_from.Count (); index ++) {
  818. VERT_INFO &info = vert_from[index];
  819. tri_obj.mesh.verts[info.index] = info.value;
  820. }
  821. } else {
  822. for (int index = 0; index < vert_to.Count (); index ++) {
  823. VERT_INFO &info = vert_to[index];
  824. tri_obj.mesh.verts[info.index] = m_pVertexArray[info.index];
  825. }
  826. }
  827. for (int index = 0; index < vert_to.Count (); index ++) {
  828. VERT_INFO &info = vert_to[index];
  829. tri_obj.mesh.verts[info.index] += state * (info.value - tri_obj.mesh.verts[info.index]);
  830. m_pMesh->verts[info.index] = tri_obj.mesh.verts[info.index];
  831. }
  832. }
  833. //
  834. // Update the vertex colors
  835. //
  836. Determine_Interpolation_Indicies (key_frame, false, from, to, state);
  837. DEFORM_LIST &color_to = m_KeyFrames[to]->colors;
  838. if (from <= m_CurrentKeyFrame) {
  839. if (from >= 0) {
  840. DEFORM_LIST &color_from = m_KeyFrames[from]->colors;
  841. for (int index = 0; index < color_from.Count (); index ++) {
  842. VERT_INFO &info = color_from[index];
  843. tri_obj.mesh.vertCol[info.index] = info.value;
  844. }
  845. } else {
  846. for (index = 0; index < color_to.Count (); index ++) {
  847. VERT_INFO &info = color_to[index];
  848. tri_obj.mesh.vertCol[info.index] = m_pMesh->vertCol[info.index];
  849. }
  850. }
  851. for (index = 0; index < color_to.Count (); index ++) {
  852. VERT_INFO &info = color_to[index];
  853. tri_obj.mesh.vertCol[info.index] = m_pMesh->vertCol[info.index] + state * (info.value - m_pMesh->vertCol[info.index]);
  854. //tri_obj.mesh.vertCol[info.index] += state * (info.value - tri_obj.mesh.vertCol[info.index]);
  855. }
  856. }
  857. }
  858. for (index = 0; index < m_pMesh->numCVerts; index ++) {
  859. m_pMesh->vertCol[index] = tri_obj.mesh.vertCol[index];
  860. }*/
  861. /*for (int vert = 0; vert < m_VertexCount; vert ++) {
  862. tri_obj.mesh.verts[vert] += (1.0F * m_pVertexDeltaArray[vert]);
  863. m_pMesh->verts[vert] = tri_obj.mesh.verts[vert];
  864. }
  865. //
  866. // Transform the vertex colors based on the current deform state
  867. //
  868. for (vert = 0; vert < m_VertexColorCount; vert ++) {
  869. tri_obj.mesh.vertCol[vert] += (1.0F * m_pVertexColors[vert]);
  870. }*/
  871. //
  872. // Pass the new selection onto the mesh
  873. //
  874. tri_obj.mesh.vertSel = m_pMesh->vertSel;
  875. tri_obj.PointsWereChanged ();
  876. } else {
  877. //
  878. // Make a copy of the mesh as it currently exists
  879. //
  880. m_pMesh = new Mesh (tri_obj.mesh);
  881. tri_obj.mesh.DeepCopy (m_pMesh, GEOM_CHANNEL | SELECT_CHANNEL | SUBSEL_TYPE_CHANNEL | VERTCOLOR_CHANNEL);
  882. }
  883. return ;
  884. }
  885. ///////////////////////////////////////////////////////////////////////////
  886. //
  887. // Update_Members
  888. //
  889. ///////////////////////////////////////////////////////////////////////////
  890. void
  891. MeshDeformSetClass::Update_Members (DEFORM_CHANNELS flags)
  892. {
  893. assert (m_CurrentKeyFrame >= 0);
  894. //
  895. // Add all the selected verts to the set array
  896. //
  897. for (int vert = 0; vert < m_pMesh->numVerts; vert ++) {
  898. if (m_pMesh->vertSel[vert]) {
  899. //
  900. // Should we add this vertex to the array of deformed verts
  901. // the current keyframe?
  902. //
  903. if (flags & VERT_POSITION) {
  904. m_KeyFrames[m_CurrentKeyFrame]->affected_verts.Set (vert, 1);
  905. }
  906. //
  907. // Should we add this vertex to the array of deformed vertex colors
  908. // the current keyframe?
  909. //
  910. if (flags & VERT_COLORS) {
  911. m_KeyFrames[m_CurrentKeyFrame]->affected_colors.Set (vert, 1);
  912. //
  913. // Map indicies in the vertex color array to the vertex array.
  914. //
  915. /*if (m_pMesh->numCVerts >= m_pMesh->numVerts) {
  916. for (int face = 0; face < m_pMesh->numFaces; face ++) {
  917. if (m_pMesh->faces[face].v[0] == vert) {
  918. int color_index = m_pMesh->vcFace[face].t[0];
  919. m_KeyFrames[m_CurrentKeyFrame]->affected_colors.Set (color_index, 1);
  920. }
  921. if (m_pMesh->faces[face].v[1] == vert) {
  922. int color_index = m_pMesh->vcFace[face].t[1];
  923. m_KeyFrames[m_CurrentKeyFrame]->affected_colors.Set (color_index, 1);
  924. }
  925. if (m_pMesh->faces[face].v[2] == vert) {
  926. int color_index = m_pMesh->vcFace[face].t[2];
  927. m_KeyFrames[m_CurrentKeyFrame]->affected_colors.Set (color_index, 1);
  928. }
  929. }
  930. }*/
  931. }
  932. //
  933. // Finally, add this vertex to the list of all
  934. // verticies affected by this set.
  935. //
  936. m_SetMembers.Set (vert, 1);
  937. }
  938. }
  939. //r (int index = m_CurrentKeyFrame; index < MAX_DEFORM_KEY_FRAMES; index ++) {
  940. Update_Key_Frame (m_CurrentKeyFrame);
  941. //
  942. return ;
  943. }
  944. ///////////////////////////////////////////////////////////////////////////
  945. //
  946. // Select_Members
  947. //
  948. ///////////////////////////////////////////////////////////////////////////
  949. void
  950. MeshDeformSetClass::Select_Members (void)
  951. {
  952. //
  953. // Loop through and select the necessary verts
  954. //
  955. for (int vert = 0; vert < m_pMesh->numVerts; vert ++) {
  956. m_pMesh->vertSel.Set (vert, m_SetMembers[vert]);
  957. }
  958. return ;
  959. }
  960. ///////////////////////////////////////////////////////////////////////////
  961. //
  962. // Restore_Members
  963. //
  964. ///////////////////////////////////////////////////////////////////////////
  965. void
  966. MeshDeformSetClass::Restore_Members (void)
  967. {
  968. /*
  969. //
  970. // Loop through and select the necessary verts
  971. //
  972. for (int vert = 0; vert < m_pMesh->numVerts; vert ++) {
  973. if (m_SetMembers[vert]) {
  974. //
  975. // Restore the vertex positions
  976. //
  977. m_pMesh->verts[vert] = m_pVertexArray[vert];
  978. m_pVertexDeltaArray[vert].x = 0;
  979. m_pVertexDeltaArray[vert].y = 0;
  980. m_pVertexDeltaArray[vert].z = 0;
  981. }
  982. }
  983. //
  984. // Restore the vertex colors
  985. //
  986. // Only do this if the mesh is using vertex coloring
  987. if (m_pMesh->numCVerts >= m_pMesh->numVerts) {
  988. for (int face = 0; face < m_pMesh->numFaces; face ++) {
  989. if (m_SetMembers[m_pMesh->faces[face].v[0]]) {
  990. int color_index = m_pMesh->vcFace[face].t[0];
  991. m_pVertexColors[color_index].x = 0;
  992. m_pVertexColors[color_index].y = 0;
  993. m_pVertexColors[color_index].z = 0;
  994. }
  995. if (m_SetMembers[m_pMesh->faces[face].v[1]]) {
  996. int color_index = m_pMesh->vcFace[face].t[1];
  997. m_pVertexColors[color_index].x = 0;
  998. m_pVertexColors[color_index].y = 0;
  999. m_pVertexColors[color_index].z = 0;
  1000. }
  1001. if (m_SetMembers[m_pMesh->faces[face].v[2]]) {
  1002. int color_index = m_pMesh->vcFace[face].t[2];
  1003. m_pVertexColors[color_index].x = 0;
  1004. m_pVertexColors[color_index].y = 0;
  1005. m_pVertexColors[color_index].z = 0;
  1006. }
  1007. }
  1008. }
  1009. */
  1010. return ;
  1011. }
  1012. ///////////////////////////////////////////////////////////////////////////
  1013. //
  1014. // Is_Empty
  1015. //
  1016. ///////////////////////////////////////////////////////////////////////////
  1017. bool
  1018. MeshDeformSetClass::Is_Empty (void) const
  1019. {
  1020. bool is_empty = true;
  1021. //
  1022. // If any vertex is 'selected' then this set is not empty.
  1023. //
  1024. for (int vert = 0; (vert < m_VertexCount) && is_empty; vert ++) {
  1025. is_empty = (m_SetMembers[vert] == 0);
  1026. }
  1027. // Return the true/false result code
  1028. return is_empty;
  1029. }
  1030. ///////////////////////////////////////////////////////////////////////////
  1031. //
  1032. // Save
  1033. //
  1034. ///////////////////////////////////////////////////////////////////////////
  1035. void
  1036. MeshDeformSetClass::Save
  1037. (
  1038. MeshBuilderClass & builder,
  1039. Mesh & mesh,
  1040. MeshDeformSaveSetClass & save_set,
  1041. Matrix3 * transform
  1042. )
  1043. {
  1044. // Start fresh
  1045. save_set.Reset ();
  1046. save_set.Set_Flag (DEFORM_SET_MANUAL_DEFORM, (m_bAutoApply == false));
  1047. //
  1048. // Determine how much each state change (from 0 - 1)
  1049. // is associated with each keyframe.
  1050. //
  1051. int key_frames = m_KeyFrames.Count ();
  1052. float state_inc = 1.0F / ((float)key_frames);
  1053. float old_state = m_State;
  1054. //
  1055. // Loop through all the keyframes
  1056. //
  1057. for (int key_frame = 0; key_frame < key_frames; key_frame ++) {
  1058. //
  1059. // Loop through all the verticies and see if this keyframe
  1060. // modifies any of them
  1061. //
  1062. bool verts_affected = false;
  1063. for (int vert = 0; (vert < m_VertexCount) && !verts_affected; vert ++) {
  1064. verts_affected = (m_KeyFrames[key_frame]->affected_verts[vert] == 1);
  1065. verts_affected |= (m_KeyFrames[key_frame]->affected_colors[vert] == 1);
  1066. }
  1067. //
  1068. // If the keyframe modifies any of these verts, then save
  1069. // the state of all verts in the set.
  1070. //
  1071. if (verts_affected) {
  1072. m_State = state_inc * (key_frame + 1);
  1073. save_set.Begin_Keyframe (m_State);
  1074. //
  1075. // Save the absolute state of all the verts and colors
  1076. // at this keyframe.
  1077. //
  1078. for (int w3d_vert_index = 0; w3d_vert_index < builder.Get_Vertex_Count (); w3d_vert_index ++) {
  1079. const MeshBuilderClass::VertClass &w3d_vert = builder.Get_Vertex(w3d_vert_index);
  1080. int max_vert_index = w3d_vert.Id;
  1081. if (m_SetMembers[max_vert_index]) {
  1082. //
  1083. // Get the absolute position of this vertex
  1084. //
  1085. Point3 position = mesh.verts[max_vert_index];
  1086. Apply_Position_Changes (max_vert_index, key_frame, position, transform);
  1087. //
  1088. // Get the absolute color of this vertex
  1089. //
  1090. VertColor color (1, 1, 1);
  1091. if (mesh.vertCol != NULL) {
  1092. int vert_col_index = w3d_vert.MaxVertColIndex;
  1093. color = mesh.vertCol[vert_col_index];
  1094. Apply_Color_Changes (max_vert_index, vert_col_index, key_frame, color);
  1095. }
  1096. //
  1097. // Save this vertex's deformations
  1098. //
  1099. save_set.Add_Vert (w3d_vert_index, position, color);
  1100. }
  1101. }
  1102. save_set.End_Keyframe ();
  1103. }
  1104. }
  1105. m_State = old_state;
  1106. return ;
  1107. }
  1108. ///////////////////////////////////////////////////////////////////////////
  1109. //
  1110. // Save
  1111. //
  1112. ///////////////////////////////////////////////////////////////////////////
  1113. IOResult
  1114. MeshDeformSetClass::Save (ISave *save_obj)
  1115. {
  1116. int key_frames = m_KeyFrames.Count ();
  1117. float state_inc = 1.0F / ((float)key_frames);
  1118. DeformChunkSetInfo set_info = { 0 };
  1119. set_info.KeyframeCount = key_frames;
  1120. set_info.flags = 0;
  1121. set_info.NumVerticies = m_VertexCount;
  1122. set_info.NumVertexColors = m_VertexColorCount;
  1123. if (m_bAutoApply == false) {
  1124. set_info.flags |= DEFORM_SET_MANUAL_DEFORM;
  1125. }
  1126. DWORD bytes = 0L;
  1127. //
  1128. // Write the set info to the chunk
  1129. //
  1130. save_obj->BeginChunk (DEFORM_CHUNK_SET_INFO);
  1131. IOResult result = save_obj->Write (&set_info, sizeof (set_info), &bytes);
  1132. //
  1133. // Now write a chunk for each keyframe
  1134. //
  1135. for (int index = 0; (index < key_frames) && (result == IO_OK); index ++) {
  1136. KEY_FRAME &key_frame = *(m_KeyFrames[index]);
  1137. DeformChunkKeyframeInfo keyframe_info = { 0 };
  1138. keyframe_info.DeformPercent = state_inc * (index + 1);
  1139. keyframe_info.VertexCount = key_frame.verticies.Count ();
  1140. keyframe_info.ColorCount = key_frame.colors.Count ();
  1141. //
  1142. // Write keyframe info to the chunk
  1143. //
  1144. //save_obj->BeginChunk (DEFORM_CHUNK_KEYFRAME_INFO);
  1145. result = save_obj->Write (&keyframe_info, sizeof (keyframe_info), &bytes);
  1146. //save_obj->EndChunk ();
  1147. //
  1148. // Loop through the verticies and save their position
  1149. //
  1150. for ( unsigned int pos_index = 0;
  1151. (pos_index < keyframe_info.VertexCount) && (result == IO_OK);
  1152. pos_index ++)
  1153. {
  1154. VERT_INFO &deform_data = key_frame.verticies[pos_index];
  1155. DeformDataChunk data;
  1156. data.VertexIndex = deform_data.index;
  1157. data.ColorIndex = deform_data.color_index;
  1158. data.Value = deform_data.value;
  1159. //
  1160. // Write vertex position info to the chunk
  1161. //
  1162. //save_obj->BeginChunk (DEFORM_CHUNK_POSITION_DATA);
  1163. result = save_obj->Write (&data, sizeof (data), &bytes);
  1164. //save_obj->EndChunk ();
  1165. }
  1166. //
  1167. // Loop through the verticies and save their color
  1168. //
  1169. for ( unsigned int color_index = 0;
  1170. (color_index < keyframe_info.ColorCount) && (result == IO_OK);
  1171. color_index ++)
  1172. {
  1173. VERT_INFO &deform_data = key_frame.colors[color_index];
  1174. DeformDataChunk data;
  1175. data.VertexIndex = deform_data.index;
  1176. data.ColorIndex = deform_data.color_index;
  1177. data.Value = deform_data.value;
  1178. //
  1179. // Write vertex color info to the chunk
  1180. //
  1181. //save_obj->BeginChunk (DEFORM_CHUNK_COLOR_DATA);
  1182. result = save_obj->Write (&data, sizeof (data), &bytes);
  1183. //save_obj->EndChunk ();
  1184. }
  1185. //
  1186. // Write the list of affected vertex positions to a chunk
  1187. //
  1188. /*if (result == IO_OK) {
  1189. //save_obj->BeginChunk (DEFORM_CHUNK_POSITION_VERTS);
  1190. result = key_frame.affected_verts.Save (save_obj);
  1191. //save_obj->EndChunk ();
  1192. }
  1193. //
  1194. // Write the list of affected vertex colors to a chunk
  1195. //
  1196. if (result == IO_OK) {
  1197. //save_obj->BeginChunk (DEFORM_CHUNK_COLOR_VERTS);
  1198. result = key_frame.affected_colors.Save (save_obj);
  1199. //save_obj->EndChunk ();
  1200. }*/
  1201. }
  1202. save_obj->EndChunk ();
  1203. // Return IO_OK on success IO_ERROR on failure
  1204. return result;
  1205. }
  1206. ///////////////////////////////////////////////////////////////////////////
  1207. //
  1208. // Load
  1209. //
  1210. ///////////////////////////////////////////////////////////////////////////
  1211. IOResult
  1212. MeshDeformSetClass::Load (ILoad *load_obj)
  1213. {
  1214. // Start fresh
  1215. Free_Key_Frames ();
  1216. Init_Key_Frames ();
  1217. DWORD bytes = 0L;
  1218. //
  1219. // Is this the chunk we were expecting?
  1220. //
  1221. IOResult result = load_obj->OpenChunk ();
  1222. if ( (result == IO_OK) &&
  1223. (load_obj->CurChunkID () == DEFORM_CHUNK_SET_INFO)) {
  1224. //
  1225. // Read information about this set from the chunk
  1226. //
  1227. DeformChunkSetInfo set_info = { 0 };
  1228. result = load_obj->Read (&set_info, sizeof (set_info), &bytes);
  1229. m_bAutoApply = !(set_info.flags & DEFORM_SET_MANUAL_DEFORM);
  1230. //
  1231. // Resize the internal data to fit the saved state
  1232. //
  1233. if (result == IO_OK) {
  1234. Resize_Vertex_Array (set_info.NumVerticies, set_info.NumVertexColors);
  1235. }
  1236. //
  1237. // Read keyframe information from the chunk
  1238. //
  1239. for ( unsigned int index = 0;
  1240. (index < set_info.KeyframeCount) && (result == IO_OK);
  1241. index ++)
  1242. {
  1243. KEY_FRAME &key_frame = *(m_KeyFrames[index]);
  1244. DeformChunkKeyframeInfo keyframe_info = { 0 };
  1245. result = load_obj->Read (&keyframe_info, sizeof (keyframe_info), &bytes);
  1246. //
  1247. // Read all the vertex positions from the chunk
  1248. //
  1249. for ( unsigned int pos_index = 0;
  1250. (pos_index < keyframe_info.VertexCount) && (result == IO_OK);
  1251. pos_index ++)
  1252. {
  1253. //
  1254. // Read vertex position info from the chunk
  1255. //
  1256. DeformDataChunk data;
  1257. result = load_obj->Read (&data, sizeof (data), &bytes);
  1258. if (result == IO_OK) {
  1259. key_frame.verticies.Add (VERT_INFO (data.VertexIndex, data.Value, data.ColorIndex));
  1260. key_frame.affected_verts.Set (data.VertexIndex, 1);
  1261. m_SetMembers.Set (data.VertexIndex, 1);
  1262. }
  1263. }
  1264. //
  1265. // Read all the vertex colors from the chunk
  1266. //
  1267. for ( unsigned int color_index = 0;
  1268. (color_index < keyframe_info.ColorCount) && (result == IO_OK);
  1269. color_index ++)
  1270. {
  1271. //
  1272. // Read vertex color info from the chunk
  1273. //
  1274. DeformDataChunk data;
  1275. result = load_obj->Read (&data, sizeof (data), &bytes);
  1276. if (result == IO_OK) {
  1277. key_frame.colors.Add (VERT_INFO (data.VertexIndex, data.Value, data.ColorIndex));
  1278. key_frame.affected_colors.Set (data.VertexIndex, 1);
  1279. m_SetMembers.Set (data.VertexIndex, 1);
  1280. }
  1281. }
  1282. //
  1283. // Read the list of affected vertex positions to a chunk
  1284. //
  1285. /*if (result == IO_OK) {
  1286. result = key_frame.affected_verts.Load (load_obj);
  1287. }
  1288. //
  1289. // Read the list of affected vertex colors to a chunk
  1290. //
  1291. if (result == IO_OK) {
  1292. result = key_frame.affected_colors.Load (load_obj);
  1293. }*/
  1294. }
  1295. }
  1296. load_obj->CloseChunk ();
  1297. // Return IO_OK on success IO_ERROR on failure
  1298. return result;
  1299. }