cogl/matrix: Change semantincs of cogl_matrix_get_array

CoglMatrix.get_array() returns the column-major CoglMatrix itself,
since the first fields of the matrix is effectively an array of
floats, and we can just pretend it is. Of course, it basically
ignores any C-specific type checking.

WIP
This commit is contained in:
Georges Basile Stavracas Neto 2019-02-22 15:06:34 -03:00
parent 88be11f6e2
commit 54f9bebeb8
No known key found for this signature in database
GPG Key ID: 886C17EE170D1385
6 changed files with 45 additions and 26 deletions

View File

@ -1996,10 +1996,11 @@ cogl_matrix_free (CoglMatrix *matrix)
g_slice_free (CoglMatrix, matrix); g_slice_free (CoglMatrix, matrix);
} }
const float * void
cogl_matrix_get_array (const CoglMatrix *matrix) cogl_matrix_get_array (const CoglMatrix *matrix,
float *array)
{ {
return (float *)matrix; memcpy (array, matrix, sizeof (float) * 16);
} }
void void
@ -2291,13 +2292,15 @@ void
cogl_matrix_transpose (CoglMatrix *matrix) cogl_matrix_transpose (CoglMatrix *matrix)
{ {
float new_values[16]; float new_values[16];
float values[16];
/* We don't need to do anything if the matrix is the identity matrix */ /* We don't need to do anything if the matrix is the identity matrix */
if (!(matrix->flags & MAT_DIRTY_TYPE) && if (!(matrix->flags & MAT_DIRTY_TYPE) &&
matrix->type == COGL_MATRIX_TYPE_IDENTITY) matrix->type == COGL_MATRIX_TYPE_IDENTITY)
return; return;
_cogl_matrix_util_transposef (new_values, cogl_matrix_get_array (matrix)); cogl_matrix_get_array (matrix, values);
_cogl_matrix_util_transposef (new_values, values);
cogl_matrix_init_from_array (matrix, new_values); cogl_matrix_init_from_array (matrix, new_values);
} }

View File

