| 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/CRenderizableShaderTriangles.h> |
| 13 |
#include <mrpt/opengl/Shader.h> |
| 14 |
#include <mrpt/opengl/TLightParameters.h> |
| 15 |
#include <mrpt/opengl/opengl_api.h> |
| 16 |
#include <mrpt/serialization/CArchive.h> |
| 17 |
|
| 18 |
using namespace mrpt; |
| 19 |
using namespace mrpt::opengl; |
| 20 |
|
| 21 |
IMPLEMENTS_VIRTUAL_SERIALIZABLE(CRenderizableShaderTriangles, CRenderizable, mrpt::opengl) |
| 22 |
|
| 23 |
CRenderizableShaderTriangles::CRenderizableShaderTriangles() |
| 24 |
{ // Initialize GlState |
| 25 |
auto gh = gls(); |
| 26 |
} |
| 27 |
|
| 28 |
// Dtor: |
| 29 |
CRenderizableShaderTriangles::~CRenderizableShaderTriangles() = default; |
| 30 |
|
| 31 |
void CRenderizableShaderTriangles::renderUpdateBuffers() const |
| 32 |
{ |
| 33 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 34 |
// Generate vertices & colors into m_triangles |
| 35 |
const_cast<CRenderizableShaderTriangles&>(*this).onUpdateBuffers_Triangles(); |
| 36 |
|
| 37 |
std::shared_lock<std::shared_mutex> trisReadLock( |
| 38 |
CRenderizableShaderTriangles::m_trianglesMtx.data); |
| 39 |
auto gh = gls(); |
| 40 |
|
| 41 |
const auto& tris = shaderTrianglesBuffer(); |
| 42 |
|
| 43 |
const auto n = tris.size(); |
| 44 |
|
| 45 |
// Define OpenGL buffers: |
| 46 |
gh.state.trianglesBuffer->createOnce(); |
| 47 |
gh.state.trianglesBuffer->bind(); |
| 48 |
gh.state.trianglesBuffer->allocate(tris.data(), sizeof(tris[0]) * n); |
| 49 |
|
| 50 |
// VAO: required to use glEnableVertexAttribArray() |
| 51 |
gh.state.vao->createOnce(); |
| 52 |
#endif |
| 53 |
} |
| 54 |
|
| 55 |
void CRenderizableShaderTriangles::render(const RenderContext& rc) const |
| 56 |
{ |
| 57 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 58 |
|
| 59 |
std::shared_lock<std::shared_mutex> trisReadLock( |
| 60 |
CRenderizableShaderTriangles::m_trianglesMtx.data); |
| 61 |
auto gh = gls(); |
| 62 |
|
| 63 |
const Program& s = *rc.shader; |
| 64 |
|
| 65 |
// Lights: |
| 66 |
if (m_enableLight && rc.lights && rc.shader->hasUniform("light_color") && |
| 67 |
(!rc.activeLights || rc.activeLights.value() != rc.lights)) |
| 68 |
{ |
| 69 |
// buffered pointer, to prevent re-setting the opengl state with the |
| 70 |
// same values, a performance killer: |
| 71 |
rc.activeLights = rc.lights; |
| 72 |
|
| 73 |
const auto& l = rc.lights; |
| 74 |
|
| 75 |
s.setFloat3("light_color", l->color.R, l->color.G, l->color.B); |
| 76 |
s.setFloat3("light_direction", l->direction.x, l->direction.y, l->direction.z); |
| 77 |
s.setFloat("light_ambient", l->ambient); |
| 78 |
s.setFloat("light_diffuse", l->diffuse); |
| 79 |
if (rc.shader->hasUniform("light_specular")) s.setFloat("light_specular", l->specular); |
| 80 |
|
| 81 |
if (rc.shader->hasUniform("light_zmax")) |
| 82 |
s.setFloat("light_zmax", rc.state->getLastLightClipZFar()); |
| 83 |
if (rc.shader->hasUniform("camera_far_plane")) |
| 84 |
s.setFloat("camera_far_plane", rc.state->getLastClipZFar()); |
| 85 |
|
| 86 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 87 |
} |
| 88 |
|
| 89 |
// Set the texture uniform: |
| 90 |
if (rc.shader->hasUniform("shadowMap")) |
| 91 |
{ |
| 92 |
// bound to GL_TEXTURE0 + "i": |
| 93 |
s.setInt("shadowMap", SHADOW_MAP_TEXTURE_UNIT); |
| 94 |
|
| 95 |
// Shadow params: |
| 96 |
if (const auto& l = rc.lights; l) |
| 97 |
{ |
| 98 |
rc.shader->setFloat("shadow_bias", l->shadow_bias); |
| 99 |
rc.shader->setFloat("shadow_bias_cam2frag", l->shadow_bias_cam2frag); |
| 100 |
rc.shader->setFloat("shadow_bias_normal", l->shadow_bias_normal); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// Set up the vertex array: |
| 105 |
std::optional<GLuint> attr_position; |
| 106 |
if (rc.shader->hasAttribute("position")) |
| 107 |
{ |
| 108 |
attr_position = rc.shader->attributeId("position"); |
| 109 |
gh.state.vao->bind(); |
| 110 |
glEnableVertexAttribArray(*attr_position); |
| 111 |
gh.state.trianglesBuffer->bind(); |
| 112 |
glVertexAttribPointer( |
| 113 |
*attr_position, /* attribute */ |
| 114 |
3, /* size */ |
| 115 |
GL_FLOAT, /* type */ |
| 116 |
GL_FALSE, /* normalized? */ |
| 117 |
sizeof(TTriangle::Vertex), /* stride */ |
| 118 |
BUFFER_OFFSET(offsetof(TTriangle::Vertex, xyzrgba.pt.x))); |
| 119 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 120 |
} |
| 121 |
|
| 122 |
// Set up the color array: |
| 123 |
std::optional<GLuint> attr_color; |
| 124 |
if (rc.shader->hasAttribute("vertexColor")) |
| 125 |
{ |
| 126 |
attr_color = rc.shader->attributeId("vertexColor"); |
| 127 |
glEnableVertexAttribArray(*attr_color); |
| 128 |
gh.state.trianglesBuffer->bind(); |
| 129 |
glVertexAttribPointer( |
| 130 |
*attr_color, /* attribute */ |
| 131 |
4, /* size */ |
| 132 |
GL_UNSIGNED_BYTE, /* type */ |
| 133 |
GL_TRUE, /* normalized? */ |
| 134 |
sizeof(TTriangle::Vertex), /* stride */ |
| 135 |
BUFFER_OFFSET(offsetof(TTriangle::Vertex, xyzrgba.r))); |
| 136 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 137 |
} |
| 138 |
|
| 139 |
// Set up the normals array: |
| 140 |
std::optional<GLuint> attr_normals; |
| 141 |
if (rc.shader->hasAttribute("vertexNormal")) |
| 142 |
{ |
| 143 |
attr_normals = rc.shader->attributeId("vertexNormal"); |
| 144 |
glEnableVertexAttribArray(*attr_normals); |
| 145 |
gh.state.trianglesBuffer->bind(); |
| 146 |
glVertexAttribPointer( |
| 147 |
*attr_normals, /* attribute */ |
| 148 |
3, /* size */ |
| 149 |
GL_FLOAT, /* type */ |
| 150 |
GL_FALSE, /* normalized? */ |
| 151 |
sizeof(TTriangle::Vertex), /* stride */ |
| 152 |
BUFFER_OFFSET(offsetof(TTriangle::Vertex, normal.x))); |
| 153 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 154 |
} |
| 155 |
|
| 156 |
if (m_cullface == TCullFace::NONE && rc.activeCullFace != TCullFace::NONE) |
| 157 |
{ |
| 158 |
rc.activeCullFace = TCullFace::NONE; |
| 159 |
glDisable(GL_CULL_FACE); |
| 160 |
} |
| 161 |
if (m_cullface != TCullFace::NONE && rc.activeCullFace != m_cullface) |
| 162 |
{ |
| 163 |
glEnable(GL_CULL_FACE); |
| 164 |
glCullFace(m_cullface == TCullFace::FRONT ? GL_FRONT : GL_BACK); |
| 165 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 166 |
rc.activeCullFace = m_cullface; |
| 167 |
} |
| 168 |
|
| 169 |
glDrawArrays(GL_TRIANGLES, 0, 3 * shaderTrianglesBuffer().size()); |
| 170 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 171 |
|
| 172 |
if (attr_position) glDisableVertexAttribArray(*attr_position); |
| 173 |
if (attr_color) glDisableVertexAttribArray(*attr_color); |
| 174 |
if (attr_normals) glDisableVertexAttribArray(*attr_normals); |
| 175 |
|
| 176 |
#endif |
| 177 |
} |
| 178 |
|
| 179 |
const math::TBoundingBoxf CRenderizableShaderTriangles::trianglesBoundingBox() const |
| 180 |
{ |
| 181 |
mrpt::math::TBoundingBoxf bb; |
| 182 |
|
| 183 |
std::shared_lock<std::shared_mutex> trisReadLock( |
| 184 |
CRenderizableShaderTriangles::m_trianglesMtx.data); |
| 185 |
|
| 186 |
if (shaderTrianglesBuffer().empty()) return bb; |
| 187 |
|
| 188 |
bb = mrpt::math::TBoundingBoxf::PlusMinusInfinity(); |
| 189 |
|
| 190 |
for (const auto& t : shaderTrianglesBuffer()) |
| 191 |
for (int i = 0; i < 3; i++) bb.updateWithPoint(t.vertices[i].xyzrgba.pt); |
| 192 |
|
| 193 |
return bb; |
| 194 |
} |
| 195 |
|
| 196 |
void CRenderizableShaderTriangles::params_serialize(mrpt::serialization::CArchive& out) const |
| 197 |
{ |
| 198 |
out.WriteAs<uint8_t>(0); // serialization version |
| 199 |
out << m_enableLight << static_cast<uint8_t>(m_cullface); |
| 200 |
} |
| 201 |
void CRenderizableShaderTriangles::params_deserialize(mrpt::serialization::CArchive& in) |
| 202 |
{ |
| 203 |
const auto version = in.ReadAs<uint8_t>(); |
| 204 |
|
| 205 |
switch (version) |
| 206 |
{ |
| 207 |
case 0: |
| 208 |
in >> m_enableLight; |
| 209 |
m_cullface = static_cast<TCullFace>(in.ReadAs<uint8_t>()); |
| 210 |
break; |
| 211 |
default: |
| 212 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 213 |
}; |
| 214 |
} |