EAGL.Examples.LearnOpenGL.GettingStarted.CameraExercise2 (eagl v0.9.0)

View Source

LearnOpenGL 7.6 - Camera Exercise 2: Custom LookAt Implementation

This exercise implements a custom LookAt function that manually calculates the view matrix instead of using the built-in camera functionality. This demonstrates the mathematical foundations behind camera transformations and provides deeper understanding of how view matrices work.

Original Exercise Context

This corresponds to the LearnOpenGL Camera Exercise 2 which challenges developers to: "Create your own LookAt function where you manually create a view matrix as discussed at the start of the camera tutorial."

Educational Objectives

Mathematical Understanding

  • View Matrix Construction: Manual calculation of view transformation
  • Coordinate System Building: Creating right-hand coordinate system from camera orientation
  • Vector Mathematics: Cross products, normalisation and vector operations
  • Matrix Operations: Translation and rotation matrix combination

Custom LookAt Function

# Built-in approach (7.4/7.5)
view = Camera.get_view_matrix(camera)  # Uses EAGL.Math.mat4_look_at/3

# Custom implementation (7.6)
view = custom_look_at(camera_pos, target_pos, up_vector)  # Manual calculation

Technical Implementation

LookAt Mathematics

The LookAt function constructs a view matrix by:

  1. Direction Vector: Calculate direction from camera to target
  2. Right Vector: Cross product of direction and world up
  3. Up Vector: Cross product of right and direction vectors
  4. View Matrix: Combine rotation and translation components

Matrix Construction

View Matrix = Translation * Rotation

Where:
- Translation moves world to camera position
- Rotation orients world to camera coordinate system

Controls

  • W/A/S/D: Move camera position
  • Mouse Movement: Rotate camera orientation
  • Scroll Wheel: Zoom in/out (field of view)
  • ENTER: Exit (when run with enter_to_exit: true)

Educational Value

This exercise demonstrates:

  • Matrix Mathematics: How view matrices are constructed from basic principles
  • Coordinate Systems: Building orthonormal coordinate systems in 3D
  • Camera Theory: Understanding the relationship between camera position and orientation
  • Performance Considerations: Comparing custom vs optimised implementations

Comparison: Built-in vs Custom

Built-in LookAt (7.4):

Custom LookAt (7.6):

  • Manual vector and matrix calculations
  • Step-by-step view matrix construction
  • Educational insight into camera mathematics
  • Demonstrates underlying principles

This exercise provides the mathematical foundation for understanding all 3D camera systems.

Summary

Functions

run_example(opts \\ [])