|
34 | 34 | #include <ignition/common/Console.hh>
|
35 | 35 | #include <ignition/rendering/Camera.hh>
|
36 | 36 | #include <ignition/rendering/Image.hh>
|
| 37 | +#include <ignition/rendering/RayQuery.hh> |
37 | 38 | #include <ignition/rendering/Scene.hh>
|
38 | 39 | #include <ignition/rendering/ThermalCamera.hh>
|
| 40 | +#include <ignition/rendering/OrbitViewController.hh> |
39 | 41 |
|
40 | 42 | #include "GlutWindow.hh"
|
41 | 43 |
|
@@ -68,6 +70,186 @@ bool g_initContext = false;
|
68 | 70 | GLXDrawable g_glutDrawable;
|
69 | 71 | #endif
|
70 | 72 |
|
| 73 | +// view control variables |
| 74 | +ir::RayQueryPtr g_rayQuery; |
| 75 | +ir::OrbitViewController g_viewControl; |
| 76 | +ir::RayQueryResult g_target; |
| 77 | +struct mouseButton |
| 78 | +{ |
| 79 | + int button = 0; |
| 80 | + int state = GLUT_UP; |
| 81 | + int x = 0; |
| 82 | + int y = 0; |
| 83 | + int motionX = 0; |
| 84 | + int motionY = 0; |
| 85 | + int dragX = 0; |
| 86 | + int dragY = 0; |
| 87 | + int scroll = 0; |
| 88 | + bool buttonDirty = false; |
| 89 | + bool motionDirty = false; |
| 90 | +}; |
| 91 | +struct mouseButton g_mouse; |
| 92 | +std::mutex g_mouseMutex; |
| 93 | + |
| 94 | +////////////////////////////////////////////////// |
| 95 | +void mouseCB(int _button, int _state, int _x, int _y) |
| 96 | +{ |
| 97 | + // ignore unknown mouse button numbers |
| 98 | + if (_button >= 5) |
| 99 | + return; |
| 100 | + |
| 101 | + std::lock_guard<std::mutex> lock(g_mouseMutex); |
| 102 | + g_mouse.button = _button; |
| 103 | + g_mouse.state = _state; |
| 104 | + g_mouse.x = _x; |
| 105 | + g_mouse.y = _y; |
| 106 | + g_mouse.motionX = _x; |
| 107 | + g_mouse.motionY = _y; |
| 108 | + g_mouse.buttonDirty = true; |
| 109 | +} |
| 110 | + |
| 111 | +////////////////////////////////////////////////// |
| 112 | +void motionCB(int _x, int _y) |
| 113 | +{ |
| 114 | + std::lock_guard<std::mutex> lock(g_mouseMutex); |
| 115 | + int deltaX = _x - g_mouse.motionX; |
| 116 | + int deltaY = _y - g_mouse.motionY; |
| 117 | + g_mouse.motionX = _x; |
| 118 | + g_mouse.motionY = _y; |
| 119 | + |
| 120 | + if (g_mouse.motionDirty) |
| 121 | + { |
| 122 | + g_mouse.dragX += deltaX; |
| 123 | + g_mouse.dragY += deltaY; |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + g_mouse.dragX = deltaX; |
| 128 | + g_mouse.dragY = deltaY; |
| 129 | + } |
| 130 | + g_mouse.motionDirty = true; |
| 131 | +} |
| 132 | + |
| 133 | +////////////////////////////////////////////////// |
| 134 | +void handleMouse() |
| 135 | +{ |
| 136 | + std::lock_guard<std::mutex> lock(g_mouseMutex); |
| 137 | + // only ogre supports ray query for now so use |
| 138 | + // ogre camera located at camera index = 0. |
| 139 | + ir::CameraPtr rayCamera = g_cameras[0]; |
| 140 | + if (!g_rayQuery) |
| 141 | + { |
| 142 | + g_rayQuery = rayCamera->Scene()->CreateRayQuery(); |
| 143 | + if (!g_rayQuery) |
| 144 | + { |
| 145 | + ignerr << "Failed to create Ray Query" << std::endl; |
| 146 | + return; |
| 147 | + } |
| 148 | + } |
| 149 | + if (g_mouse.buttonDirty) |
| 150 | + { |
| 151 | + g_mouse.buttonDirty = false; |
| 152 | + |
| 153 | + // TODO: enable for thermal cameras |
| 154 | +#if 0 |
| 155 | + // test mouse picking |
| 156 | + if (g_mouse.button == GLUT_LEFT_BUTTON && g_mouse.state == GLUT_DOWN) |
| 157 | + { |
| 158 | + // Get visual using Selection Buffer from Camera |
| 159 | + ir::VisualPtr visual; |
| 160 | + ignition::math::Vector2i mousePos(g_mouse.x, g_mouse.y); |
| 161 | + visual = rayCamera->VisualAt(mousePos); |
| 162 | + if (visual) |
| 163 | + { |
| 164 | + std::cout << "Selected visual at position: "; |
| 165 | + std::cout << g_mouse.x << " " << g_mouse.y << ": "; |
| 166 | + std::cout << visual->Name() << "\n"; |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + std::cout << "No visual found at position: "; |
| 171 | + std::cout << g_mouse.x << " " << g_mouse.y << std::endl; |
| 172 | + } |
| 173 | + } |
| 174 | +#endif |
| 175 | + |
| 176 | + // camera orbit |
| 177 | + double nx = |
| 178 | + 2.0 * g_mouse.x / static_cast<double>(rayCamera->ImageWidth()) - 1.0; |
| 179 | + double ny = 1.0 - |
| 180 | + 2.0 * g_mouse.y / static_cast<double>(rayCamera->ImageHeight()); |
| 181 | + g_rayQuery->SetFromCamera(rayCamera, ignition::math::Vector2d(nx, ny)); |
| 182 | + g_target = g_rayQuery->ClosestPoint(); |
| 183 | + if (!g_target) |
| 184 | + { |
| 185 | + // set point to be 10m away if no intersection found |
| 186 | + g_target.point = g_rayQuery->Origin() + g_rayQuery->Direction() * 10; |
| 187 | + return; |
| 188 | + } |
| 189 | + |
| 190 | + // mouse wheel scroll zoom |
| 191 | + if ((g_mouse.button == 3 || g_mouse.button == 4) && |
| 192 | + g_mouse.state == GLUT_UP) |
| 193 | + { |
| 194 | + double scroll = (g_mouse.button == 3) ? -1.0 : 1.0; |
| 195 | + double distance = rayCamera->WorldPosition().Distance( |
| 196 | + g_target.point); |
| 197 | + int factor = 1; |
| 198 | + double amount = -(scroll * factor) * (distance / 5.0); |
| 199 | + for (ir::CameraPtr camera : g_cameras) |
| 200 | + { |
| 201 | + g_viewControl.SetCamera(camera); |
| 202 | + g_viewControl.SetTarget(g_target.point); |
| 203 | + g_viewControl.Zoom(amount); |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + if (g_mouse.motionDirty) |
| 209 | + { |
| 210 | + g_mouse.motionDirty = false; |
| 211 | + auto drag = ignition::math::Vector2d(g_mouse.dragX, g_mouse.dragY); |
| 212 | + |
| 213 | + // left mouse button pan |
| 214 | + if (g_mouse.button == GLUT_LEFT_BUTTON && g_mouse.state == GLUT_DOWN) |
| 215 | + { |
| 216 | + for (ir::CameraPtr camera : g_cameras) |
| 217 | + { |
| 218 | + g_viewControl.SetCamera(camera); |
| 219 | + g_viewControl.SetTarget(g_target.point); |
| 220 | + g_viewControl.Pan(drag); |
| 221 | + } |
| 222 | + } |
| 223 | + else if (g_mouse.button == GLUT_MIDDLE_BUTTON && g_mouse.state == GLUT_DOWN) |
| 224 | + { |
| 225 | + for (ir::CameraPtr camera : g_cameras) |
| 226 | + { |
| 227 | + g_viewControl.SetCamera(camera); |
| 228 | + g_viewControl.SetTarget(g_target.point); |
| 229 | + g_viewControl.Orbit(drag); |
| 230 | + } |
| 231 | + } |
| 232 | + // right mouse button zoom |
| 233 | + else if (g_mouse.button == GLUT_RIGHT_BUTTON && g_mouse.state == GLUT_DOWN) |
| 234 | + { |
| 235 | + double hfov = rayCamera->HFOV().Radian(); |
| 236 | + double vfov = 2.0f * atan(tan(hfov / 2.0f) / |
| 237 | + rayCamera->AspectRatio()); |
| 238 | + double distance = rayCamera->WorldPosition().Distance( |
| 239 | + g_target.point); |
| 240 | + double amount = ((-g_mouse.dragY / |
| 241 | + static_cast<double>(rayCamera->ImageHeight())) |
| 242 | + * distance * tan(vfov/2.0) * 6.0); |
| 243 | + for (ir::CameraPtr camera : g_cameras) |
| 244 | + { |
| 245 | + g_viewControl.SetCamera(camera); |
| 246 | + g_viewControl.SetTarget(g_target.point); |
| 247 | + g_viewControl.Zoom(amount); |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | +} |
| 252 | + |
71 | 253 | //////////////////////////////////////////////////
|
72 | 254 | void OnNewThermalFrame(const uint16_t *_scan,
|
73 | 255 | unsigned int _width, unsigned int _height,
|
@@ -123,6 +305,8 @@ void displayCB()
|
123 | 305 |
|
124 | 306 | g_cameras[g_cameraIndex]->Update();
|
125 | 307 |
|
| 308 | + handleMouse(); |
| 309 | + |
126 | 310 | #if __APPLE__
|
127 | 311 | CGLSetCurrentContext(g_glutContext);
|
128 | 312 | #elif _WIN32
|
@@ -191,6 +375,9 @@ void initContext()
|
191 | 375 | glutDisplayFunc(displayCB);
|
192 | 376 | glutIdleFunc(idleCB);
|
193 | 377 | glutKeyboardFunc(keyboardCB);
|
| 378 | + |
| 379 | + glutMouseFunc(mouseCB); |
| 380 | + glutMotionFunc(motionCB); |
194 | 381 | }
|
195 | 382 |
|
196 | 383 | //////////////////////////////////////////////////
|
|
0 commit comments