Cogl 2.0 Reference Manual  

Top  Description 
CoglMatrixStack; CoglMatrixEntry; CoglMatrixStack * cogl_matrix_stack_new (CoglContext *ctx
); void cogl_matrix_stack_push (CoglMatrixStack *stack
); void cogl_matrix_stack_pop (CoglMatrixStack *stack
); void cogl_matrix_stack_load_identity (CoglMatrixStack *stack
); void cogl_matrix_stack_scale (CoglMatrixStack *stack
,float x
,float y
,float z
); void cogl_matrix_stack_translate (CoglMatrixStack *stack
,float x
,float y
,float z
); void cogl_matrix_stack_rotate (CoglMatrixStack *stack
,float angle
,float x
,float y
,float z
); void cogl_matrix_stack_rotate_quaternion (CoglMatrixStack *stack
,const CoglQuaternion *quaternion
); void cogl_matrix_stack_rotate_euler (CoglMatrixStack *stack
,const CoglEuler *euler
); void cogl_matrix_stack_multiply (CoglMatrixStack *stack
,const CoglMatrix *matrix
); void cogl_matrix_stack_frustum (CoglMatrixStack *stack
,float left
,float right
,float bottom
,float top
,float z_near
,float z_far
); void cogl_matrix_stack_perspective (CoglMatrixStack *stack
,float fov_y
,float aspect
,float z_near
,float z_far
); void cogl_matrix_stack_orthographic (CoglMatrixStack *stack
,float x_1
,float y_1
,float x_2
,float y_2
,float near
,float far
); CoglBool cogl_matrix_stack_get_inverse (CoglMatrixStack *stack
,CoglMatrix *inverse
); CoglMatrixEntry * cogl_matrix_stack_get_entry (CoglMatrixStack *stack
); CoglMatrix * cogl_matrix_stack_get (CoglMatrixStack *stack
,CoglMatrix *matrix
); CoglMatrix * cogl_matrix_entry_get (CoglMatrixEntry *entry
,CoglMatrix *matrix
); void cogl_matrix_stack_set (CoglMatrixStack *stack
,const CoglMatrix *matrix
); CoglBool cogl_matrix_entry_calculate_translation (CoglMatrixEntry *entry0
,CoglMatrixEntry *entry1
,float *x
,float *y
,float *z
); CoglBool cogl_matrix_entry_is_identity (CoglMatrixEntry *entry
); CoglBool cogl_matrix_entry_equal (CoglMatrixEntry *entry0
,CoglMatrixEntry *entry1
); CoglMatrixEntry * cogl_matrix_entry_ref (CoglMatrixEntry *entry
); void cogl_matrix_entry_unref (CoglMatrixEntry *entry
);
Matrices can be used (for example) to describe the modelview transforms of objects, texture transforms, and projective transforms.
The CoglMatrix api provides a good way to manipulate individual matrices representing a single transformation but if you need to track manymany such transformations for many objects that are organized in a scenegraph for example then using a separate CoglMatrix for each object may not be the most efficient way.
A CoglMatrixStack enables applications to track lots of transformations that are related to each other in some kind of hierarchy. In a scenegraph for example if you want to know how to transform a particular node then you usually have to walk up through the ancestors and accumulate their transforms before finally applying the transform of the node itself. In this model things are grouped together spatially according to their ancestry and all siblings with the same parent share the same initial transformation. The CoglMatrixStack API is suited to tracking lots of transformations that fit this kind of model.
Compared to using the CoglMatrix api directly to track many related transforms, these can be some advantages to using a CoglMatrixStack:
For reference (to give an idea of when a CoglMatrixStack can provide a space saving) a CoglMatrix can be expected to take 72 bytes whereas a single CoglMatrixEntry in a CoglMatrixStack is currently around 32 bytes on a 32bit CPU or 36 bytes on a 64bit CPU. An entry is needed for each individual operation applied to the stack (such as rotate, scale, translate) so if most of your leaf node transformations only need one or two simple operations relative to their parent then a matrix stack will likely take less space than having a CoglMatrix for each node.
Even without any space saving though the ability to perform fast comparisons and avoid redundant arithmetic (especially sine and cosine calculations for rotations) can make using a matrix stack worthwhile.
typedef struct _CoglMatrixStack CoglMatrixStack;
Tracks your current position within a hierarchy and lets you build up a graph of transformations as you traverse through a hierarchy such as a scenegraph.
A CoglMatrixStack always maintains a reference to a single
transformation at any point in time, representing the
transformation at the current position in the hierarchy. You can
get a reference to the current transformation by calling
cogl_matrix_stack_get_entry()
.
When a CoglMatrixStack is first created with
cogl_matrix_stack_new()
then it is conceptually positioned at the
root of your hierarchy and the current transformation simply
represents an identity transformation.
As you traverse your object hierarchy (your scenegraph) then you
should call cogl_matrix_stack_push()
whenever you move down one
level and call cogl_matrix_stack_pop()
whenever you move back up
one level towards the root.
At any time you can apply a set of operations, such as "rotate",
"scale", "translate" on top of the current transformation of a
CoglMatrixStack using functions such as
cogl_matrix_stack_rotate()
, cogl_matrix_stack_scale()
and
cogl_matrix_stack_translate()
. These operations will derive a new
current transformation and will never affect a transformation
that you have referenced using cogl_matrix_stack_get_entry()
.
Internally applying operations to a CoglMatrixStack builds up a graph of CoglMatrixEntry structures which each represent a single immutable transform.
typedef struct _CoglMatrixEntry CoglMatrixEntry;
Represents a single immutable transformation that was retrieved
from a CoglMatrixStack using cogl_matrix_stack_get_entry()
.
Internally a CoglMatrixEntry represents a single matrix operation (such as "rotate", "scale", "translate") which is applied to the transform of a single parent entry.
Using the CoglMatrixStack api effectively builds up a graph of these immutable CoglMatrixEntry structures whereby operations that can be shared between multiple transformations will result in shared CoglMatrixEntry nodes in the graph.
When a CoglMatrixStack is first created it references one CoglMatrixEntry that represents a single "load identity" operation. This serves as the root entry and all operations that are then applied to the stack will extend the graph starting from this root "load identity" entry.
Given the typical usage model for a CoglMatrixStack and the way the entries are built up while traversing a scenegraph then in most cases where an application is interested in comparing two transformations for equality then it is enough to simply compare two CoglMatrixEntry pointers directly. Technically this can lead to false negatives that could be identified with a deeper comparison but often these false negatives are unlikely and don't matter anyway so this enables extremely cheap comparisons.
cogl_matrix_entry_ref()
and cogl_matrix_entry_unref()
not with
cogl_object_ref()
and cogl_object_unref()
.
CoglMatrixStack * cogl_matrix_stack_new (CoglContext *ctx
);
Allocates a new CoglMatrixStack that can be used to build up transformations relating to objects in a scenegraph like hierarchy. (See the description of CoglMatrixStack and CoglMatrixEntry for more details of what a matrix stack is best suited for)
When a CoglMatrixStack is first allocated it is conceptually
positioned at the root of your scenegraph hierarchy. As you
traverse your scenegraph then you should call
cogl_matrix_stack_push()
whenever you move down a level and
cogl_matrix_stack_pop()
whenever you move back up a level towards
the root.
Once you have allocated a CoglMatrixStack you can get a reference
to the current transformation for the current position in the
hierarchy by calling cogl_matrix_stack_get_entry()
.
Once you have allocated a CoglMatrixStack you can apply operations
such as rotate, scale and translate to modify the current transform
for the current position in the hierarchy by calling
cogl_matrix_stack_rotate()
, cogl_matrix_stack_scale()
and
cogl_matrix_stack_translate()
.

