EAGL.Examples.LearnOpenGL.GettingStarted.CameraClass (eagl v0.8.0)

View Source

LearnOpenGL 7.4 - Camera Class

This example demonstrates the power of proper camera abstraction using the EAGL.Camera module. It provides smooth, natural first-person camera controls with WASD movement, mouse look, and scroll zoom functionality.

Original C++ Source

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

Learning Objectives

This example teaches:

  • Camera Abstraction: Encapsulating complex camera mathematics in a clean API
  • Code Organisation: Moving from scattered manual implementation to modular design
  • Software Engineering: Benefits of abstraction, encapsulation, and tested code
  • API Design: Simple, intuitive interfaces hiding complex functionality

Educational Progression Context

7.1-7.3: Manual camera implementation with increasing complexity 7.4: Code organisation and abstraction (this example) 7.5: Addressing specific camera behavior limitations 7.6: Understanding the mathematical foundations

This example represents a crucial step in the learning progression - moving from "how to implement camera controls" to "how to organize and structure camera code."

Key Improvements Over 7.3

Code Simplification

# Manual Implementation (7.3) - Complex and error-prone
camera_pos: camera_pos,
camera_front: camera_front,
camera_up: camera_up,
yaw: yaw,
pitch: pitch,
fov: fov,
# Plus manual front vector calculation...
# Plus manual movement processing...
# Plus manual mouse handling...

# Camera Class (7.4) - Clean and maintainable
camera: Camera.new(position: vec3(0.0, 0.0, 3.0))
camera = Camera.process_keyboard(camera, :forward, delta_time)
camera = Camera.process_mouse_movement(camera, x_offset, y_offset)
view = Camera.get_view_matrix(camera)

Technical Benefits

  • Encapsulation: Camera state contained in single struct
  • Tested Implementation: Reduces manual calculation errors
  • Clean API: Simple function calls replace complex manual code
  • Maintainability: Changes to camera behavior centralized
  • Mouse Sensitivity: Automatically adjusted for natural first-person feel

Controls

  • W/A/S/D: Move forward/left/backward/right
  • Mouse Movement: Look around (first-person view)
  • Scroll Wheel: Zoom in/out (field of view)
  • ENTER: Exit (when run with enter_to_exit: true)

Camera Features Demonstrated

  • Encapsulated Movement: Simple function calls for complex movement logic
  • Automatic Vector Management: Front/right/up vectors maintained automatically
  • Pitch Constraints: Prevents camera flipping (±89° limit)
  • Delta Time: Frame-rate independent movement
  • Zoom Control: Field of view adjustment (1° to 45° range)

Educational Value

This example demonstrates fundamental software engineering principles:

  • Abstraction: Complex camera math hidden behind simple API
  • Encapsulation: Related functionality grouped in cohesive module
  • Reusability: Camera implementation can be used across multiple projects
  • Maintainability: Bug fixes and improvements centralized
  • Focus: Developers can focus on content rather than camera implementation details

Pedagogical Note: While this example significantly improves code organization and reduces complexity, some advanced camera behaviors (like FPS ground constraints and mathematical understanding) are explored in exercises 7.5 and 7.6.

The progression from manual implementation (7.1-7.3) to camera class (7.4) to specialized exercises (7.5-7.6) demonstrates both practical software development and deeper understanding of 3D camera systems.

Summary

Functions

run_example(opts \\ [])