libsc 2.8.7.187-a49fd
The SC library provides support for parallel scientific applications.
Loading...
Searching...
No Matches
Data Structures | Typedefs | Enumerations | Functions
sc_camera.h File Reference

Camera data structure for computer graphic applications. More...

#include <sc_containers.h>
Include dependency graph for sc_camera.h:

Go to the source code of this file.

Data Structures

struct  sc_camera
 Represents a camera with parameters for rendering a 3D scene. More...
 

Typedefs

typedef double sc_camera_coords_t
 The data type of the coordinates for sc_camera.
 
typedef sc_camera_coords_t sc_camera_vec3_t[3]
 Points in R^3 for sc_camera.
 
typedef sc_camera_coords_t sc_camera_vec4_t[4]
 Points in R^4 for sc_camera.
 
typedef sc_camera_coords_t sc_camera_mat4x4_t[16]
 4 times 4 matrix for sc_camera.
 
typedef sc_camera_coords_t sc_camera_mat3x3_t[9]
 3 times 3 matrix for sc_camera.
 
typedef enum sc_camera_plane sc_camera_plane_t
 Defines the order of the 6 planes of the camera's view frustum.
 
typedef struct sc_camera sc_camera_t
 Represents a camera with parameters for rendering a 3D scene.
 

Enumerations

enum  sc_camera_plane {
  SC_CAMERA_PLANE_NEAR ,
  SC_CAMERA_PLANE_FAR ,
  SC_CAMERA_PLANE_LEFT ,
  SC_CAMERA_PLANE_RIGHT ,
  SC_CAMERA_PLANE_TOP ,
  SC_CAMERA_PLANE_BOTTOM
}
 Defines the order of the 6 planes of the camera's view frustum. More...
 

Functions

sc_camera_t * sc_camera_new (void)
 Creates a new camera structure with the default values (see sc_camera_init).
 
void sc_camera_init (sc_camera_t *camera)
 Initializes a camera with the default parameters.
 
void sc_camera_destroy (sc_camera_t *camera)
 Destroys a camera structure.
 
void sc_camera_position (sc_camera_t *camera, const sc_camera_vec3_t position)
 Sets the position.
 
void sc_camera_yaw (sc_camera_t *camera, double angle)
 Rotating the camera around the up axis.
 
void sc_camera_pitch (sc_camera_t *camera, double angle)
 Rotating the camera around the axis to the right.
 
void sc_camera_roll (sc_camera_t *camera, double angle)
 Rotating the camera around the backward axis.
 
void sc_camera_walk (sc_camera_t *camera, double distance)
 Move the camera backward/forward along the view direction.
 
void sc_camera_fov (sc_camera_t *camera, double angle)
 Sets the horizontal field of view.
 
void sc_camera_aspect_ratio (sc_camera_t *camera, int width, int height)
 Sets the aspect ratio.
 
void sc_camera_clipping_dist (sc_camera_t *camera, sc_camera_coords_t near_plane, sc_camera_coords_t far_plane)
 Sets the clipping distances.
 
void sc_camera_look_at (sc_camera_t *camera, const sc_camera_vec3_t eye, const sc_camera_vec3_t center, const sc_camera_vec3_t up)
 Sets the position and rotation of the camera object.
 
void sc_camera_view_transform (const sc_camera_t *camera, sc_array_t *points_in, sc_array_t *points_out)
 Performs the view transformation from world space to view space.
 
void sc_camera_get_view_mat (const sc_camera_t *camera, sc_camera_mat4x4_t view_matrix)
 Returns the view transformation matrix.
 
void sc_camera_get_projection_mat (const sc_camera_t *camera, sc_camera_mat4x4_t proj_matrix)
 Returns the projection transformation matrix.
 
void sc_camera_projection_transform (const sc_camera_t *camera, sc_array_t *points_in, sc_array_t *points_out)
 Performs the projection transformation from view space to normalized device coordinates.
 
