EAGL.Model (eagl v0.7.0)

View Source

3D model loading and OpenGL integration.

Handles model loading with automatic VAO creation. Currently only supports OBJ files but can be extended to support other file formats.

Usage

import EAGL.Model

# Load model directly to VAO (most common)
{:ok, model} = load_model_to_vao("teapot.obj")

# Render the model
:gl.bindVertexArray(model.vao)
:gl.drawElements(@gl_triangles, model.vertex_count, @gl_unsigned_int, 0)

# Load model data for custom processing
{:ok, model_data} = load_model("teapot.obj")

# Options for normal generation
{:ok, model} = load_model_to_vao("teapot.obj",
  smooth_normals: true,
  flip_normal_direction: false
)

# List available models
models = list_models()

# Clean up
delete_vao(model.vao)

Summary

Functions

Deletes a vertex array object and its associated buffers.

Lists all available models in the priv/models directory.

Loads a model from the priv/models directory. Returns the processed model data ready for OpenGL.

Loads a model and creates a VAO with the model data. Returns {:ok, %{vao: vao, vertex_count: count}} or {:error, reason}

Functions

delete_vao(vao)

@spec delete_vao(integer()) :: :ok

Deletes a vertex array object and its associated buffers.

list_models()

@spec list_models() :: [String.t()]

Lists all available models in the priv/models directory.

load_model(filename, opts \\ [])

@spec load_model(
  String.t(),
  keyword()
) :: {:ok, map()} | {:error, String.t()}

Loads a model from the priv/models directory. Returns the processed model data ready for OpenGL.

Options:

  • :flip_normal_direction - boolean, set to true to flip normal direction for all models (default: false)
                         This works for both models with existing normals and models that need generated normals.
  • :smooth_normals - boolean, set to true to generate smooth normals by averaging across adjacent faces (default: false)
                  This gives a smoother appearance by eliminating the faceted look.

load_model_to_vao(filename, opts \\ [])

@spec load_model_to_vao(
  String.t(),
  keyword()
) :: {:ok, %{vao: integer(), vertex_count: integer()}} | {:error, String.t()}

Loads a model and creates a VAO with the model data. Returns {:ok, %{vao: vao, vertex_count: count}} or {:error, reason}

The VAO will have the following attributes:

  • Location 0: Vertex positions (vec3)
  • Location 1: Texture coordinates (vec2)
  • Location 2: Normals (vec3)

Options:

  • :flip_normal_direction - boolean, set to true to flip normal direction for all models (default: false)
                         This works for both models with existing normals and models that need generated normals.
                         Useful when model normals are pointing in the wrong direction for your lighting setup.
  • :smooth_normals - boolean, set to true to generate smooth normals by averaging across adjacent faces (default: false)
                  This gives a smoother appearance by eliminating the faceted look.
                  When true, existing normals are ignored and smooth normals are generated.