[Cogl] the cogl_get_*_matrix functions now work with CoglMatrix types

Since the CoglMatrix type was added for supporting texture matrices recently
it made sense to be consistent accross the Cogl API and use the Cogl type
over the GL style GLfloat m[16] arrays.
This commit is contained in:
Robert Bragg 2009-02-18 18:54:54 +00:00
parent e6a7a70d54
commit e91fe8817d
6 changed files with 117 additions and 47 deletions

View File

@ -140,6 +140,42 @@ void cogl_matrix_scale (CoglMatrix *matrix,
float sy, float sy,
float sz); float sz);
/**
* cogl_matrix_transform_point:
* @matrix: A 4x4 transformation matrix
* @x: The X component of your points position [in:out]
* @y: The Y component of your points position [in:out]
* @z: The Z component of your points position [in:out]
* @w: The W component of your points position [in:out]
*
* This transforms a point whos position is given and returned
* as four float components.
*/
void
cogl_matrix_transform_point (const CoglMatrix *matrix,
float *x,
float *y,
float *z,
float *w);
/**
* cogl_matrix_init_from_gl_matrix:
* @matrix: A 4x4 transformation matrix
* @gl_matrix: A linear array of 16 Glfloats (column-major)
*
* This initialises @matrix with the contents of @gl_matrix
*/
void cogl_matrix_init_from_gl_matrix (CoglMatrix *matrix, const float *gl_matrix);
/**
* cogl_matrix_get_gl_matrix:
* @matrix: A 4x4 transformation matrix
*
* This casts a CoglMatrix to a GLfloat array which can be directly passed to
* OpenGL.
*/
const float *cogl_matrix_get_gl_matrix (const CoglMatrix *matrix);
G_END_DECLS G_END_DECLS
#endif /* __COGL_MATRIX_H */ #endif /* __COGL_MATRIX_H */

View File