void sc_camera_get_frustum (const sc_camera_t *camera, sc_array_t *planes)
 Returns the 6 planes of the view frustum.
 
void sc_camera_clipping_pre (const sc_camera_t *camera, sc_array_t *points, sc_array_t *indices)
 Determines the indices of points that are visible within the camera's view frustum.
 
void sc_camera_frustum_dist (const sc_camera_t *camera, const sc_camera_vec3_t point, sc_camera_coords_t distances[6])
 Computes the signed distances of a point to the 6 planes of the camera's view frustum.
 
void sc_camera_clipping_post (sc_array_t *points, sc_array_t *indices)
 Performs clipping in normalized device coordinates (NDC) space.
 
void sc_camera_perspective_division (sc_array_t *points_in, sc_array_t *points_out)
 Performs perspective division.
 

Detailed Description

Camera data structure for computer graphic applications.

The camera module page provides a detailed example of the rendering workflow.

Typedef Documentation

◆ sc_camera_mat3x3_t

typedef sc_camera_coords_t sc_camera_mat3x3_t[9]

3 times 3 matrix for sc_camera.

The entries of the matrix are in column-major order.

◆ sc_camera_mat4x4_t

typedef sc_camera_coords_t sc_camera_mat4x4_t[16]

4 times 4 matrix for sc_camera.

The entries of the matrix are in column-major order.

◆ sc_camera_plane_t

Defines the order of the 6 planes of the camera's view frustum.

The planes are enumerated as: near, far, left, right, top, bottom.

◆ sc_camera_t

typedef struct sc_camera sc_camera_t

Represents a camera with parameters for rendering a 3D scene.

The sc_camera object stores essential properties of a camera, including its position, rotation, field of view, viewport dimensions, and clipping planes. These parameters define how the camera projects the 3D world onto the 2D image plane.

Enumeration Type Documentation

◆ sc_camera_plane

Defines the order of the 6 planes of the camera's view frustum.

The planes are enumerated as: near, far, left, right, top, bottom.

Function Documentation

◆ sc_camera_aspect_ratio()

void sc_camera_aspect_ratio ( sc_camera_t *  camera,
int  width,
int  height 
)

Sets the aspect ratio.

The aspect ratio is determined by the ratio of a width and a height value.

Parameters
[out]cameraThe camera object.
[in]widthA width value for the view.
[in]heightA height value for the view.

◆ sc_camera_clipping_dist()

void sc_camera_clipping_dist ( sc_camera_t *  camera,
sc_camera_coords_t  near_plane,
sc_camera_coords_t  far_plane 
)

Sets the clipping distances.

Parameters
[out]cameraThe camera object.
[in]near_planeThe new distance from the camera to the near clipping plane.
[in]far_planeThe new distance from the camera to the far clipping plane.

◆ sc_camera_clipping_post()

void sc_camera_clipping_post ( sc_array_t *  points,
sc_array_t *  indices 
)

Performs clipping in normalized device coordinates (NDC) space.

This function identifies points that lie within the NDC cube ([-1,1]^3).

The input array must store points in homogeneuos coordinates as quadruples of coordinates, i.e.: points_in->elem_size = 4 * sizeof(sc_camera_coords_t)

Checks if x, y, z in [-w, w] for each point (x, y, z, w) in homogeneous coordinates.

(x,y,z,w) = (0,0,0,0) is not a valid homogeneous coordinate and will trigger an assertion failure.

Parameters
[in]pointsAn array of points in homogeneous coordinates (4D).
[out]indicesAn array that will contain the indices of points that lie inside the NDC cube [-1, 1]^3.

◆ sc_camera_clipping_pre()

void sc_camera_clipping_pre ( const sc_camera_t *  camera,
sc_array_t *  points,
sc_array_t *  indices 
)

Determines the indices of points that are visible within the camera's view frustum.

