| 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/DefaultShaders.h> |
| 13 |
#include <mrpt/opengl/opengl_api.h> |
| 14 |
|
| 15 |
#include <regex> |
| 16 |
|
| 17 |
using namespace mrpt::opengl; |
| 18 |
|
| 19 |
Program::Ptr mrpt::opengl::LoadDefaultShader(const shader_id_t id) |
| 20 |
{ |
| 21 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 22 |
|
| 23 |
// Vertex shader: |
| 24 |
const char* vertex_shader = nullptr; |
| 25 |
const char* fragment_shader = nullptr; |
| 26 |
const char* fragShaderIncludes = nullptr; |
| 27 |
std::vector<std::string> attribs, uniforms; |
| 28 |
|
| 29 |
switch (id) |
| 30 |
{ |
| 31 |
// ============================== |
| 32 |
// Regular geometric elements |
| 33 |
// ============================== |
| 34 |
case DefaultShaderID::POINTS: |
| 35 |
vertex_shader = |
| 36 |
#include "../shaders/points.v.glsl" |
| 37 |
; |
| 38 |
fragment_shader = |
| 39 |
#include "../shaders/points.f.glsl" |
| 40 |
; |
| 41 |
uniforms = { |
| 42 |
"p_matrix", |
| 43 |
"mv_matrix", |
| 44 |
"vertexPointSize", |
| 45 |
"enableVariablePointSize", |
| 46 |
"variablePointSize_K", |
| 47 |
"variablePointSize_DepthScale"}; |
| 48 |
attribs = {"position", "vertexColor"}; |
| 49 |
break; |
| 50 |
|
| 51 |
case DefaultShaderID::WIREFRAME: |
| 52 |
vertex_shader = |
| 53 |
#include "../shaders/wireframe.v.glsl" |
| 54 |
; |
| 55 |
fragment_shader = |
| 56 |
#include "../shaders/wireframe.f.glsl" |
| 57 |
; |
| 58 |
uniforms = {"p_matrix", "mv_matrix"}; |
| 59 |
attribs = {"position", "vertexColor"}; |
| 60 |
break; |
| 61 |
|
| 62 |
case DefaultShaderID::TRIANGLES_LIGHT: |
| 63 |
vertex_shader = |
| 64 |
#include "../shaders/triangles-light.v.glsl" |
| 65 |
; |
| 66 |
fragment_shader = |
| 67 |
#include "../shaders/triangles-light.f.glsl" |
| 68 |
; |
| 69 |
uniforms = {"p_matrix", "v_matrix", "m_matrix", "light_diffuse", |
| 70 |
"light_ambient", "light_specular", "light_color", "light_direction", |
| 71 |
"cam_position", "materialSpecular"}; |
| 72 |
attribs = {"position", "vertexColor", "vertexNormal"}; |
| 73 |
break; |
| 74 |
|
| 75 |
case DefaultShaderID::TRIANGLES_NO_LIGHT: |
| 76 |
vertex_shader = |
| 77 |
#include "../shaders/triangles-no-light.v.glsl" |
| 78 |
; |
| 79 |
fragment_shader = |
| 80 |
#include "../shaders/triangles-no-light.f.glsl" |
| 81 |
; |
| 82 |
uniforms = {"pmv_matrix"}; |
| 83 |
attribs = {"position", "vertexColor"}; |
| 84 |
break; |
| 85 |
|
| 86 |
case DefaultShaderID::TEXTURED_TRIANGLES_LIGHT: |
| 87 |
vertex_shader = |
| 88 |
#include "../shaders/textured-triangles-light.v.glsl" |
| 89 |
; |
| 90 |
fragment_shader = |
| 91 |
#include "../shaders/textured-triangles-light.f.glsl" |
| 92 |
; |
| 93 |
uniforms = {"p_matrix", "v_matrix", "m_matrix", "light_diffuse", |
| 94 |
"light_ambient", "light_specular", "light_color", "cam_position", |
| 95 |
"light_direction", "textureSampler", "materialSpecular"}; |
| 96 |
attribs = {"position", "vertexUV", "vertexNormal"}; |
| 97 |
break; |
| 98 |
|
| 99 |
case DefaultShaderID::TEXTURED_TRIANGLES_NO_LIGHT: |
| 100 |
vertex_shader = |
| 101 |
#include "../shaders/textured-triangles-no-light.v.glsl" |
| 102 |
; |
| 103 |
fragment_shader = |
| 104 |
#include "../shaders/textured-triangles-no-light.f.glsl" |
| 105 |
; |
| 106 |
uniforms = {"pmv_matrix", "textureSampler"}; |
| 107 |
attribs = {"position", "vertexUV"}; |
| 108 |
break; |
| 109 |
|
| 110 |
case DefaultShaderID::TEXT: |
| 111 |
vertex_shader = |
| 112 |
#include "../shaders/text.v.glsl" |
| 113 |
; |
| 114 |
fragment_shader = |
| 115 |
#include "../shaders/text.f.glsl" |
| 116 |
; |
| 117 |
uniforms = {"p_matrix", "mv_matrix"}; |
| 118 |
attribs = {"position", "vertexColor"}; |
| 119 |
break; |
| 120 |
|
| 121 |
// =========================================== |
| 122 |
// Shadow generation 1st/2nd pass shaders |
| 123 |
// =========================================== |
| 124 |
case DefaultShaderID::TRIANGLES_SHADOW_1ST: |
| 125 |
vertex_shader = |
| 126 |
#include "../shaders/triangles-shadow-1st.v.glsl" |
| 127 |
; |
| 128 |
fragment_shader = |
| 129 |
#include "../shaders/triangles-shadow-1st.f.glsl" |
| 130 |
; |
| 131 |
uniforms = {"m_matrix", "light_pv_matrix"}; |
| 132 |
attribs = {"position"}; |
| 133 |
break; |
| 134 |
|
| 135 |
case DefaultShaderID::TRIANGLES_SHADOW_2ND: |
| 136 |
vertex_shader = |
| 137 |
#include "../shaders/triangles-shadow-2nd.v.glsl" |
| 138 |
; |
| 139 |
fragment_shader = |
| 140 |
#include "../shaders/triangles-shadow-2nd.f.glsl" |
| 141 |
; |
| 142 |
fragShaderIncludes = |
| 143 |
#include "../shaders/shadow-calculation.f.glsl" |
| 144 |
; |
| 145 |
uniforms = { |
| 146 |
"p_matrix", |
| 147 |
"v_matrix", |
| 148 |
"m_matrix", |
| 149 |
"light_pv_matrix", |
| 150 |
"shadowMap", |
| 151 |
"light_diffuse", |
| 152 |
"light_ambient", |
| 153 |
"light_specular", |
| 154 |
"light_direction", |
| 155 |
"shadow_bias", |
| 156 |
"shadow_bias_cam2frag", |
| 157 |
"shadow_bias_normal", |
| 158 |
"light_color", |
| 159 |
"cam_position", |
| 160 |
"materialSpecular"}; |
| 161 |
attribs = {"position", "vertexColor", "vertexNormal"}; |
| 162 |
break; |
| 163 |
|
| 164 |
// 1st stage is the same for textured and non-textured triangles: |
| 165 |
case DefaultShaderID::TEXTURED_TRIANGLES_SHADOW_1ST: |
| 166 |
vertex_shader = |
| 167 |
#include "../shaders/triangles-shadow-1st.v.glsl" |
| 168 |
; |
| 169 |
fragment_shader = |
| 170 |
#include "../shaders/triangles-shadow-1st.f.glsl" |
| 171 |
; |
| 172 |
uniforms = {"m_matrix", "light_pv_matrix"}; |
| 173 |
attribs = {"position"}; |
| 174 |
break; |
| 175 |
|
| 176 |
case DefaultShaderID::TEXTURED_TRIANGLES_SHADOW_2ND: |
| 177 |
vertex_shader = |
| 178 |
#include "../shaders/textured-triangles-shadow-2nd.v.glsl" |
| 179 |
; |
| 180 |
fragment_shader = |
| 181 |
#include "../shaders/textured-triangles-shadow-2nd.f.glsl" |
| 182 |
; |
| 183 |
fragShaderIncludes = |
| 184 |
#include "../shaders/shadow-calculation.f.glsl" |
| 185 |
; |
| 186 |
uniforms = { |
| 187 |
"p_matrix", |
| 188 |
"v_matrix", |
| 189 |
"m_matrix", |
| 190 |
"light_pv_matrix", |
| 191 |
"shadowMap", |
| 192 |
"light_diffuse", |
| 193 |
"light_ambient", |
| 194 |
"light_specular", |
| 195 |
"light_direction", |
| 196 |
"shadow_bias", |
| 197 |
"shadow_bias_cam2frag", |
| 198 |
"shadow_bias_normal", |
| 199 |
"light_color", |
| 200 |
"cam_position", |
| 201 |
"materialSpecular", |
| 202 |
"textureSampler"}; |
| 203 |
attribs = {"position", "vertexNormal", "vertexUV"}; |
| 204 |
break; |
| 205 |
|
| 206 |
// =========================================== |
| 207 |
// Special effects |
| 208 |
// =========================================== |
| 209 |
case DefaultShaderID::SKYBOX: |
| 210 |
vertex_shader = |
| 211 |
#include "../shaders/skybox.v.glsl" |
| 212 |
; |
| 213 |
fragment_shader = |
| 214 |
#include "../shaders/skybox.f.glsl" |
| 215 |
; |
| 216 |
uniforms = {"p_matrix", "v_matrix_no_translation", "skybox"}; |
| 217 |
attribs = {"position"}; |
| 218 |
break; |
| 219 |
|
| 220 |
case DefaultShaderID::DEBUG_TEXTURE_TO_SCREEN: |
| 221 |
vertex_shader = |
| 222 |
#include "../shaders/debug_show_texture.v.glsl" |
| 223 |
; |
| 224 |
fragment_shader = |
| 225 |
#include "../shaders/debug_show_texture.f.glsl" |
| 226 |
; |
| 227 |
uniforms = {"textureId"}; |
| 228 |
attribs = {"position", "vertexUV"}; |
| 229 |
break; |
| 230 |
|
| 231 |
default: |
| 232 |
THROW_EXCEPTION_FMT("Unknown shader_id_t=%u", static_cast<unsigned>(id)); |
| 233 |
}; |
| 234 |
|
| 235 |
// Init GLEW if not already done: |
| 236 |
#ifdef _WIN32 |
| 237 |
glewInit(); |
| 238 |
#endif |
| 239 |
|
| 240 |
#if defined(__EMSCRIPTEN__) |
| 241 |
// in emscripten + GLES3 we need to insert this line after version: |
| 242 |
std::string sOrgV = vertex_shader; |
| 243 |
sOrgV = std::regex_replace( |
| 244 |
sOrgV, std::regex("#version 300 es"), "#version 300 es\r\nprecision mediump float;\r\n"); |
| 245 |
|
| 246 |
vertex_shader = sOrgV.c_str(); |
| 247 |
|
| 248 |
std::string sOrgF = fragment_shader; |
| 249 |
sOrgF = std::regex_replace( |
| 250 |
sOrgF, std::regex("#version 300 es"), "#version 300 es\r\nprecision mediump float;\r\n"); |
| 251 |
|
| 252 |
fragment_shader = sOrgF.c_str(); |
| 253 |
|
| 254 |
#endif |
| 255 |
|
| 256 |
auto shader = std::make_shared<Program>(); |
| 257 |
|
| 258 |
std::string errMsgs; |
| 259 |
std::vector<Shader> lstShaders; |
| 260 |
lstShaders.resize(2); |
| 261 |
if (!lstShaders[0].compile(GL_VERTEX_SHADER, {vertex_shader}, errMsgs)) |
| 262 |
{ |
| 263 |
THROW_EXCEPTION_FMT( |
| 264 |
"Error compiling GL_VERTEX_SHADER (%s):\n%s", vertex_shader, errMsgs.c_str()); |
| 265 |
} |
| 266 |
if (!lstShaders[1].compile( |
| 267 |
GL_FRAGMENT_SHADER, {fragShaderIncludes ? fragShaderIncludes : "", fragment_shader}, |
| 268 |
errMsgs)) |
| 269 |
{ |
| 270 |
THROW_EXCEPTION_FMT( |
| 271 |
"Error compiling GL_FRAGMENT_SHADER (%s):\n%s", fragment_shader, errMsgs.c_str()); |
| 272 |
} |
| 273 |
if (!shader->linkProgram(lstShaders, errMsgs)) |
| 274 |
{ |
| 275 |
THROW_EXCEPTION_FMT( |
| 276 |
"Error linking Opengl Shader programs (%s, %s):\n%s", vertex_shader, fragment_shader, |
| 277 |
errMsgs.c_str()); |
| 278 |
} |
| 279 |
|
| 280 |
#if 0 |
| 281 |
// Debug: |
| 282 |
std::cout << "Built Shader program #" << int(id) << "\n"; |
| 283 |
shader->dumpProgramDescription(std::cout); |
| 284 |
std::cout << "\n"; |
| 285 |
#endif |
| 286 |
|
| 287 |
// Uniforms: |
| 288 |
for (const auto& name : uniforms) |
| 289 |
{ |
| 290 |
try |
| 291 |
{ |
| 292 |
shader->declareUniform(name); |
| 293 |
} |
| 294 |
catch (const std::exception& e) |
| 295 |
{ |
| 296 |
std::cerr << "Exception while declaring Uniform of shader ID #" << static_cast<int>(id) |
| 297 |
<< std::endl; |
| 298 |
throw; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
// Attributes: |
| 303 |
for (const auto& name : attribs) shader->declareAttribute(name); |
| 304 |
|
| 305 |
return shader; |
| 306 |
#else |
| 307 |
THROW_EXCEPTION("MRPT built without OpenGL support."); |
| 308 |
#endif |
| 309 |
} |