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.

Sprite.cpp 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*!
  2. * \file src/Sprite.cpp
  3. * \brief 2D Billboard Collection
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Camera.h"
  9. #include "Render.h"
  10. #include "TextureManager.h"
  11. #include "World.h"
  12. #include "Sprite.h"
  13. const static float scale = 4.0f;
  14. const static float texelScale = 256.0f;
  15. const static int texelOffset = 2;
  16. Sprite::Sprite(int tile, int x, int y, int width, int height) : texture(tile) {
  17. width >>= 8;
  18. height >>= 8;
  19. int width2 = static_cast<int>(width * scale);
  20. int height2 = static_cast<int>(height * scale);
  21. uvBuff.emplace_back(float(x + texelOffset) / texelScale, float(y + height) / texelScale);
  22. uvBuff.emplace_back(float(x + texelOffset) / texelScale, float(y + texelOffset) / texelScale);
  23. uvBuff.emplace_back(float(x + width) / texelScale, float(y + texelOffset) / texelScale);
  24. uvBuff.emplace_back(float(x + width) / texelScale, float(y + height) / texelScale);
  25. uvBuff.emplace_back(uvBuff.at(0));
  26. uvBuff.emplace_back(uvBuff.at(2));
  27. vertexBuff.emplace_back(float(-width2) / 2.0f, 0.0f, 0.0f);
  28. vertexBuff.emplace_back(float(-width2) / 2.0f, float(-height2), 0.0f);
  29. vertexBuff.emplace_back(float(width2) / 2.0f, float(-height2), 0.0f);
  30. vertexBuff.emplace_back(float(width2) / 2.0f, 0.0f, 0.0f);
  31. vertexBuff.emplace_back(vertexBuff.at(0));
  32. vertexBuff.emplace_back(vertexBuff.at(2));
  33. uv2D = glm::vec4(uvBuff.at(0), uvBuff.at(2));
  34. glm::vec3 average(0.0f, 0.0f, 0.0f);
  35. int averageCount = 0;
  36. for (auto& vert : vertexBuff) {
  37. average += vert;
  38. averageCount++;
  39. }
  40. glm::vec3 center = average / float(averageCount);
  41. float radius = 0.0f;
  42. for (auto& vert : vertexBuff) {
  43. float dist = glm::distance(center, vert);
  44. if (dist > radius) radius = dist;
  45. }
  46. boundingSphere.setPosition(center);
  47. boundingSphere.setRadius(radius);
  48. }
  49. void Sprite::display(glm::mat4 MVP) {
  50. Shader::drawGL(vertexBuff, uvBuff, MVP, texture, TextureStorage::GAME);
  51. }
  52. // ----------------------------------------------------------------------------
  53. void SpriteSequence::display(glm::mat4 MVP, int index) {
  54. orAssertGreaterThanEqual(index, 0);
  55. orAssertLessThan(index, length);
  56. World::getSprite(start + index).display(MVP);
  57. }