This function takes an array of 3D points in world space (x, y, z) and performs clipping against the camera's view frustum. It returns the indices of the points that are inside the frustum and thus visible to the camera.

The input array points must store the 3D coordinates of the points, i.e.: points->elem_size = 3 * sizeof(sc_camera_coords_t)

The output array indices stores the indices of the points from points that are inside the view frustum. Each index is stored as a size_t value. Thus indices must be initialized sc_array with elem_size = sizeof(size_t).

Parameters
[in]cameraThe camera object.
[in]pointsAn array of 3D points in world space (x, y, z).
[out]indicesThe indices (as size_t) of the points that are visible within the camera's view frustum.

◆ sc_camera_destroy()

void sc_camera_destroy ( sc_camera_t *  camera)

Destroys a camera structure.

This function destroys a camera object allocated with sc_camera_new. It is not valid to call this function on stack-allocated camera objects.

Parameters
[in]cameraThe camera to be destroyed.

◆ sc_camera_fov()

void sc_camera_fov ( sc_camera_t *  camera,
double  angle 
)

Sets the horizontal field of view.

Parameters
[out]cameraThe camera object.
[in]angleThe new horizontal field of view angle of the camera.

◆ sc_camera_frustum_dist()

void sc_camera_frustum_dist ( const sc_camera_t *  camera,
const sc_camera_vec3_t  point,
sc_camera_coords_t  distances[6] 
)

Computes the signed distances of a point to the 6 planes of the camera's view frustum.

For a point in world space, this function calculates its signed orthogonal distance to each frustum plane (near, far, left, right, top, bottom), stored in distances[0..5] in this order, which must be allocated by the user.

A positive distance means that a point lies on the outside side of this plane. If all values are negative the point lies in the view frustum.

Parameters
[in]cameraThe camera object.
[in]pointA 3D point in world space.
[out]distancesSix signed distances (sc_camera_coords_t) to the frustum planes. Must be allocated on input.

◆ sc_camera_get_frustum()

void sc_camera_get_frustum ( const sc_camera_t *  camera,
sc_array_t *  planes 
)

Returns the 6 planes of the view frustum.

This function returns the bounding planes of the visible world space (the view frustum). The planes are ordered as: near, far, left, right, top, and bottom (see enum sc_camera_plane). The naming is from the perspective of the camera; for example, the left plane bounds the view to the left. The near and far planes bound the field of view so that objects, that are too close or too far, are not visible. The distances to these planes are defined by the user in the camera object.

The array planes contains 6 entries, each consisting of 4 values (a, b, c, d): planes->elem_count = 6 planes->elem_size = 4 * sizeof(sc_camera_coords_t)

Each plane is represented by its outward-facing normal vector (a, b, c) and offset d, such that the plane equation is: a*x + b*y + c*z + d = 0 Points (x, y, z) satisfying this equation lie on the plane.

Parameters
[in]cameraThe camera object.
[out]planesThe 6 planes of the view frustum, in the order near, far, left, right, top, and bottom (see enum sc_camera_plane).

◆ sc_camera_get_projection_mat()

void sc_camera_get_projection_mat ( const sc_camera_t *  camera,
sc_camera_mat4x4_t  proj_matrix 
)

Returns the projection transformation matrix.

The projection matrix transforms coordinates from view space into normalized device coordinates (NDC).

  1. Switch from right-handed to left-handed coordinate system by negating the z-axis.
  2. Apply the perspective projection into the cuboid [-2/s_x, 2/s_x] x [-2/s_y, 2/s_y] x [n,f] with use of homogeneous coordinates. Here s_x and s_y are width and height of the visible picture on the near plane.
  3. Map the cuboid to the NDC cube [-1, 1]^3.
