30 m_shader = o.m_shader;
36 if (!m_shader)
return;
37 #if MRPT_HAS_OPENGL_GLUT 38 glDeleteShader(m_shader);
44 unsigned int type,
const std::string& shaderCode,
47 #if MRPT_HAS_OPENGL_GLUT 50 m_shader = glCreateShader(static_cast<GLenum>(type));
52 const GLchar* source = shaderCode.c_str();
53 const GLint length = shaderCode.size();
55 glShaderSource(m_shader, 1, &source, &length);
56 glCompileShader(m_shader);
59 glGetShaderiv(m_shader, GL_COMPILE_STATUS, &shader_ok);
64 glGetShaderiv(m_shader, GL_INFO_LOG_LENGTH, &log_length);
65 log.resize(log_length);
66 glGetShaderInfoLog(m_shader, log_length, NULL, &log[0]);
69 outErrorMessages.value().get() = std::move(log);
71 std::cerr <<
"[Shader::compile] Compile error: " << log <<
"\n";
73 glDeleteShader(m_shader);
85 Program::~Program() {
clear(); }
89 if (!m_program)
return;
91 #if MRPT_HAS_OPENGL_GLUT 93 for (
const Shader& s : m_shaders) glDetachShader(m_program, s.handle());
96 glDeleteProgram(m_program);
109 bool Program::linkProgram(
110 std::vector<Shader>& shaders,
113 #if MRPT_HAS_OPENGL_GLUT 116 m_program = glCreateProgram();
120 m_shaders = std::move(shaders);
122 for (
const auto& shader : m_shaders)
123 glAttachShader(m_program, shader.handle());
125 glLinkProgram(m_program);
126 CHECK_OPENGL_ERROR();
129 glGetProgramiv(m_program, GL_LINK_STATUS, &program_ok);
134 glGetProgramiv(m_program, GL_INFO_LOG_LENGTH, &log_length);
135 log.resize(log_length);
136 glGetProgramInfoLog(m_program, log_length, NULL, &log[0]);
138 if (outErrorMessages)
139 outErrorMessages.value().get() = std::move(log);
141 std::cerr <<
"[Program::linkProgram] Link error: " << log <<
"\n";
152 void Program::declareUniform(
const std::string& name)
154 #if MRPT_HAS_OPENGL_GLUT 157 if (m_uniforms.count(name) != 0)
159 "declareUniform: Name `%s` already registered", name.c_str());
161 const auto ret = glGetUniformLocation(
162 m_program, static_cast<const GLchar*>(name.c_str()));
165 "declareUniform: glGetUniformLocation() returned error for `%s`",
167 m_uniforms[name] = ret;
172 void Program::declareAttribute(
const std::string& name)
174 #if MRPT_HAS_OPENGL_GLUT 177 if (m_attribs.count(name) != 0)
179 "declareAttribute: Name `%s` already registered", name.c_str());
181 const auto ret = glGetAttribLocation(
182 m_program, static_cast<const GLchar*>(name.c_str()));
185 "declareAttribute: glGetAttribLocation() returned error for `%s`",
187 m_attribs[name] = ret;
194 void Program::dumpProgramDescription(std::ostream& o)
const 196 #if MRPT_HAS_OPENGL_GLUT 204 const GLsizei bufSize = 32;
205 GLchar name[bufSize];
209 glGetProgramiv(m_program, GL_ACTIVE_ATTRIBUTES, &count);
212 for (GLint i = 0; i < count; i++)
215 m_program, (GLuint)i, bufSize, &length, &
size, &type, name);
217 o <<
mrpt::format(
"Attribute #%d Type: %u Name: %s\n", i, type, name);
221 glGetProgramiv(m_program, GL_ACTIVE_UNIFORMS, &count);
222 printf(
"Active Uniforms: %d\n", count);
224 for (GLint i = 0; i < count; i++)
227 m_program, (GLuint)i, bufSize, &length, &
size, &type, name);
229 o <<
mrpt::format(
"Uniform #%d Type: %u Name: %s\n", i, type, name);
#define THROW_EXCEPTION(msg)
std::string std::string format(std::string_view fmt, ARGS &&... args)
size_t size(const MATRIXLIKE &m, const int dim)
std::optional< std::reference_wrapper< T > > optional_ref
Shorter name for std::optional<std::reference_wrapper<T>>
#define ASSERT_(f)
Defines an assertion mechanism.
A resource handling helper for OpenGL "Shader" compiled code fragment.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
The namespace for 3D scene representation and rendering.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
void clear()
Clear the contents of this container.