MRPT  2.0.4
RenderQueue.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, 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 
13 #include <mrpt/opengl/CText.h>
15 #include <mrpt/opengl/opengl_api.h>
16 #include <mrpt/system/os.h>
17 #include <Eigen/Dense>
18 #include <map>
19 
20 using namespace std;
21 using namespace mrpt;
22 using namespace mrpt::math;
23 using namespace mrpt::poses;
24 using namespace mrpt::system;
25 using namespace mrpt::opengl;
26 
27 // Render a set of objects
31 {
32 #if MRPT_HAS_OPENGL_GLUT
34 
35  const char* curClassName = nullptr;
36  try
37  {
38  for (const auto& objPtr : objs)
39  {
40  if (!objPtr) continue;
41  // Use plain pointers, faster than smart pointers:
42  const CRenderizable* obj = objPtr.get();
43  // Save class name: just in case we have an exception, for error
44  // reporting:
45  curClassName = obj->GetRuntimeClass()->className;
46 
47  // Regenerate opengl vertex buffers?
48  if (obj->hasToUpdateBuffers()) obj->updateBuffers();
49 
50  if (!obj->isVisible()) continue;
51 
52  const CPose3D& thisPose = obj->getPoseRef();
53  CMatrixFloat44 HM =
55  .cast_float();
56 
57  // Scaling:
58  if (obj->getScaleX() != 1 || obj->getScaleY() != 1 ||
59  obj->getScaleZ() != 1)
60  {
61  auto scale = CMatrixFloat44::Identity();
62  scale(0, 0) = obj->getScaleX();
63  scale(1, 1) = obj->getScaleY();
64  scale(2, 2) = obj->getScaleZ();
65  HM.asEigen() = HM.asEigen() * scale.asEigen();
66  }
67 
68  // Make a copy of rendering state, so we always have the original
69  // version of my parent intact.
70  auto _ = state;
71 
72  // Compose relative to my parent pose:
73  _.mv_matrix.asEigen() = _.mv_matrix.asEigen() * HM.asEigen();
74 
75  // Precompute pmv_matrix to be used in shaders:
76  _.pmv_matrix.asEigen() =
77  _.p_matrix.asEigen() * _.mv_matrix.asEigen();
78 
79  // Get a representative depth for this object (to sort objects from
80  // eye-distance):
82 
83  Eigen::Vector4f lrp_hm(lrp.x, lrp.y, lrp.z, 1.0f);
84  const auto lrp_proj = (_.pmv_matrix.asEigen() * lrp_hm).eval();
85  const float depth =
86  (lrp_proj(3) != 0) ? lrp_proj(2) / lrp_proj(3) : .001f;
87 
88  // Enqeue this object...
89  const auto lst_shaders = obj->requiredShaders();
90  for (const auto shader_id : lst_shaders)
91  {
92  // eye-to-object depth:
93  rq[shader_id].emplace(depth, RenderQueueElement(obj, _));
94  }
95 
96  // ...and its children:
97  obj->enqueForRenderRecursive(_, rq);
98 
99  if (obj->isShowNameEnabled())
100  {
101  CText& label = obj->labelObject();
102 
103  // Update the label, only if it changed:
104  if (label.getString() != obj->getName())
105  label.setString(obj->getName());
106 
107  // Regenerate opengl vertex buffers, if first time or label
108  // changed:
109  if (label.hasToUpdateBuffers()) label.updateBuffers();
110 
111  rq[DefaultShaderID::TEXT].emplace(
112  depth, RenderQueueElement(&label, _));
113  }
114 
115  } // end foreach object
116  }
117  catch (const exception& e)
118  {
120  "Exception while rendering class '%s':\n%s",
121  curClassName ? curClassName : "(undefined)", e.what());
122  }
123 #endif
124 }
125 
127  const RenderQueue& rq,
128  std::map<shader_id_t, mrpt::opengl::Program::Ptr>& shaders,
129  const mrpt::opengl::TLightParameters& lights)
130 {
131 #if MRPT_HAS_OPENGL_GLUT
133 
134  for (const auto& rqSet : rq)
135  {
136  // bind the shader for this sequence of objects:
137  mrpt::opengl::Program& shader = *shaders.at(rqSet.first);
138 
139  glUseProgram(shader.programId());
140  CHECK_OPENGL_ERROR();
141 
142  // Process all objects using this shader:
143  const auto& rqMap = rqSet.second;
144 
145  // Render in reverse depth order:
146  for (auto it = rqMap.rbegin(); it != rqMap.rend(); ++it)
147  {
148  const RenderQueueElement& rqe = it->second;
149 
150  // Load matrices in shader:
151  const auto IS_TRANSPOSED = GL_TRUE;
152  glUniformMatrix4fv(
153  shader.uniformId("p_matrix"), 1, IS_TRANSPOSED,
154  rqe.renderState.p_matrix.data());
155 
156  glUniformMatrix4fv(
157  shader.uniformId("mv_matrix"), 1, IS_TRANSPOSED,
158  rqe.renderState.mv_matrix.data());
159 
160  if (shader.hasUniform("pmv_matrix"))
161  glUniformMatrix4fv(
162  shader.uniformId("pmv_matrix"), 1, IS_TRANSPOSED,
163  rqe.renderState.pmv_matrix.data());
164 
165  // Use Texture0:
166  if (shader.hasUniform("textureSampler"))
167  glUniform1i(shader.uniformId("textureSampler"), 0);
168 
170  rc.shader = &shader;
171  rc.shader_id = rqSet.first;
172  rc.state = &rqe.renderState;
173  rc.lights = &lights;
174 
175  // Render object:
176  ASSERT_(rqe.object != nullptr);
177  {
178  rqe.object->render(rc);
179  CHECK_OPENGL_ERROR();
180  }
181  }
182  }
183 
184 #endif
185 }
186 
187 #if MRPT_HAS_OPENGL_GLUT
188 void mrpt::opengl::checkOpenGLErr_impl(
189  unsigned int glErrorCode, const char* filename, int lineno)
190 {
191  if (glErrorCode == GL_NO_ERROR) return;
192  const std::string sErr = mrpt::format(
193  "[%s:%i] OpenGL error: %s", filename, lineno,
194  reinterpret_cast<const char*>(gluErrorString(glErrorCode)));
195  std::cerr << "[gl_utils::checkOpenGLError] " << sErr << std::endl;
196  THROW_EXCEPTION(sErr);
197 }
198 #endif
mrpt::opengl::Program
A resource handling helper for OpenGL Shader "programs".
Definition: Shader.h:78
os.h
opengl_api.h
mrpt::opengl::TRenderMatrices::mv_matrix
mrpt::math::CMatrixFloat44 mv_matrix
Model-view matrix.
Definition: TRenderMatrices.h:53
mrpt::opengl::CRenderizable::enqueForRenderRecursive
virtual void enqueForRenderRecursive([[maybe_unused]] const mrpt::opengl::TRenderMatrices &state, [[maybe_unused]] RenderQueue &rq) const
Process all children objects recursively, if the object is a container.
Definition: CRenderizable.h:285
mrpt::opengl::RenderQueueElement::object
const mrpt::opengl::CRenderizable * object
Definition: RenderQueue.h:35
mrpt::opengl::CRenderizable::RenderContext::shader
const mrpt::opengl::Program * shader
Definition: CRenderizable.h:271
mrpt::opengl::RenderQueueElement::renderState
mrpt::opengl::TRenderMatrices renderState
Definition: RenderQueue.h:36
mrpt::opengl::RenderQueue
std::map< shader_id_t, std::multimap< float, RenderQueueElement > > RenderQueue
A queue for rendering, sorted by shader program to minimize changes of OpenGL shader programs while r...
Definition: RenderQueue.h:46
mrpt::math::TPoint3D_< float >
mrpt::poses::CPoseOrPoint::getHomogeneousMatrixVal
MATRIX44 getHomogeneousMatrixVal() const
Returns the corresponding 4x4 homogeneous transformation matrix for the point(translation) or pose (t...
Definition: CPoseOrPoint.h:278
CSetOfObjects.h
mrpt::opengl::CRenderizable
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:49
mrpt::opengl::CRenderizable::labelObject
mrpt::opengl::CText & labelObject() const
Returns or constructs (in its first invokation) the associated mrpt::opengl::CText object representin...
Definition: CRenderizable.cpp:238
mrpt::opengl::processRenderQueue
void processRenderQueue(const RenderQueue &rq, std::map< shader_id_t, mrpt::opengl::Program::Ptr > &shaders, const mrpt::opengl::TLightParameters &lights)
After enqueForRendering(), actually executes the rendering tasks, grouped shader by shader.
Definition: RenderQueue.cpp:126
mrpt::math::TPoint3D_data::z
T z
Definition: TPoint3D.h:29
mrpt::opengl::CRenderizable::getScaleX
float getScaleX() const
Get the current scaling factor in one axis.
Definition: CRenderizable.h:223
mrpt::opengl::CRenderizable::isShowNameEnabled
bool isShowNameEnabled() const
Definition: CRenderizable.h:92
mrpt::rtti::TRuntimeClassId::className
const char * className
Definition: CObject.h:34
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: BaseAppDataSource.h:16
mrpt::opengl::CRenderizable::getScaleZ
float getScaleZ() const
Get the current scaling factor in one axis.
Definition: CRenderizable.h:227
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:23
mrpt::math::CMatrixDouble44
CMatrixFixed< double, 4, 4 > CMatrixDouble44
Definition: CMatrixFixed.h:368
mrpt::opengl::CText::setString
void setString(const std::string &s)
Sets the text to display.
Definition: CText.h:47
mrpt::opengl::CRenderizable::GetRuntimeClass
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
mrpt::math::CMatrixFixed< float, 4, 4 >
mrpt::opengl::CRenderizable::getPoseRef
const mrpt::poses::CPose3D & getPoseRef() const
Returns a const ref to the 3D pose of the object as mrpt::poses::CPose3D (which explicitly contains t...
Definition: CRenderizable.h:117
mrpt::opengl::CRenderizable::getScaleY
float getScaleY() const
Get the current scaling factor in one axis.
Definition: CRenderizable.h:225
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:87
mrpt::opengl::Program::uniformId
int uniformId(const char *name) const
Definition: Shader.h:113
mrpt::opengl::Program::programId
unsigned int programId() const
Definition: Shader.h:107
mrpt::opengl::TRenderMatrices::p_matrix
mrpt::math::CMatrixFloat44 p_matrix
Projection matrix, computed by renderNormalScene() from all the parameters above.
Definition: TRenderMatrices.h:50
mrpt::opengl::CRenderizable::hasToUpdateBuffers
bool hasToUpdateBuffers() const
Returns whether notifyChange() has been invoked since the last call to renderUpdateBuffers(),...
Definition: CRenderizable.h:324
mrpt::opengl::CRenderizable::RenderContext
Context for calls to render()
Definition: CRenderizable.h:267
mrpt::opengl::RenderQueueElement
Each element in the queue to be rendered for each keyframe.
Definition: RenderQueue.h:25
mrpt::opengl::CRenderizable::getName
const std::string & getName() const
Returns the name of the object.
Definition: CRenderizable.h:75
mrpt::opengl::CRenderizable::RenderContext::shader_id
mrpt::opengl::shader_id_t shader_id
Definition: CRenderizable.h:272
mrpt::math::TPoint3D_data::y
T y
Definition: TPoint3D.h:29
mrpt::opengl::enqueForRendering
void enqueForRendering(const mrpt::opengl::CListOpenGLObjects &objs, const mrpt::opengl::TRenderMatrices &state, RenderQueue &rq)
Processes, recursively, all objects in the list, classifying them by shader programs into a list suit...
Definition: RenderQueue.cpp:28
opengl-precomp.h
mrpt::opengl::TRenderMatrices
Rendering state related to the projection and model-view matrices.
Definition: TRenderMatrices.h:26
mrpt::math::CMatrixFixed::data
const T * data() const
Return raw pointer to row-major data buffer.
Definition: CMatrixFixed.h:278
mrpt::math::CMatrixFixed::asEigen
EIGEN_MAP asEigen()
Get as an Eigen-compatible Eigen::Map object
Definition: CMatrixFixed.h:251
mrpt::opengl::CRenderizable::updateBuffers
void updateBuffers() const
Calls renderUpdateBuffers() and clear the flag that is set with notifyChange()
Definition: CRenderizable.h:307
mrpt::opengl::CRenderizable::RenderContext::lights
const mrpt::opengl::TLightParameters * lights
Definition: CRenderizable.h:273
mrpt::opengl::CListOpenGLObjects
std::deque< CRenderizable::Ptr > CListOpenGLObjects
A list of smart pointers to renderizable objects.
Definition: CRenderizable.h:376
mrpt::math::TPoint3D_data::x
T x
X,Y,Z coordinates.
Definition: TPoint3D.h:29
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:12
mrpt::opengl::CText::getString
std::string getString() const
Return the current text associated to this label.
Definition: CText.h:54
RenderQueue.h
mrpt::opengl::TLightParameters
Lighting parameters, mostly for triangle shaders.
Definition: TLightParameters.h:23
mrpt::opengl::CRenderizable::getLocalRepresentativePoint
virtual mrpt::math::TPoint3Df getLocalRepresentativePoint() const
Provide a representative point (in object local coordinates), used to sort objects by eye-distance wh...
Definition: CRenderizable.h:341
mrpt::opengl::CRenderizable::requiredShaders
virtual shader_list_t requiredShaders() const
Returns the ID of the OpenGL shader program required to render this class.
Definition: CRenderizable.h:300
mrpt::opengl::CRenderizable::RenderContext::state
const mrpt::opengl::TRenderMatrices * state
Definition: CRenderizable.h:270
mrpt::opengl::Program::hasUniform
bool hasUniform(const char *name) const
Definition: Shader.h:116
mrpt::opengl::TRenderMatrices::pmv_matrix
mrpt::math::CMatrixFloat44 pmv_matrix
Result of p_matrix * mv_matrix.
Definition: TRenderMatrices.h:58
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:14
mrpt::opengl::CText
A 2D text (bitmap rendering): it always "faces the observer" despite it's at some 3D location.
Definition: CText.h:36
mrpt::opengl::CRenderizable::isVisible
bool isVisible() const
Is the object visible?
Definition: CRenderizable.h:76
mrpt::format
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
mrpt::system
Definition: backtrace.h:15
CText.h
MRPT_PROFILE_FUNC_START
#define MRPT_PROFILE_FUNC_START
Definition: exceptions.h:234
mrpt::opengl::CRenderizable::render
virtual void render(const RenderContext &rc) const =0
Implements the rendering of 3D objects in each class derived from CRenderizable.



Page generated by Doxygen 1.8.18 for MRPT 2.0.4 at Thu Sep 24 07:14:18 UTC 2020