[ 2/s_x 0 0 0 ] [ n 0 0 0 ] [ 1 0 0 0 ]
[ 0 2/s_y 0 0 ] [ 0 n 0 0 ] [ 0 1 0 0 ]
[ 0 0 2/(f-n) -2n/(f-n)-1 ] * [ 0 0 (f+n) -fn ] * [ 0 0 -1 0 ]
[ 0 0 0 1 ] [ 0 0 1 0 ] [ 0 0 0 1 ]
Parameters
[in]cameraThe camera object defining the projection.
[out]proj_matrixThe resulting 4x4 projection transformation matrix in column-major order. Must be allocated by the user.

◆ sc_camera_get_view_mat()

void sc_camera_get_view_mat ( const sc_camera_t *  camera,
sc_camera_mat4x4_t  view_matrix 
)

Returns the view transformation matrix.

Let R ∈ ℝ^{3×3} be the rotation matrix corresponding to the camera's rotation quaternion, and p ∈ ℝ³ be the camera's position vector. Let I be the 3×3 identity matrix. view_matrix is defined as:

[ R -R*p ] [ R 0 ] [ I -p ]
[ 0 1 ] = [ 0 1 ] * [ 0 1 ]

This matrix transforms coordinates from world space into the camera's local (view) coordinate system.

Parameters
[in]cameraThe camera object defining the view transformation.
[out]view_matrixThe resulting 4×4 view transformation matrix in column-major order. Must be allocated by the user.

◆ sc_camera_init()

void sc_camera_init ( sc_camera_t *  camera)

Initializes a camera with the default parameters.

As default the camera is positioned at (0, 0, 1) and looking at (0, 0, 0) with the upwards direction (0, 1, 0). The default horizontal field of view is Pi/2, the image has aspect ratio 1 (width = height) and near is set at 1/100 far at 100. The default is realised by the following values.

  • position : (0, 0, 1)
  • rotation : (0, 0, 0, 1)
  • FOV : Pi / 2 = 1.57079632679
  • width : 1000
  • height : 1000
  • near_plane : 0.01
  • far_plane : 100.0.
Parameters
[out]cameraCamera that is initialized.

◆ sc_camera_look_at()

void sc_camera_look_at ( sc_camera_t *  camera,
const sc_camera_vec3_t  eye,
const sc_camera_vec3_t  center,
const sc_camera_vec3_t  up 
)

Sets the position and rotation of the camera object.

The vectors (eye - center) and up have to be linear independent (thus also not zero).

Parameters
[out]cameraCamera that is changed.
[in]eyePosition of the camera.
[in]centerPoint that is in the center of the image.
[in]upUpward direction of the camera.

◆ sc_camera_new()

sc_camera_t * sc_camera_new ( void  )

Creates a new camera structure with the default values (see sc_camera_init).

The returned pointer must be eventually freed with sc_camera_destroy.

Returns
Camera with default values.

◆ sc_camera_perspective_division()

void sc_camera_perspective_division ( sc_array_t *  points_in,
sc_array_t *  points_out 
)

Performs perspective division.

This function converts points from homogeneous coordinates (4D) to normalized device coordinates (3D) by dividing the x, y, and z components by the w component.

The input array must store points in homogeneuos coordinates as quadruples of coordinates, i.e.: points_in->elem_size = 4 * sizeof(sc_camera_coords_t)

The output array will store points as triples of coordinates, i.e.: points_out->elem_size = 3 * sizeof(sc_camera_coords_t)

For each input point, the homogeneous component w must be non-zero and not too small in magnitude relative to the maximum absolute component of the 4D vector. In debug builds, this is enforced by a scale-invariant assertion.

Parameters
[in]points_inAn array of points in homogeneous coordinates (4D).
[out]points_outAn array that will contain the points in normalized device coordinates (3D) after perspective division.

◆ sc_camera_pitch()

void sc_camera_pitch ( sc_camera_t *  camera,
double  angle 
)

Rotating the camera around the axis to the right.

