MRPT  2.0.1
CRenderizableShaderText.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/Shader.h>
14 #include <mrpt/opengl/opengl_api.h>
15 
16 using namespace mrpt;
17 using namespace mrpt::opengl;
18 
21 
22 // Dtor:
24 
26 {
27 #if MRPT_HAS_OPENGL_GLUT
28  // Generate vertices & colors:
29  const_cast<CRenderizableShaderText&>(*this).onUpdateBuffers_Text();
30 
31  // ======== LINES ========
32  // Define OpenGL buffers:
36  m_vertex_buffer_data.data(),
37  sizeof(m_vertex_buffer_data[0]) * m_vertex_buffer_data.size());
38 
39  // color buffer:
43  m_color_buffer_data.data(),
44  sizeof(m_color_buffer_data[0]) * m_color_buffer_data.size());
45 
46  // ======== TRIANGLES ========
47  const auto n = m_triangles.size();
48 
49  // Define OpenGL buffers:
52  m_trianglesBuffer.allocate(m_triangles.data(), sizeof(m_triangles[0]) * n);
53 
54  // VAO: required to use glEnableVertexAttribArray()
55  m_vao.createOnce();
56 
57 #endif
58 }
59 
61 {
62 #if MRPT_HAS_OPENGL_GLUT
63  glEnable(GL_LINE_SMOOTH);
64  CHECK_OPENGL_ERROR();
65 
66  // === LINES ===
67  // Set up the vertex array:
68  const GLuint attr_position = rc.shader->attributeId("position");
69  m_vao.bind();
70  glEnableVertexAttribArray(attr_position);
72  glVertexAttribPointer(
73  attr_position, /* attribute */
74  3, /* size */
75  GL_FLOAT, /* type */
76  GL_FALSE, /* normalized? */
77  0, /* stride */
78  BUFFER_OFFSET(0) /* array buffer offset */
79  );
80  CHECK_OPENGL_ERROR();
81 
82  // Set up the color array:
83  const GLuint attr_color = rc.shader->attributeId("vertexColor");
84  glEnableVertexAttribArray(attr_color);
86  glVertexAttribPointer(
87  attr_color, /* attribute */
88  4, /* size */
89  GL_UNSIGNED_BYTE, /* type */
90  GL_TRUE, /* normalized? */
91  0, /* stride */
92  BUFFER_OFFSET(0) /* array buffer offset */
93  );
94  CHECK_OPENGL_ERROR();
95 
96  glDrawArrays(GL_LINES, 0, m_vertex_buffer_data.size());
97  CHECK_OPENGL_ERROR();
98 
99  // === TRIANGLES ===
101  glVertexAttribPointer(
102  attr_position, /* attribute */
103  3, /* size */
104  GL_FLOAT, /* type */
105  GL_FALSE, /* normalized? */
106  sizeof(TTriangle::Vertex), /* stride */
107  BUFFER_OFFSET(offsetof(TTriangle::Vertex, xyzrgba.pt.x)));
108  CHECK_OPENGL_ERROR();
109 
110  // Set up the color array:
112  glVertexAttribPointer(
113  attr_color, /* attribute */
114  4, /* size */
115  GL_UNSIGNED_BYTE, /* type */
116  GL_TRUE, /* normalized? */
117  sizeof(TTriangle::Vertex), /* stride */
118  BUFFER_OFFSET(offsetof(TTriangle::Vertex, xyzrgba.r)));
119  CHECK_OPENGL_ERROR();
120 
121  // normals array: not used to render text
122 
123  // Draw:
124  glDrawArrays(GL_TRIANGLES, 0, 3 * m_triangles.size());
125  CHECK_OPENGL_ERROR();
126 
127  glDisableVertexAttribArray(attr_position);
128  glDisableVertexAttribArray(attr_color);
129  CHECK_OPENGL_ERROR();
130 #endif
131 }
void createOnce()
Calls create() only if the buffer has not been created yet.
Definition: COpenGLBuffer.h:60
#define IMPLEMENTS_VIRTUAL_SERIALIZABLE(class_name, base_class, NS)
This must be inserted as implementation of some required members for virtual CSerializable classes: ...
const mrpt::opengl::Program * shader
void allocate(const void *data, int byteCount)
Reserves byteCount bytes in the buffer and copy to it the provided data.
Definition: COpenGLBuffer.h:78
virtual void onUpdateBuffers_Text()=0
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:48
int attributeId(const char *name) const
Definition: Shader.h:114
Context for calls to render()
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
List of lines.
Renderizable generic renderer for objects using the "text shader".
virtual ~CRenderizableShaderText() override
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
void createOnce()
Calls create() only if the buffer has not been created yet.
std::vector< mrpt::opengl::TTriangle > m_triangles
List of triangles.
#define BUFFER_OFFSET(offset)
std::vector< mrpt::img::TColor > m_color_buffer_data



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020