Top |
graphene_matrix_t is a type that provides a 4x4 square matrix, useful for representing 3D transformations.
The matrix is treated as row-major, i.e. it has four vectors (x, y, z, and w) representing rows, and elements of each vector are a column:
⎡ m.x ⎤ ⎛ x.x x.y x.z x.w ⎞ ⎜ m.y ⎟ -\ ⎜ y.x y.y y.z y.w ⎟ ⎜ m.z ⎟ -/ ⎜ z.x z.y z.z z.w ⎟ ⎣ m.w ⎦ ⎝ w.x w.y w.z w.w ⎠
It is possible to easily convert a graphene_matrix_t to and from an array of floating point values that can be used with other libraries.
The contents of a graphene_matrix_t are private, and direct access is not possible. You can modify and read the contents of a graphene_matrix_t only through the provided API.
Graphene uses left-multiplication for all its operations on vectors and
matrices; in other words, given a matrix A
and a vector b
, the result
of a multiplication is going to be:
1 |
res = b × A |
Multiplying two matrices, on the other hand, will use right-multiplication;
given two matrices A
and B
, the result of the multiplication is going
to be
1 |
res = A × B |
as the implementation will multiply each row vector of matrix A
with the
matrix B
to obtain the new row vectors of the result matrix:
1 2 3 4 |
res = ⎡ A.x × B ⎤ ⎜ A.y × B ⎟ ⎜ A.z × B ⎟ ⎣ A.w × B ⎦ |
For more information, see the documentation for graphene_simd4x4f_t, especially the following functions:
graphene_simd4x4f_vec4_mul()
graphene_simd4x4f_vec3_mul()
graphene_simd4x4f_point3_mul()
graphene_simd4x4f_matrix_mul()
graphene_matrix_t *
graphene_matrix_alloc (void
);
Allocates a new graphene_matrix_t.
[constructor]
Since: 1.0
void
graphene_matrix_free (graphene_matrix_t *m
);
Frees the resources allocated by graphene_matrix_alloc()
.
Since: 1.0
graphene_matrix_t *
graphene_matrix_init_identity (graphene_matrix_t *m
);
Initializes a graphene_matrix_t with the identity matrix.
Since: 1.0
graphene_matrix_t * graphene_matrix_init_from_float (graphene_matrix_t *m
,const float *v
);
Initializes a graphene_matrix_t with the given array of floating point values.
Since: 1.0
graphene_matrix_t * graphene_matrix_init_from_vec4 (graphene_matrix_t *m
,const graphene_vec4_t *v0
,const graphene_vec4_t *v1
,const graphene_vec4_t *v2
,const graphene_vec4_t *v3
);
Initializes a graphene_matrix_t with the given four row vectors.
m |
||
v0 |
the first row vector |
|
v1 |
the second row vector |
|
v2 |
the third row vector |
|
v3 |
the fourth row vector |
Since: 1.0
graphene_matrix_t * graphene_matrix_init_from_matrix (graphene_matrix_t *m
,const graphene_matrix_t *src
);
Initializes a graphene_matrix_t using the values of the given matrix.
Since: 1.0
graphene_matrix_t * graphene_matrix_init_from_2d (graphene_matrix_t *m
,double xx
,double yx
,double xy
,double yy
,double x_0
,double y_0
);
Initializes a graphene_matrix_t from the values of an affine transformation matrix.
The arguments map to the following matrix layout:
⎛ xx yx ⎞ ⎛ a b 0 ⎞ ⎜ xy yy ⎟ = ⎜ c d 0 ⎟ ⎝ x0 y0 ⎠ ⎝ tx ty 1 ⎠
This function can be used to convert between an affine matrix type from other libraries and a graphene_matrix_t.
m |
||
xx |
the xx member |
|
yx |
the yx member |
|
xy |
the xy member |
|
yy |
the yy member |
|
x_0 |
the x0 member |
|
y_0 |
the y0 member |
Since: 1.0
graphene_matrix_t * graphene_matrix_init_perspective (graphene_matrix_t *m
,float fovy
,float aspect
,float z_near
,float z_far
);
Initializes a graphene_matrix_t with a perspective projection.
m |
||
fovy |
the field of view angle, in degrees |
|
aspect |
the aspect value |
|
z_near |
the near Z plane |
|
z_far |
the far Z plane |
Since: 1.0
graphene_matrix_t * graphene_matrix_init_ortho (graphene_matrix_t *m
,float left
,float right
,float top
,float bottom
,float z_near
,float z_far
);
Initializes a graphene_matrix_t with an orthographic projection.
m |
||
left |
the left edge of the clipping plane |
|
right |
the right edge of the clipping plane |
|
top |
the top edge of the clipping plane |
|
bottom |
the bottom edge of the clipping plane |
|
z_near |
the distance of the near clipping plane |
|
z_far |
the distance of the far clipping plane |
Since: 1.0
graphene_matrix_t * graphene_matrix_init_look_at (graphene_matrix_t *m
,const graphene_vec3_t *eye
,const graphene_vec3_t *center
,const graphene_vec3_t *up
);
Initializes a graphene_matrix_t so that it positions the "camera"
at the given eye
coordinates towards an object at the center
coordinates. The top of the camera is aligned to the direction
of the up
vector.
Before the transform, the camera is assumed to be placed at the origin, looking towards the negative Z axis, with the top side of the camera facing in the direction of the Y axis and the right side in the direction of the X axis.
In theory, one could use m
to transform a model of such a camera
into world-space. However, it is more common to use the inverse of
m
to transform another object from world coordinates to the view
coordinates of the camera. Typically you would then apply the
camera projection transform to get from view to screen
coordinates.
m |
||
eye |
the vector describing the position to look from |
|
center |
the vector describing the position to look at |
|
up |
the vector describing the world's upward direction; usually,
this is the |
Since: 1.0
graphene_matrix_t * graphene_matrix_init_frustum (graphene_matrix_t *m
,float left
,float right
,float bottom
,float top
,float z_near
,float z_far
);
Initializes a graphene_matrix_t compatible with graphene_frustum_t.
See also: graphene_frustum_init_from_matrix()
m |
||
left |
distance of the left clipping plane |
|
right |
distance of the right clipping plane |
|
bottom |
distance of the bottom clipping plane |
|
top |
distance of the top clipping plane |
|
z_near |
distance of the near clipping plane |
|
z_far |
distance of the far clipping plane |
Since: 1.2
graphene_matrix_t * graphene_matrix_init_scale (graphene_matrix_t *m
,float x
,float y
,float z
);
Initializes a graphene_matrix_t with the given scaling factors.
m |
||
x |
the scale factor on the X axis |
|
y |
the scale factor on the Y axis |
|
z |
the scale factor on the Z axis |
Since: 1.0
graphene_matrix_t * graphene_matrix_init_translate (graphene_matrix_t *m
,const graphene_point3d_t *p
);
Initializes a graphene_matrix_t with a translation to the given coordinates.
Since: 1.0
graphene_matrix_t * graphene_matrix_init_rotate (graphene_matrix_t *m
,float angle
,const graphene_vec3_t *axis
);
Initializes m
to represent a rotation of angle
degrees on
the axis represented by the axis
vector.
Since: 1.0
graphene_matrix_t * graphene_matrix_init_skew (graphene_matrix_t *m
,float x_skew
,float y_skew
);
Initializes a graphene_matrix_t with a skew transformation with the given factors.
m |
||
x_skew |
skew factor, in radians, on the X axis |
|
y_skew |
skew factor, in radians, on the Y axis |
Since: 1.0
bool
graphene_matrix_is_identity (const graphene_matrix_t *m
);
Checks whether the given graphene_matrix_t is the identity matrix.
Since: 1.0
bool
graphene_matrix_is_2d (const graphene_matrix_t *m
);
Checks whether the given graphene_matrix_t is compatible with an a 2D affine transformation matrix.
Since: 1.0
bool
graphene_matrix_is_backface_visible (const graphene_matrix_t *m
);
Checks whether a graphene_matrix_t has a visible back face.
Since: 1.0
bool
graphene_matrix_is_singular (const graphene_matrix_t *m
);
Checks whether a matrix is singular.
Since: 1.0
void graphene_matrix_to_float (const graphene_matrix_t *m
,float *v
);
Converts a graphene_matrix_t to an array of floating point values.
m |
||
v |
return location for an array of floating point values. The array must be capable of holding at least 16 values. |
[array fixed-size=16][out caller-allocates] |
Since: 1.0
bool graphene_matrix_to_2d (const graphene_matrix_t *m
,double *xx
,double *yx
,double *xy
,double *yy
,double *x_0
,double *y_0
);
Converts a graphene_matrix_t to an affine transformation matrix, if the given matrix is compatible.
The returned values have the following layout:
⎛ xx yx ⎞ ⎛ a b 0 ⎞ ⎜ xy yy ⎟ = ⎜ c d 0 ⎟ ⎝ x0 y0 ⎠ ⎝ tx ty 1 ⎠
This function can be used to convert between a graphene_matrix_t and an affine matrix type from other libraries.
m |
||
xx |
return location for the xx member. |
[out] |
yx |
return location for the yx member. |
[out] |
xy |
return location for the xy member. |
[out] |
yy |
return location for the yy member. |
[out] |
x_0 |
return location for the x0 member. |
[out] |
y_0 |
return location for the y0 member. |
[out] |
Since: 1.0
void graphene_matrix_get_row (const graphene_matrix_t *m
,unsigned int index_
,graphene_vec4_t *res
);
Retrieves the given row vector at index_
inside a matrix.
m |
||
index_ |
the index of the row vector, between 0 and 3 |
|
res |
return location for the graphene_vec4_t that is used to store the row vector. |
[out caller-allocates] |
Since: 1.0
float graphene_matrix_get_value (const graphene_matrix_t *m
,unsigned int row
,unsigned int col
);
Retrieves the value at the given row
and col
index.
Since: 1.0
void graphene_matrix_multiply (const graphene_matrix_t *a
,const graphene_matrix_t *b
,graphene_matrix_t *res
);
Multiplies two graphene_matrix_t.
Matrix multiplication is not commutative in general; the order of the factors matters.
The product of this multiplication is (a
× b
)
Since: 1.0
float
graphene_matrix_determinant (const graphene_matrix_t *m
);
Computes the determinant of the given matrix.
Since: 1.0
void graphene_matrix_transform_vec4 (const graphene_matrix_t *m
,const graphene_vec4_t *v
,graphene_vec4_t *res
);
Transforms the given graphene_vec4_t using the matrix m
.
See also: graphene_simd4x4f_vec4_mul()
Since: 1.0
void graphene_matrix_transform_vec3 (const graphene_matrix_t *m
,const graphene_vec3_t *v
,graphene_vec3_t *res
);
Transforms the given graphene_vec3_t using the matrix m
.
This function will multiply the X, Y, and Z row vectors of the matrix m
with the corresponding components of the vector v
. The W row vector will
be ignored.
See also: graphene_simd4x4f_vec3_mul()
Since: 1.0
void graphene_matrix_transform_point (const graphene_matrix_t *m
,const graphene_point_t *p
,graphene_point_t *res
);
Transforms the given graphene_point_t using the matrix m
.
Unlike graphene_matrix_transform_vec3()
, this function will take into
account the fourth row vector of the graphene_matrix_t when computing
the dot product of each row vector of the matrix.
See also: graphene_simd4x4f_point3_mul()
Since: 1.0
void graphene_matrix_transform_point3d (const graphene_matrix_t *m
,const graphene_point3d_t *p
,graphene_point3d_t *res
);
Transforms the given graphene_point3d_t using the matrix m
.
Unlike graphene_matrix_transform_vec3()
, this function will take into
account the fourth row vector of the graphene_matrix_t when computing
the dot product of each row vector of the matrix.
See also: graphene_simd4x4f_point3_mul()
Since: 1.2
void graphene_matrix_transform_rect (const graphene_matrix_t *m
,const graphene_rect_t *r
,graphene_quad_t *res
);
Transforms each corner of a graphene_rect_t using the given matrix m
.
The result is a coplanar quadrilateral.
See also: graphene_matrix_transform_point()
Since: 1.0
void graphene_matrix_transform_bounds (const graphene_matrix_t *m
,const graphene_rect_t *r
,graphene_rect_t *res
);
Transforms each corner of a graphene_rect_t using the given matrix m
.
The result is the axis aligned bounding rectangle containing the coplanar quadrilateral.
See also: graphene_matrix_transform_point()
m |
||
r |
||
res |
return location for the bounds of the transformed rectangle. |
[out caller-allocates] |
Since: 1.0
void graphene_matrix_transform_box (const graphene_matrix_t *m
,const graphene_box_t *b
,graphene_box_t *res
);
Transforms the vertices of a graphene_box_t using the given matrix m
.
The result is the axis aligned bounding box containing the transformed vertices.
Since: 1.2
void graphene_matrix_transform_sphere (const graphene_matrix_t *m
,const graphene_sphere_t *s
,graphene_sphere_t *res
);
Transforms a graphene_sphere_t using the given matrix m
. The
result is the bounding sphere containing the transformed sphere.
Since: 1.2
void graphene_matrix_transform_ray (const graphene_matrix_t *m
,const graphene_ray_t *r
,graphene_ray_t *res
);
Transform a graphene_ray_t using the given matrix m
.
Since: 1.4
void graphene_matrix_project_point (const graphene_matrix_t *m
,const graphene_point_t *p
,graphene_point_t *res
);
Projects a graphene_point_t using the matrix m
.
Since: 1.0
void graphene_matrix_project_rect_bounds (const graphene_matrix_t *m
,const graphene_rect_t *r
,graphene_rect_t *res
);
Projects a graphene_rect_t using the given matrix.
The resulting rectangle is the axis aligned bounding rectangle capable of fully containing the projected rectangle.
Since: 1.0
void graphene_matrix_project_rect (const graphene_matrix_t *m
,const graphene_rect_t *r
,graphene_quad_t *res
);
Projects all corners of a graphene_rect_t using the given matrix.
See also: graphene_matrix_project_point()
Since: 1.2
bool graphene_matrix_untransform_point (const graphene_matrix_t *m
,const graphene_point_t *p
,const graphene_rect_t *bounds
,graphene_point_t *res
);
Undoes the transformation of a graphene_point_t using the
given matrix, within the given axis aligned rectangular bounds
.
m |
||
p |
||
bounds |
the bounds of the transformation |
|
res |
return location for the untransformed point. |
[out caller-allocates] |
Since: 1.0
void graphene_matrix_untransform_bounds (const graphene_matrix_t *m
,const graphene_rect_t *r
,const graphene_rect_t *bounds
,graphene_rect_t *res
);
Undoes the transformation on the corners of a graphene_rect_t using the
given matrix, within the given axis aligned rectangular bounds
.
m |
||
r |
||
bounds |
the bounds of the transformation |
|
res |
return location for the untransformed rectangle. |
[out caller-allocates] |
Since: 1.0
void graphene_matrix_unproject_point3d (const graphene_matrix_t *projection
,const graphene_matrix_t *modelview
,const graphene_point3d_t *point
,graphene_point3d_t *res
);
Unprojects the given point
using the projection
matrix and
a modelview
matrix.
projection |
a graphene_matrix_t for the projection matrix |
|
modelview |
a graphene_matrix_t for the modelview matrix; this is the inverse of the modelview used when projecting the point |
|
point |
a graphene_point3d_t with the coordinates of the point |
|
res |
return location for the unprojected point. |
[out caller-allocates] |
Since: 1.2
void graphene_matrix_translate (graphene_matrix_t *m
,const graphene_point3d_t *pos
);
Adds a translation transformation to m
using the coordinates
of the given graphene_point3d_t.
This is the equivalent of calling graphene_matrix_init_translate()
and
then multiplying m
with the translation matrix.
Since: 1.0
void graphene_matrix_rotate (graphene_matrix_t *m
,float angle
,const graphene_vec3_t *axis
);
Adds a rotation transformation to m
, using the given angle
and axis
vector.
This is the equivalent of calling graphene_matrix_init_rotate()
and
then multiplying the matrix m
with the rotation matrix.
Since: 1.0
void graphene_matrix_rotate_x (graphene_matrix_t *m
,float angle
);
Adds a rotation transformation around the X axis to m
, using
the given angle
.
See also: graphene_matrix_rotate()
Since: 1.0
void graphene_matrix_rotate_y (graphene_matrix_t *m
,float angle
);
Adds a rotation transformation around the Y axis to m
, using
the given angle
.
See also: graphene_matrix_rotate()
Since: 1.0
void graphene_matrix_rotate_z (graphene_matrix_t *m
,float angle
);
Adds a rotation transformation around the Z axis to m
, using
the given angle
.
See also: graphene_matrix_rotate()
Since: 1.0
void graphene_matrix_rotate_quaternion (graphene_matrix_t *m
,const graphene_quaternion_t *q
);
Adds a rotation transformation to m
, using the given
graphene_quaternion_t.
This is the equivalent of calling graphene_quaternion_to_matrix()
and
then multiplying m
with the rotation matrix.
Since: 1.2
void graphene_matrix_rotate_euler (graphene_matrix_t *m
,const graphene_euler_t *e
);
Adds a rotation transformation to m
, using the given
graphene_euler_t.
Since: 1.2
void graphene_matrix_scale (graphene_matrix_t *m
,float factor_x
,float factor_y
,float factor_z
);
Adds a scaling transformation to m
, using the three
given factors.
This is the equivalent of calling graphene_matrix_init_scale()
and then
multiplying the matrix m
with the scale matrix.
m |
||
factor_x |
scaling factor on the X axis |
|
factor_y |
scaling factor on the Y axis |
|
factor_z |
scaling factor on the Z axis |
Since: 1.0
void graphene_matrix_skew_xy (graphene_matrix_t *m
,float factor
);
Adds a skew of factor
on the X and Y axis to the given matrix.
Since: 1.0
void graphene_matrix_skew_xz (graphene_matrix_t *m
,float factor
);
Adds a skew of factor
on the X and Z axis to the given matrix.
Since: 1.0
void graphene_matrix_skew_yz (graphene_matrix_t *m
,float factor
);
Adds a skew of factor
on the Y and Z axis to the given matrix.
Since: 1.0
void graphene_matrix_transpose (const graphene_matrix_t *m
,graphene_matrix_t *res
);
Transposes the given matrix.
Since: 1.0
bool graphene_matrix_inverse (const graphene_matrix_t *m
,graphene_matrix_t *res
);
Inverts the given matrix.
Since: 1.0
void graphene_matrix_perspective (const graphene_matrix_t *m
,float depth
,graphene_matrix_t *res
);
Applies a perspective of depth
to the matrix.
m |
||
depth |
the depth of the perspective |
|
res |
return location for the perspective matrix. |
[out caller-allocates] |
Since: 1.0
void graphene_matrix_normalize (const graphene_matrix_t *m
,graphene_matrix_t *res
);
Normalizes the given graphene_matrix_t.
Since: 1.0
float
graphene_matrix_get_x_translation (const graphene_matrix_t *m
);
Retrieves the translation component on the X axis from m
.
Since: 1.10
float
graphene_matrix_get_y_translation (const graphene_matrix_t *m
);
Retrieves the translation component on the Y axis from m
.
Since: 1.10
float
graphene_matrix_get_z_translation (const graphene_matrix_t *m
);
Retrieves the translation component on the Z axis from m
.
Since: 1.10
float
graphene_matrix_get_x_scale (const graphene_matrix_t *m
);
Retrieves the scaling factor on the X axis in m
.
Since: 1.0
float
graphene_matrix_get_y_scale (const graphene_matrix_t *m
);
Retrieves the scaling factor on the Y axis in m
.
Since: 1.0
float
graphene_matrix_get_z_scale (const graphene_matrix_t *m
);
Retrieves the scaling factor on the Z axis in m
.
Since: 1.0
bool graphene_matrix_decompose (const graphene_matrix_t *m
,graphene_vec3_t *translate
,graphene_vec3_t *scale
,graphene_quaternion_t *rotate
,graphene_vec3_t *shear
,graphene_vec4_t *perspective
);
Decomposes a transformation matrix into its component transformations.
The algorithm for decomposing a matrix is taken from the CSS3 Transforms specification; specifically, the decomposition code is based on the equivalent code published in "Graphics Gems II", edited by Jim Arvo, and available online.
void graphene_matrix_interpolate (const graphene_matrix_t *a
,const graphene_matrix_t *b
,double factor
,graphene_matrix_t *res
);
Linearly interpolates the two given graphene_matrix_t by interpolating the decomposed transformations separately.
If either matrix cannot be reduced to their transformations then the interpolation cannot be performed, and this function will return an identity matrix.
a |
||
b |
||
factor |
the linear interpolation factor |
|
res |
return location for the interpolated matrix. |
[out caller-allocates] |
Since: 1.0
bool graphene_matrix_equal (const graphene_matrix_t *a
,const graphene_matrix_t *b
);
Checks whether the two given graphene_matrix_t matrices are equal.
Since: 1.10
bool graphene_matrix_equal_fast (const graphene_matrix_t *a
,const graphene_matrix_t *b
);
Checks whether the two given graphene_matrix_t matrices are byte-by-byte equal.
While this function is faster than graphene_matrix_equal()
, it
can also return false negatives, so it should be used in
conjuction with either graphene_matrix_equal()
or
graphene_matrix_near()
. For instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
if (graphene_matrix_equal_fast (a, b)) { // matrices are definitely the same } else { if (graphene_matrix_equal (a, b)) // matrices contain the same values within an epsilon of FLT_EPSILON else if (graphene_matrix_near (a, b, 0.0001)) // matrices contain the same values within an epsilon of 0.0001 else // matrices are not equal } |
Since: 1.10
bool graphene_matrix_near (const graphene_matrix_t *a
,const graphene_matrix_t *b
,float epsilon
);
Compares the two given graphene_matrix_t matrices and checks
whether their values are within the given epsilon
of each
other.
Since: 1.10
void
graphene_matrix_print (const graphene_matrix_t *m
);
Prints the contents of a matrix to the standard error stream.
This function is only useful for debugging; there are no guarantees made on the format of the output.
Since: 1.0
typedef struct { } graphene_matrix_t;
A structure capable of holding a 4x4 matrix.
The contents of the graphene_matrix_t structure are private and should never be accessed directly.