| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include <mrpt/gui/CDisplayWindow3D.h> |
| 11 |
#include <mrpt/hwdrivers/CCameraSensor.h> |
| 12 |
#include <mrpt/opengl.h> |
| 13 |
|
| 14 |
#include <chrono> |
| 15 |
#include <iostream> |
| 16 |
#include <thread> |
| 17 |
|
| 18 |
using namespace std; |
| 19 |
using namespace mrpt; |
| 20 |
using namespace mrpt::gui; |
| 21 |
using namespace mrpt::opengl; |
| 22 |
using namespace mrpt::math; |
| 23 |
using namespace mrpt::obs; |
| 24 |
|
| 25 |
// ------------------------------------------------------ |
| 26 |
// TestOpenGLVideo |
| 27 |
// ------------------------------------------------------ |
| 28 |
void TestOpenGLVideo() |
| 29 |
{ |
| 30 |
// Show to the user a list of possible camera drivers and creates and open |
| 31 |
// the selected camera. |
| 32 |
cout << "Please, select the input video file or camera...\n"; |
| 33 |
|
| 34 |
mrpt::hwdrivers::CCameraSensor::Ptr cam = mrpt::hwdrivers::prepareVideoSourceFromUserSelection(); |
| 35 |
if (!cam) return; |
| 36 |
|
| 37 |
cout << "Video stream open OK\n"; |
| 38 |
|
| 39 |
// Create 3D window: |
| 40 |
CDisplayWindow3D win("#1: Demo of image mode viewport", 320, 240); |
| 41 |
CDisplayWindow3D win2("#2: Demo of image mode viewport", 500, 400); |
| 42 |
|
| 43 |
// Win #1: |
| 44 |
// Get the smart pointer to the main viewport object in this window: |
| 45 |
Viewport::Ptr gl_view_main; |
| 46 |
{ |
| 47 |
Scene::Ptr& theScene = win.get3DSceneAndLock(); |
| 48 |
gl_view_main = theScene->getViewport("main"); |
| 49 |
ASSERT_(gl_view_main); |
| 50 |
// IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED! |
| 51 |
win.unlockAccess3DScene(); |
| 52 |
} |
| 53 |
|
| 54 |
// Win #2: |
| 55 |
// Get the smart pointer to the main viewport object in this window: |
| 56 |
Viewport::Ptr gl_view_aux; |
| 57 |
{ |
| 58 |
Scene::Ptr& theScene = win2.get3DSceneAndLock(); |
| 59 |
theScene->insert(mrpt::opengl::CGridPlaneXY::Create()); |
| 60 |
|
| 61 |
// Create small auxiliary viewport |
| 62 |
gl_view_aux = theScene->createViewport("aux"); |
| 63 |
|
| 64 |
gl_view_aux->setViewportPosition(10, 10, 300, 200); |
| 65 |
gl_view_aux->setTransparent(true); |
| 66 |
|
| 67 |
// IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED! |
| 68 |
win2.unlockAccess3DScene(); |
| 69 |
} |
| 70 |
|
| 71 |
win.setPos(10, 10); |
| 72 |
win2.setPos(400, 100); |
| 73 |
|
| 74 |
cout << "Close any window to end.\n"; |
| 75 |
while (win.isOpen() && win2.isOpen()) |
| 76 |
{ |
| 77 |
win.addTextMessage(5, 5, mrpt::format("%.02fFPS", win.getRenderingFPS())); |
| 78 |
win2.addTextMessage(5, 5, mrpt::format("%.02fFPS", win2.getRenderingFPS())); |
| 79 |
std::this_thread::sleep_for(1ms); |
| 80 |
|
| 81 |
// Grab new video frame: |
| 82 |
CObservation::Ptr obs = cam->getNextFrame(); |
| 83 |
if (obs) |
| 84 |
{ |
| 85 |
if (IS_CLASS(*obs, CObservationImage)) |
| 86 |
{ |
| 87 |
CObservationImage::Ptr o = std::dynamic_pointer_cast<CObservationImage>(obs); |
| 88 |
win.get3DSceneAndLock(); |
| 89 |
gl_view_main->setImageView(o->image); |
| 90 |
win.unlockAccess3DScene(); |
| 91 |
win.repaint(); |
| 92 |
|
| 93 |
win2.get3DSceneAndLock(); |
| 94 |
gl_view_aux->setImageView(std::move(o->image)); |
| 95 |
win2.unlockAccess3DScene(); |
| 96 |
win2.repaint(); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// ------------------------------------------------------ |
| 103 |
// MAIN |
| 104 |
// ------------------------------------------------------ |
| 105 |
int main() |
| 106 |
{ |
| 107 |
try |
| 108 |
{ |
| 109 |
TestOpenGLVideo(); |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
catch (const std::exception& e) |
| 114 |
{ |
| 115 |
std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl; |
| 116 |
return -1; |
| 117 |
} |
| 118 |
} |