MRPT  1.9.9
DefaultShaders.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, 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 
13 #include <mrpt/opengl/opengl_api.h>
14 
15 using namespace mrpt::opengl;
16 
17 // TODO: May be, allow users to register custom shaders?
18 
20 {
21 #if MRPT_HAS_OPENGL_GLUT
22 
23 #if defined(MRPT_OS_LINUX)
24  // Workaround to enfore wxWidgets to use GLSL>=3.3 even for wxWidgets<3.0.4
25  // See CWxGLCanvasBase::CWxGLCanvasBase.
26  if (!::getenv("MESA_GL_VERSION_OVERRIDE"))
27  {
28  ::putenv("MESA_GL_VERSION_OVERRIDE=3.3");
29  }
30 #endif
31 
32  // Vertex shader:
33  const char* vertex_shader = nullptr;
34  const char* fragment_shader = nullptr;
35  std::vector<std::string> attribs, uniforms;
36 
37  switch (id)
38  {
40  vertex_shader =
41 #include "../shaders/points.v.glsl"
42  ;
43  fragment_shader =
44 #include "../shaders/points.f.glsl"
45  ;
46  uniforms = {"p_matrix",
47  "mv_matrix",
48  "vertexPointSize",
49  "enableVariablePointSize",
50  "variablePointSize_K",
51  "variablePointSize_DepthScale"};
52  attribs = {"position", "vertexColor"};
53  break;
54 
55  // ==============================
57  vertex_shader =
58 #include "../shaders/wireframe.v.glsl"
59  ;
60  fragment_shader =
61 #include "../shaders/wireframe.f.glsl"
62  ;
63  uniforms = {"p_matrix", "mv_matrix"};
64  attribs = {"position", "vertexColor"};
65  break;
66 
67  // ==============================
69  vertex_shader =
70 #include "../shaders/triangles.v.glsl"
71  ;
72  fragment_shader =
73 #include "../shaders/triangles.f.glsl"
74  ;
75  uniforms = {"p_matrix", "mv_matrix", "light_diffuse",
76  "light_ambient", "light_specular", "light_direction"};
77  attribs = {"position", "vertexColor", "vertexNormal"};
78  break;
79  // ==============================
81  vertex_shader =
82 #include "../shaders/textured-triangles.v.glsl"
83  ;
84  fragment_shader =
85 #include "../shaders/textured-triangles.f.glsl"
86  ;
87  uniforms = {"p_matrix", "mv_matrix", "pmv_matrix",
88  "light_diffuse", "light_ambient", "light_specular",
89  "light_direction", "textureSampler", "enableLight"};
90  attribs = {"position", "vertexUV", "vertexNormal"};
91  break;
92  // ==============================
94  vertex_shader =
95 #include "../shaders/text.v.glsl"
96  ;
97  fragment_shader =
98 #include "../shaders/text.f.glsl"
99  ;
100  uniforms = {"p_matrix", "mv_matrix"};
101  attribs = {"position", "vertexColor"};
102  break;
103 
104  default:
106  "Unknown shader_id_t=%u", static_cast<unsigned>(id));
107  };
108 
109  // Init GLEW if not already done:
110 #ifdef _WIN32
111  glewInit();
112 #endif
113 
114  auto shader = std::make_shared<Program>();
115 
116  std::string errMsgs;
117  std::vector<Shader> lstShaders;
118  lstShaders.resize(2);
119  if (!lstShaders[0].compile(GL_VERTEX_SHADER, vertex_shader, errMsgs))
120  {
122  "Error compiling GL_VERTEX_SHADER:\n%s", errMsgs.c_str());
123  }
124  if (!lstShaders[1].compile(GL_FRAGMENT_SHADER, fragment_shader, errMsgs))
125  {
127  "Error compiling GL_FRAGMENT_SHADER:\n%s", errMsgs.c_str());
128  }
129  if (!shader->linkProgram(lstShaders, errMsgs))
130  {
132  "Error linking Opengl Shader programs:\n%s", errMsgs.c_str());
133  }
134 
135 #if 0
136  // Debug:
137  std::cout << "Built Shader program #" << int(id) << "\n";
138  shader->dumpProgramDescription(std::cout);
139  std::cout << "\n";
140 #endif
141 
142  // Uniforms:
143  for (const auto& name : uniforms) shader->declareUniform(name);
144 
145  // Attributes:
146  for (const auto& name : attribs) shader->declareAttribute(name);
147 
148  return shader;
149 #else
150  THROW_EXCEPTION("MRPT built without OpenGL support.");
151 #endif
152 }
static constexpr shader_id_t TEXTURED_TRIANGLES
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
static constexpr shader_id_t TEXT
static constexpr shader_id_t WIREFRAME
uint8_t shader_id_t
Type for IDs of shaders.
Definition: Shader.h:23
static constexpr shader_id_t TRIANGLES
Program::Ptr LoadDefaultShader(const shader_id_t id)
Loads a set of OpenGL Vertex+Fragment shaders from the default library available in mrpt::opengl...
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
static constexpr shader_id_t POINTS



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020