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.

Selector.cpp 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "system/Window.h"
  14. #include "Selector.h"
  15. bool Selector::visible = false;
  16. static int lastX = -1, lastY = -1;
  17. static bool workToDo = false;
  18. void Selector::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  19. if ((button == leftmouseKey) && (!released)) {
  20. lastX = x;
  21. lastY = y;
  22. if (workToDo) {
  23. Log::get(LOG_DEBUG) << "Selector missed mouse click event!" << Log::endl;
  24. }
  25. workToDo = true;
  26. }
  27. }
  28. void Selector::display() {
  29. if (!visible)
  30. return;
  31. if (!ImGui::Begin("Object Selector", &visible, ImVec2(500, 200))) {
  32. ImGui::End();
  33. return;
  34. }
  35. static glm::vec3 rayWorld;
  36. if (workToDo) {
  37. glm::vec2 normalized = glm::vec2((2.0f * lastX) / Window::getSize().x - 1.0f,
  38. 1.0f - (2.0f * lastY) / Window::getSize().y);
  39. glm::vec4 rayClip(normalized.x, normalized.y, -1.0f, 1.0f);
  40. glm::vec4 rayEye(glm::inverse(Camera::getProjectionMatrix()) * rayClip);
  41. rayEye = glm::vec4(rayEye.x, rayEye.y, -1.0f, 0.0f);
  42. rayWorld = glm::vec3(glm::inverse(Camera::getViewMatrix()) * rayEye);
  43. rayWorld = glm::normalize(rayWorld);
  44. workToDo = false;
  45. }
  46. ImGui::Text("Screenspace: (%d %d)", lastX, lastY);
  47. ImGui::Text("Camera: (%.2f %.2f %.2f)", Camera::getPosition().x, Camera::getPosition().y, Camera::getPosition().z);
  48. if ((lastX < 0) || (lastY < 0)) {
  49. ImGui::Text("Normalized Ray: (? ? ?)");
  50. } else {
  51. ImGui::Text("Normalized Ray: (%.3f %.3f %.3f)", rayWorld.x, rayWorld.y, rayWorld.z);
  52. }
  53. if (ImGui::Button("Hide Selector")) {
  54. visible = false;
  55. }
  56. ImGui::End();
  57. }