A CoglContext 
Returns : 
A newly allocated CoglMatrixStack 
void cogl_matrix_stack_push (CoglMatrixStack *stack
);
Saves the current transform and starts a new transform that derives from the current transform.
This is usually called while traversing a scenegraph whenever you
traverse one level deeper. cogl_matrix_stack_pop()
can then be
called when going back up one layer to restore the previous
transform of an ancestor.

A CoglMatrixStack 
void cogl_matrix_stack_pop (CoglMatrixStack *stack
);
Restores the previous transform that was last saved by calling
cogl_matrix_stack_push()
.
This is usually called while traversing a scenegraph whenever you return up one level in the graph towards the root node.

A CoglMatrixStack 
void cogl_matrix_stack_load_identity (CoglMatrixStack *stack
);
Resets the current matrix to the identity matrix.

A CoglMatrixStack 
void cogl_matrix_stack_scale (CoglMatrixStack *stack
,float x
,float y
,float z
);
Multiplies the current matrix by one that scales the x, y and z axes by the given values.

A CoglMatrixStack 

Amount to scale along the xaxis 

Amount to scale along the yaxis 

Amount to scale along the zaxis 
void cogl_matrix_stack_translate (CoglMatrixStack *stack
,float x
,float y
,float z
);
Multiplies the current matrix by one that translates along all three axes according to the given values.

