EAGL.Examples.LearnOpenGL.GettingStarted.Transformations (eagl v0.6.0)
View SourceLearnOpenGL 5.1 - Transformations
This example demonstrates basic matrix transformations in OpenGL - using translation, rotation, and scaling matrices to transform objects without modifying vertex data. It corresponds to the Transformations 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/5.1.transformations
Framework Adaptation Notes
In the original LearnOpenGL C++ tutorial, this example introduces:
- Creating transformation matrices using GLM library functions
- Passing matrices to shaders as uniforms
- Applying transformations in vertex shaders
- Understanding transformation order (Scale → Rotate → Translate)
- Time-based animations using continuous transformations
EAGL's framework preserves all these concepts while providing enhanced functionality:
- Comprehensive Math Library: Uses EAGL.Math with GLM-compatible functions
- Simplified Uniform Setting: Enhanced
set_uniform()
function handles matrix types automatically - Time-based Animation: Smooth rotation animation using system time
- Error Handling: Comprehensive error checking throughout setup and rendering
- Educational Value: Clear documentation of transformation concepts
EAGL vs Original Implementation
Original LearnOpenGL approach: Manual GLM matrix creation and uniform setting:
glm::mat4 trans = glm::mat4(1.0f);
trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f));
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));
EAGL approach: Streamlined matrix operations with automatic uniform handling:
transform = mat4_identity()
|> mat4_mul(mat4_translate(vec3(0.5, -0.5, 0.0)))
|> mat4_mul(mat4_rotate_z(time))
set_uniform(program, "transform", transform)
Key Learning Points
- Transformation Matrices: How scale, rotation, and translation matrices work
- Matrix Multiplication: Understanding the order of matrix operations (right-to-left)
- Vertex Transformation: How the vertex shader applies transformations
- Homogeneous Coordinates: Why we use 4x4 matrices for 3D transformations
- Animation: Creating smooth movement through time-based transformations
Mathematical Background
This example demonstrates the fundamental 2D transformations:
- Translation: Moving objects in space using a translation matrix
- Rotation: Rotating objects around the Z-axis using trigonometric functions
- Scaling: Changing object size uniformly or non-uniformly
- Combination: Multiplying matrices to combine multiple transformations
Visual Effect
The example shows a textured rectangle that orbits around the world origin in a circular motion. This demonstrates the importance of transformation order:
- Translation first, then rotation:
T * R
= Object orbits around world origin - The rectangle is first moved to (0.5, -0.5), then rotated around (0, 0, 0)
- This creates a circular orbit motion, not rotation around the rectangle's center
To rotate around the rectangle's center instead, you would use: R * T
(rotate first, then translate).
Usage
EAGL.Examples.LearnOpenGL.GettingStarted.Transformations.run_example()
Press ENTER to exit the example.
Summary
Functions
@spec run_example() :: :ok | {:error, term()}