Open Source Tomb Raider Engine
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Selector.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*!
  2. * \file src/Selector.cpp
  3. * \brief Selector Window
  4. *
  5. * http://antongerdelan.net/opengl/raycasting.html
  6. *
  7. * \author xythobuz
  8. */
  9. #include "imgui/imgui.h"
  10. #include "global.h"
  11. #include "Camera.h"
  12. #include "Log.h"
  13. #include "World.h"
  14. #include "system/Window.h"
  15. #include "Selector.h"
  16. #include <glm/gtx/intersect.hpp>
  17. bool Selector::visible = false;
  18. WorldObjects Selector::lastClickedObject = WorldObjectCount;
  19. std::array<bool, WorldObjectCount> Selector::clickOnObject = {{ false, true, true, true, false, false }};
  20. glm::i32vec2 Selector::rayScreen(-1, -1);
  21. glm::vec3 Selector::rayWorld, Selector::lastIntersectPos, Selector::lastIntersectNorm;
  22. unsigned long Selector::lastIndexA, Selector::lastIndexB;
  23. void Selector::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  24. if ((button == leftmouseKey) && (!released)) {
  25. // Calculate click ray
  26. rayScreen = glm::vec2(x, y);
  27. glm::vec2 normalized = glm::vec2((2.0f * rayScreen.x) / Window::getSize().x - 1.0f,
  28. 1.0f - (2.0f * rayScreen.y) / Window::getSize().y);
  29. glm::vec4 rayClip(normalized.x, normalized.y, -1.0f, 1.0f);
  30. glm::vec4 rayEye(glm::inverse(Camera::getProjectionMatrix()) * rayClip);
  31. rayEye = glm::vec4(rayEye.x, rayEye.y, -1.0f, 0.0f);
  32. rayWorld = glm::vec3(glm::inverse(Camera::getViewMatrix()) * rayEye);
  33. rayWorld = glm::normalize(rayWorld);
  34. // Check for any intersections with object bounding spheres
  35. bool foundSomething = false;
  36. float depth = -1.0f;
  37. if (clickOnObject[modelObject]) {
  38. }
  39. if (clickOnObject[meshObject]) {
  40. }
  41. if (clickOnObject[spriteObject]) {
  42. }
  43. if (clickOnObject[roomModelObject]) {
  44. for (unsigned long i = 0; i < World::sizeRoom(); i++) {
  45. Room &r = World::getRoom(i);
  46. for (unsigned long j = 0; j < r.sizeModels(); j++) {
  47. StaticModel &sm = r.getModel(j);
  48. glm::vec3 pos, norm;
  49. if (glm::intersectRaySphere(Camera::getPosition(), rayWorld, sm.getCenter(), sm.getRadius(),
  50. pos, norm)) {
  51. float newDepth = glm::abs(glm::distance(sm.getCenter(), Camera::getPosition()));
  52. if ((newDepth < depth) || (depth < 0.0f)) {
  53. depth = newDepth;
  54. lastIndexA = i;
  55. lastIndexB = j;
  56. lastIntersectPos = pos;
  57. lastIntersectNorm = norm;
  58. lastClickedObject = roomModelObject;
  59. foundSomething = true;
  60. }
  61. }
  62. }
  63. }
  64. }
  65. if (clickOnObject[roomSpriteObject]) {
  66. }
  67. if (clickOnObject[geometryObject]) {
  68. }
  69. if (!foundSomething) {
  70. lastClickedObject = WorldObjectCount;
  71. }
  72. }
  73. }
  74. void Selector::displaySelection() {
  75. if (lastClickedObject == roomModelObject) {
  76. World::getRoom(lastIndexA).getModel(lastIndexB).displayBoundingSphere(Camera::getProjectionMatrix() * Camera::getViewMatrix(), glm::vec3(1.0f, 0.0f, 0.0f));
  77. }
  78. }
  79. void Selector::display() {
  80. if (!visible)
  81. return;
  82. if (!ImGui::Begin("Object Selector", &visible, ImVec2(500, 200))) {
  83. ImGui::End();
  84. return;
  85. }
  86. ImGui::Checkbox("Geometry", &clickOnObject[geometryObject]);
  87. ImGui::SameLine();
  88. ImGui::Checkbox("RoomModels", &clickOnObject[roomModelObject]);
  89. ImGui::SameLine();
  90. ImGui::Checkbox("RoomSprites", &clickOnObject[roomSpriteObject]);
  91. ImGui::Checkbox("Sprites", &clickOnObject[spriteObject]);
  92. ImGui::SameLine();
  93. ImGui::Checkbox("Meshes", &clickOnObject[meshObject]);
  94. ImGui::SameLine();
  95. ImGui::Checkbox("Models", &clickOnObject[modelObject]);
  96. ImGui::SameLine();
  97. if (ImGui::Button("Hide Selector")) {
  98. visible = false;
  99. }
  100. ImGui::Separator();
  101. // Not yet implemented!
  102. clickOnObject[modelObject] = false;
  103. clickOnObject[meshObject] = false;
  104. clickOnObject[spriteObject] = false;
  105. clickOnObject[roomSpriteObject] = false;
  106. clickOnObject[geometryObject] = false;
  107. ImGui::Text("Camera: (%.2f %.2f %.2f)", Camera::getPosition().x, Camera::getPosition().y, Camera::getPosition().z);
  108. ImGui::Text("Last click: (%d %d)", rayScreen.x, rayScreen.y);
  109. if ((rayScreen.x >= 0) && (rayScreen.y >= 0)) {
  110. ImGui::Text("Normalized Ray: (%.3f %.3f %.3f)", rayWorld.x, rayWorld.y, rayWorld.z);
  111. }
  112. if (lastClickedObject != WorldObjectCount) {
  113. ImGui::Text("Intersect Pos: (%.2f %.2f %.2f)", lastIntersectPos.x, lastIntersectPos.y, lastIntersectPos.z);
  114. ImGui::Text("Intersect Norm: (%.2f %.2f %.2f)", lastIntersectNorm.x, lastIntersectNorm.y, lastIntersectNorm.z);
  115. }
  116. if (lastClickedObject == roomModelObject) {
  117. ImGui::Text("Last Room: %lu", lastIndexA);
  118. ImGui::Text("Last RoomModel: %lu", lastIndexB);
  119. }
  120. ImGui::End();
  121. }