Skip to main content

OpenGLShader Class

An OpenGL-specific implementation of a Shader program, consisting of a vertex and a fragment shader. More...

Declaration

class helios::ext::opengl::rendering::shader::OpenGLShader { ... }

Public Constructors Index

OpenGLShader (const std::string &vertexShaderPath, const std::string &fragmentShaderPath, const helios::util::io::StringFileReader &stringFileReader)

Creates and initializes this OpenGLShader. An instance of this class is guaranteed to have a progId_ != 0, hence shader-files where successfully loaded and compiled, ready to be used. More...

Public Destructor Index

~OpenGLShader ()

Deletes the program object upon destruction of this instance. More...

Public Member Functions Index

voiduse () const noexcept

Activates this OpenGLShader for subsequent draw calls. This implementation calls `glUseProgram` with the `progId_` received after compilation. More...

voidsetUniformLocationMap (const OpenGLUniformLocationMap &uniformLocationMap) noexcept

Sets the OpenGLUniformLocationMap for this OpenGLShader. Ownership is transferred to this instance. More...

intuniformLocation (helios::rendering::shader::UniformSemantics uniformSemantics) const noexcept

Returns the uniform location for the uniform represented by the specified UniformSemantics. More...

voidapplyUniformValues (const helios::rendering::shader::UniformValueMap &uniformValueMap) const noexcept

Applies the specified UniformValueMap to the uniforms defined by the shader. This method does not change the binding state of the shader, but it passes the uniform values to the underlying rendering backend. Implementations should ensure that the shader is properly bound before this method is called. More...

Private Member Functions Index

voidload (const std::string &vertexShaderPath, const std::string &fragmentShaderPath, const helios::util::io::StringFileReader &stringFileReader)

Loads the specified vertex and fragment shader. More...

voidcompile ()

Compiles the vertex and fragment shader represented by this instance. More...

Protected Member Attributes Index

unsigned intprogId_ = 0

The program id as assigned by the underlying rendering backend. More...

std::optional< OpenGLUniformLocationMap >uniformLocationMap_

The OpenGLUniformLocationMap this shader uses. More...

Private Member Attributes Index

std::stringvertexShaderSource_

Source of the shader. Not guaranteed to be persisted once compilation was successful. More...

std::stringfragmentShaderSource_

Source of the shader. Not guaranteed to be persisted once compilation was successful. More...

Description

An OpenGL-specific implementation of a Shader program, consisting of a vertex and a fragment shader.

This class manages the lifecycle of the Shaders. Source files are getting loaded via a StringFileReader and immediately compiled after loading. Any occupied memory for source-files and file-paths to the shader is being cleared once compilation succeeded and are not guaranteed to persist the compilation process.

Definition at line 36 of file OpenGLShader.ixx.

Public Constructors

OpenGLShader()

helios::ext::opengl::rendering::shader::OpenGLShader::OpenGLShader (const std::string & vertexShaderPath, const std::string & fragmentShaderPath, const helios::util::io::StringFileReader & stringFileReader)
inline

Creates and initializes this OpenGLShader. An instance of this class is guaranteed to have a progId_ != 0, hence shader-files where successfully loaded and compiled, ready to be used.

Parameters
vertexShaderPath

The path to the vertex shader.

fragmentShaderPath

The path to the fragment shader.

stringFileReader

The StringFileReader used for loading the shader source files.

Exceptions
if

creating this shader failed.

Definition at line 160 of file OpenGLShader.ixx.

161 const std::string& vertexShaderPath,
162 const std::string& fragmentShaderPath,
163 const helios::util::io::StringFileReader& stringFileReader
164 ) {
165 try {
166 load(vertexShaderPath, fragmentShaderPath, stringFileReader);
167 compile();
168 } catch (std::runtime_error& e) {
169 throw std::runtime_error("Could not initialize shader");
170 }
171 }

Public Destructor

~OpenGLShader()

helios::ext::opengl::rendering::shader::OpenGLShader::~OpenGLShader ()
inline

Deletes the program object upon destruction of this instance.

See Also

https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDeleteProgram.xhtml

Definition at line 191 of file OpenGLShader.ixx.

192 if (progId_ != 0) {
193 glDeleteProgram(progId_);
194 }
195 }

Reference progId_.

Public Member Functions

applyUniformValues()

void helios::ext::opengl::rendering::shader::OpenGLShader::applyUniformValues (const helios::rendering::shader::UniformValueMap & uniformValueMap)
inline noexcept

Applies the specified UniformValueMap to the uniforms defined by the shader. This method does not change the binding state of the shader, but it passes the uniform values to the underlying rendering backend. Implementations should ensure that the shader is properly bound before this method is called.

