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
A 2D text (bitmap rendering): it always "faces the observer" despite it&#39;s at some 3D location...
Definition: CText.h:35
const mrpt::opengl::Program * shader
virtual void render(const RenderContext &rc) const =0
Implements the rendering of 3D objects in each class derived from CRenderizable.
const mrpt::opengl::TRenderMatrices * state
float getScaleX() const
Get the current scaling factor in one axis.
const T * data() const
Return raw pointer to row-major data buffer.
Definition: CMatrixFixed.h:278
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
const mrpt::opengl::CRenderizable * object
Definition: RenderQueue.h:35
bool hasToUpdateBuffers() const
Returns whether notifyChange() has been invoked since the last call to renderUpdateBuffers(), meaning the latter needs to be called again before rendering.
std::deque< CRenderizable::Ptr > CListOpenGLObjects
A list of smart pointers to renderizable objects.
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...
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:48
const mrpt::opengl::TLightParameters * lights
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.
STL namespace.
mrpt::opengl::CText & labelObject() const
Returns or constructs (in its first invokation) the associated mrpt::opengl::CText object representin...
Context for calls to render()
#define MRPT_PROFILE_FUNC_START
Definition: exceptions.h:234
unsigned int programId() const
Definition: Shader.h:107
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
std::string getString() const
Return the current text associated to this label.
Definition: CText.h:54
virtual mrpt::math::TPoint3Df getLocalRepresentativePoint() const
Provide a representative point (in object local coordinates), used to sort objects by eye-distance wh...
This base provides a set of functions for maths stuff.
mrpt::math::CMatrixFloat44 pmv_matrix
Result of p_matrix * mv_matrix.
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
const char * className
Definition: CObject.h:34
bool isVisible() const
Is the object visible?
Definition: CRenderizable.h:76
const std::string & getName() const
Returns the name of the object.
Definition: CRenderizable.h:75
bool hasUniform(const char *name) const
Definition: Shader.h:116
mrpt::math::CMatrixFloat44 mv_matrix
Model-view matrix.
Rendering state related to the projection and model-view matrices.
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
Each element in the queue to be rendered for each keyframe.
Definition: RenderQueue.h:24
virtual shader_list_t requiredShaders() const
Returns the ID of the OpenGL shader program required to render this class.
T x
X,Y,Z coordinates.
Definition: TPoint3D.h:29
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::opengl::TRenderMatrices renderState
Definition: RenderQueue.h:36
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
EIGEN_MAP asEigen()
Get as an Eigen-compatible Eigen::Map object.
Definition: CMatrixFixed.h:251
int uniformId(const char *name) const
Definition: Shader.h:113
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...
float getScaleY() const
Get the current scaling factor in one axis.
MATRIX44 getHomogeneousMatrixVal() const
Returns the corresponding 4x4 homogeneous transformation matrix for the point(translation) or pose (t...
Definition: CPoseOrPoint.h:278
A resource handling helper for OpenGL Shader "programs".
Definition: Shader.h:77
mrpt::math::CMatrixFloat44 p_matrix
Projection matrix, computed by renderNormalScene() from all the parameters above. ...
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
CMatrixFixed< double, 4, 4 > CMatrixDouble44
Definition: CMatrixFixed.h:368
void updateBuffers() const
Calls renderUpdateBuffers() and clear the flag that is set with notifyChange()
Lighting parameters, mostly for triangle shaders.
float getScaleZ() const
Get the current scaling factor in one axis.
void setString(const std::string &s)
Sets the text to display.
Definition: CText.h:47
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



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020