💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

586 lines
16 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. * Copyright (C) 2017-2019 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "resource-loader.hpp"
  20. #include "resource-manager.hpp"
  21. #include "../graphics/vertex-format.hpp"
  22. #include <emergent/emergent.hpp>
  23. using namespace Emergent;
  24. struct MaterialGroup
  25. {
  26. std::string materialName;
  27. std::uint32_t indexOffset;
  28. std::uint32_t triangleCount;
  29. AABB bounds;
  30. };
  31. enum VertexFlags
  32. {
  33. UV = 1,
  34. TANGENT = 2,
  35. WEIGHTS = 4
  36. };
  37. struct ModelData
  38. {
  39. std::uint32_t groupCount;
  40. MaterialGroup* groups;
  41. std::uint32_t vertexFormat;
  42. std::uint32_t vertexCount;
  43. AABB bounds;
  44. float* vertexData;
  45. std::uint32_t* indexData;
  46. };
  47. struct BoneData
  48. {
  49. std::string name;
  50. std::uint16_t parent;
  51. std::uint16_t childCount;
  52. std::uint16_t* children;
  53. Vector3 translation;
  54. Quaternion rotation;
  55. float length;
  56. };
  57. struct KeyframeData
  58. {
  59. float time;
  60. Transform transform;
  61. };
  62. struct ChannelData
  63. {
  64. std::uint16_t id;
  65. std::uint16_t keyframeCount;
  66. KeyframeData* keyframes;
  67. };
  68. struct AnimationData
  69. {
  70. std::string name;
  71. float startTime;
  72. float endTime;
  73. std::uint16_t channelCount;
  74. ChannelData* channels;
  75. };
  76. struct SkeletonData
  77. {
  78. std::uint16_t boneCount;
  79. BoneData* bones;
  80. std::uint16_t animationCount;
  81. AnimationData* animations;
  82. };
  83. static void constructBoneHierarchy(Bone* bone, const BoneData* data, std::uint16_t index)
  84. {
  85. bone->setName(data[index].name);
  86. Transform transform;
  87. transform.translation = data[index].translation;
  88. transform.rotation = data[index].rotation;
  89. transform.scale = Vector3(1.0f);
  90. bone->setRelativeTransform(transform);
  91. bone->setLength(data[index].length);
  92. for (std::uint16_t i = 0; i < data[index].childCount; ++i)
  93. {
  94. constructBoneHierarchy(bone->createChild(), data, data[index].children[i]);
  95. }
  96. }
  97. template <typename T>
  98. inline static void read8(T* result, unsigned char** data)
  99. {
  100. std::uint8_t temp = (*data)[0];
  101. *result = *reinterpret_cast<T*>(&temp);
  102. *data += 1;
  103. }
  104. template <typename T>
  105. inline static void read16(T* result, unsigned char** data)
  106. {
  107. std::uint16_t temp = ((*data)[0] << 0) | ((*data)[1] << 8);
  108. *result = *reinterpret_cast<T*>(&temp);
  109. *data += 2;
  110. }
  111. template <typename T>
  112. inline static void read32(T* result, unsigned char** data)
  113. {
  114. std::uint32_t temp = ((*data)[0] << 0) | ((*data)[1] << 8) | ((*data)[2] << 16) | ((*data)[3] << 24);
  115. *result = *reinterpret_cast<T*>(&temp);
  116. *data += 4;
  117. }
  118. inline static void readString(std::string* result, unsigned char** data)
  119. {
  120. result->resize((*data)[0]);
  121. for (std::size_t i = 0; i < result->size(); ++i)
  122. {
  123. (*result)[i] = (*data)[i + 1];
  124. }
  125. *data += result->size() + 1;
  126. }
  127. template <>
  128. Model* ResourceLoader<Model>::load(ResourceManager* resourceManager, std::istream* is)
  129. {
  130. // Allocate file data buffer
  131. is->seekg(0, is->end);
  132. int filesize = is->tellg();
  133. unsigned char* buffer = new unsigned char[filesize];
  134. // Read file data into buffer
  135. is->seekg(0, is->beg);
  136. is->read(reinterpret_cast<char*>(&buffer[0]), filesize);
  137. unsigned char* bufferOffset = &buffer[0];
  138. // Allocate model data
  139. ModelData* modelData = new ModelData();
  140. SkeletonData* skeletonData = nullptr;
  141. // Allocate material groups
  142. read32(&modelData->groupCount, &bufferOffset);
  143. modelData->groups = new MaterialGroup[modelData->groupCount];
  144. // Read material groups (and calculate triangle count)
  145. std::uint32_t triangleCount = 0;
  146. for (std::size_t i = 0; i < modelData->groupCount; ++i)
  147. {
  148. MaterialGroup* group = &modelData->groups[i];
  149. readString(&group->materialName, &bufferOffset);
  150. read32(&group->indexOffset, &bufferOffset);
  151. read32(&group->triangleCount, &bufferOffset);
  152. // Read bounds
  153. Vector3 min;
  154. Vector3 max;
  155. read32(&min.x, &bufferOffset);
  156. read32(&min.y, &bufferOffset);
  157. read32(&min.z, &bufferOffset);
  158. read32(&max.x, &bufferOffset);
  159. read32(&max.y, &bufferOffset);
  160. read32(&max.z, &bufferOffset);
  161. group->bounds.setMin(min);
  162. group->bounds.setMax(max);
  163. triangleCount += group->triangleCount;
  164. }
  165. // Read vertex format and count
  166. read32(&modelData->vertexFormat, &bufferOffset);
  167. read32(&modelData->vertexCount, &bufferOffset);
  168. // Read bounds
  169. Vector3 min;
  170. Vector3 max;
  171. read32(&min.x, &bufferOffset);
  172. read32(&min.y, &bufferOffset);
  173. read32(&min.z, &bufferOffset);
  174. read32(&max.x, &bufferOffset);
  175. read32(&max.y, &bufferOffset);
  176. read32(&max.z, &bufferOffset);
  177. modelData->bounds.setMin(min);
  178. modelData->bounds.setMax(max);
  179. // Calculate vertex size
  180. std::uint32_t vertexSize =
  181. 3 // Position
  182. + 3 // Normal
  183. + 2 * ((modelData->vertexFormat & UV) != 0) // UV
  184. + 4 * ((modelData->vertexFormat & TANGENT) != 0) // Tangent
  185. + 4 * ((modelData->vertexFormat & TANGENT) != 0) // Bitangent
  186. + 4 * ((modelData->vertexFormat & WEIGHTS) != 0) // Indices
  187. + 4 * ((modelData->vertexFormat & WEIGHTS) != 0); // Weights
  188. // Allocate vertex data
  189. modelData->vertexData = new float[modelData->vertexCount * vertexSize];
  190. // Read vertex data
  191. float* vertexDataOffset = &modelData->vertexData[0];
  192. for (std::size_t i = 0; i < modelData->vertexCount; ++i)
  193. {
  194. for (std::size_t j = 0; j < vertexSize; ++j)
  195. {
  196. read32(vertexDataOffset, &bufferOffset);
  197. ++vertexDataOffset;
  198. }
  199. }
  200. // Allocate index data
  201. std::uint32_t indexCount = triangleCount * 3;
  202. modelData->indexData = new std::uint32_t[indexCount];
  203. // Read index data
  204. for (std::size_t i = 0; i < indexCount; ++i)
  205. {
  206. read32(&modelData->indexData[i], &bufferOffset);
  207. }
  208. // Read skeleton data
  209. if (modelData->vertexFormat & WEIGHTS)
  210. {
  211. // Allocate skeleton data
  212. skeletonData = new SkeletonData();
  213. skeletonData->animations = nullptr;
  214. // Read bone count
  215. read16(&skeletonData->boneCount, &bufferOffset);
  216. // Allocate bones
  217. skeletonData->bones = new BoneData[skeletonData->boneCount];
  218. // Read bones
  219. for (std::size_t i = 0; i < skeletonData->boneCount; ++i)
  220. {
  221. BoneData* bone = &skeletonData->bones[i];
  222. bone->children = nullptr;
  223. readString(&bone->name, &bufferOffset);
  224. read16(&bone->parent, &bufferOffset);
  225. read16(&bone->childCount, &bufferOffset);
  226. bone->children = new std::uint16_t[bone->childCount];
  227. for (std::size_t j = 0; j < bone->childCount; ++j)
  228. {
  229. read16(&bone->children[j], &bufferOffset);
  230. }
  231. read32(&bone->translation.x, &bufferOffset);
  232. read32(&bone->translation.y, &bufferOffset);
  233. read32(&bone->translation.z, &bufferOffset);
  234. read32(&bone->rotation.w, &bufferOffset);
  235. read32(&bone->rotation.x, &bufferOffset);
  236. read32(&bone->rotation.y, &bufferOffset);
  237. read32(&bone->rotation.z, &bufferOffset);
  238. read32(&bone->length, &bufferOffset);
  239. }
  240. // Read animation count
  241. read16(&skeletonData->animationCount, &bufferOffset);
  242. if (skeletonData->animationCount != 0)
  243. {
  244. // Allocate animations
  245. skeletonData->animations = new AnimationData[skeletonData->animationCount];
  246. // Read animations
  247. for (std::size_t i = 0; i < skeletonData->animationCount; ++i)
  248. {
  249. AnimationData* animation = &skeletonData->animations[i];
  250. // Read animation name
  251. readString(&animation->name, &bufferOffset);
  252. // Read time frame
  253. read32(&animation->startTime, &bufferOffset);
  254. read32(&animation->endTime, &bufferOffset);
  255. // Read channel count
  256. read16(&animation->channelCount, &bufferOffset);
  257. // Allocate channels
  258. animation->channels = new ChannelData[animation->channelCount];
  259. // Read channels
  260. for (std::size_t j = 0; j < animation->channelCount; ++j)
  261. {
  262. ChannelData* channel = &animation->channels[j];
  263. // Read channel ID
  264. read16(&channel->id, &bufferOffset);
  265. // Read keyframe count
  266. read16(&channel->keyframeCount, &bufferOffset);
  267. // Allocate keyframes
  268. channel->keyframes = new KeyframeData[channel->keyframeCount];
  269. // Read keyframes
  270. for (std::size_t k = 0; k < channel->keyframeCount; ++k)
  271. {
  272. KeyframeData* keyframe = &channel->keyframes[k];
  273. // Read keyframe time
  274. read32(&keyframe->time, &bufferOffset);
  275. // Read keyframe translation
  276. read32(&keyframe->transform.translation.x, &bufferOffset);
  277. read32(&keyframe->transform.translation.y, &bufferOffset);
  278. read32(&keyframe->transform.translation.z, &bufferOffset);
  279. // Read keyframe rotation
  280. read32(&keyframe->transform.rotation.w, &bufferOffset);
  281. read32(&keyframe->transform.rotation.x, &bufferOffset);
  282. read32(&keyframe->transform.rotation.y, &bufferOffset);
  283. read32(&keyframe->transform.rotation.z, &bufferOffset);
  284. // Read keyframe scale
  285. read32(&keyframe->transform.scale.x, &bufferOffset);
  286. read32(&keyframe->transform.scale.y, &bufferOffset);
  287. read32(&keyframe->transform.scale.z, &bufferOffset);
  288. }
  289. }
  290. }
  291. }
  292. }
  293. // Free file data buffer
  294. delete[] buffer;
  295. #if defined(DEBUG)
  296. std::uint32_t newVertexCount = triangleCount * 3;
  297. std::uint32_t newVertexSize = vertexSize + 3;
  298. float* newVertexData = new float[newVertexCount * newVertexSize];
  299. const Vector3 barycentricCoordinates[3] =
  300. {
  301. Vector3(1, 0, 0),
  302. Vector3(0, 1, 0),
  303. Vector3(0, 0, 1)
  304. };
  305. for (std::size_t i = 0; i < indexCount; i += 3)
  306. {
  307. // For each triangle vertex
  308. for (std::size_t j = 0; j < 3; ++j)
  309. {
  310. float* oldVertex = &modelData->vertexData[modelData->indexData[i + j] * vertexSize];
  311. float* newVertex = &newVertexData[(i + j) * newVertexSize];
  312. // Copy old vertex data
  313. for (std::size_t k = 0; k < vertexSize; ++k)
  314. {
  315. *(newVertex++) = *(oldVertex++);
  316. }
  317. // Add barycentric coordinates
  318. *(newVertex++) = barycentricCoordinates[j].x;
  319. *(newVertex++) = barycentricCoordinates[j].y;
  320. *(newVertex) = barycentricCoordinates[j].z;
  321. // Reassign indices
  322. modelData->indexData[i + j] = i + j;
  323. }
  324. }
  325. // Replace old vertex buffer with new vertex buffer
  326. vertexSize = newVertexSize;
  327. delete[] modelData->vertexData;
  328. modelData->vertexData = newVertexData;
  329. modelData->vertexCount = newVertexCount;
  330. #endif // DEBUG
  331. GLuint vao;
  332. GLuint vbo;
  333. GLuint ibo;
  334. // Generate and bind VAO
  335. glGenVertexArrays(1, &vao);
  336. glBindVertexArray(vao);
  337. // Generate and bind VBO, then upload vertex data
  338. glGenBuffers(1, &vbo);
  339. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  340. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * modelData->vertexCount, modelData->vertexData, GL_STATIC_DRAW);
  341. // Setup vertex attribute arrays
  342. std::size_t attribOffset = 0;
  343. std::size_t attribSize = 0;
  344. // Vertex position attribute
  345. attribSize = 3;
  346. glEnableVertexAttribArray(VERTEX_POSITION);
  347. glVertexAttribPointer(VERTEX_POSITION, attribSize, GL_FLOAT, GL_FALSE, sizeof(float) * vertexSize, (char*)0 + attribOffset * sizeof(float));
  348. attribOffset += attribSize;
  349. // Vertex normal attribute
  350. attribSize = 3;
  351. glEnableVertexAttribArray(VERTEX_NORMAL);
  352. glVertexAttribPointer(VERTEX_NORMAL, attribSize, GL_FLOAT, GL_FALSE, sizeof(float) * vertexSize, (char*)0 + attribOffset * sizeof(float));
  353. attribOffset += attribSize;
  354. // Vertex UV attribute
  355. if ((modelData->vertexFormat & UV) != 0)
  356. {
  357. attribSize = 2;
  358. glEnableVertexAttribArray(VERTEX_TEXCOORD);
  359. glVertexAttribPointer(VERTEX_TEXCOORD, attribSize, GL_FLOAT, GL_FALSE, sizeof(float) * vertexSize, (char*)0 + attribOffset * sizeof(float));
  360. attribOffset += attribSize;
  361. }
  362. // Vertex tangent and bitangent attributes
  363. if ((modelData->vertexFormat & TANGENT) != 0)
  364. {
  365. // Tangent
  366. attribSize = 4;
  367. glEnableVertexAttribArray(VERTEX_TANGENT);
  368. glVertexAttribPointer(VERTEX_TANGENT, attribSize, GL_FLOAT, GL_FALSE, sizeof(float) * vertexSize, (char*)0 + attribOffset * sizeof(float));
  369. attribOffset += attribSize;
  370. // Bitangent
  371. attribSize = 4;
  372. glEnableVertexAttribArray(VERTEX_BITANGENT);
  373. glVertexAttribPointer(VERTEX_BITANGENT, attribSize, GL_FLOAT, GL_FALSE, sizeof(float) * vertexSize, (char*)0 + attribOffset * sizeof(float));
  374. attribOffset += attribSize;
  375. }
  376. // Vertex indices and weights attributes
  377. if ((modelData->vertexFormat & WEIGHTS) != 0)
  378. {
  379. // Indices
  380. attribSize = 4;
  381. glEnableVertexAttribArray(VERTEX_BONE_INDICES);
  382. glVertexAttribPointer(VERTEX_BONE_INDICES, attribSize, GL_FLOAT, GL_FALSE, sizeof(float) * vertexSize, (char*)0 + attribOffset * sizeof(float));
  383. attribOffset += attribSize;
  384. // Weights
  385. attribSize = 4;
  386. glEnableVertexAttribArray(VERTEX_BONE_WEIGHTS);
  387. glVertexAttribPointer(VERTEX_BONE_WEIGHTS, attribSize, GL_FLOAT, GL_FALSE, sizeof(float) * vertexSize, (char*)0 + attribOffset * sizeof(float));
  388. attribOffset += attribSize;
  389. }
  390. #if defined(DEBUG)
  391. {
  392. // Vertex barycentric coordinates attribute
  393. attribSize = 3;
  394. glEnableVertexAttribArray(VERTEX_BARYCENTRIC);
  395. glVertexAttribPointer(VERTEX_BARYCENTRIC, attribSize, GL_FLOAT, GL_FALSE, sizeof(float) * vertexSize, (char*)0 + attribOffset * sizeof(float));
  396. attribOffset += attribSize;
  397. }
  398. #endif // DEBUG
  399. // Generate and bind IBO, then upload index data
  400. glGenBuffers(1, &ibo);
  401. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  402. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(std::uint32_t) * indexCount, modelData->indexData, GL_STATIC_DRAW);
  403. // Delete vertex and index data
  404. delete[] modelData->vertexData;
  405. delete[] modelData->indexData;
  406. // Allocate model
  407. Model* model = new Model();
  408. model->setVAO(vao);
  409. model->setVBO(vbo);
  410. model->setIBO(ibo);
  411. model->setVertexFormat(modelData->vertexFormat);
  412. model->setBounds(modelData->bounds);
  413. // Create model groups
  414. for (std::size_t i = 0; i < modelData->groupCount; ++i)
  415. {
  416. MaterialGroup* modelDataGroup = &modelData->groups[i];
  417. // Allocate model group
  418. Model::Group* modelGroup = new Model::Group();
  419. // Set model group name
  420. modelGroup->name = modelDataGroup->materialName;
  421. // Load material
  422. std::string materialFilename = modelDataGroup->materialName + std::string(".mtl");
  423. modelGroup->material = resourceManager->load<Material>(materialFilename);
  424. // Setup model group geometry
  425. modelGroup->indexOffset = modelDataGroup->indexOffset;
  426. modelGroup->triangleCount = modelDataGroup->triangleCount;
  427. modelGroup->bounds = modelDataGroup->bounds;
  428. // Add model group to model
  429. model->addGroup(modelGroup);
  430. }
  431. // Create skeleton
  432. if (skeletonData != nullptr)
  433. {
  434. // Allocate skeleton
  435. Skeleton* skeleton = new Skeleton();
  436. // Construct bone hierarchy from bone data
  437. constructBoneHierarchy(skeleton->getRootBone(), skeletonData->bones, 0);
  438. // Calculate bind pose
  439. skeleton->calculateBindPose();
  440. // Create animations
  441. for (std::size_t i = 0; i < skeletonData->animationCount; ++i)
  442. {
  443. AnimationData* animationData = &skeletonData->animations[i];
  444. AnimationClip<Transform>* clip = new AnimationClip<Transform>();
  445. clip->setInterpolator(lerp<Transform>);
  446. for (std::size_t j = 0; j < animationData->channelCount; ++j)
  447. {
  448. ChannelData* channelData = &animationData->channels[j];
  449. AnimationChannel<Transform>* channel = clip->addChannel(channelData->id);
  450. for (std::size_t k = 0; k < channelData->keyframeCount; ++k)
  451. {
  452. KeyframeData* keyframeData = &channelData->keyframes[k];
  453. channel->insertKeyframe(keyframeData->time, keyframeData->transform);
  454. }
  455. }
  456. // Add animation clip to skeleton
  457. skeleton->addAnimationClip(animationData->name, clip);
  458. }
  459. // Add skeleton to model
  460. model->setSkeleton(skeleton);
  461. }
  462. // Delete model data groups
  463. delete[] modelData->groups;
  464. // Delete model data
  465. delete modelData;
  466. // Delete skeleton data
  467. if (skeletonData != nullptr)
  468. {
  469. for (std::size_t i = 0; i < skeletonData->boneCount; ++i)
  470. {
  471. delete[] skeletonData->bones[i].children;
  472. }
  473. delete[] skeletonData->bones;
  474. for (std::size_t i = 0; i < skeletonData->animationCount; ++i)
  475. {
  476. AnimationData* animation = &skeletonData->animations[i];
  477. for (std::size_t j = 0; j < animation->channelCount; ++j)
  478. {
  479. delete[] animation->channels[j].keyframes;
  480. }
  481. delete[] animation->channels;
  482. }
  483. delete[] skeletonData->animations;
  484. delete skeletonData;
  485. }
  486. return model;
  487. }