EAGL.Examples.LearnOpenGL.GettingStarted.ShadersUniform (eagl v0.5.0)
View SourceLearnOpenGL 3.1 - Shaders Uniform
This example demonstrates uniform variables in shaders - global variables that remain constant across all vertices in a draw call but can be updated between draw calls. It corresponds to the Shaders Uniform tutorial in the LearnOpenGL series.
Original C++ Source
This example is based on the original LearnOpenGL C++ tutorial: https://github.com/JoeyDeVries/LearnOpenGL/tree/master/src/1.getting_started/3.1.shaders_uniform
Framework Adaptation Notes
In the original LearnOpenGL C++ tutorial, this example introduces uniform variables:
- How to declare uniforms in GLSL
- How to get uniform locations from the application
- How to update uniform values using glUniform* functions
- Creating animated effects by updating uniforms per frame
EAGL's framework preserves all these concepts while providing convenience functions:
- EAGL.Shader.set_uniform() handles the glGetUniformLocation + glUniform* pattern
- Error checking and type detection are automated
- The core OpenGL uniform concepts remain unchanged and visible
Key Difference: Animation Timing
Original LearnOpenGL approach: Time calculation happens directly in the render loop:
while (!glfwWindowShouldClose(window)) {
float timeValue = glfwGetTime(); // Get time in render loop
float greenValue = sin(timeValue) / 2.0f + 0.5f;
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
// ... render
}
EAGL approach: Uses the tick handler pattern for clean separation of concerns:
handle_event(:tick, state)
updates time state each framerender/3
uses the current time state for animation- This separates state management from rendering logic
Original Tutorial Concepts Demonstrated
- Uniform Declaration: Declaring
uniform vec4 ourColor
in the fragment shader - Uniform Location: Getting the uniform location using
glGetUniformLocation
- Uniform Updates: Using
glUniform4f
to update the uniform value - Animation: Updating the uniform each frame using
sin(time)
for color cycling - Shader Communication: Passing data from application to shader via uniforms
Key Learning Points
- Understanding the difference between attributes and uniforms
- Uniforms are global variables that stay constant per draw call
- Uniforms can be updated between draw calls to create dynamic effects
- Time-based animation using mathematical functions
- The OpenGL uniform system and how it bridges CPU and GPU
Color Animation
The triangle color cycles through a green color animation:
- Color varies from dark green (0.0) to bright green (1.0)
- Uses
sin(time)
to create smooth oscillation - Red and blue components remain at 0.0
- Alpha remains at 1.0 (fully opaque)
Difference from Previous Examples
- 2.1 Hello Triangle: Static orange triangle with hardcoded fragment color
- 3.1 Shaders Uniform: Dynamic color animation using uniform variables
Usage
EAGL.Examples.LearnOpenGL.GettingStarted.ShadersUniform.run_example()
Press ENTER to exit the example.
Summary
Functions
@spec run_example() :: :ok | {:error, term()}