EAGL.Examples.LearnOpenGL.GettingStarted.TexturesCombined (eagl v0.7.0)

View Source

LearnOpenGL 4.2 - Textures Combined

This example demonstrates using multiple texture units to combine textures in a single fragment shader. It builds on the basic texture concepts from 4.1 and shows how to mix two textures together using GLSL's mix() function.

Original C++ Source

This example is based on the original LearnOpenGL C++ tutorial: https://github.com/JoeyDeVries/LearnOpenGL/tree/master/src/1.getting_started/4.2.textures_combined

Framework Adaptation Notes

In the original LearnOpenGL C++ tutorial, this example introduces:

  • Multiple texture units (GL_TEXTURE0, GL_TEXTURE1)
  • Binding textures to different texture units
  • Using multiple sampler2D uniforms in fragment shader
  • The GLSL mix() function for blending textures
  • Setting sampler uniform values to specify texture units

EAGL's framework preserves all these concepts while providing enhanced functionality:

  • Simplified texture loading: Uses EAGL's texture helpers with fallback support
  • Graceful degradation: Works with or without external image dependencies
  • Error handling: Comprehensive error checking and user feedback
  • Educational value: Clear documentation of texture unit concepts

EAGL vs Original Implementation

Original LearnOpenGL approach: Manual texture unit management:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glUniform1i(glGetUniformLocation(shaderProgram, "texture1"), 0);
glUniform1i(glGetUniformLocation(shaderProgram, "texture2"), 1);

EAGL approach: Texture helpers with explicit texture unit management:

# Load textures with fallback
{:ok, texture1_id, _, _} = load_texture_from_file("image1.jpg")
{:ok, texture2_id, _, _} = create_checkerboard_texture(256, 32)

# Set shader uniforms
set_uniforms(program, [
  texture1: 0,  # Texture unit 0
  texture2: 1   # Texture unit 1
])

Key Learning Points

  • Texture Units: OpenGL provides multiple texture units (GL_TEXTURE0, GL_TEXTURE1, etc.)
  • Sampler Uniforms: Fragment shaders can have multiple sampler2D uniforms
  • Texture Binding: Each texture unit can have a different texture bound
  • Mix Function: GLSL's mix() function linearly interpolates between two values
  • Uniform Assignment: Sampler uniforms need to be set to the correct texture unit number

Texture Mixing

The fragment shader uses GLSL's mix() function:

FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
  • First parameter: First texture sample
  • Second parameter: Second texture sample
  • Third parameter: Mix factor (0.2 = 80% first texture, 20% second texture)

Usage

EAGL.Examples.LearnOpenGL.GettingStarted.TexturesCombined.run_example()

Press ENTER to exit the example.

Summary

Functions

run_example(opts \\ [])