DaFile.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears 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. #include <iostream>
  16. #include <bitset>
  17. #include <cstdint>
  18. #include "data/DaFile.h"
  19. // Root offsets are in the same units as the model geometry (the root height of an idle Cloud is 466,
  20. // against leg bones of 277 and 253), so they are not scaled.
  21. const float DaFile::OFFSET_SCALE = 1.0f;
  22. DaFile::DaFile(File file): da_file_(file){
  23. animations_.clear();
  24. // Save file contents as a byte array.
  25. file.SetOffset(0);
  26. da_bytes_ = new u8[file.GetFileSize()];
  27. file.GetFileBuffer(da_bytes_, 0, file.GetFileSize());
  28. bit_offset_ = 0;
  29. file.SetOffset(0);
  30. Read();
  31. }
  32. std::vector<std::string> DaFile::GenerateAFiles(std::string model_id, std::string path){
  33. std::vector<std::string> file_list;
  34. u32 zero = 0x00000000;
  35. u32 version = 0x00000001;
  36. u32 rotation_order = 0x00020001;
  37. for (int anim = 0; anim < animations_.size(); anim ++){
  38. /*
  39. * .a files have a 36 byte header header, and after that, each animation frame. Each
  40. * animation frame has three floats (of 4 bytes) for root rotation, three for root offset,
  41. * and after them, the bone rotation data.
  42. */
  43. std::string file_index_name = std::to_string(animations_[anim].index);
  44. while (file_index_name.size() < 2) file_index_name = "0" + file_index_name;
  45. std::string file_name = path + model_id + "_" + file_index_name + ".a";
  46. std::ofstream a(file_name, std::ios::out | std::ios::binary);
  47. int bone_count = animations_[anim].bone_count - 1;
  48. a.write(reinterpret_cast<const char*>(&version), 4);
  49. a.write(reinterpret_cast<const char*>(&animations_[anim].frame_count), 4);
  50. a.write(reinterpret_cast<const char*>(&bone_count), 4);
  51. a.write(reinterpret_cast<const char*>(&rotation_order), 4);
  52. a.write(reinterpret_cast<const char*>(&zero), 4); // Runtime data, skip 5 bytes.
  53. a.write(reinterpret_cast<const char*>(&zero), 4);
  54. a.write(reinterpret_cast<const char*>(&zero), 4);
  55. a.write(reinterpret_cast<const char*>(&zero), 4);
  56. a.write(reinterpret_cast<const char*>(&zero), 4);
  57. for (int f = 0; f < animations_[anim].frames.size(); f ++){
  58. // Root rotation (always 0).
  59. a.write(reinterpret_cast<const char*>(&zero), 4);
  60. a.write(reinterpret_cast<const char*>(&zero), 4);
  61. a.write(reinterpret_cast<const char*>(&zero), 4);
  62. // Root offset.
  63. a.write(reinterpret_cast<const char*>(&animations_[anim].frames[f].offset.f_x), 4);
  64. a.write(reinterpret_cast<const char*>(&animations_[anim].frames[f].offset.f_y), 4);
  65. a.write(reinterpret_cast<const char*>(&animations_[anim].frames[f].offset.f_z), 4);
  66. // Bones.
  67. for (int b = 1; b < animations_[anim].frames[f].bones.size(); b ++){
  68. a.write(reinterpret_cast<const char*>(&animations_[anim].frames[f].bones[b].f_x), 4);
  69. a.write(reinterpret_cast<const char*>(&animations_[anim].frames[f].bones[b].f_y), 4);
  70. a.write(reinterpret_cast<const char*>(&animations_[anim].frames[f].bones[b].f_z), 4);
  71. }
  72. }
  73. a.close();
  74. file_list.push_back(file_name);
  75. }
  76. return file_list;
  77. }
  78. int DaFile::ExtendSignInteger(int val, int len){
  79. // Two's complement value of "len" bits to a signed integer.
  80. if (len > 0 && (val & (1 << (len - 1))) != 0) return val - (1 << len);
  81. return val;
  82. }
  83. int DaFile::GetSignExtendedShort(int val, int len){
  84. return ExtendSignInteger(val, len);
  85. }
  86. void DaFile::Read(){
  87. bit_offset_ = 0;
  88. da_file_.SetOffset(0);
  89. int num_animations = da_file_.readU32LE();
  90. bit_offset_ += 32;
  91. for (int a = 0; a < num_animations; a ++){
  92. //for (int a = 0; a < 1; a ++){ // TODO DEBUG
  93. int start_offset = bit_offset_ / 8; // In bytes.
  94. Animation anim = ReadAnimation();
  95. anim.index = a;
  96. if (anim.frame_count > 0) animations_.push_back(anim);
  97. // Manually go to the next animation
  98. bit_offset_ = (start_offset + anim.size + 12) * 8; // + 12 is for skipping the main header.
  99. da_file_.SetOffset(start_offset + anim.size + 12);
  100. }
  101. }
  102. DaFile::Animation DaFile::ReadAnimation(){
  103. Animation anim;
  104. // Read the animation headers.
  105. da_file_.SetOffset(bit_offset_ / 8);
  106. anim.bone_count = da_file_.readU32LE();
  107. bit_offset_ += 32;
  108. anim.frame_count = da_file_.readU32LE();
  109. bit_offset_ += 32;
  110. anim.size = da_file_.readU32LE();
  111. // Skip small (< 11 bytes) animations.
  112. if (anim.size < 11){
  113. anim.frame_count = 0;
  114. return anim;
  115. }
  116. bit_offset_ += 32;
  117. da_file_.readU32LE(); // Duplicated Frames + Size
  118. bit_offset_ += 32;
  119. anim.key = da_file_.readU8();
  120. bit_offset_ += 8;
  121. Frame frame = ReadFirstFrame(anim.bone_count, anim.key);
  122. anim.frames.push_back(frame);
  123. for (int i = 1; i < anim.frame_count; i ++){
  124. frame = ReadFrame(anim.bone_count, anim.key, frame);
  125. anim.frames.push_back(frame);
  126. }
  127. return anim;
  128. }
  129. DaFile::Frame DaFile::ReadFirstFrame(u32 bones, u8 key){
  130. Frame frame;
  131. // Read root offset. In the first frame, always 16 bytes each, as short.
  132. frame.offset.s_x = GetBitsS(16);
  133. frame.offset.s_y = GetBitsS(16);
  134. frame.offset.s_z = GetBitsS(16);
  135. // Turn offset to float. Any scaling that needs to be done would be done here.
  136. frame.offset.f_x = static_cast<float>(frame.offset.s_x) * OFFSET_SCALE;
  137. // Y points down in the original data, up in the converted models.
  138. frame.offset.f_y = static_cast<float>(frame.offset.s_y) * -OFFSET_SCALE;
  139. frame.offset.f_z = static_cast<float>(frame.offset.s_z) * OFFSET_SCALE;
  140. // Read bones.
  141. for (int i = 0; i < bones; i ++){
  142. TriValue bone;
  143. // Now get each bone rotation (the first bone is actually the root, not part of the
  144. // skeleton). During the first frame, the rotations are always (12 - key) bits. The values
  145. // are shifted by key to align it to 12 bits.
  146. bone.s_x = GetBitsS(12 - key) * (1 << key);
  147. bone.s_y = GetBitsS(12 - key) * (1 << key);
  148. bone.s_z = GetBitsS(12 - key) * (1 << key);
  149. // Store the INT version as the angle in the 0-4095 range.
  150. bone.i_x = bone.s_x & 0xFFF;
  151. bone.i_y = bone.s_y & 0xFFF;
  152. bone.i_z = bone.s_z & 0xFFF;
  153. // Convert to float.
  154. bone.f_x = static_cast<float>(bone.i_x) / 4096.0f * 360.0f;
  155. bone.f_y = static_cast<float>(bone.i_y) / 4096.0f * 360.0f;
  156. bone.f_z = static_cast<float>(bone.i_z) / 4096.0f * 360.0f;
  157. frame.bones.push_back(bone);
  158. }
  159. return frame;
  160. }
  161. DaFile::Frame DaFile::ReadFrame(u32 bones, u8 key, Frame prev){
  162. Frame frame;
  163. // Get the root offset delta and add it to the previous frame values. Deltas are in the same
  164. // units as the first frame offset, so they get the same scale.
  165. frame.offset.s_x = 0;
  166. frame.offset.s_y = 0;
  167. frame.offset.s_z = 0;
  168. frame.offset.i_x = GetDynamicOffsetFromStream();
  169. frame.offset.i_y = GetDynamicOffsetFromStream();
  170. frame.offset.i_z = GetDynamicOffsetFromStream();
  171. frame.offset.f_x = static_cast<float>(frame.offset.i_x) * OFFSET_SCALE + prev.offset.f_x;
  172. frame.offset.f_y = static_cast<float>(frame.offset.i_y) * -OFFSET_SCALE + prev.offset.f_y;
  173. frame.offset.f_z = static_cast<float>(frame.offset.i_z) * OFFSET_SCALE + prev.offset.f_z;
  174. for (int i = 0; i < bones; i ++){
  175. TriValue bone;
  176. // Rotations are 12 bit angles (4096 is a full turn). Deltas are added to the previous
  177. // value, already shifted to 12 bits, and the result wraps around a full turn.
  178. bone.s_x = GetCompressedDeltaFromStream(key) + prev.bones[i].s_x;
  179. bone.s_y = GetCompressedDeltaFromStream(key) + prev.bones[i].s_y;
  180. bone.s_z = GetCompressedDeltaFromStream(key) + prev.bones[i].s_z;
  181. bone.i_x = bone.s_x & 0xFFF;
  182. bone.i_y = bone.s_y & 0xFFF;
  183. bone.i_z = bone.s_z & 0xFFF;
  184. bone.f_x = static_cast<float>(bone.i_x) / 4096.0f * 360.0f;
  185. bone.f_y = static_cast<float>(bone.i_y) / 4096.0f * 360.0f;
  186. bone.f_z = static_cast<float>(bone.i_z) / 4096.0f * 360.0f;
  187. frame.bones.push_back(bone);
  188. }
  189. return frame;
  190. }
  191. int DaFile::GetDynamicOffsetFromStream(){
  192. // The control bit is an unsigned flag: 1 for a 16 bit value, 0 for a 7 bit one.
  193. if (GetBitsU(1) == 1) return GetBitsS(16);
  194. return GetBitsS(7);
  195. }
  196. int DaFile::GetBitsU(int bits){
  197. // Bits are read most significant first.
  198. int value = 0;
  199. for (int i = 0; i < bits; i ++){
  200. const int byte = da_bytes_[bit_offset_ / 8];
  201. const int bit = (byte >> (7 - (bit_offset_ % 8))) & 1;
  202. value = (value << 1) | bit;
  203. bit_offset_ ++;
  204. }
  205. return value;
  206. }
  207. int DaFile::GetBitsS(int bits){
  208. return GetSignExtendedShort(GetBitsU(bits), bits);
  209. }
  210. int DaFile::GetCompressedDeltaFromStream(int lowered_precision_bits){
  211. unsigned int bits;
  212. int first_bit = GetBitsU(1);
  213. if (first_bit == 1){
  214. unsigned int key = GetBitsU(3);
  215. int temp, sign, orig_temp;
  216. unsigned int msb_mask;
  217. switch (key){
  218. case 0: // Return the smallest possible decrement delta (at given precision).
  219. return (-1 << lowered_precision_bits);
  220. case 1: case 2: case 3: case 4: case 5: case 6:
  221. bits = key;
  222. // Read a corresponding number of bits from the stream.
  223. temp = GetBitsS(bits);
  224. // Invert the value of the last bit
  225. sign = (int) pow(2, bits - 1);
  226. // Set sign.
  227. if (temp < 0) temp -= sign;
  228. else temp += sign;
  229. // Convert to 12-bits value
  230. temp *= (int) pow(2, lowered_precision_bits);
  231. return temp;
  232. case 7:
  233. // Read an uncompressed value from the stream (at requested precision),
  234. // and return.
  235. temp = GetBitsS(12 - lowered_precision_bits);
  236. return (temp << lowered_precision_bits);
  237. }
  238. }
  239. // Default/error: return zero.
  240. return 0;
  241. }