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.

BoundingSphere.cpp 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*!
  2. * \file src/BoundingSphere.cpp
  3. * \brief 3D Axis Aligned Bounding Sphere
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Camera.h"
  9. #include "World.h"
  10. #include "system/Shader.h"
  11. #include "BoundingSphere.h"
  12. #include <glbinding/gl/gl.h>
  13. std::vector<glm::vec4> BoundingSphere::vertices;
  14. std::vector<glm::vec3> BoundingSphere::colors;
  15. void BoundingSphere::display(glm::mat4 VP, glm::vec3 color) {
  16. for (int w = 0; w < resolution; w++) {
  17. for (int h = (-resolution / 2); h < (resolution / 2); h++){
  18. float inc1 = (w / float(resolution)) * 2.0f * glm::pi<float>();
  19. float inc2 = ((w + 1) / float(resolution)) * 2.0f * glm::pi<float>();
  20. float inc3 = (h / float(resolution)) * glm::pi<float>();
  21. float inc4 = ((h + 1) / float(resolution)) * glm::pi<float>();
  22. float x1 = glm::sin(inc1);
  23. float y1 = glm::cos(inc1);
  24. float x2 = glm::sin(inc2);
  25. float y2 = glm::cos(inc2);
  26. float radius1 = radius * glm::cos(inc3);
  27. float radius2 = radius * glm::cos(inc4);
  28. float z1 = radius * glm::sin(inc3);
  29. float z2 = radius * glm::sin(inc4);
  30. vertices.emplace_back(VP * (glm::vec4(radius1 * x1, z1, radius1 * y1, 1.0f) + glm::vec4(pos, 0.0f)));
  31. vertices.emplace_back(VP * (glm::vec4(radius1 * x2, z1, radius1 * y2, 1.0f) + glm::vec4(pos, 0.0f)));
  32. vertices.emplace_back(VP * (glm::vec4(radius2 * x2, z2, radius2 * y2, 1.0f) + glm::vec4(pos, 0.0f)));
  33. vertices.emplace_back(VP * (glm::vec4(radius1 * x1, z1, radius1 * y1, 1.0f) + glm::vec4(pos, 0.0f)));
  34. vertices.emplace_back(VP * (glm::vec4(radius2 * x2, z2, radius2 * y2, 1.0f) + glm::vec4(pos, 0.0f)));
  35. vertices.emplace_back(VP * (glm::vec4(radius2 * x1, z2, radius2 * y1, 1.0f) + glm::vec4(pos, 0.0f)));
  36. for (int i = 0; i < 6; i++) {
  37. colors.emplace_back(color);
  38. }
  39. }
  40. }
  41. }
  42. void BoundingSphere::display() {
  43. if (vertices.size() > 0) {
  44. Shader::drawGL(vertices, colors, gl::GL_POINTS);
  45. }
  46. vertices.clear();
  47. colors.clear();
  48. }