Overview
TheShader class provides a high-level interface for working with OpenGL shader programs. It handles loading shader source files, compiling vertex and fragment shaders, linking programs, and setting uniform variables.
Header: src/renderer/shader.h
Constructor
Shader
vertexPath- File path to the vertex shader source (.glsl)fragmentPath- File path to the fragment shader source (.glsl)
- Reads shader source files from disk
- Compiles vertex and fragment shaders
- Links shaders into a program
- Sets
IDto 0 if compilation or linking fails - Outputs compilation/linking errors to console
- Automatically cleans up shader objects after linking
Destructor
~Shader
glDeleteProgram if the shader is valid.
Public Members
ID
Methods
valid
true if the shader is valid (ID != 0), false otherwise
use
glUseProgram.
Example:
find_uniform
name- Name of the uniform variable
setMat4
location- Uniform location (fromfind_uniform)mat- GLM mat4 matrix to set
setInt
location- Uniform locationvalue- Integer value to set
setFloat
location- Uniform locationvalue- Float value to set
setVec2i
location- Uniform locationx- X componenty- Y component
setVec2
location- Uniform locationvalue- GLM vec2 to set
setVec3
location- Uniform locationvalue- GLM vec3 to set
setMat4Array
location- Uniform locationmats- Vector of GLM mat4 matrices
- Returns early if location is invalid (< 0) or vector is empty
- Uploads all matrices in the vector to the shader
setFloatArray
location- Uniform locationvalues- Vector of float values
- Returns early if location is invalid (< 0) or vector is empty
- Uploads all values in the vector to the shader
Implementation Details
Error Handling
The shader class performs comprehensive error checking:- File reading errors: Catches
std::ifstream::failureexceptions and sets ID to 0 - Compilation errors: Checks
GL_COMPILE_STATUSand outputs shader info log - Linking errors: Checks
GL_LINK_STATUSand outputs program info log
std::cout with descriptive messages.
OpenGL Integration
Usage Pattern
See Also
- TextureManager - Manages texture arrays used with shaders
- Shader source files in
assets/shaders/ World::draw()- Main shader usage example in src/world.cpp