Shader

SafiEngine::Shader compiles and manages GLSL shader programs with automatic uniform location caching.

Header: Core/include/Core/Shader.h

Methods

LoadFromFile

bool LoadFromFile(const std::string &vertPath, const std::string &fragPath)

Reads vertex and fragment shader source from files, then compiles and links them into a program.

Returns true on success, false on failure (logs errors via Log::Error).

ParameterTypeDescription
vertPathconst std::string&Path to vertex shader file
fragPathconst std::string&Path to fragment shader file

Load

bool Load(const std::string &vertexSrc, const std::string &fragmentSrc)

Compiles a vertex and fragment shader from source strings, links them into a program.

Returns true on success, false on failure (logs compile/link errors).

ParameterTypeDescription
vertexSrcconst std::string&GLSL vertex shader source
fragmentSrcconst std::string&GLSL fragment shader source

Use

void Use() const

Activates the shader program for subsequent draw calls.

Uniform Setters

void SetMat4(const std::string &name, const glm::mat4 &mat) const
void SetMat3(const std::string &name, const glm::mat3 &mat) const
void SetMat4Array(const std::string &name, const glm::mat4 *data, int count) const
void SetVec3(const std::string &name, const glm::vec3 &vec) const
void SetVec4(const std::string &name, const glm::vec4 &vec) const
void SetInt(const std::string &name, int value) const
void SetFloat(const std::string &name, float value) const
void SetBool(const std::string &name, bool value) const

Set uniform values on the shader program by name. The shader must be active (via Use()) before calling these. All setters check that the program is valid and skip silently if the uniform name is not found (returns -1).

Uniform locations are cached in an std::unordered_map — repeated calls with the same name avoid the glGetUniformLocation string lookup.

GetID

GLuint GetID() const

Returns the OpenGL program ID.

Default Shader Uniforms

The engine's built-in default shader uses these uniforms:

Vertex Shader

UniformTypeDescription
uModelmat4Model transform matrix
uViewmat4Camera view matrix
uProjectionmat4Perspective projection matrix
uNormalMatrixmat3Precomputed normal matrix (CPU-side)
uHasSkeletonboolWhether skeletal animation is active
uBones[128]mat4Bone matrices for skeletal animation

Fragment Shader

UniformTypeDescription
uBaseColorvec4Material base color (from glTF PBR)
uHasTextureboolWhether the mesh has a base texture
uTexturesampler2DBase color texture (unit 0)
uViewPosvec3Camera position in world space
uSpecularExponentfloatBlinn-Phong specular exponent (default 32)
uNumPointLightsintNumber of active point lights (max 8)
uPointLightPos[i]vec3Point light position
uPointLightColor[i]vec3Point light color (RGB * intensity)
uPointLightConstant[i]floatAttenuation constant factor
uPointLightLinear[i]floatAttenuation linear factor
uPointLightQuadratic[i]floatAttenuation quadratic factor
uNumDirLightsintNumber of active directional lights (max 4)
uDirLightDir[i]vec3Directional light direction
uDirLightColor[i]vec3Directional light color (RGB * intensity)
uNumSpotLightsintNumber of active spot lights (max 4)
uSpotLightPos[i]vec3Spot light position
uSpotLightDir[i]vec3Spot light direction
uSpotLightColor[i]vec3Spot light color (RGB * intensity)
uSpotLightInnerCutoff[i]floatSpot light inner cone cosine
uSpotLightOuterCutoff[i]floatSpot light outer cone cosine

If no lights are present, the shader uses a fallback directional fill light with ambient lighting.