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);
}
const float *
cogl_matrix_get_array (const CoglMatrix *matrix)
void
cogl_matrix_get_array (const CoglMatrix *matrix,
float *array)
{
return (float *)matrix;
memcpy (array, matrix, sizeof (float) * 16);
}
void
@ -2291,13 +2292,15 @@ void
cogl_matrix_transpose (CoglMatrix *matrix)
{
float new_values[16];
float values[16];
/* We don't need to do anything if the matrix is the identity matrix */
if (!(matrix->flags & MAT_DIRTY_TYPE) &&
matrix->type == COGL_MATRIX_TYPE_IDENTITY)
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);
}

View File

@ -508,13 +508,13 @@ cogl_matrix_init_from_array (CoglMatrix *matrix,
/**
* cogl_matrix_get_array:
* @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.
*
* Return value: a pointer to the float array
*/
const float *
cogl_matrix_get_array (const CoglMatrix *matrix);
void
cogl_matrix_get_array (const CoglMatrix *matrix,
float *array);
/**
* cogl_matrix_init_from_quaternion:

View File

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

View File

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

View File

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

View File

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