Skip to content

Shader class

TechnologicalTurtle edited this page Jul 15, 2026 · 1 revision

Shader

Note

LibGui has it's own build-in shaders, so if you don't need to have custom ones, you can just skip this part.

LibGui::Shader is wrapper for OpenGL shader programs, they are programs that run on GPU and calculate positions of vertexes and colors of individual pixels.

Warning

If you aren't familiar with shaders, please read this first.

Initialization

Initialization from file

To initialize from file you can simply do this:

LibGui::Shader MyShader("my_vertex_shader_source.txt", "my_fragment_shader_source.txt");

Initialization from source string

If you have string variable you want to create shader from, you can do this:

std::string vertex_source;
std::string fragment_source;

LibGui::Shader MyShader;
MyShader.Compile(vertex_source, fragment_source);

Methods

void Bind()

Binds shader.

boid Unbind()

Unbinds shader.

void Compile(...)

here

void CompileFromFile(...)

equivalent of this

unsigned int GetID()

Returns shader's OpenGL id.

Uniform setters

To set an uniform, you can call one of these:

int unsigned int float
1 value SetUniform1Int SetUniform1UInt SetUniform1Float
Vec2 SetUniformVec2i SetUniform2UInt SetUniformVec2
Vec3 SetUniform3Int SetUniform3UInt SetUniform3Float
Vec4 SetUniform4Int SetUniform4UInt SetUniform4Float

Debugging

If the shader can run, the compilationSuccessful property will be true. Otherwise the errors will be printed to console, and added to std::vector<std::string> compilationErrors.

Default shader's uniforms

Vertex shader uniforms

Name Description
vec2 u_ParentPos Object.pos; will be added to vertexes final coord
vec2 u_Scale Object.scale; will be multiplied with vertexes local coord
vec2 u_RightVector first half of rotation matrix (come on, we don't even have linear algebra at school yet)
vec2 u_UpVector second half of rotation matrix
vec4 u_ParentPos Object.color; will be multiplied with vertexes color

Fragment shader uniforms

Name Desription
sampler2D myTexture Object.texture
bool u_HasTexture Object.texture!=nullptr
vec2 u_EdgeMin Upper left corner of render mask
vec2 u_EdgeMax Lower right corner of render mask

Clone this wiki locally