Debian Codegraph @

libs/opengl/src/CRenderizableShaderWireFrame.cpp

raw
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/CRenderizableShaderWireFrame.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(CRenderizableShaderWireFrame, CRenderizable, mrpt::opengl)
20
21 CRenderizableShaderWireFrame::CRenderizableShaderWireFrame()
22 { // Initialize GlState
23 auto gh = gls();
24 }
25
26 // Dtor:
27 CRenderizableShaderWireFrame::~CRenderizableShaderWireFrame() = default;
28
29 void CRenderizableShaderWireFrame::renderUpdateBuffers() const
30 {
31 #if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL
32
33 // Generate vertices & colors:
34 const_cast<CRenderizableShaderWireFrame&>(*this).onUpdateBuffers_Wireframe();
35
36 const std::shared_lock<std::shared_mutex> wfReadLock(
37 CRenderizableShaderWireFrame::m_wireframeMtx.data);
38 auto gh = gls();
39
40 // Define OpenGL buffers:
41 gh.state.vertexBuffer->createOnce();
42 gh.state.vertexBuffer->bind();
43 gh.state.vertexBuffer->allocate(
44 m_vertex_buffer_data.data(), sizeof(m_vertex_buffer_data[0]) * m_vertex_buffer_data.size());
45
46 // color buffer:
47 gh.state.colorBuffer->createOnce();
48 gh.state.colorBuffer->bind();
49 gh.state.colorBuffer->allocate(
50 m_color_buffer_data.data(), sizeof(m_color_buffer_data[0]) * m_color_buffer_data.size());
51
52 // VAO: required to use glEnableVertexAttribArray()
53 gh.state.vao->createOnce();
54 #endif
55 }
56
57 void CRenderizableShaderWireFrame::render(const RenderContext& rc) const
58 {
59 #if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL
60 // TODO: Port thick lines to opengl3?
61 // glLineWidth(m_lineWidth);
62
63 // Skip these geometric entities when in the 1st pass of shadow map:
64 if (rc.state->is1stShadowMapPass) return;
65
66 const std::shared_lock<std::shared_mutex> wfReadLock(
67 CRenderizableShaderWireFrame::m_wireframeMtx.data);
68 auto gh = gls();
69
70 // Set up the vertex array:
71 std::optional<GLuint> attr_position;
72 if (rc.shader->hasAttribute("position"))
73 {
74 attr_position = rc.shader->attributeId("position");
75 gh.state.vao->bind();
76 glEnableVertexAttribArray(*attr_position);
77 gh.state.vertexBuffer->bind();
78 glVertexAttribPointer(
79 *attr_position, /* attribute */
80 3, /* size */
81 GL_FLOAT, /* type */
82 GL_FALSE, /* normalized? */
83 0, /* stride */
84 BUFFER_OFFSET(0) /* array buffer offset */
85 );
86 CHECK_OPENGL_ERROR_IN_DEBUG();
87 }
88
89 // Set up the color array:
90 std::optional<GLuint> attr_color;
91 if (rc.shader->hasAttribute("vertexColor"))
92 {
93 attr_color = rc.shader->attributeId("vertexColor");
94 glEnableVertexAttribArray(*attr_color);
95 gh.state.colorBuffer->bind();
96 glVertexAttribPointer(
97 *attr_color, /* attribute */
98 4, /* size */
99 GL_UNSIGNED_BYTE, /* type */
100 GL_TRUE, /* normalized? */
101 0, /* stride */
102 BUFFER_OFFSET(0) /* array buffer offset */
103 );
104 CHECK_OPENGL_ERROR_IN_DEBUG();
105 }
106
107 glDrawArrays(GL_LINES, 0, m_vertex_buffer_data.size());
108 CHECK_OPENGL_ERROR_IN_DEBUG();
109
110 if (attr_position) glDisableVertexAttribArray(*attr_position);
111 if (attr_color) glDisableVertexAttribArray(*attr_color);
112 CHECK_OPENGL_ERROR_IN_DEBUG();
113 #endif
114 }
115
116 const mrpt::math::TBoundingBox CRenderizableShaderWireFrame::wireframeVerticesBoundingBox() const
117 {
118 mrpt::math::TBoundingBox bb;
119 std::shared_lock<std::shared_mutex> wfReadLock(CRenderizableShaderWireFrame::m_wireframeMtx.data);
120
121 if (m_vertex_buffer_data.empty()) return bb;
122
123 bb.min = mrpt::math::TPoint3D(
124 std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
125 std::numeric_limits<double>::max());
126 bb.max = mrpt::math::TPoint3D(
127 -std::numeric_limits<double>::max(), -std::numeric_limits<double>::max(),
128 -std::numeric_limits<double>::max());
129
130 for (const auto& p : m_vertex_buffer_data)
131 {
132 keep_min(bb.min.x, p.x);
133 keep_max(bb.max.x, p.x);
134 keep_min(bb.min.y, p.y);
135 keep_max(bb.max.y, p.y);
136 keep_min(bb.min.z, p.z);
137 keep_max(bb.max.z, p.z);
138 }
139 return bb;
140 }