Class PerspectiveCamera

Perspective camera class for 3D rendering.

Provides a perspective projection camera with configurable field of view, aspect ratio, and near/far clipping planes. Supports view transformations and coordinate conversions.

Fields

perspectivecamera.m_projection Projection matrix for perspective transformation.
perspectivecamera.m_inv_projection Inverse projection matrix.
perspectivecamera.m_view View matrix computed from eye, center, and up vectors.
perspectivecamera.center Position where the camera is looking at.
perspectivecamera.eye Position of the camera in world space.
perspectivecamera.up Normalized up vector defining camera orientation.

Methods

perspectivecamera:init (fov, aspect, nclip, fclip) Creates a new PerspectiveCamera instance.
perspectivecamera:update_view_matrix () Updates the view matrix based on current eye, center, and up vectors.
perspectivecamera:screen_point_to_ray (x, y, viewport) Generates a ray from the camera through a screen point.
perspectivecamera:world_to_screen_point (x, y, z, viewport) Transforms a position from world space into screen space.
perspectivecamera:get_direction () Gets the normalized direction vector the camera is facing.


Fields

perspectivecamera.m_projection
Projection matrix for perspective transformation.
perspectivecamera.m_inv_projection
Inverse projection matrix.
  • m_inv_projection mat4
perspectivecamera.m_view
View matrix computed from eye, center, and up vectors. Updated by calling updateviewmatrix().
  • m_view mat4 readonly
perspectivecamera.center
Position where the camera is looking at.
perspectivecamera.eye
Position of the camera in world space.
perspectivecamera.up
Normalized up vector defining camera orientation.

Methods

perspectivecamera:init (fov, aspect, nclip, fclip)
Creates a new PerspectiveCamera instance. Initializes the camera with perspective projection.

Parameters:

  • fov number Field of view of the camera, in degrees (default: 60)
  • aspect number The aspect ratio (width/height) (default: 1.6666667)
  • nclip number The distance of the near clipping plane from the camera (default: 0.1)
  • fclip number The distance of the far clipping plane from the camera (default: 2048.0)
perspectivecamera:update_view_matrix ()
Updates the view matrix based on current eye, center, and up vectors. Call this method after modifying the camera position or orientation.
perspectivecamera:screen_point_to_ray (x, y, viewport)
Generates a ray from the camera through a screen point. Useful for ray casting, mouse picking, and collision detection.

Parameters:

  • x number Screen position x coordinate
  • y number Screen position y coordinate
  • viewport table Viewport rectangle {x, y, width, height}

Returns:

    table Ray data containing {origin = vec3, direction = vec3}
perspectivecamera:world_to_screen_point (x, y, z, viewport)
Transforms a position from world space into screen space. Converts 3D world coordinates to 2D screen coordinates.

Parameters:

  • x number or table World position x coordinate, or vec3 object containing world position
  • y number World position y coordinate (ignored if x is vec3)
  • z number World position z coordinate (ignored if x is vec3)
  • viewport table Screen viewport {x, y, width, height}

Returns:

    vec2 Screen space coordinates
perspectivecamera:get_direction ()
Gets the normalized direction vector the camera is facing.

Returns:

    vec3 Normalized direction vector from eye to center
generated by LDoc 1.5.0 Last updated 2025-08-18 00:01:40