Parameters
uniformValueMap

A const ref to the `UniformValueMap` containing the values for the uniforms in this shader.

Definition at line 229 of file OpenGLShader.ixx.

230 const helios::rendering::shader::UniformValueMap& uniformValueMap) const noexcept {
231
232 if (const auto viewMatrixUniform = uniformLocation(helios::rendering::shader::UniformSemantics::ViewMatrix); viewMatrixUniform != -1) {
233 if (const auto* mat4f_ptr = uniformValueMap.mat4f_ptr(helios::rendering::shader::UniformSemantics::ViewMatrix)) {
234 glUniformMatrix4fv(viewMatrixUniform, 1, false, mat4f_ptr);
235 }
236 }
237
238 if (const auto projectionMatrixUniform = uniformLocation(helios::rendering::shader::UniformSemantics::ProjectionMatrix); projectionMatrixUniform != -1) {
239 if (const auto* mat4f_ptr = uniformValueMap.mat4f_ptr(helios::rendering::shader::UniformSemantics::ProjectionMatrix)) {
240 glUniformMatrix4fv(projectionMatrixUniform, 1, false, mat4f_ptr);
241 }
242 }
243
244 if (const auto modelMatrixUniform = uniformLocation(helios::rendering::shader::UniformSemantics::ModelMatrix); modelMatrixUniform != -1) {
245 if (const auto* mat4f_ptr = uniformValueMap.mat4f_ptr(helios::rendering::shader::UniformSemantics::ModelMatrix)) {
246 glUniformMatrix4fv(modelMatrixUniform, 1, false, mat4f_ptr);
247 }
248 }
249 if (const auto materialBaseColorUniform = uniformLocation(helios::rendering::shader::UniformSemantics::MaterialBaseColor); materialBaseColorUniform != -1) {
250 if (const auto* vec4f_ptr = uniformValueMap.vec4f_ptr(helios::rendering::shader::UniformSemantics::MaterialBaseColor)) {
251 glUniform4fv(materialBaseColorUniform, 1, vec4f_ptr);
252 }
253 }
254
255 // texture
256 if (const auto textColorUniform = uniformLocation(helios::rendering::shader::UniformSemantics::TextColor); textColorUniform != -1) {
257 if (const auto* vec4f_ptr = uniformValueMap.vec4f_ptr(helios::rendering::shader::UniformSemantics::TextColor)) {
258 glUniform4fv(textColorUniform, 1, vec4f_ptr);
259 }
260 }
261 if (const auto textTextureUniform = uniformLocation(helios::rendering::shader::UniformSemantics::TextTexture); textTextureUniform != -1) {
262 if (const auto* int_ptr = uniformValueMap.int_ptr(helios::rendering::shader::UniformSemantics::TextTexture)) {
263 glUniform1i(textTextureUniform, *int_ptr);
264 }
265 }
266 }

References helios::rendering::shader::MaterialBaseColor, helios::rendering::shader::ModelMatrix, helios::rendering::shader::ProjectionMatrix, helios::rendering::shader::TextColor, helios::rendering::shader::TextTexture, uniformLocation and helios::rendering::shader::ViewMatrix.

setUniformLocationMap()

void helios::ext::opengl::rendering::shader::OpenGLShader::setUniformLocationMap (const OpenGLUniformLocationMap & uniformLocationMap)
inline noexcept

Sets the OpenGLUniformLocationMap for this OpenGLShader. Ownership is transferred to this instance.

Parameters
uniformLocationMap

The OpenGLUniformMap providing the mappings for the uniforms of the underlying GLSL shader.

Definition at line 204 of file OpenGLShader.ixx.

204 void setUniformLocationMap(const OpenGLUniformLocationMap& uniformLocationMap) noexcept {
205 uniformLocationMap_ = uniformLocationMap;
206 }

Reference uniformLocationMap_.

uniformLocation()

int helios::ext::opengl::rendering::shader::OpenGLShader::uniformLocation (helios::rendering::shader::UniformSemantics uniformSemantics)
inline nodiscard noexcept

Returns the uniform location for the uniform represented by the specified UniformSemantics.

Parameters
uniformSemantics

The `UniformSemantics` identifier.

Returns

The integer representing the uniform variable location in the shader, or -1 if no location map was registered with this shader or if the uniform with the specified semantics was not found.

Definition at line 218 of file OpenGLShader.ixx.

218 [[nodiscard]] int uniformLocation(helios::rendering::shader::UniformSemantics uniformSemantics) const noexcept {
220 return uniformLocationMap_->get(uniformSemantics);
221 }
222
223 return -1;
224 }

Reference uniformLocationMap_.

Referenced by applyUniformValues.

use()

