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.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*!
  2. * \file src/Entity.cpp
  3. * \brief Things in the World
  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. /*
  22. * The order in which to look for matching objects with the same ID
  23. * seems to be very important!
  24. * If sprites and meshes are searched before models, many objects will
  25. * be displayed wrong (eg. 'bad guy' becomes 'clothes' in tr2/boat)...
  26. */
  27. for (int i = 0; (i < getWorld().sizeSkeletalModel()) && (cache == -1); i++) {
  28. auto& s = getWorld().getSkeletalModel(i);
  29. if (s.getID() == id) {
  30. cacheType = CACHE_MODEL;
  31. cache = i;
  32. break;
  33. }
  34. }
  35. for (int i = 0; (i < getWorld().sizeStaticMesh()) && (cache == -1); i++) {
  36. auto& s = getWorld().getStaticMesh(i);
  37. if (s.getID() == id) {
  38. cacheType = CACHE_MESH;
  39. cache = i;
  40. break;
  41. }
  42. }
  43. for (int i = 0; i < getWorld().sizeSpriteSequence(); i++) {
  44. auto& s = getWorld().getSpriteSequence(i);
  45. if (s.getID() == id) {
  46. cacheType = CACHE_SPRITE;
  47. cache = i;
  48. break;
  49. }
  50. }
  51. orAssertGreaterThan(cache, -1);
  52. orAssertGreaterThan(cacheType, -1);
  53. }
  54. glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(pos.x, -pos.y, pos.z));
  55. glm::mat4 rotate;
  56. if (cacheType == 0) {
  57. rotate = glm::rotate(glm::mat4(1.0f), Camera::getRotation().x, glm::vec3(0.0f, 1.0f, 0.0f));
  58. } else {
  59. rotate = glm::rotate(glm::mat4(1.0f), rot.y, glm::vec3(0.0f, 1.0f, 0.0f));
  60. }
  61. glm::mat4 model = translate * rotate;
  62. glm::mat4 MVP = VP * model;
  63. if (cacheType == CACHE_SPRITE) {
  64. if (showEntitySprites)
  65. getWorld().getSpriteSequence(cache).display(MVP, sprite);
  66. } else if (cacheType == CACHE_MESH) {
  67. if (showEntityMeshes)
  68. getWorld().getStaticMesh(cache).display(MVP);
  69. } else if (cacheType == CACHE_MODEL) {
  70. if (showEntityModels)
  71. getWorld().getSkeletalModel(cache).display(MVP, animation, frame);
  72. }
  73. }