Skip to content

Commit 2a0347c

Browse files
authored
Merge branch 'main' into fix_win_warnings
2 parents 76832bb + a49ebcf commit 2a0347c

24 files changed

+509
-48
lines changed

examples/segmentation_camera/GlutWindow.cc

+35-16
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ void motionCB(int _x, int _y)
136136
void handleMouse()
137137
{
138138
std::lock_guard<std::mutex> lock(g_mouseMutex);
139-
// only ogre supports ray query for now so use
140-
// ogre camera located at camera index = 0.
139+
141140
ir::CameraPtr rayCamera = g_camera;
142141
if (!g_rayQuery)
143142
{
@@ -152,23 +151,44 @@ void handleMouse()
152151
if (g_mouse.buttonDirty)
153152
{
154153
g_mouse.buttonDirty = false;
154+
155+
// TODO: enable for segmentation cameras
156+
#if 0
157+
// test mouse picking
158+
if (g_mouse.button == GLUT_LEFT_BUTTON && g_mouse.state == GLUT_DOWN)
159+
{
160+
// Get visual using Selection Buffer from Camera
161+
ir::VisualPtr visual;
162+
ignition::math::Vector2i mousePos(g_mouse.x, g_mouse.y);
163+
visual = rayCamera->VisualAt(mousePos);
164+
if (visual)
165+
{
166+
std::cout << "Selected visual at position: ";
167+
std::cout << g_mouse.x << " " << g_mouse.y << ": ";
168+
std::cout << visual->Name() << "\n";
169+
}
170+
else
171+
{
172+
std::cout << "No visual found at position: ";
173+
std::cout << g_mouse.x << " " << g_mouse.y << std::endl;
174+
}
175+
}
176+
#endif
177+
178+
// camera orbit
155179
double nx =
156180
2.0 * g_mouse.x / static_cast<double>(rayCamera->ImageWidth()) - 1.0;
157181
double ny = 1.0 -
158182
2.0 * g_mouse.y / static_cast<double>(rayCamera->ImageHeight());
183+
g_rayQuery->SetFromCamera(rayCamera, ignition::math::Vector2d(nx, ny));
184+
g_target = g_rayQuery->ClosestPoint();
185+
if (!g_target)
186+
{
187+
// set point to be 10m away if no intersection found
188+
g_target.point = g_rayQuery->Origin() + g_rayQuery->Direction() * 10;
189+
return;
190+
}
159191

160-
// TODO(anyone) figure out why this code is causing a crash
161-
// g_rayQuery->SetFromCamera(rayCamera, ignition::math::Vector2d(nx, ny));
162-
// g_target = g_rayQuery->ClosestPoint();
163-
// if (!g_target)
164-
// {
165-
// // set point to be 10m away if no intersection found
166-
// g_target.point = g_rayQuery->Origin() + g_rayQuery->Direction() * 10;
167-
// return;
168-
// }
169-
170-
// TODO(anyone) get mouse wheel scroll zoom to work (currently isn't
171-
// working)
172192
// mouse wheel scroll zoom
173193
if ((g_mouse.button == 3 || g_mouse.button == 4) &&
174194
g_mouse.state == GLUT_UP)
@@ -203,8 +223,7 @@ void handleMouse()
203223
g_viewControl.SetTarget(g_target.point);
204224
g_viewControl.Orbit(drag);
205225
}
206-
// TODO(anyone) get right mouse button zoom to work. Seems to crash when
207-
// used with the RayQuery
226+
208227
// right mouse button zoom
209228
else if (g_mouse.button == GLUT_RIGHT_BUTTON && g_mouse.state == GLUT_DOWN)
210229
{

examples/thermal_camera/GlutWindow.cc

+187
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
#include <ignition/common/Console.hh>
3535
#include <ignition/rendering/Camera.hh>
3636
#include <ignition/rendering/Image.hh>
37+
#include <ignition/rendering/RayQuery.hh>
3738
#include <ignition/rendering/Scene.hh>
3839
#include <ignition/rendering/ThermalCamera.hh>
40+
#include <ignition/rendering/OrbitViewController.hh>
3941

4042
#include "GlutWindow.hh"
4143

@@ -68,6 +70,186 @@ bool g_initContext = false;
6870
GLXDrawable g_glutDrawable;
6971
#endif
7072

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+
71253
//////////////////////////////////////////////////
72254
void OnNewThermalFrame(const uint16_t *_scan,
73255
unsigned int _width, unsigned int _height,
@@ -123,6 +305,8 @@ void displayCB()
123305

124306
g_cameras[g_cameraIndex]->Update();
125307

308+
handleMouse();
309+
126310
#if __APPLE__
127311
CGLSetCurrentContext(g_glutContext);
128312
#elif _WIN32
@@ -191,6 +375,9 @@ void initContext()
191375
glutDisplayFunc(displayCB);
192376
glutIdleFunc(idleCB);
193377
glutKeyboardFunc(keyboardCB);
378+
379+
glutMouseFunc(mouseCB);
380+
glutMotionFunc(motionCB);
194381
}
195382

196383
//////////////////////////////////////////////////

ogre/include/ignition/rendering/ogre/OgreCamera.hh

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "ignition/rendering/base/BaseCamera.hh"
2323
#include "ignition/rendering/ogre/OgreRenderTypes.hh"
24+
#include "ignition/rendering/ogre/OgreObjectInterface.hh"
2425
#include "ignition/rendering/ogre/OgreSensor.hh"
2526
#include "ignition/rendering/ogre/OgreSelectionBuffer.hh"
2627

@@ -39,7 +40,8 @@ namespace ignition
3940
class OgreSelectionBuffer;
4041

4142
class IGNITION_RENDERING_OGRE_VISIBLE OgreCamera :
42-
public BaseCamera<OgreSensor>
43+
public virtual BaseCamera<OgreSensor>,
44+
public virtual OgreObjectInterface
4345
{
4446
protected: OgreCamera();
4547

@@ -118,8 +120,8 @@ namespace ignition
118120
// Documentation inherited.
119121
public: virtual void SetVisibilityMask(uint32_t _mask) override;
120122

121-
/// \brief Get underlying Ogre camera
122-
public: Ogre::Camera *Camera() const;
123+
// Documentation inherited.
124+
public: virtual Ogre::Camera *Camera() const override;
123125

124126
// Documentation inherited.
125127
// public: virtual uint32_t VisibilityMask() const override;

ogre/include/ignition/rendering/ogre/OgreDepthCamera.hh

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "ignition/rendering/base/BaseDepthCamera.hh"
3232
#include "ignition/rendering/ogre/OgreConversions.hh"
3333
#include "ignition/rendering/ogre/OgreIncludes.hh"
34+
#include "ignition/rendering/ogre/OgreObjectInterface.hh"
3435
#include "ignition/rendering/ogre/OgreRenderTarget.hh"
3536
#include "ignition/rendering/ogre/OgreRenderTypes.hh"
3637
#include "ignition/rendering/ogre/OgreScene.hh"
@@ -63,7 +64,8 @@ namespace ignition
6364
**/
6465
/// \brief Depth camera used to render depth data into an image buffer
6566
class IGNITION_RENDERING_OGRE_VISIBLE OgreDepthCamera :
66-
public BaseDepthCamera<OgreSensor>
67+
public virtual BaseDepthCamera<OgreSensor>,
68+
public virtual OgreObjectInterface
6769
{
6870
/// \brief Constructor
6971
protected: OgreDepthCamera();
@@ -125,6 +127,9 @@ namespace ignition
125127
// Documentation inherited
126128
public: virtual void Destroy() override;
127129

130+
// Documentation inherited.
131+
public: virtual Ogre::Camera *Camera() const override;
132+
128133
/// \brief Update a render target
129134
/// \param[in] _target Render target to update
130135
/// \param[in] _material Material to use

0 commit comments

Comments
 (0)