EAGL.Examples.LearnOpenGL.GettingStarted.HelloTriangleIndexed (eagl v0.3.0)

View Source

LearnOpenGL 2.2 - Hello Triangle Indexed (Element Buffer Objects)

This example demonstrates Element Buffer Objects (EBO) for efficient indexed rendering. It shows how to draw a rectangle using two triangles with shared vertices.

Original C++ Source

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

Framework Adaptation Notes

In the original LearnOpenGL C++ tutorial, this example introduces Element Buffer Objects (EBOs) as a way to avoid vertex duplication when drawing complex shapes.

EAGL's framework maintains the same concepts while providing helper functions:

  • EBO creation and binding are handled by EAGL.Buffer functions
  • The core concept of indexed rendering remains unchanged
  • glDrawElements vs glDrawArrays distinction is preserved

Original Tutorial Concepts Demonstrated

  1. Element Buffer Objects (EBO): Storing vertex indices for reuse
  2. Indexed Rendering: Drawing with glDrawElements instead of glDrawArrays
  3. Vertex Sharing: Using 4 vertices to draw 2 triangles (6 vertices worth)
  4. Memory Efficiency: Avoiding duplicate vertex data
  5. Triangle Assembly: How indices define triangle connectivity

Key Learning Points

  • Understanding indexed vs non-indexed rendering
  • Memory efficiency through vertex sharing
  • The relationship between vertices and indices
  • When to use EBOs vs simple vertex arrays
  • Triangle winding order and face culling implications

Rectangle Geometry

The rectangle is defined by 4 vertices with 6 indices (2 triangles):

3 ---- 2
|    / |
|   /  |
|  /   |
| /    |
0 ---- 1

Vertices: 4 unique positions Indices: [0,1,3, 1,2,3] (2 triangles sharing vertices 0, 1, and 3)

Difference from Previous Examples

  • 2.1 Hello Triangle: 3 vertices, glDrawArrays (no sharing)
  • 2.2 Hello Triangle Indexed: 4 vertices + indices, glDrawElements (vertex sharing)

Usage

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

Press ENTER to exit the example.

Summary

Functions

run_example()

@spec run_example() :: :ok | {:error, term()}