MRPT  2.0.4
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 
16 using namespace mrpt::opengl;
17 
18 // TODO: May be, allow users to register custom shaders?
19 
21 {
22 #if MRPT_HAS_OPENGL_GLUT
23 
24 #if defined(MRPT_OS_LINUX)
25  // Workaround to enfore wxWidgets to use GLSL>=3.3 even for wxWidgets<3.0.4
26  // See CWxGLCanvasBase::CWxGLCanvasBase.
27  if (!::getenv("MESA_GL_VERSION_OVERRIDE"))
28  {
29  std::string s = "MESA_GL_VERSION_OVERRIDE=3.3";
30  ::putenv(s.data());
31  }
32 #endif
33 
34  // Vertex shader:
35  const char* vertex_shader = nullptr;
36  const char* fragment_shader = nullptr;
37  std::vector<std::string> attribs, uniforms;
38 
39  switch (id)
40  {
42  vertex_shader =
43 #include "../shaders/points.v.glsl"
44  ;
45  fragment_shader =
46 #include "../shaders/points.f.glsl"
47  ;
48  uniforms = {
49  "p_matrix",
50  "mv_matrix",
51  "vertexPointSize",
52  "enableVariablePointSize",
53  "variablePointSize_K",
54  "variablePointSize_DepthScale"};
55  attribs = {"position", "vertexColor"};
56  break;
57 
58  // ==============================
60  vertex_shader =
61 #include "../shaders/wireframe.v.glsl"
62  ;
63  fragment_shader =
64 #include "../shaders/wireframe.f.glsl"
65  ;
66  uniforms = {"p_matrix", "mv_matrix"};
67  attribs = {"position", "vertexColor"};
68  break;
69 
70  // ==============================
72  vertex_shader =
73 #include "../shaders/triangles.v.glsl"
74  ;
75  fragment_shader =
76 #include "../shaders/triangles.f.glsl"
77  ;
78  uniforms = {"p_matrix", "mv_matrix", "light_diffuse",
79  "light_ambient", "light_specular", "light_direction"};
80  attribs = {"position", "vertexColor", "vertexNormal"};
81  break;
82  // ==============================
84  vertex_shader =
85 #include "../shaders/textured-triangles.v.glsl"
86  ;
87  fragment_shader =
88 #include "../shaders/textured-triangles.f.glsl"
89  ;
90  uniforms = {"p_matrix", "mv_matrix", "pmv_matrix",
91  "light_diffuse", "light_ambient", "light_specular",
92  "light_direction", "textureSampler", "enableLight"};
93  attribs = {"position", "vertexUV", "vertexNormal"};
94  break;
95  // ==============================
97  vertex_shader =
98 #include "../shaders/text.v.glsl"
99  ;
100  fragment_shader =
101 #include "../shaders/text.f.glsl"
102  ;
103  uniforms = {"p_matrix", "mv_matrix"};
104  attribs = {"position", "vertexColor"};
105  break;
106 
107  default:
109  "Unknown shader_id_t=%u", static_cast<unsigned>(id));
110  };
111 
112  // Init GLEW if not already done:
113 #ifdef _WIN32
114  glewInit();
115 #endif
116 
117  auto shader = std::make_shared<Program>();
118 
119  std::string errMsgs;
120  std::vector<Shader> lstShaders;
121  lstShaders.resize(2);
122  if (!lstShaders[0].compile(GL_VERTEX_SHADER, vertex_shader, errMsgs))
123  {
125  "Error compiling GL_VERTEX_SHADER:\n%s", errMsgs.c_str());
126  }
127  if (!lstShaders[1].compile(GL_FRAGMENT_SHADER, fragment_shader, errMsgs))
128  {
130  "Error compiling GL_FRAGMENT_SHADER:\n%s", errMsgs.c_str());
131  }
132  if (!shader->linkProgram(lstShaders, errMsgs))
133  {
135  "Error linking Opengl Shader programs:\n%s", errMsgs.c_str());
136  }
137 
138 #if 0
139  // Debug:
140  std::cout << "Built Shader program #" << int(id) << "\n";
141  shader->dumpProgramDescription(std::cout);
142  std::cout << "\n";
143 #endif
144 
145  // Uniforms:
146  for (const auto& name : uniforms) shader->declareUniform(name);
147 
148  // Attributes:
149  for (const auto& name : attribs) shader->declareAttribute(name);
150 
151  return shader;
152 #else
153  THROW_EXCEPTION("MRPT built without OpenGL support.");
154 #endif
155 }
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 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020