Open Source Tomb Raider Engine
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.

Entity.cpp 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*!
  2. * \file src/Entity.cpp
  3. * \brief World Entities
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Camera.h"
  9. #include "Log.h"
  10. #include "World.h"
  11. #include "Entity.h"
  12. #include <glm/gtc/matrix_transform.hpp>
  13. #define CACHE_SPRITE 0
  14. #define CACHE_MESH 1
  15. #define CACHE_MODEL 2
  16. bool Entity::showEntitySprites = true;
  17. bool Entity::showEntityMeshes = true;
  18. bool Entity::showEntityModels = true;
  19. void Entity::display(glm::mat4 VP) {
  20. if ((cache == -1) || (cacheType == -1)) {
  21. for (int i = 0; i < getWorld().sizeSpriteSequence(); i++) {
  22. auto& s = getWorld().getSpriteSequence(i);
  23. if (s.getID() == id) {
  24. cacheType = CACHE_SPRITE;
  25. cache = i;
  26. break;
  27. }
  28. }
  29. for (int i = 0; (i < getWorld().sizeStaticMesh()) && (cache == -1); i++) {
  30. auto& s = getWorld().getStaticMesh(i);
  31. if (s.getID() == id) {
  32. cacheType = CACHE_MESH;
  33. cache = i;
  34. break;
  35. }
  36. }
  37. for (int i = 0; (i < getWorld().sizeSkeletalModel()) && (cache == -1); i++) {
  38. auto& s = getWorld().getSkeletalModel(i);
  39. if (s.getID() == id) {
  40. cacheType = CACHE_MODEL;
  41. cache = i;
  42. break;
  43. }
  44. }
  45. assertGreaterThan(cache, -1);
  46. assertGreaterThan(cacheType, -1);
  47. }
  48. glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(pos.x, -pos.y, pos.z));
  49. glm::mat4 rotate;
  50. if (cacheType == 0) {
  51. rotate = glm::rotate(glm::mat4(1.0f), Camera::getRotation().x, glm::vec3(0.0f, 1.0f, 0.0f));
  52. } else {
  53. rotate = glm::rotate(glm::mat4(1.0f), rot.y, glm::vec3(0.0f, 1.0f, 0.0f));
  54. }
  55. glm::mat4 model = translate * rotate;
  56. glm::mat4 MVP = VP * model;
  57. if (cacheType == CACHE_SPRITE) {
  58. if (showEntitySprites)
  59. getWorld().getSpriteSequence(cache).display(MVP, sprite);
  60. } else if (cacheType == CACHE_MESH) {
  61. if (showEntityMeshes)
  62. getWorld().getStaticMesh(cache).display(MVP);
  63. } else if (cacheType == CACHE_MODEL) {
  64. if (showEntityModels)
  65. getWorld().getSkeletalModel(cache).display(MVP, animation, frame);
  66. } else {
  67. assert(false && "This should not happen...");
  68. }
  69. }