model.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #pragma once
  2. #include <glad/glad.h>
  3. #include <vector>
  4. #include <gl2d/gl2d.h>
  5. struct KeyFrame
  6. {
  7. float timestamp = 0;
  8. glm::vec3 pos = {};
  9. glm::vec3 scale = {1,1,1};
  10. glm::quat rotation = {0,0,0,1};
  11. glm::mat4 getMatrix() const
  12. {
  13. glm::mat4 translation = glm::translate(glm::mat4(1.0f), pos);
  14. glm::mat4 rotationMat = glm::mat4_cast(rotation);
  15. //glm::mat4 scaleMat = glm::scale(glm::mat4(1.0f), scale);
  16. glm::mat4 scaleMat = glm::scale(glm::mat4(1.0f), glm::vec3(1.f));
  17. return translation * rotationMat * scaleMat;
  18. }
  19. };
  20. inline KeyFrame interpolateKeyFrames(const KeyFrame &kf1, const KeyFrame &kf2, float time)
  21. {
  22. if (kf1.timestamp == kf2.timestamp) return kf1; // No blending needed
  23. float t = (time - kf1.timestamp) / (kf2.timestamp - kf1.timestamp);
  24. t = glm::clamp(t, 0.0f, 1.0f);
  25. KeyFrame result;
  26. result.timestamp = time;
  27. result.pos = glm::mix(kf1.pos, kf2.pos, t);
  28. result.scale = glm::mix(kf1.scale, kf2.scale, t);
  29. result.rotation = glm::slerp(kf1.rotation, kf2.rotation, t);
  30. return result;
  31. }
  32. struct Animation
  33. {
  34. float animationLength = 0;
  35. std::vector<std::vector<KeyFrame>> kayFrames;
  36. enum AnimationType
  37. {
  38. none,
  39. idle,
  40. running,
  41. falling,
  42. meleHit,
  43. ANIMATIONS_COUNT
  44. };
  45. };
  46. //
  47. struct AnimationStateClient
  48. {
  49. int currentAnimation = 0;
  50. float attackTimer = 0;
  51. bool isAttacking = false;
  52. void signalAttack()
  53. {
  54. if (!isAttacking)
  55. {
  56. attackTimer = 0;
  57. isAttacking = true;
  58. }
  59. }
  60. void setAnimation(int type)
  61. {
  62. currentAnimation = type;
  63. };
  64. float animationTime = 0;
  65. void update(float deltaTime) { animationTime += deltaTime; if (isAttacking) { attackTimer += deltaTime; } };
  66. };
  67. struct AnimationStateServer
  68. {
  69. float runningTime = 0;
  70. bool attacked = 0;
  71. void update(float deltaTime) { runningTime -= deltaTime; runningTime = std::max(runningTime, 0.f); };
  72. };
  73. //used to be inharented by an entity
  74. struct Animatable
  75. {
  76. AnimationStateServer animationStateServer;
  77. };
  78. struct Model
  79. {
  80. GLuint geometry = 0;
  81. GLuint indexBuffer = 0;
  82. GLuint vao = 0;
  83. size_t vertexCount = 0;
  84. std::vector<glm::mat4> transforms;
  85. std::int8_t headIndex = -1;
  86. std::int8_t bodyIndex = -1;
  87. std::int8_t rLegIndex = -1;
  88. std::int8_t lLefIndex = -1;
  89. std::int8_t rArmIndex = -1;
  90. std::int8_t lArmIndex = -1;
  91. std::int8_t pupilsIndex = -1;
  92. std::int8_t lEyeIndex = -1;
  93. std::int8_t rEyeIndex = -1;
  94. std::int8_t headArmourIndex = -1;
  95. std::int8_t bodyArmourIndex = -1;
  96. std::int8_t rLegArmourIndex = -1;
  97. std::int8_t lLefArmourIndex = -1;
  98. std::int8_t rArmArmourIndex = -1;
  99. std::int8_t lArmArmourIndex = -1;
  100. std::vector<Animation> animations;
  101. std::int8_t animationsIndex[Animation::ANIMATIONS_COUNT] = {-1};
  102. void cleanup();
  103. };
  104. struct BoneTransform
  105. {
  106. glm::quat rotation = {0,0,0,1};
  107. glm::vec3 position = {};
  108. glm::mat4 getPoseMatrix();
  109. bool goTowards(BoneTransform &other, float speed);
  110. };
  111. struct BlockModel
  112. {
  113. std::vector<float> vertices;
  114. std::vector<float> uvs;
  115. glm::vec3 minPos = {};
  116. glm::vec3 maxPos = {};
  117. glm::vec3 getDimensions() { return maxPos - minPos; }
  118. void cleanup() { *this = {}; }
  119. };
  120. struct ModelsManager
  121. {
  122. void loadAllModels(std::string path, bool reportErrors);
  123. void clearAllModels();
  124. Model human;
  125. Model pig;
  126. Model cat;
  127. Model rightHand;
  128. Model goblin;
  129. Model trainingDummy;
  130. Model scareCrow;
  131. enum BlockModels
  132. {
  133. chairModel,
  134. mugModel,
  135. gobletModel,
  136. wineBottleModel,
  137. skullModel,
  138. skullTorchModel,
  139. booksModel,
  140. candleHolderModel,
  141. potModel,
  142. jarModel,
  143. globeModel,
  144. keg,
  145. workBenchModel,
  146. tableModel,
  147. workItemsModel,
  148. chairBigModel,
  149. cookingPotModel,
  150. chickenCaracasModel,
  151. chickenWingsPlateModel,
  152. fishPlateModel,
  153. ladderModel,
  154. vinesModel,
  155. smallRockModel,
  156. chestModel,
  157. crateModel,
  158. torchModel,
  159. torchHolderModel,
  160. lampModel,
  161. lampHolderModel,
  162. slabModel,
  163. stairsModel,
  164. wallModel,
  165. trainingDummyBaseModel,
  166. targetModel,
  167. furnaceModel,
  168. goblinWorkBenchModel,
  169. goblinChairModel,
  170. goblinTableModel,
  171. goblinStitchingPostModel,
  172. BLOCK_MODELS_COUNT
  173. };
  174. BlockModel blockModels[BLOCK_MODELS_COUNT];
  175. enum TexturesLoaded
  176. {
  177. DefaultTexture,
  178. SteveTexture,
  179. ZombieTexture,
  180. PigTexture,
  181. CatTexture,
  182. GoblinTexture,
  183. TrainingDummyTexture,
  184. scareCrowTexture,
  185. HelmetTestTexture,
  186. };
  187. std::vector<GLuint64> gpuIds;
  188. std::vector<GLuint> texturesIds;
  189. GLuint texturesSSBO = 0;
  190. gl2d::Texture temporaryPlayerHandTexture = {};
  191. GLuint64 temporaryPlayerHandBindlessTexture = 0;
  192. void setupSSBO();
  193. };
  194. void animatePlayerLegs(glm::mat4 *poseVector, float &currentAngle, int &direction, float deltaTime);
  195. gl2d::Texture loadPlayerSkin(const char *path);
  196. constexpr static int PLAYER_SKIN_SIZE = 128;
  197. int getDefaultBlockShapeForFurniture(unsigned int b);