Struct ShaderProgram

Struct Documentation

struct ShaderProgram

Encapsulates an OpenGL shader program composed of a vertex and fragment shader.

The ShaderProgram class manages the lifecycle of an OpenGL shader program, including creation, usage, and resource cleanup. It takes ownership of the provided vertex and fragment shaders, links them into a program, and provides utility methods for program usage and uniform location retrieval.

Note

Copy operations are deleted to prevent accidental resource duplication.

Public Functions

ShaderProgram() = default

Constructs a ShaderProgram by linking a vertex and fragment shader.

This constructor takes ownership of the provided VertexShader and FragmentShader objects, attaches them to a new OpenGL program, and links the program.

Parameters:
Throws:

std::runtime_error – if the shader program fails to link.

ShaderProgram(const ShaderProgram&) = delete
ShaderProgram &operator=(const ShaderProgram&) = delete
~ShaderProgram()
bool Init(std::unique_ptr<VertexShader> vertexShader, std::unique_ptr<FragmentShader> fragmentShader)
inline GLuint Get() const
inline void Use() const

Activates the shader program for rendering.

This method sets the current OpenGL shader program to the one associated with this Shader instance. All subsequent rendering calls will use this shader program until another program is activated.

inline GLint GetUniformLocation(const std::string &name) const

Retrieves the location of a uniform variable within the shader program.

This function queries the OpenGL shader program for the location of a uniform variable specified by its name. The returned location can be used to set the value of the uniform.

Parameters:

name – The name of the uniform variable whose location is to be retrieved.

Returns:

GLint The location of the uniform variable, or -1 if the name does not correspond to an active uniform variable.