@ -508,13 +508,13 @@ cogl_matrix_init_from_array (CoglMatrix *matrix,
/** /**
* cogl_matrix_get_array: * cogl_matrix_get_array:
* @matrix: A 4x4 transformation matrix * @matrix: A 4x4 transformation matrix
* @array: the array to copy the contents of @matrix to.
* *
* Casts @matrix to a float array which can be directly passed to OpenGL. * Casts @matrix to a float array which can be directly passed to OpenGL.
*
* Return value: a pointer to the float array
*/ */
const float * void
cogl_matrix_get_array (const CoglMatrix *matrix); cogl_matrix_get_array (const CoglMatrix *matrix,
float *array);
/** /**
* cogl_matrix_init_from_quaternion: * cogl_matrix_init_from_quaternion:

View File

@ -84,9 +84,15 @@ flush_matrix_to_gl_builtin (CoglContext *ctx,
} }
if (is_identity) if (is_identity)
{
GE (ctx, glLoadIdentity ()); GE (ctx, glLoadIdentity ());
}
else else
GE (ctx, glLoadMatrixf (cogl_matrix_get_array (matrix))); {
float array[16];
cogl_matrix_get_array (matrix, array);
GE (ctx, glLoadMatrixf (array));
}
#endif #endif
} }

View File

@ -440,10 +440,10 @@ update_constants_cb (CoglPipeline *pipeline,
(state->update_all || unit_state->dirty_texture_matrix)) (state->update_all || unit_state->dirty_texture_matrix))
{ {
const CoglMatrix *matrix; const CoglMatrix *matrix;
const float *array; float array[16];
matrix = _cogl_pipeline_get_layer_matrix (pipeline, layer_index); matrix = _cogl_pipeline_get_layer_matrix (pipeline, layer_index);
array = cogl_matrix_get_array (matrix); cogl_matrix_get_array (matrix, array);
GE (ctx, glUniformMatrix4fv (unit_state->texture_matrix_uniform, GE (ctx, glUniformMatrix4fv (unit_state->texture_matrix_uniform,
1, FALSE, array)); 1, FALSE, array));
unit_state->dirty_texture_matrix = FALSE; unit_state->dirty_texture_matrix = FALSE;
@ -965,6 +965,8 @@ _cogl_pipeline_progend_glsl_pre_paint (CoglPipeline *pipeline,
if (modelview_changed || projection_changed) if (modelview_changed || projection_changed)
{ {
float array[16];
if (program_state->mvp_uniform != -1) if (program_state->mvp_uniform != -1)
need_modelview = need_projection = TRUE; need_modelview = need_projection = TRUE;
else else
@ -992,16 +994,22 @@ _cogl_pipeline_progend_glsl_pre_paint (CoglPipeline *pipeline,
} }
if (projection_changed && program_state->projection_uniform != -1) if (projection_changed && program_state->projection_uniform != -1)
{
cogl_matrix_get_array (&projection, array);
GE (ctx, glUniformMatrix4fv (program_state->projection_uniform, GE (ctx, glUniformMatrix4fv (program_state->projection_uniform,
1, /* count */ 1, /* count */
FALSE, /* transpose */ FALSE, /* transpose */
cogl_matrix_get_array (&projection))); array));
}
if (modelview_changed && program_state->modelview_uniform != -1) if (modelview_changed && program_state->modelview_uniform != -1)
{
cogl_matrix_get_array (&modelview, array);
GE (ctx, glUniformMatrix4fv (program_state->modelview_uniform, GE (ctx, glUniformMatrix4fv (program_state->modelview_uniform,
1, /* count */ 1, /* count */
FALSE, /* transpose */ FALSE, /* transpose */
cogl_matrix_get_array (&modelview))); array));
}
if (program_state->mvp_uniform != -1) if (program_state->mvp_uniform != -1)
{ {
@ -1010,11 +1018,12 @@ _cogl_pipeline_progend_glsl_pre_paint (CoglPipeline *pipeline,
avoiding the matrix multiplication */ avoiding the matrix multiplication */
if (cogl_matrix_entry_is_identity (modelview_entry)) if (cogl_matrix_entry_is_identity (modelview_entry))
{ {
cogl_matrix_get_array (&projection, array);
GE (ctx, GE (ctx,
glUniformMatrix4fv (program_state->mvp_uniform, glUniformMatrix4fv (program_state->mvp_uniform,
1, /* count */ 1, /* count */
FALSE, /* transpose */ FALSE, /* transpose */
cogl_matrix_get_array (&projection))); array));
} }
else else
{ {
@ -1023,11 +1032,12 @@ _cogl_pipeline_progend_glsl_pre_paint (CoglPipeline *pipeline,
cogl_matrix_multiply (&combined, cogl_matrix_multiply (&combined,
&projection, &projection,
&modelview); &modelview);
cogl_matrix_get_array (&combined, array);
GE (ctx, GE (ctx,
glUniformMatrix4fv (program_state->mvp_uniform, glUniformMatrix4fv (program_state->mvp_uniform,
1, /* count */ 1, /* count */
FALSE, /* transpose */ FALSE, /* transpose */
cogl_matrix_get_array (&combined))); array));
} }
} }
} }

View File

@ -242,9 +242,7 @@ paint_matrix_pipeline (CoglPipeline *pipeline)
cogl_matrix_transpose (matrices + 3); cogl_matrix_transpose (matrices + 3);
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
memcpy (matrix_floats + i * 16, cogl_matrix_get_array (matrices + i, matrix_floats + i * 16);
cogl_matrix_get_array (matrices + i),
sizeof (float) * 16);
/* Set the first three matrices as transposed */ /* Set the first three matrices as transposed */
uniform_location = uniform_location =

View File

@ -492,6 +492,7 @@ test_vertex_transform_hook (TestState *state)
CoglSnippet *snippet; CoglSnippet *snippet;
CoglMatrix identity_matrix; CoglMatrix identity_matrix;
CoglMatrix matrix; CoglMatrix matrix;
float array[16];
int location; int location;
/* Test the vertex transform hook */ /* Test the vertex transform hook */
@ -513,12 +514,13 @@ test_vertex_transform_hook (TestState *state)
/* Copy the current projection matrix to a uniform */ /* Copy the current projection matrix to a uniform */
cogl_framebuffer_get_projection_matrix (test_fb, &matrix); cogl_framebuffer_get_projection_matrix (test_fb, &matrix);
location = cogl_pipeline_get_uniform_location (pipeline, "pmat"); location = cogl_pipeline_get_uniform_location (pipeline, "pmat");
cogl_matrix_get_array (&matrix, array);
cogl_pipeline_set_uniform_matrix (pipeline, cogl_pipeline_set_uniform_matrix (pipeline,
location, location,
4, /* dimensions */ 4, /* dimensions */
1, /* count */ 1, /* count */
FALSE, /* don't transpose */ FALSE, /* don't transpose */
cogl_matrix_get_array (&matrix)); array);
/* Replace the real projection matrix with the identity. This should /* Replace the real projection matrix with the identity. This should
mess up the drawing unless the snippet replacement is working */ mess up the drawing unless the snippet replacement is working */