EAGL.Examples.LearnOpenGL.GettingStarted.ShadersExercise2 (eagl v0.7.0)
View SourceLearnOpenGL 3.5 - Shaders Exercise 2
This example demonstrates using a uniform variable to apply a horizontal offset to the triangle position. It combines uniform variables with vertex transformations to show how application data can control shader behavior. It corresponds to the second shader exercise 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.5.shaders_exercise2
Exercise Description
Original Exercise: "Specify a horizontal offset via a uniform and move the triangle to the right side of the screen in the vertex shader using this offset value"
Solution Approach: Add uniform to vertex shader and apply to x-coordinate:
- Add:
uniform float xOffset;
to vertex shader - Modify:
gl_Position = vec4(aPos.x + xOffset, aPos.y, aPos.z, 1.0);
- Set uniform from application:
set_uniform(program, "xOffset", 0.5)
Framework Adaptation Notes
In the original LearnOpenGL C++ tutorial, this exercise teaches:
- How to combine uniforms with vertex transformations
- Passing application data to control shader behavior
- Understanding the difference between vertex attributes and uniforms
- How uniforms affect all vertices in a draw call uniformly
EAGL's framework preserves all these concepts:
- EAGL.Shader.set_uniform/3 handles the uniform setting with type detection
- Same vertex data, transformation applied via uniform in shader
- Demonstrates application-controlled geometry manipulation
Key Concept: Uniform-Controlled Transformations
Why use uniforms for transformations:
- Can change transformation without recompiling shaders
- Same transformation applied to all vertices in draw call
- Efficient way to animate or control geometry
- Foundation for more complex transformations (matrices)
Uniform vs. Vertex Attribute:
- Uniform: Same value for all vertices (global to draw call)
- Vertex Attribute: Different value per vertex (per-vertex data)
- Uniforms perfect for transformations, lighting parameters, time, etc.
Visual Result
Triangle with interpolated colors moved to the right side of screen:
- Same colour interpolation as 3.2/3.3
- Horizontally offset by uniform value (0.5 units to the right)
- Demonstrates uniform-controlled positioning
Learning Objectives
- Understanding uniform variables in vertex shaders
- Combining uniforms with vertex transformations
- Application-controlled shader behavior
- Difference between uniforms and vertex attributes
Usage
EAGL.Examples.LearnOpenGL.GettingStarted.ShadersExercise2.run_example()
Press ENTER to exit the example.