Raftel Engine
 
Loading...
Searching...
No Matches
Raftel::ShaderProgram Class Reference

A class to represent an OpenGL shader program, combining vertex and fragment shaders. More...

#include <shader.hpp>

Public Member Functions

 ShaderProgram ()=default
 Default constructor for ShaderProgram.
 
 ShaderProgram (const std::string &vertexPath, const std::string &fragmentPath)
 Constructor for ShaderProgram, loading and linking shaders from files.
 
 ~ShaderProgram ()
 Destructor for ShaderProgram.
 
 MOVABLE_BUT_NOT_COPYABLE (ShaderProgram)
 Move constructor for ShaderProgram.
 
bool load (const std::string &vertexPath, const std::string &fragmentPath)
 Loads and links shaders from files.
 
void use ()
 Activates the shader program for use.
 
void unUse ()
 Deactivates the shader program.
 
void setUniform (const std::string &name, int value)
 Sets an integer uniform in the shader program.
 
void setUniform (const std::string &name, float value)
 Sets a float uniform in the shader program.
 
void setUniform (const std::string &name, glm::vec3 value)
 Sets a glm::vec3 uniform in the shader program.
 
void setUniform (const std::string &name, glm::vec4 value)
 Sets a glm::vec4 uniform in the shader program.
 
void setUniform (const std::string &name, const glm::mat4 &matrix)
 Sets a glm::mat4 uniform in the shader program.
 
GLuint GetProgramID () const
 Gets the OpenGL program ID.
 
const std::string & getLastError () const
 Gets the last error message.
 
void attachShader (const Shader &shader)
 Attaches a shader to the shader program.
 
void printActiveUniforms ()
 Prints active uniforms in the shader program.
 
bool loadFromMemory (const char *vertexSource, const char *fragmentSource)
 Loads and compiles vertex and fragment shaders from memory.
 

Detailed Description

A class to represent an OpenGL shader program, combining vertex and fragment shaders.

This class provides functionality to link multiple shaders into a program, use the program, and set uniforms in the program. It supports handling of both vertex and fragment shaders, and provides methods to set uniform values of various types.

Definition at line 119 of file shader.hpp.

Constructor & Destructor Documentation

◆ ShaderProgram()

Raftel::ShaderProgram::ShaderProgram ( const std::string & vertexPath,
const std::string & fragmentPath )

Constructor for ShaderProgram, loading and linking shaders from files.

Parameters
vertexPathPath to the vertex shader file.
fragmentPathPath to the fragment shader file.

◆ ~ShaderProgram()

Raftel::ShaderProgram::~ShaderProgram ( )

Destructor for ShaderProgram.

Deletes the shader program if it was created successfully.

Member Function Documentation

◆ attachShader()

void Raftel::ShaderProgram::attachShader ( const Shader & shader)

Attaches a shader to the shader program.

Parameters
shaderThe shader to attach.

◆ getLastError()

const std::string & Raftel::ShaderProgram::getLastError ( ) const
inline

Gets the last error message.

Returns
The error message.

Definition at line 220 of file shader.hpp.

◆ GetProgramID()

GLuint Raftel::ShaderProgram::GetProgramID ( ) const
inline

Gets the OpenGL program ID.

Returns
The program ID.

Definition at line 213 of file shader.hpp.

◆ load()

bool Raftel::ShaderProgram::load ( const std::string & vertexPath,
const std::string & fragmentPath )

Loads and links shaders from files.

Parameters
vertexPathPath to the vertex shader file.
fragmentPathPath to the fragment shader file.
Returns
True if the shaders were successfully loaded and linked, false otherwise.

◆ loadFromMemory()

bool Raftel::ShaderProgram::loadFromMemory ( const char * vertexSource,
const char * fragmentSource )

Loads and compiles vertex and fragment shaders from memory.

This function allows loading shaders directly from memory instead of files. It compiles the given vertex and fragment shader source codes and links them into a shader program.

Parameters
vertexSourceThe source code of the vertex shader as a null-terminated string.
fragmentSourceThe source code of the fragment shader as a null-terminated string.
Returns
True if the shaders are successfully compiled and linked, false otherwise.
Note
If compilation or linking fails, the error message can be retrieved using getLastError().

◆ MOVABLE_BUT_NOT_COPYABLE()

Raftel::ShaderProgram::MOVABLE_BUT_NOT_COPYABLE ( ShaderProgram )

Move constructor for ShaderProgram.

Moves the resources of another ShaderProgram to this one.

Parameters
otherThe ShaderProgram to move from.

◆ setUniform() [1/5]

void Raftel::ShaderProgram::setUniform ( const std::string & name,
const glm::mat4 & matrix )

Sets a glm::mat4 uniform in the shader program.

Parameters
nameThe name of the uniform variable.
matrixThe glm::mat4 value to set.

◆ setUniform() [2/5]

void Raftel::ShaderProgram::setUniform ( const std::string & name,
float value )

Sets a float uniform in the shader program.

Parameters
nameThe name of the uniform variable.
valueThe float value to set.

◆ setUniform() [3/5]

void Raftel::ShaderProgram::setUniform ( const std::string & name,
glm::vec3 value )

Sets a glm::vec3 uniform in the shader program.

Parameters
nameThe name of the uniform variable.
valueThe glm::vec3 value to set.

◆ setUniform() [4/5]

void Raftel::ShaderProgram::setUniform ( const std::string & name,
glm::vec4 value )

Sets a glm::vec4 uniform in the shader program.

Parameters
nameThe name of the uniform variable.
valueThe glm::vec4 value to set.

◆ setUniform() [5/5]

void Raftel::ShaderProgram::setUniform ( const std::string & name,
int value )

Sets an integer uniform in the shader program.

Parameters
nameThe name of the uniform variable.
valueThe integer value to set.

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