| 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 "opengl-precomp.h" // Precompiled header |
| 11 |
// |
| 12 |
#include <mrpt/io/CCompressedInputStream.h> |
| 13 |
#include <mrpt/io/CCompressedOutputStream.h> |
| 14 |
#include <mrpt/opengl/CRenderizable.h> |
| 15 |
#include <mrpt/opengl/Scene.h> |
| 16 |
#include <mrpt/opengl/opengl_api.h> |
| 17 |
#include <mrpt/serialization/CArchive.h> |
| 18 |
#include <mrpt/serialization/metaprogramming_serialization.h> |
| 19 |
|
| 20 |
using namespace mrpt; |
| 21 |
using namespace mrpt::opengl; |
| 22 |
using namespace mrpt::serialization::metaprogramming; |
| 23 |
using namespace mrpt::math; |
| 24 |
using namespace std; |
| 25 |
|
| 26 |
// Include libraries in linking: |
| 27 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 28 |
#ifdef _WIN32 |
| 29 |
// WINDOWS: |
| 30 |
#if defined(_MSC_VER) |
| 31 |
#pragma comment(lib, "opengl32.lib") |
| 32 |
#pragma comment(lib, "GlU32.lib") |
| 33 |
#endif |
| 34 |
#endif // _WIN32 |
| 35 |
#endif // MRPT_HAS_OPENGL_GLUT |
| 36 |
|
| 37 |
IMPLEMENTS_SERIALIZABLE(Scene, CRenderizable, mrpt::opengl) |
| 38 |
|
| 39 |
/*--------------------------------------------------------------- |
| 40 |
Constructor |
| 41 |
---------------------------------------------------------------*/ |
| 42 |
Scene::Scene() { createViewport("main"); } |
| 43 |
/*-------------------------------------------------------------- |
| 44 |
Copy constructor |
| 45 |
---------------------------------------------------------------*/ |
| 46 |
Scene::Scene(const Scene& obj) : CSerializable() { (*this) = obj; } |
| 47 |
|
| 48 |
Scene::~Scene() { m_viewports.clear(); } |
| 49 |
|
| 50 |
void Scene::unloadShaders() |
| 51 |
{ |
| 52 |
for (auto& v : m_viewports) |
| 53 |
if (v) v->unloadShaders(); |
| 54 |
} |
| 55 |
|
| 56 |
void Scene::clear(bool createMainViewport) |
| 57 |
{ |
| 58 |
m_viewports.clear(); |
| 59 |
|
| 60 |
if (createMainViewport) createViewport("main"); |
| 61 |
} |
| 62 |
|
| 63 |
/*--------------------------------------------------------------- |
| 64 |
= |
| 65 |
---------------------------------------------------------------*/ |
| 66 |
Scene& Scene::operator=(const Scene& obj) |
| 67 |
{ |
| 68 |
if (this != &obj) |
| 69 |
{ |
| 70 |
m_followCamera = obj.m_followCamera; |
| 71 |
|
| 72 |
clear(); |
| 73 |
m_viewports = obj.m_viewports; |
| 74 |
for_each( |
| 75 |
m_viewports.begin(), m_viewports.end(), |
| 76 |
[](auto& ptr) |
| 77 |
{ |
| 78 |
// make a unique copy of each object (copied as a shared ptr) |
| 79 |
ptr.reset(dynamic_cast<mrpt::opengl::Viewport*>(ptr->clone())); |
| 80 |
}); |
| 81 |
} |
| 82 |
return *this; |
| 83 |
} |
| 84 |
|
| 85 |
/*--------------------------------------------------------------- |
| 86 |
render |
| 87 |
---------------------------------------------------------------*/ |
| 88 |
void Scene::render() const |
| 89 |
{ |
| 90 |
MRPT_START |
| 91 |
|
| 92 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 93 |
// We need the size of the viewport at the beginning: should be the whole |
| 94 |
// window: |
| 95 |
GLint win_dims[4]; |
| 96 |
glGetIntegerv(GL_VIEWPORT, win_dims); |
| 97 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 98 |
|
| 99 |
for (const auto& m_viewport : m_viewports) m_viewport->render(win_dims[2], win_dims[3]); |
| 100 |
|
| 101 |
// Assure we restore the original viewport: |
| 102 |
glViewport(win_dims[0], win_dims[1], win_dims[2], win_dims[3]); |
| 103 |
CHECK_OPENGL_ERROR(); |
| 104 |
|
| 105 |
#else |
| 106 |
THROW_EXCEPTION( |
| 107 |
"MRPT has been compiled with MRPT_HAS_OPENGL_GLUT=0! OpenGL " |
| 108 |
"functions are not implemented"); |
| 109 |
#endif |
| 110 |
MRPT_END |
| 111 |
} |
| 112 |
|
| 113 |
uint8_t Scene::serializeGetVersion() const { return 1; } |
| 114 |
void Scene::serializeTo(mrpt::serialization::CArchive& out) const |
| 115 |
{ |
| 116 |
out << m_followCamera; |
| 117 |
|
| 118 |
uint32_t n; |
| 119 |
n = (uint32_t)m_viewports.size(); |
| 120 |
out << n; |
| 121 |
for (const auto& m_viewport : m_viewports) out << *m_viewport; |
| 122 |
} |
| 123 |
|
| 124 |
void Scene::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 125 |
{ |
| 126 |
switch (version) |
| 127 |
{ |
| 128 |
case 0: |
| 129 |
{ |
| 130 |
// Old style: Just one viewport: |
| 131 |
clear(true); |
| 132 |
Viewport::Ptr view = m_viewports[0]; |
| 133 |
|
| 134 |
// Load objects: |
| 135 |
uint32_t n; |
| 136 |
in >> n; |
| 137 |
|
| 138 |
view->clear(); |
| 139 |
view->m_objects.resize(n); |
| 140 |
for_each(view->m_objects.begin(), view->m_objects.end(), ObjectReadFromStream(&in)); |
| 141 |
} |
| 142 |
break; |
| 143 |
case 1: |
| 144 |
{ |
| 145 |
in >> m_followCamera; |
| 146 |
|
| 147 |
uint32_t i, n; |
| 148 |
in >> n; |
| 149 |
clear(false); |
| 150 |
|
| 151 |
for (i = 0; i < n; i++) |
| 152 |
{ |
| 153 |
CSerializable::Ptr newObj; |
| 154 |
in >> newObj; |
| 155 |
|
| 156 |
Viewport::Ptr newView = std::dynamic_pointer_cast<Viewport>(newObj); |
| 157 |
newView->m_parent = this; |
| 158 |
m_viewports.push_back(newView); |
| 159 |
} |
| 160 |
} |
| 161 |
break; |
| 162 |
default: |
| 163 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 164 |
}; |
| 165 |
} |
| 166 |
|
| 167 |
/*--------------------------------------------------------------- |
| 168 |
insert |
| 169 |
---------------------------------------------------------------*/ |
| 170 |
void Scene::insert(const CRenderizable::Ptr& newObject, const std::string& viewportName) |
| 171 |
{ |
| 172 |
MRPT_START |
| 173 |
for (auto& m_viewport : m_viewports) |
| 174 |
{ |
| 175 |
if (m_viewport->m_name == viewportName) |
| 176 |
{ |
| 177 |
m_viewport->insert(newObject); |
| 178 |
return; |
| 179 |
} |
| 180 |
} |
| 181 |
THROW_EXCEPTION_FMT("Error: viewport '%s' not found.", viewportName.c_str()); |
| 182 |
MRPT_END |
| 183 |
} |
| 184 |
|
| 185 |
/*--------------------------------------------------------------- |
| 186 |
getByName |
| 187 |
---------------------------------------------------------------*/ |
| 188 |
CRenderizable::Ptr Scene::getByName(const string& str, [[maybe_unused]] const string& viewportName) |
| 189 |
{ |
| 190 |
CRenderizable::Ptr obj; |
| 191 |
for (auto& m_viewport : m_viewports) |
| 192 |
if ((obj = m_viewport->getByName(str))) break; |
| 193 |
return obj; |
| 194 |
} |
| 195 |
|
| 196 |
void Scene::initializeTextures() |
| 197 |
{ |
| 198 |
for (auto& m_viewport : m_viewports) m_viewport->initializeTextures(); |
| 199 |
} |
| 200 |
|
| 201 |
/*-------------------------------------------------------------- |
| 202 |
dumpListOfObjects |
| 203 |
---------------------------------------------------------------*/ |
| 204 |
void Scene::dumpListOfObjects(std::vector<std::string>& lst) const |
| 205 |
{ |
| 206 |
using namespace std::string_literals; |
| 207 |
lst.clear(); |
| 208 |
|
| 209 |
for (auto& v : m_viewports) |
| 210 |
{ |
| 211 |
lst.emplace_back("Viewport: '"s + v->m_name + "'"s); |
| 212 |
lst.emplace_back("============================================"); |
| 213 |
v->dumpListOfObjects(lst); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
mrpt::containers::yaml Scene::asYAML() const |
| 218 |
{ |
| 219 |
mrpt::containers::yaml d = mrpt::containers::yaml::Map(); |
| 220 |
auto vs = d["viewports"]; |
| 221 |
|
| 222 |
for (auto& v : m_viewports) vs[v->m_name] = v->asYAML(); |
| 223 |
|
| 224 |
return d; |
| 225 |
} |
| 226 |
|
| 227 |
/*-------------------------------------------------------------- |
| 228 |
createViewport |
| 229 |
---------------------------------------------------------------*/ |
| 230 |
Viewport::Ptr Scene::createViewport(const string& viewportName) |
| 231 |
{ |
| 232 |
MRPT_START |
| 233 |
|
| 234 |
Viewport::Ptr old = getViewport(viewportName); |
| 235 |
if (old) return old; |
| 236 |
|
| 237 |
auto theNew = std::make_shared<Viewport>(this, viewportName); |
| 238 |
m_viewports.push_back(theNew); |
| 239 |
return theNew; |
| 240 |
|
| 241 |
MRPT_END |
| 242 |
} |
| 243 |
|
| 244 |
/*-------------------------------------------------------------- |
| 245 |
getViewport |
| 246 |
---------------------------------------------------------------*/ |
| 247 |
Viewport::Ptr Scene::getViewport(const std::string& viewportName) const |
| 248 |
{ |
| 249 |
MRPT_START |
| 250 |
for (const auto& m_viewport : m_viewports) |
| 251 |
if (m_viewport->m_name == viewportName) return m_viewport; |
| 252 |
return Viewport::Ptr(); |
| 253 |
MRPT_END |
| 254 |
} |
| 255 |
|
| 256 |
/*-------------------------------------------------------------- |
| 257 |
removeObject |
| 258 |
---------------------------------------------------------------*/ |
| 259 |
void Scene::removeObject(const CRenderizable::Ptr& obj, const std::string& viewportName) |
| 260 |
{ |
| 261 |
MRPT_START |
| 262 |
|
| 263 |
Viewport::Ptr view = getViewport(viewportName); |
| 264 |
ASSERT_(view); |
| 265 |
view->removeObject(obj); |
| 266 |
|
| 267 |
MRPT_END |
| 268 |
} |
| 269 |
|
| 270 |
bool Scene::traceRay(const mrpt::poses::CPose3D& o, double& dist) const |
| 271 |
{ |
| 272 |
bool found = false; |
| 273 |
double tmp; |
| 274 |
for (const auto& vp : m_viewports) |
| 275 |
{ |
| 276 |
for (auto it2 = vp->m_objects.begin(); it2 != vp->m_objects.end(); ++it2) |
| 277 |
if ((*it2)->traceRay(o, tmp)) |
| 278 |
{ |
| 279 |
if (!found) |
| 280 |
{ |
| 281 |
found = true; |
| 282 |
dist = tmp; |
| 283 |
} |
| 284 |
else if (tmp < dist) |
| 285 |
dist = tmp; |
| 286 |
} |
| 287 |
} |
| 288 |
return found; |
| 289 |
} |
| 290 |
|
| 291 |
bool Scene::saveToFile(const std::string& fil) const |
| 292 |
{ |
| 293 |
try |
| 294 |
{ |
| 295 |
mrpt::io::CCompressedOutputStream f(fil); |
| 296 |
mrpt::serialization::archiveFrom(f) << *this; |
| 297 |
return true; |
| 298 |
} |
| 299 |
catch (...) |
| 300 |
{ |
| 301 |
return false; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
bool Scene::loadFromFile(const std::string& fil) |
| 306 |
{ |
| 307 |
try |
| 308 |
{ |
| 309 |
mrpt::io::CCompressedInputStream f(fil); |
| 310 |
mrpt::serialization::archiveFrom(f) >> *this; |
| 311 |
return true; |
| 312 |
} |
| 313 |
catch (...) |
| 314 |
{ |
| 315 |
return false; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
/** Evaluates the bounding box of this object (including possible children) in |
| 320 |
* the coordinate frame of the object parent. */ |
| 321 |
auto Scene::getBoundingBox(const std::string& vpn) const -> mrpt::math::TBoundingBox |
| 322 |
{ |
| 323 |
Viewport::Ptr vp = this->getViewport(vpn); |
| 324 |
ASSERTMSG_(vp, "No opengl viewport exists with the given name"); |
| 325 |
|
| 326 |
return vp->getBoundingBox(); |
| 327 |
} |
| 328 |
|
| 329 |
void Scene::freeOpenGLResources() |
| 330 |
{ |
| 331 |
auto do_free = [](const mrpt::opengl::CRenderizable::Ptr& o) { o->freeOpenGLResources(); }; |
| 332 |
|
| 333 |
visitAllObjects(do_free); |
| 334 |
} |