void helios::ext::opengl::rendering::shader::OpenGLShader::use ()
inline noexcept

Activates this OpenGLShader for subsequent draw calls. This implementation calls `glUseProgram` with the `progId_` received after compilation.

See Also

https://registry.khronos.org/OpenGL-Refpages/gl4/html/glUseProgram.xhtml

Definition at line 181 of file OpenGLShader.ixx.

181 void use() const noexcept {
182 assert(progId_ != 0 && "Shader program ID must be valid before use.");
183 glUseProgram(progId_);
184 }

Reference progId_.

Private Member Functions

compile()

void helios::ext::opengl::rendering::shader::OpenGLShader::compile ()
inline

Compiles the vertex and fragment shader represented by this instance.

Returns

true if compilation succeeded, otherwise false.

Exceptions
if

compilation failed.

Definition at line 78 of file OpenGLShader.ixx.

78 void compile() {
79 if (progId_ != 0) {
80 return;
81 }
82
83 const GLchar* vertexSrc = vertexShaderSource_.c_str();
84 const GLchar* fragmentSrc = fragmentShaderSource_.c_str();
85
86 const unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
87 glShaderSource(vertexShader, 1, &vertexSrc, nullptr);
88 glCompileShader(vertexShader);
89
90 const unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
91 glShaderSource(fragmentShader, 1, &fragmentSrc, nullptr);
92 glCompileShader(fragmentShader);
93
94
95 int success;
96 char infoLog[512];
97 glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
98
99 if (!success) {
100 glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
101
102 throw std::runtime_error("Vertex Shader Compilation failed.");
103 }
104
105 glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
106 if (!success) {
107 glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
108
109 throw std::runtime_error("Fragment Shader Compilation failed.");
110 }
111
112 progId_ = glCreateProgram();
113
114 glAttachShader(progId_, vertexShader);
115 glAttachShader(progId_, fragmentShader);
116 glLinkProgram(progId_);
117
118 glGetProgramiv(progId_, GL_LINK_STATUS, &success);
119 if (!success) {
120 glGetProgramInfoLog(progId_, 512, nullptr, infoLog);
121
122 throw std::runtime_error("Program linking failed.");
123 }
124
125 glDeleteShader(vertexShader);
126 glDeleteShader(fragmentShader);
127
128 vertexShaderSource_.clear();
129 vertexShaderSource_.shrink_to_fit();
130 fragmentShaderSource_.clear();
131 fragmentShaderSource_.shrink_to_fit();
132
133 }

load()

void helios::ext::opengl::rendering::shader::OpenGLShader::load (const std::string & vertexShaderPath, const std::string & fragmentShaderPath, const helios::util::io::StringFileReader & stringFileReader)
inline

Loads the specified vertex and fragment shader.

Returns

true if loading succeeded, otherwise false.

Exceptions
if

loading the specified files failed.

Definition at line 59 of file OpenGLShader.ixx.

59 void load(
60 const std::string& vertexShaderPath,
61 const std::string& fragmentShaderPath,
62 const helios::util::io::StringFileReader& stringFileReader
63 ) {
64 if (!stringFileReader.readInto(fragmentShaderPath, fragmentShaderSource_) ||
65 !stringFileReader.readInto(vertexShaderPath, vertexShaderSource_)) {
66 throw std::runtime_error("Could not load shader");
67 }
68 }

Protected Member Attributes

progId_

unsigned int helios::ext::opengl::rendering::shader::OpenGLShader::progId_ = 0
protected

The program id as assigned by the underlying rendering backend.

Definition at line 140 of file OpenGLShader.ixx.

140 unsigned int progId_ = 0;

Referenced by use and ~OpenGLShader.

uniformLocationMap_

std::optional<OpenGLUniformLocationMap> helios::ext::opengl::rendering::shader::OpenGLShader::uniformLocationMap_
protected

The OpenGLUniformLocationMap this shader uses.

Definition at line 145 of file OpenGLShader.ixx.

145 std::optional<OpenGLUniformLocationMap> uniformLocationMap_;

Referenced by setUniformLocationMap and uniformLocation.

Private Member Attributes

fragmentShaderSource_

std::string helios::ext::opengl::rendering::shader::OpenGLShader::fragmentShaderSource_

Source of the shader. Not guaranteed to be persisted once compilation was successful.

Definition at line 50 of file OpenGLShader.ixx.

50 std::string fragmentShaderSource_;

vertexShaderSource_

std::string helios::ext::opengl::rendering::shader::OpenGLShader::vertexShaderSource_

Source of the shader. Not guaranteed to be persisted once compilation was successful.

Definition at line 43 of file OpenGLShader.ixx.

43 std::string vertexShaderSource_;

The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.