MouseCoordinates.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <algorithm>
  2. #include <catch2/catch_test_macros.hpp>
  3. #include <cmath>
  4. #include <glm/glm.hpp>
  5. /**
  6. * Test GLFW to OpenGL coordinate conversion
  7. * GLFW: Y=0 at top, Y=height at bottom
  8. * OpenGL: Y=0 at bottom, Y=height at top
  9. */
  10. TEST_CASE ("GLFW to OpenGL coordinate conversion") {
  11. const int framebufferHeight = 1080;
  12. // Mouse at top of screen (GLFW: Y=0)
  13. double glfwY = 0.0;
  14. double openglY = static_cast<double> (framebufferHeight) - glfwY;
  15. CHECK (openglY == 1080.0); // Should be at top in OpenGL (Y=height)
  16. // Mouse at bottom of screen (GLFW: Y=height)
  17. glfwY = 1080.0;
  18. openglY = static_cast<double> (framebufferHeight) - glfwY;
  19. CHECK (openglY == 0.0); // Should be at bottom in OpenGL (Y=0)
  20. // Mouse at middle of screen
  21. glfwY = 540.0;
  22. openglY = static_cast<double> (framebufferHeight) - glfwY;
  23. CHECK (openglY == 540.0); // Should be at middle in both systems
  24. }
  25. /**
  26. * Test Wayland to OpenGL coordinate conversion
  27. * Wayland: Y=0 at top, Y=height at bottom
  28. * OpenGL: Y=0 at bottom, Y=height at top
  29. */
  30. TEST_CASE ("Wayland to OpenGL coordinate conversion") {
  31. const double viewportHeight = 1080.0;
  32. // Mouse at top of screen (Wayland: Y=0)
  33. double waylandY = 0.0;
  34. double openglY = viewportHeight - waylandY;
  35. CHECK (openglY == 1080.0); // Should be at top in OpenGL
  36. // Mouse at bottom of screen (Wayland: Y=height)
  37. waylandY = 1080.0;
  38. openglY = viewportHeight - waylandY;
  39. CHECK (openglY == 0.0); // Should be at bottom in OpenGL
  40. // Mouse at middle of screen
  41. waylandY = 540.0;
  42. openglY = viewportHeight - waylandY;
  43. CHECK (openglY == 540.0); // Should be at middle
  44. }
  45. /**
  46. * Test OpenGL to normalized coordinate conversion
  47. * OpenGL: Y=0 at bottom, Y=height at top
  48. * Normalized: 0=bottom, 1=top (OpenGL convention)
  49. */
  50. TEST_CASE ("OpenGL to normalized coordinate conversion") {
  51. const int viewportY = 0;
  52. const int viewportHeight = 1080;
  53. // Mouse at top in OpenGL (Y=height)
  54. double openglY = 1080.0;
  55. double normalizedY = glm::clamp ((openglY - viewportY) / static_cast<double> (viewportHeight), 0.0, 1.0);
  56. CHECK (normalizedY == 1.0); // Should be 1.0 (top)
  57. // Mouse at bottom in OpenGL (Y=0)
  58. openglY = 0.0;
  59. normalizedY = glm::clamp ((openglY - viewportY) / static_cast<double> (viewportHeight), 0.0, 1.0);
  60. CHECK (normalizedY == 0.0); // Should be 0.0 (bottom)
  61. // Mouse at middle
  62. openglY = 540.0;
  63. normalizedY = glm::clamp ((openglY - viewportY) / static_cast<double> (viewportHeight), 0.0, 1.0);
  64. CHECK (std::abs (normalizedY - 0.5) < 0.001); // Should be ~0.5 (middle)
  65. }
  66. /**
  67. * Test OpenGL to CEF coordinate conversion
  68. * OpenGL: Y=0 at bottom, Y=height at top
  69. * CEF: Y=0 at top, Y=height at bottom
  70. */
  71. TEST_CASE ("OpenGL to CEF coordinate conversion") {
  72. const int viewportHeight = 1080;
  73. const int viewportY = 0;
  74. // Mouse at top in OpenGL (Y=height)
  75. double openglY = 1080.0;
  76. int clampedY = std::clamp (static_cast<int> (openglY - viewportY), 0, viewportHeight);
  77. int cefY = viewportHeight - clampedY;
  78. CHECK (cefY == 0); // Should be 0 (top in CEF)
  79. // Mouse at bottom in OpenGL (Y=0)
  80. openglY = 0.0;
  81. clampedY = std::clamp (static_cast<int> (openglY - viewportY), 0, viewportHeight);
  82. cefY = viewportHeight - clampedY;
  83. CHECK (cefY == 1080); // Should be height (bottom in CEF)
  84. // Mouse at middle
  85. openglY = 540.0;
  86. clampedY = std::clamp (static_cast<int> (openglY - viewportY), 0, viewportHeight);
  87. cefY = viewportHeight - clampedY;
  88. CHECK (cefY == 540); // Should be middle
  89. }
  90. /**
  91. * Test complete coordinate flow: GLFW → OpenGL → Normalized
  92. * Verifies the full pipeline works correctly
  93. */
  94. TEST_CASE ("Complete coordinate flow: GLFW to normalized") {
  95. const int framebufferHeight = 1080;
  96. const int viewportY = 0;
  97. const int viewportHeight = 1080;
  98. // Mouse at top of screen
  99. double glfwY = 0.0;
  100. double openglY = static_cast<double> (framebufferHeight) - glfwY; // Convert to OpenGL
  101. double normalizedY = glm::clamp ((openglY - viewportY) / static_cast<double> (viewportHeight), 0.0, 1.0);
  102. CHECK (normalizedY == 1.0); // Top should normalize to 1.0
  103. // Mouse at bottom of screen
  104. glfwY = 1080.0;
  105. openglY = static_cast<double> (framebufferHeight) - glfwY;
  106. normalizedY = glm::clamp ((openglY - viewportY) / static_cast<double> (viewportHeight), 0.0, 1.0);
  107. CHECK (normalizedY == 0.0); // Bottom should normalize to 0.0
  108. }
  109. /**
  110. * Test coordinate conversion with different viewport sizes
  111. * Ensures conversion works with non-standard viewport dimensions
  112. */
  113. TEST_CASE ("Coordinate conversion with different viewport sizes") {
  114. // Test with 1920x1080 viewport
  115. {
  116. const int height = 1080;
  117. double glfwY = 0.0;
  118. double openglY = static_cast<double> (height) - glfwY;
  119. CHECK (openglY == 1080.0);
  120. }
  121. // Test with 2560x1440 viewport
  122. {
  123. const int height = 1440;
  124. double glfwY = 0.0;
  125. double openglY = static_cast<double> (height) - glfwY;
  126. CHECK (openglY == 1440.0);
  127. }
  128. // Test with 800x600 viewport
  129. {
  130. const int height = 600;
  131. double glfwY = 0.0;
  132. double openglY = static_cast<double> (height) - glfwY;
  133. CHECK (openglY == 600.0);
  134. }
  135. }