Debian Codegraph @

libs/opengl/src/CRenderizableShaderPoints.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/CRenderizableShaderPoints.h>
13 #include <mrpt/opengl/Shader.h>
14 #include <mrpt/opengl/opengl_api.h>
15 #include <mrpt/serialization/CArchive.h>
16
17 using namespace mrpt;
18 using namespace mrpt::opengl;
19
20 IMPLEMENTS_VIRTUAL_SERIALIZABLE(CRenderizableShaderPoints, CRenderizable, mrpt::opengl)
21
22 CRenderizableShaderPoints::CRenderizableShaderPoints()
23 { // Initialize GlState
24 auto gh = gls();
25 }
26
27 // Dtor:
28 CRenderizableShaderPoints::~CRenderizableShaderPoints() = default;
29
30 void CRenderizableShaderPoints::renderUpdateBuffers() const
31 {
32 #if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL
33
34 // Generate vertices & colors:
35 const_cast<CRenderizableShaderPoints&>(*this).onUpdateBuffers_Points();
36
37 std::shared_lock<std::shared_mutex> wfReadLock(CRenderizableShaderPoints::m_pointsMtx.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 CRenderizableShaderPoints::render(const RenderContext& rc) const
58 {
59 #if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL
60
61 // Skip these geometric entities when in the 1st pass of shadow map:
62 if (rc.state->is1stShadowMapPass) return;
63
64 std::shared_lock<std::shared_mutex> wfReadLock(CRenderizableShaderPoints::m_pointsMtx.data);
65 auto gh = gls();
66
67 // Point size as uniform:
68 glUniform1f(rc.shader->uniformId("vertexPointSize"), m_pointSize);
69
70 // Variable point size code in the shader:
71 glUniform1i(rc.shader->uniformId("enableVariablePointSize"), m_variablePointSize ? 1 : 0);
72
73 glUniform1f(rc.shader->uniformId("variablePointSize_K"), m_variablePointSize_K);
74 glUniform1f(rc.shader->uniformId("variablePointSize_DepthScale"), m_variablePointSize_DepthScale);
75
76 // Set up the vertex array:
77 std::optional<GLuint> attr_position;
78 if (rc.shader->hasAttribute("position"))
79 {
80 attr_position = rc.shader->attributeId("position");
81 gh.state.vao->bind();
82 glEnableVertexAttribArray(*attr_position);
83 gh.state.vertexBuffer->bind();
84 glVertexAttribPointer(
85 *attr_position, /* attribute */
86 3, /* size */
87 GL_FLOAT, /* type */
88 GL_FALSE, /* normalized? */
89 0, /* stride */
90 BUFFER_OFFSET(0) /* array buffer offset */
91 );
92 CHECK_OPENGL_ERROR_IN_DEBUG();
93 }
94
95 // Set up the color array:
96 std::optional<GLuint> attr_color;
97 if (rc.shader->hasAttribute("vertexColor"))
98 {
99 attr_color = rc.shader->attributeId("vertexColor");
100 glEnableVertexAttribArray(*attr_color);
101 gh.state.colorBuffer->bind();
102 glVertexAttribPointer(
103 *attr_color, /* attribute */
104 4, /* size */
105 GL_UNSIGNED_BYTE, /* type */
106 GL_TRUE, /* normalized? */
107 0, /* stride */
108 BUFFER_OFFSET(0) /* array buffer offset */
109 );
110 CHECK_OPENGL_ERROR_IN_DEBUG();
111 }
112
113 glDrawArrays(GL_POINTS, 0, m_vertex_buffer_data.size());
114 CHECK_OPENGL_ERROR_IN_DEBUG();
115
116 if (attr_position) glDisableVertexAttribArray(*attr_position);
117 if (attr_color) glDisableVertexAttribArray(*attr_color);
118 CHECK_OPENGL_ERROR_IN_DEBUG();
119 #endif
120 }
121
122 void CRenderizableShaderPoints::params_serialize(mrpt::serialization::CArchive& out) const
123 {
124 out.WriteAs<uint8_t>(0); // serialization version
125 out << m_pointSize << m_variablePointSize << m_variablePointSize_K
126 << m_variablePointSize_DepthScale;
127 }
128 void CRenderizableShaderPoints::params_deserialize(mrpt::serialization::CArchive& in)
129 {
130 const auto version = in.ReadAs<uint8_t>();
131
132 switch (version)
133 {
134 case 0:
135 in >> m_pointSize >> m_variablePointSize >> m_variablePointSize_K >>
136 m_variablePointSize_DepthScale;
137 break;
138 default:
139 MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
140 };
141 }
142
143 const mrpt::math::TBoundingBoxf CRenderizableShaderPoints::verticesBoundingBox() const
144 {
145 std::shared_lock<std::shared_mutex> wfReadLock(CRenderizableShaderPoints::m_pointsMtx.data);
146
147 mrpt::math::TBoundingBoxf bb;
148
149 if (m_vertex_buffer_data.empty()) return bb;
150
151 bb = mrpt::math::TBoundingBoxf::PlusMinusInfinity();
152
153 for (const auto& p : m_vertex_buffer_data) bb.updateWithPoint(p);
154
155 return bb;
156 }