model.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. //modelsloader modelloader
  121. struct ModelsManager
  122. {
  123. void loadAllModels(std::string path, bool reportErrors);
  124. void clearAllModels();
  125. Model human;
  126. Model pig;
  127. Model cat;
  128. Model rightHand;
  129. Model goblin;
  130. Model trainingDummy;
  131. Model scareCrow;
  132. enum BlockModels
  133. {
  134. chairModel,
  135. mugModel,
  136. gobletModel,
  137. wineBottleModel,
  138. skullModel,
  139. skullTorchModel,
  140. booksModel,
  141. candleHolderModel,
  142. potModel,
  143. jarModel,
  144. globeModel,
  145. keg,
  146. workBenchModel,
  147. tableModel,
  148. workItemsModel,
  149. chairBigModel,
  150. cookingPotModel,
  151. chickenCaracasModel,
  152. chickenWingsPlateModel,
  153. fishPlateModel,
  154. ladderModel,
  155. vinesModel,
  156. smallRockModel,
  157. chestModel,
  158. crateModel,
  159. torchModel,
  160. torchHolderModel,
  161. lampModel,
  162. lampHolderModel,
  163. slabModel,
  164. stairsModel,
  165. wallModel,
  166. trainingDummyBaseModel,
  167. targetModel,
  168. furnaceModel,
  169. goblinWorkBenchModel,
  170. goblinChairModel,
  171. goblinTableModel,
  172. goblinStitchingPostModel,
  173. fence,
  174. fenceFront,
  175. fenceBack,
  176. fenceLeft,
  177. fenceRight,
  178. BLOCK_MODELS_COUNT
  179. };
  180. BlockModel blockModels[BLOCK_MODELS_COUNT];
  181. enum TexturesLoaded
  182. {
  183. DefaultTexture,
  184. SteveTexture,
  185. ZombieTexture,
  186. PigTexture,
  187. CatTexture,
  188. GoblinTexture,
  189. TrainingDummyTexture,
  190. scareCrowTexture,
  191. HelmetTestTexture,
  192. };
  193. std::vector<GLuint64> gpuIds;
  194. std::vector<GLuint> texturesIds;
  195. GLuint texturesSSBO = 0;
  196. gl2d::Texture temporaryPlayerHandTexture = {};
  197. GLuint64 temporaryPlayerHandBindlessTexture = 0;
  198. void setupSSBO();
  199. };
  200. void animatePlayerLegs(glm::mat4 *poseVector, float &currentAngle, int &direction, float deltaTime);
  201. gl2d::Texture loadPlayerSkin(const char *path);
  202. constexpr static int PLAYER_SKIN_SIZE = 128;
  203. int getDefaultBlockShapeForFurniture(unsigned int b);