The camera is rotated around the x-axis (right direction). The function rotates the camera about angle (radians) amount by the right hand rule. This means a positive angle rotates the camera up.

Parameters
[out]cameraThe camera object.
[in]angleThe angle the camera is rotated.

◆ sc_camera_position()

void sc_camera_position ( sc_camera_t *  camera,
const sc_camera_vec3_t  position 
)

Sets the position.

Parameters
[out]cameraThe camera object.
[in]positionThe new position of the camera object in world space.

◆ sc_camera_projection_transform()

void sc_camera_projection_transform ( const sc_camera_t *  camera,
sc_array_t *  points_in,
sc_array_t *  points_out 
)

Performs the projection transformation from view space to normalized device coordinates.

This function transforms an array of points from view space into normalized device coordinates (NDC) as described in the example. Each input point is a 3D vector (x, y, z), and each output point is represented in homogeneous coordinates (x, y, z, w).

Note that this function maps to 4D since performing the perspective division can lead to numerical instabilties if the mapped point was not clipped before (cf. sc_camera_clipping_pre). Alternatively, one can apply sc_camera_clipping_post to the result of this function and then perform a safe perspective division.

The input array must store points as triples of coordinates, i.e.: points_in->elem_size = 3 * sizeof(sc_camera_coords_t)

The output array stores points as quadruples of coordinates, i.e.: points_out->elem_size = 4 * sizeof(sc_camera_coords_t)

Note
This function is provided for convenience and does not come with optimized linear algebra. Please use sc_camera_get_projection_mat for creating an optimized pipeline.
Parameters
[in]cameraThe camera object defining the projection.
[in]points_inAn array of points, each represented by 3 coordinates (x, y, z) to be transformed.
[out]points_outAn array that will contain the transformed points in homogeneous coordinates (x, y, z, w).

◆ sc_camera_roll()

void sc_camera_roll ( sc_camera_t *  camera,
double  angle 
)

Rotating the camera around the backward axis.

The camera is rotated around the z-axis (backwards direction). The function rotates the camera about angle (radians) amount by the right hand rule. This means a positive angle tilts the camera counter clockwise.

Parameters
[out]cameraThe camera object.
[in]angleThe angle the camera is rotated.

◆ sc_camera_view_transform()

void sc_camera_view_transform ( const sc_camera_t *  camera,
sc_array_t *  points_in,
sc_array_t *  points_out 
)

Performs the view transformation from world space to view space.

This function transforms an array of points from world coordinates into view space as defined by the given camera. Each input point is a 3D vector (x, y, z), and each output point is represented in the same way.

The input array must store points as triples of coordinates, i.e.: points_in->elem_size = 3 * sizeof(sc_camera_coords_t)

The output array stores points in the same format: points_out->elem_size = 3 * sizeof(sc_camera_coords_t)

Note
This function is provided for convenience and does not come with optimized linear algebra. Please use sc_camera_get_view_mat for creating an optimized pipeline.
Parameters
[in]cameraThe camera object defining the view transformation.
[in]points_inAn array of points, each represented by 3 coordinates (x, y, z) in world space.
[out]points_outAn array that will contain the transformed points in view space.

◆ sc_camera_walk()

void sc_camera_walk ( sc_camera_t *  camera,
double  distance 
)

Move the camera backward/forward along the view direction.

The camera position is translated parallel to the view direction. Since the camera looks down the negative z-axis, a positive walking distance moves the camera backwards.

Parameters
[out]cameraThe camera object.
[in]distanceDistance to move the camera by. A positive value moves the camera backwards.

◆ sc_camera_yaw()

void sc_camera_yaw ( sc_camera_t *  camera,
double  angle 
)

Rotating the camera around the up axis.

The camera is rotated around the y-axis (up direction). The function rotates the camera about angle (radians) amount by the right hand rule. This means a positive angle rotates the camera to the left.

Parameters
[out]cameraThe camera object.
[in]angleThe angle the camera is rotated.