| 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/opengl/CRenderizableShaderText.h> |
| 13 |
#include <mrpt/opengl/Shader.h> |
| 14 |
#include <mrpt/opengl/opengl_api.h> |
| 15 |
|
| 16 |
using namespace mrpt; |
| 17 |
using namespace mrpt::opengl; |
| 18 |
|
| 19 |
IMPLEMENTS_VIRTUAL_SERIALIZABLE(CRenderizableShaderText, CRenderizable, mrpt::opengl) |
| 20 |
|
| 21 |
CRenderizableShaderText::CRenderizableShaderText() |
| 22 |
{ // Initialize GlState |
| 23 |
auto gh = gls(); |
| 24 |
} |
| 25 |
|
| 26 |
// Dtor: |
| 27 |
CRenderizableShaderText::~CRenderizableShaderText() = default; |
| 28 |
|
| 29 |
void CRenderizableShaderText::renderUpdateBuffers() const |
| 30 |
{ |
| 31 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 32 |
// Generate vertices & colors: |
| 33 |
const_cast<CRenderizableShaderText&>(*this).onUpdateBuffers_Text(); |
| 34 |
|
| 35 |
std::shared_lock<std::shared_mutex> readLock(m_textDataMtx.data); |
| 36 |
auto gh = gls(); |
| 37 |
|
| 38 |
// ======== LINES ======== |
| 39 |
// Define OpenGL buffers: |
| 40 |
gh.state.linesVertexBuffer->createOnce(); |
| 41 |
gh.state.linesVertexBuffer->bind(); |
| 42 |
gh.state.linesVertexBuffer->allocate( |
| 43 |
m_vertex_buffer_data.data(), sizeof(m_vertex_buffer_data[0]) * m_vertex_buffer_data.size()); |
| 44 |
|
| 45 |
// color buffer: |
| 46 |
gh.state.linesColorBuffer->createOnce(); |
| 47 |
gh.state.linesColorBuffer->bind(); |
| 48 |
gh.state.linesColorBuffer->allocate( |
| 49 |
m_color_buffer_data.data(), sizeof(m_color_buffer_data[0]) * m_color_buffer_data.size()); |
| 50 |
|
| 51 |
// ======== TRIANGLES ======== |
| 52 |
const auto n = m_triangles.size(); |
| 53 |
|
| 54 |
// Define OpenGL buffers: |
| 55 |
gh.state.trianglesBuffer->createOnce(); |
| 56 |
gh.state.trianglesBuffer->bind(); |
| 57 |
gh.state.trianglesBuffer->allocate(m_triangles.data(), sizeof(m_triangles[0]) * n); |
| 58 |
|
| 59 |
// VAO: required to use glEnableVertexAttribArray() |
| 60 |
gh.state.vao->createOnce(); |
| 61 |
#endif |
| 62 |
} |
| 63 |
|
| 64 |
void CRenderizableShaderText::render(const RenderContext& rc) const |
| 65 |
{ |
| 66 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 67 |
|
| 68 |
// Skip these geometric entities when in the 1st pass of shadow map: |
| 69 |
if (rc.state->is1stShadowMapPass) return; |
| 70 |
|
| 71 |
std::shared_lock<std::shared_mutex> readLock(m_textDataMtx.data); |
| 72 |
auto gh = gls(); |
| 73 |
|
| 74 |
// === LINES === |
| 75 |
std::optional<GLuint> attr_position; |
| 76 |
|
| 77 |
glDisable(GL_CULL_FACE); |
| 78 |
|
| 79 |
// Set up the vertex array: |
| 80 |
if (rc.shader->hasAttribute("position")) |
| 81 |
{ |
| 82 |
attr_position = rc.shader->attributeId("position"); |
| 83 |
gh.state.vao->bind(); |
| 84 |
glEnableVertexAttribArray(*attr_position); |
| 85 |
gh.state.linesVertexBuffer->bind(); |
| 86 |
glVertexAttribPointer( |
| 87 |
*attr_position, /* attribute */ |
| 88 |
3, /* size */ |
| 89 |
GL_FLOAT, /* type */ |
| 90 |
GL_FALSE, /* normalized? */ |
| 91 |
0, /* stride */ |
| 92 |
BUFFER_OFFSET(0) /* array buffer offset */ |
| 93 |
); |
| 94 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 95 |
} |
| 96 |
|
| 97 |
// Set up the color array: |
| 98 |
std::optional<GLuint> attr_color; |
| 99 |
{ |
| 100 |
attr_color = rc.shader->attributeId("vertexColor"); |
| 101 |
glEnableVertexAttribArray(*attr_color); |
| 102 |
gh.state.linesColorBuffer->bind(); |
| 103 |
glVertexAttribPointer( |
| 104 |
*attr_color, /* attribute */ |
| 105 |
4, /* size */ |
| 106 |
GL_UNSIGNED_BYTE, /* type */ |
| 107 |
GL_TRUE, /* normalized? */ |
| 108 |
0, /* stride */ |
| 109 |
BUFFER_OFFSET(0) /* array buffer offset */ |
| 110 |
); |
| 111 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 112 |
} |
| 113 |
|
| 114 |
if (attr_position) |
| 115 |
{ |
| 116 |
glDrawArrays(GL_LINES, 0, m_vertex_buffer_data.size()); |
| 117 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 118 |
} |
| 119 |
|
| 120 |
if (attr_position) |
| 121 |
{ |
| 122 |
// === TRIANGLES === |
| 123 |
gh.state.trianglesBuffer->bind(); |
| 124 |
glVertexAttribPointer( |
| 125 |
*attr_position, /* attribute */ |
| 126 |
3, /* size */ |
| 127 |
GL_FLOAT, /* type */ |
| 128 |
GL_FALSE, /* normalized? */ |
| 129 |
sizeof(TTriangle::Vertex), /* stride */ |
| 130 |
BUFFER_OFFSET(offsetof(TTriangle::Vertex, xyzrgba.pt.x))); |
| 131 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 132 |
|
| 133 |
if (attr_color) |
| 134 |
{ |
| 135 |
// Set up the color array: |
| 136 |
gh.state.trianglesBuffer->bind(); |
| 137 |
glVertexAttribPointer( |
| 138 |
*attr_color, /* attribute */ |
| 139 |
4, /* size */ |
| 140 |
GL_UNSIGNED_BYTE, /* type */ |
| 141 |
GL_TRUE, /* normalized? */ |
| 142 |
sizeof(TTriangle::Vertex), /* stride */ |
| 143 |
BUFFER_OFFSET(offsetof(TTriangle::Vertex, xyzrgba.r))); |
| 144 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 145 |
} |
| 146 |
|
| 147 |
// normals array: not used to render text |
| 148 |
|
| 149 |
// Draw: |
| 150 |
glDrawArrays(GL_TRIANGLES, 0, 3 * m_triangles.size()); |
| 151 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 152 |
} |
| 153 |
|
| 154 |
if (attr_position) glDisableVertexAttribArray(*attr_position); |
| 155 |
if (attr_color) glDisableVertexAttribArray(*attr_color); |
| 156 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 157 |
#endif |
| 158 |
} |