EAGL.Examples.LearnOpenGL.GettingStarted.HelloTriangleExercise2 (eagl v0.8.0)
View SourceLearnOpenGL 2.4 - Hello Triangle Exercise 2 (Rectangle with EBO)
This example demonstrates creating two triangles using Element Buffer Objects. It solves the second exercise from the Hello Triangle tutorial.
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.4.hello_triangle_exercise2
Framework Adaptation Notes
In the original LearnOpenGL C++ tutorial, this exercise asks you to create two triangles using a single VAO and VBO, with an Element Buffer Object (EBO) to avoid vertex duplication.
EAGL's framework maintains the same approach:
- Single VAO/VBO/EBO for efficient vertex sharing
- glDrawElements for indexed rendering
- 4 vertices create 2 triangles through clever indexing
Original Tutorial Exercise
Exercise 2: Now create the same 2 triangles using an EBO so there are only 4 vertices in total instead of 6. This is a solution to the previous exercise using indices.
Solution Concepts Demonstrated
- Element Buffer Objects (EBO): Storing indices for vertex reuse
- Vertex Sharing: 4 vertices used to create 2 triangles
- Indexed Rendering: glDrawElements with index buffer
- Memory Efficiency: Reducing vertex data through sharing
- Rectangle Formation: Two triangles forming a quad
Key Learning Points
- How EBOs enable vertex sharing between primitives
- The efficiency difference between indexed and non-indexed rendering
- Understanding triangle winding order and connectivity
- When indexed rendering provides the most benefit
- The relationship between vertex count and primitive count
Rectangle Geometry
Two triangles sharing vertices to form a rectangle:
3 ---- 2
| / |
| / |
| / |
| / |
0 ---- 1
4 vertices total, 6 indices: [0,1,3, 1,2,3] Triangle 1: vertices 0,1,3 | Triangle 2: vertices 1,2,3
Difference from Previous Examples
- 2.3 Exercise 1: 2 triangles, 6 vertices (no sharing)
- 2.4 Exercise 2: 2 triangles, 4 vertices + indices (vertex sharing)
Usage
EAGL.Examples.LearnOpenGL.GettingStarted.HelloTriangleExercise2.run_example()
Press ENTER to exit the example.