@ -268,21 +268,19 @@ void cogl_rotate (float angle,
/** /**
* cogl_get_modelview_matrix: * cogl_get_modelview_matrix:
* @m: pointer to a 4x4 array of #float<!-- -->s to receive the matrix * @matrix: pointer to a CoglMatrix to recieve the matrix
* *
* Stores the current model-view matrix in @m. The matrix is in * Stores the current model-view matrix in @matrix.
* column-major order.
*/ */
void cogl_get_modelview_matrix (float m[16]); void cogl_get_modelview_matrix (CoglMatrix *matrix);
/** /**
* cogl_get_projection_matrix: * cogl_get_projection_matrix:
* @m: pointer to a 4x4 array of #float<!-- -->s to receive the matrix * @matrix: pointer to a CoglMatrix to recieve the matrix
* *
* Stores the current projection matrix in @m. The matrix is in * Stores the current projection matrix in @matrix.
* column-major order.
*/ */
void cogl_get_projection_matrix (float m[16]); void cogl_get_projection_matrix (CoglMatrix *matrix);
/** /**
* cogl_get_viewport: * cogl_get_viewport:

View File

@ -52,7 +52,7 @@ void _cogl_add_path_to_stencil_buffer (floatVec2 nodes_min,
void _cogl_enable_clip_planes (void); void _cogl_enable_clip_planes (void);
void _cogl_disable_clip_planes (void); void _cogl_disable_clip_planes (void);
void _cogl_disable_stencil_buffer (void); void _cogl_disable_stencil_buffer (void);
void _cogl_set_matrix (const float *matrix); void _cogl_set_matrix (const CoglMatrix *matrix);
typedef struct _CoglClipStack CoglClipStack; typedef struct _CoglClipStack CoglClipStack;
@ -81,7 +81,7 @@ struct _CoglClipStackEntryRect
float height; float height;
/* The matrix that was current when the clip was set */ /* The matrix that was current when the clip was set */
float matrix[16]; CoglMatrix matrix;
}; };
struct _CoglClipStackEntryPath struct _CoglClipStackEntryPath
@ -89,7 +89,7 @@ struct _CoglClipStackEntryPath
CoglClipStackEntryType type; CoglClipStackEntryType type;
/* The matrix that was current when the clip was set */ /* The matrix that was current when the clip was set */
float matrix[16]; CoglMatrix matrix;
floatVec2 path_nodes_min; floatVec2 path_nodes_min;
floatVec2 path_nodes_max; floatVec2 path_nodes_max;
@ -120,7 +120,7 @@ cogl_clip_push (float x_offset,
entry->width = width; entry->width = width;
entry->height = height; entry->height = height;
cogl_get_modelview_matrix (entry->matrix); cogl_get_modelview_matrix (&entry->matrix);
/* Store it in the stack */ /* Store it in the stack */
stack->stack_top = g_list_prepend (stack->stack_top, entry); stack->stack_top = g_list_prepend (stack->stack_top, entry);
@ -148,7 +148,7 @@ cogl_clip_push_from_path_preserve (void)
memcpy (entry->path, ctx->path_nodes->data, memcpy (entry->path, ctx->path_nodes->data,
sizeof (CoglPathNode) * ctx->path_nodes->len); sizeof (CoglPathNode) * ctx->path_nodes->len);
cogl_get_modelview_matrix (entry->matrix); cogl_get_modelview_matrix (&entry->matrix);
/* Store it in the stack */ /* Store it in the stack */
stack->stack_top = g_list_prepend (stack->stack_top, entry); stack->stack_top = g_list_prepend (stack->stack_top, entry);
@ -226,7 +226,7 @@ _cogl_clip_stack_rebuild (void)
CoglClipStackEntryPath *path = (CoglClipStackEntryPath *) entry; CoglClipStackEntryPath *path = (CoglClipStackEntryPath *) entry;
cogl_push_matrix (); cogl_push_matrix ();
_cogl_set_matrix (path->matrix); _cogl_set_matrix (&path->matrix);
_cogl_add_path_to_stencil_buffer (path->path_nodes_min, _cogl_add_path_to_stencil_buffer (path->path_nodes_min,
path->path_nodes_max, path->path_nodes_max,
@ -246,7 +246,7 @@ _cogl_clip_stack_rebuild (void)
CoglClipStackEntryRect *rect = (CoglClipStackEntryRect *) entry; CoglClipStackEntryRect *rect = (CoglClipStackEntryRect *) entry;
cogl_push_matrix (); cogl_push_matrix ();
_cogl_set_matrix (rect->matrix); _cogl_set_matrix (&rect->matrix);
/* If this is the first entry and we support clip planes then use /* If this is the first entry and we support clip planes then use
that instead */ that instead */

View File

@ -2,6 +2,7 @@
#include <glib.h> #include <glib.h>
#include <math.h> #include <math.h>
#include <string.h>
void void
cogl_matrix_init_identity (CoglMatrix *matrix) cogl_matrix_init_identity (CoglMatrix *matrix)
@ -51,19 +52,6 @@ cogl_matrix_multiply (CoglMatrix *result,
*result = r; *result = r;
} }
/**
* cogl_3dmatrix_rotate:
* @matrix: A 3D Affine transformation matrix
* @angle: The angle in degrees you want to rotate by
* @x: The X component of your rotation vector
* @y: The Y component of your rotation vector
* @z: The Z component of your rotation vector
*
* The matrix is multiplied with a rotation matrix representing a rotation
* of angle degress around the vector (x,y,z)
*
* Since: 1.0
*/
void void
cogl_matrix_rotate (CoglMatrix *matrix, cogl_matrix_rotate (CoglMatrix *matrix,
float angle, float angle,
@ -137,4 +125,30 @@ cogl_matrix_invert (CoglMatrix *matrix)
} }
#endif #endif
void
cogl_matrix_transform_point (const CoglMatrix *matrix,
float *x,
float *y,
float *z,
float *w)
{
float _x = *x, _y = *y, _z = *z, _w = *w;
*x = matrix->xx * _x + matrix->xy * _y + matrix->xz * _z + matrix->xw * _w;
*y = matrix->yx * _x + matrix->yy * _y + matrix->yz * _z + matrix->yw * _w;
*z = matrix->zx * _x + matrix->zy * _y + matrix->zz * _z + matrix->zw * _w;
*w = matrix->wx * _x + matrix->wy * _y + matrix->wz * _z + matrix->ww * _w;
}
void
cogl_matrix_init_from_gl_matrix (CoglMatrix *matrix, const float *gl_matrix)
{
memcpy (matrix, gl_matrix, sizeof (float) * 16);
}
const float *
cogl_matrix_get_gl_matrix (const CoglMatrix *matrix)
{
return (float *)matrix;
}

View File

@ -530,10 +530,11 @@ _cogl_add_stencil_clip (float x_offset,
} }
void void
_cogl_set_matrix (const float *matrix) _cogl_set_matrix (const CoglMatrix *matrix)
{ {
GE( glLoadIdentity () ); const GLfloat *gl_matrix = cogl_matrix_get_gl_matrix (matrix);
GE( glMultMatrixf (matrix) );
GE (glLoadMatrixf (gl_matrix));
} }
void void
@ -678,7 +679,7 @@ cogl_setup_viewport (guint width,
float z_far) float z_far)
{ {
float z_camera; float z_camera;
float projection_matrix[16]; CoglMatrix projection_matrix;
GE( glViewport (0, 0, width, height) ); GE( glViewport (0, 0, width, height) );
@ -727,8 +728,8 @@ cogl_setup_viewport (guint width,
* doesn't make sense. * doesn't make sense.
*/ */
cogl_get_projection_matrix (projection_matrix); cogl_get_projection_matrix (&projection_matrix);
z_camera = 0.5 * projection_matrix[0]; z_camera = 0.5 * projection_matrix.xx;
GE( glLoadIdentity () ); GE( glLoadIdentity () );
@ -1122,15 +1123,25 @@ cogl_features_available (CoglFeatureFlags features)
} }
void void
cogl_get_modelview_matrix (float m[16]) cogl_get_modelview_matrix (CoglMatrix *matrix)
{ {
float m[16];
glGetFloatv (GL_MODELVIEW_MATRIX, m); glGetFloatv (GL_MODELVIEW_MATRIX, m);
/* Since it's internal to Cogl and CoglMatrix doesn't currently have
* any flag members, we could avoid this extra copy if it really
* bothers anyone */
cogl_matrix_init_from_gl_matrix (matrix, m);
} }
void void
cogl_get_projection_matrix (float m[16]) cogl_get_projection_matrix (CoglMatrix *matrix)
{ {
float m[16];
glGetFloatv (GL_PROJECTION_MATRIX, m); glGetFloatv (GL_PROJECTION_MATRIX, m);
/* Since it's internal to Cogl and CoglMatrix doesn't currently have
* any flag members, we could avoid this extra copy if it really
* bothers anyone */
cogl_matrix_init_from_gl_matrix (matrix, m);
} }
void void

View File

@ -453,10 +453,11 @@ _cogl_add_stencil_clip (float x_offset,
} }
void void
_cogl_set_matrix (const float *matrix) _cogl_set_matrix (const CoglMatrix *matrix)
{ {
GE( glLoadIdentity () ); const GLfloat *gl_matrix = cogl_matrix_get_gl_matrix (matrix);
GE( glMultMatrixf (matrix) );
GE (glLoadMatrixf (gl_matrix));
} }
void void
@ -601,7 +602,7 @@ cogl_setup_viewport (guint width,
float z_far) float z_far)
{ {
float z_camera; float z_camera;
float projection_matrix[16]; CoglMatrix projection_matrix;
GE( glViewport (0, 0, width, height) ); GE( glViewport (0, 0, width, height) );
@ -617,8 +618,8 @@ cogl_setup_viewport (guint width,
* See comments in ../gl/cogl.c * See comments in ../gl/cogl.c
*/ */
cogl_get_projection_matrix (projection_matrix); cogl_get_projection_matrix (&projection_matrix);
z_camera = 0.5 * projection_matrix[0]; z_camera = 0.5 * projection_matrix.xx;
GE( glLoadIdentity () ); GE( glLoadIdentity () );
@ -679,15 +680,25 @@ cogl_features_available (CoglFeatureFlags features)
} }
void void
cogl_get_modelview_matrix (float m[16]) cogl_get_modelview_matrix (CoglMatrix *matrix)
{ {
float m[16];
glGetFloatv (GL_MODELVIEW_MATRIX, m); glGetFloatv (GL_MODELVIEW_MATRIX, m);
/* Since it's internal to Cogl and CoglMatrix doesn't currently have
* any flag members, we could avoid this extra copy if it really
* bothers anyone */
cogl_matrix_init_from_gl_matrix (matrix, m);
} }
void void
cogl_get_projection_matrix (float m[16]) cogl_get_projection_matrix (CoglMatrix *matrix)
{ {
float m[16];
glGetFloatv (GL_PROJECTION_MATRIX, m); glGetFloatv (GL_PROJECTION_MATRIX, m);
/* Since it's internal to Cogl and CoglMatrix doesn't currently have
* any flag members, we could avoid this extra copy if it really
* bothers anyone */
cogl_matrix_init_from_gl_matrix (matrix, m);
} }
void void