A CoglMatrixStack 

Distance to translate along the xaxis 

Distance to translate along the yaxis 

Distance to translate along the zaxis 
void cogl_matrix_stack_rotate (CoglMatrixStack *stack
,float angle
,float x
,float y
,float z
);
Multiplies the current matrix by one that rotates the around the
axisvector specified by x
, y
and z
. The rotation follows the
righthand thumb rule so for example rotating by 10 degrees about
the axisvector (0, 0, 1) causes a small counterclockwise
rotation.

A CoglMatrixStack 

Angle in degrees to rotate. 

Xcomponent of vertex to rotate around. 

Ycomponent of vertex to rotate around. 

Zcomponent of vertex to rotate around. 
void cogl_matrix_stack_rotate_quaternion (CoglMatrixStack *stack
,const CoglQuaternion *quaternion
);
Multiplies the current matrix by one that rotates according to the
rotation described by quaternion
.

A CoglMatrixStack 

A CoglQuaternion 
void cogl_matrix_stack_rotate_euler (CoglMatrixStack *stack
,const CoglEuler *euler
);
Multiplies the current matrix by one that rotates according to the
rotation described by euler
.

A CoglMatrixStack 

A CoglEuler 
void cogl_matrix_stack_multiply (CoglMatrixStack *stack
,const CoglMatrix *matrix
);
Multiplies the current matrix by the given matrix.

A CoglMatrixStack 

the matrix to multiply with the current modelview 
void cogl_matrix_stack_frustum (CoglMatrixStack *stack
,float left
,float right
,float bottom
,float top
,float z_near
,float z_far
);
Replaces the current matrix with a perspective matrix for a given viewing frustum defined by 4 side clip planes that all cross through the origin and 2 near and far clip planes.

A CoglMatrixStack 

X position of the left clipping plane where it intersects the near clipping plane 

X position of the right clipping plane where it intersects the near clipping plane 

Y position of the bottom clipping plane where it intersects the near clipping plane 

Y position of the top clipping plane where it intersects the near clipping plane 

The distance to the near clipping plane (Must be positive) 

The distance to the far clipping plane (Must be positive) 
void cogl_matrix_stack_perspective (CoglMatrixStack *stack
,float fov_y
,float aspect
,float z_near
,float z_far
);
Replaces the current matrix with a perspective matrix based on the provided values.
z_far
/ z_near
ratio since that will reduce the effectiveness of depth testing
since there wont be enough precision to identify the depth of
objects near to each other.

A CoglMatrixStack 

Vertical field of view angle in degrees. 

The (width over height) aspect ratio for display 

The distance to the near clipping plane (Must be positive, and must not be 0) 

The distance to the far clipping plane (Must be positive) 
void cogl_matrix_stack_orthographic (CoglMatrixStack *stack
,float x_1
,float y_1
,float x_2
,float y_2
,float near
,float far
);
Replaces the current matrix with an orthographic projection matrix.

A CoglMatrixStack 

The x coordinate for the first vertical clipping plane 

The y coordinate for the first horizontal clipping plane 

The x coordinate for the second vertical clipping plane 

The y coordinate for the second horizontal clipping plane 

The distance to the near clipping plane (will be negative if the plane is behind the viewer) 

The distance to the far clipping plane (will be negative if the plane is behind the viewer) 
CoglBool cogl_matrix_stack_get_inverse (CoglMatrixStack *stack
,CoglMatrix *inverse
);
Gets the inverse transform of the current matrix and uses it to initialize a new CoglMatrix.

A CoglMatrixStack 

The destination for a 4x4 inverse transformation matrix. [out] 
Returns : 
TRUE if the inverse was successfully calculated or FALSE
for degenerate transformations that can't be inverted (in this case the
inverse matrix will simply be initialized with the identity matrix) 
CoglMatrixEntry * cogl_matrix_stack_get_entry (CoglMatrixStack *stack
);
Gets a reference to the current transform represented by a CoglMatrixEntry pointer.
cogl_matrix_entry_ref()
and cogl_matrix_entry_unref()
and you
should call cogl_matrix_entry_unref()
when you are finished with
and entry you get via cogl_matrix_stack_get_entry()
.

A CoglMatrixStack 
Returns : 
A pointer to the CoglMatrixEntry representing the current matrix stack transform. [transfer none] 
CoglMatrix * cogl_matrix_stack_get (CoglMatrixStack *stack
,CoglMatrix *matrix
);
Resolves the current stack
transform into a CoglMatrix by
combining the operations that have been applied to build up the
current transform.
There are two possible ways that this function may return its result depending on whether the stack is able to directly point to an internal CoglMatrix or whether the result needs to be composed of multiple operations.
If an internal matrix contains the required result then this
function will directly return a pointer to that matrix, otherwise
if the function returns NULL
then matrix
will be initialized
to match the current transform of stack
.
matrix
will be left untouched if a direct pointer is
returned.

A CoglMatrixStack 

The potential destination for the current matrix. [out] 
Returns : 
A direct pointer to the current transform or NULL
and in that case matrix will be initialized with
the value of the current transform. 
CoglMatrix * cogl_matrix_entry_get (CoglMatrixEntry *entry
,CoglMatrix *matrix
);
Resolves the current entry
transform into a CoglMatrix by
combining the sequence of operations that have been applied to
build up the current transform.
There are two possible ways that this function may return its result depending on whether it's possible to directly point to an internal CoglMatrix or whether the result needs to be composed of multiple operations.
If an internal matrix contains the required result then this
function will directly return a pointer to that matrix, otherwise
if the function returns NULL
then matrix
will be initialized
to match the transform of entry
.
matrix
will be left untouched if a direct pointer is
returned.

A CoglMatrixEntry 

The potential destination for the transform as a matrix. [out] 
Returns : 
A direct pointer to a CoglMatrix transform or NULL
and in that case matrix will be initialized with
the effective transform represented by entry . 
void cogl_matrix_stack_set (CoglMatrixStack *stack
,const CoglMatrix *matrix
);
Replaces the current stack
matrix value with the value of matrix
.
This effectively discards any other operations that were applied
since the last time cogl_matrix_stack_push()
was called or since
the stack was initialized.

A CoglMatrixStack 

A CoglMatrix replace the current matrix value with 
CoglBool cogl_matrix_entry_calculate_translation (CoglMatrixEntry *entry0
,CoglMatrixEntry *entry1
,float *x
,float *y
,float *z
);
Determines if the only difference between two transforms is a
translation and if so returns what the x
, y
, and z
components of
the translation are.
If the difference between the two translations involves anything
other than a translation then the function returns FALSE
.

The first reference transform 

A second reference transform 

The destination for the xcomponent of the translation. [out] 

The destination for the ycomponent of the translation. [out] 

The destination for the zcomponent of the translation. [out] 
Returns : 
TRUE if the only difference between the transform of
entry0 and the transform of entry1 is a translation,
otherwise FALSE . 
CoglBool cogl_matrix_entry_is_identity (CoglMatrixEntry *entry
);
Determines whether entry
is known to represent an identity
transform.
If this returns TRUE
then the entry is definitely the identity
matrix. If it returns FALSE
it may or may not be the identity
matrix but no expensive comparison is performed to verify it.

A CoglMatrixEntry 
Returns : 
TRUE if entry is definitely an identity transform,
otherwise FALSE . 
CoglBool cogl_matrix_entry_equal (CoglMatrixEntry *entry0
,CoglMatrixEntry *entry1
);
Compares two arbitrary CoglMatrixEntry transforms for equality
returning TRUE
if they are equal or FALSE
otherwise.

The first CoglMatrixEntry to compare 

A second CoglMatrixEntry to compare 
Returns : 
TRUE if entry0 represents the same transform as
entry1 , otherwise FALSE . 
CoglMatrixEntry * cogl_matrix_entry_ref (CoglMatrixEntry *entry
);
Takes a reference on the given entry
to ensure the entry
stays
alive and remains valid. When you are finished with the entry
then
you should call cogl_matrix_entry_unref()
.
It is an error to pass an entry
pointer to cogl_object_ref()
and
cogl_object_unref()

A CoglMatrixEntry 
void cogl_matrix_entry_unref (CoglMatrixEntry *entry
);
Releases a reference on entry
either taken by calling
cogl_matrix_entry_unref()
or to release the reference given when
calling cogl_matrix_stack_get_entry()
.

A CoglMatrixEntry 