gl/utils: fixup matrix math again for column major

e4bf9ed8f0 was not quite right and changed
the wrong thing. Intead we needed to change the multiplication order
and should have kept the previous to/from matrices as is done in this
patch.
This commit is contained in:
Matthew Waters 2018-07-10 20:03:12 +10:00
parent 173e2d8db3
commit 16cba63d43
3 changed files with 65 additions and 36 deletions

View file

@ -115,17 +115,17 @@ static const gfloat identity_matrix[] = {
}; };
static const gfloat from_ndc_matrix[] = { static const gfloat from_ndc_matrix[] = {
0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.5, 0.0,
0.0, 0.0, 0.0, 1.0, 0.5, 0.5, 0.5, 1.0,
}; };
static const gfloat to_ndc_matrix[] = { static const gfloat to_ndc_matrix[] = {
2.0, 0.0, 0.0, -1.0, 2.0, 0.0, 0.0, 0.0,
0.0, 2.0, 0.0, -1.0, 0.0, 2.0, 0.0, 0.0,
0.0, 0.0, 2.0, -1.0, 0.0, 0.0, 2.0, 0.0,
0.0, 0.0, 0.0, 1.0, -1.0, -1.0, -1.0, 1.0,
}; };
void void
@ -161,8 +161,8 @@ void gst_gl_get_affine_transformation_meta_as_ndc_ext
} else { } else {
float tmp[16]; float tmp[16];
gst_gl_multiply_matrix4 (to_ndc_matrix, meta->matrix, tmp); gst_gl_multiply_matrix4 (from_ndc_matrix, meta->matrix, tmp);
gst_gl_multiply_matrix4 (tmp, from_ndc_matrix, matrix); gst_gl_multiply_matrix4 (tmp, to_ndc_matrix, matrix);
} }
} }
@ -173,6 +173,6 @@ void gst_gl_set_affine_transformation_meta_from_ndc_ext
g_return_if_fail (meta != NULL); g_return_if_fail (meta != NULL);
gst_gl_multiply_matrix4 (from_ndc_matrix, matrix, tmp); gst_gl_multiply_matrix4 (to_ndc_matrix, matrix, tmp);
gst_gl_multiply_matrix4 (tmp, to_ndc_matrix, meta->matrix); gst_gl_multiply_matrix4 (tmp, from_ndc_matrix, meta->matrix);
} }

View file

@ -793,17 +793,17 @@ static const gfloat identity_matrix[] = {
}; };
static const gfloat from_ndc_matrix[] = { static const gfloat from_ndc_matrix[] = {
0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.5, 0.0,
0.0, 0.0, 0.0, 1.0, 0.5, 0.5, 0.5, 1.0,
}; };
static const gfloat to_ndc_matrix[] = { static const gfloat to_ndc_matrix[] = {
2.0, 0.0, 0.0, -1.0, 2.0, 0.0, 0.0, 0.0,
0.0, 2.0, 0.0, -1.0, 0.0, 2.0, 0.0, 0.0,
0.0, 0.0, 2.0, -1.0, 0.0, 0.0, 2.0, 0.0,
0.0, 0.0, 0.0, 1.0, -1.0, -1.0, -1.0, 1.0,
}; };
/* multiplies two 4x4 matrices, @a X @b, and stores the result in @result /* multiplies two 4x4 matrices, @a X @b, and stores the result in @result
@ -856,8 +856,8 @@ gst_gl_get_affine_transformation_meta_as_ndc (GstVideoAffineTransformationMeta *
float tmp[16]; float tmp[16];
/* change of basis multiplications */ /* change of basis multiplications */
gst_gl_multiply_matrix4 (to_ndc_matrix, meta->matrix, tmp); gst_gl_multiply_matrix4 (from_ndc_matrix, meta->matrix, tmp);
gst_gl_multiply_matrix4 (tmp, from_ndc_matrix, matrix); gst_gl_multiply_matrix4 (tmp, to_ndc_matrix, matrix);
} }
} }
@ -869,6 +869,6 @@ void gst_gl_set_affine_transformation_meta_from_ndc
g_return_if_fail (meta != NULL); g_return_if_fail (meta != NULL);
/* change of basis multiplications */ /* change of basis multiplications */
gst_gl_multiply_matrix4 (from_ndc_matrix, matrix, tmp); gst_gl_multiply_matrix4 (to_ndc_matrix, matrix, tmp);
gst_gl_multiply_matrix4 (tmp, to_ndc_matrix, meta->matrix); gst_gl_multiply_matrix4 (tmp, from_ndc_matrix, meta->matrix);
} }

View file

@ -137,7 +137,7 @@ GST_START_TEST (test_matrix_ndc)
} }
GST_END_TEST; GST_END_TEST;
#if 0
static void static void
transpose_matrix4 (float *m, float *res) transpose_matrix4 (float *m, float *res)
{ {
@ -148,6 +148,8 @@ transpose_matrix4 (float *m, float *res)
int idx = i + (j * 4); int idx = i + (j * 4);
int swapped_idx = j + (i * 4); int swapped_idx = j + (i * 4);
GST_ERROR ("swapping %i %f into %i", idx, m[idx], swapped_idx);
if (i == j) if (i == j)
fail_unless (idx == swapped_idx); fail_unless (idx == swapped_idx);
@ -155,7 +157,7 @@ transpose_matrix4 (float *m, float *res)
} }
} }
} }
#endif
static float static float
dot4 (float *v1, float *v2) dot4 (float *v1, float *v2)
{ {
@ -174,21 +176,22 @@ _matrix_mult_vertex4 (float *m, float *v, float *res)
res[3] = dot4 (&m[12], v); res[3] = dot4 (&m[12], v);
} }
#if 0
/* v * m */ /* v * m */
/* Not the prevailing multiplication convention and not really used in /* Used because the default is for OpenGL to read matrices transposed on
* GStreamer. Kept around for extra testing */ * uploading */
static void static void
_vertex_mult_matrix4 (float *v, float *m, float *res) _vertex_mult_matrix4 (float *m, float *v, float *res)
{ {
float tmp[16] = { 0., }; float tmp[16] = { 0., };
GST_TRACE ("original matrix");
debug_matrix (m);
transpose_matrix4 (m, tmp); transpose_matrix4 (m, tmp);
GST_TRACE ("transposed matrix"); GST_TRACE ("transposed matrix");
debug_matrix (tmp); debug_matrix (tmp);
_matrix_mult_vertex4 (tmp, v, res); _matrix_mult_vertex4 (tmp, v, res);
} }
#endif
GST_START_TEST (test_matrix_vertex_identity) GST_START_TEST (test_matrix_vertex_identity)
{ {
float identity[] = { float identity[] = {
@ -202,7 +205,7 @@ GST_START_TEST (test_matrix_vertex_identity)
float res[4] = { 0., }; float res[4] = { 0., };
int i; int i;
_matrix_mult_vertex4 (identity, v, res); _vertex_mult_matrix4 (identity, v, res);
GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v)); GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v));
GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res)); GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res));
@ -228,7 +231,7 @@ GST_START_TEST (test_matrix_vertex_scale)
float res[4] = { 0., }; float res[4] = { 0., };
int i; int i;
_matrix_mult_vertex4 (scale, v, res); _vertex_mult_matrix4 (scale, v, res);
GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v)); GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v));
GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res)); GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res));
@ -241,6 +244,31 @@ GST_START_TEST (test_matrix_vertex_scale)
GST_END_TEST; GST_END_TEST;
GST_START_TEST (test_matrix_vertex_translate)
{
float translate_1[] = {
1., 0., 0., 0.,
0., 1., 0., 0.,
0., 0., 1., 0.,
1., 2., 3., 1.,
};
float v[] = { 1., 1., 1., 1. };
float expected[] = { 2., 3., 4., 1. };
float res[4] = { 0., };
int i;
_vertex_mult_matrix4 (translate_1, v, res);
for (i = 0; i < 4; i++) {
fail_unless (FEQ (res[i], expected[i]),
"value %f at index %u does not match " "expected value %f", res[i], i,
expected[i]);
}
}
GST_END_TEST;
GST_START_TEST (test_matrix_vertex_y_invert) GST_START_TEST (test_matrix_vertex_y_invert)
{ {
GstBuffer *buffer = gst_buffer_new (); GstBuffer *buffer = gst_buffer_new ();
@ -266,7 +294,7 @@ GST_START_TEST (test_matrix_vertex_y_invert)
GST_DEBUG ("y-invert"); GST_DEBUG ("y-invert");
debug_matrix (y_invert); debug_matrix (y_invert);
_matrix_mult_vertex4 (y_invert, v, res); _vertex_mult_matrix4 (y_invert, v, res);
GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v)); GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v));
GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res)); GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res));
@ -283,7 +311,7 @@ GST_START_TEST (test_matrix_vertex_y_invert)
GST_DEBUG ("y-invert from ndc [-1,1]^3 to [0,1]^3"); GST_DEBUG ("y-invert from ndc [-1,1]^3 to [0,1]^3");
debug_matrix (aff_meta->matrix); debug_matrix (aff_meta->matrix);
_matrix_mult_vertex4 (aff_meta->matrix, v, res); _vertex_mult_matrix4 (aff_meta->matrix, v, res);
GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v)); GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v));
GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res)); GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res));
@ -296,7 +324,7 @@ GST_START_TEST (test_matrix_vertex_y_invert)
/* test vec4(1,0,1,1) -> vec4(1,1,1,1) */ /* test vec4(1,0,1,1) -> vec4(1,1,1,1) */
v[1] = 0.; v[1] = 0.;
expected[1] = 1.; expected[1] = 1.;
_matrix_mult_vertex4 (aff_meta->matrix, v, res); _vertex_mult_matrix4 (aff_meta->matrix, v, res);
GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v)); GST_DEBUG ("vertex: %" VEC4_FORMAT, VEC4_ARGS (v));
GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res)); GST_DEBUG ("result: %" VEC4_FORMAT, VEC4_ARGS (res));
@ -320,6 +348,7 @@ gst_gl_matrix_suite (void)
tcase_add_test (tc_chain, test_matrix_ndc); tcase_add_test (tc_chain, test_matrix_ndc);
tcase_add_test (tc_chain, test_matrix_vertex_identity); tcase_add_test (tc_chain, test_matrix_vertex_identity);
tcase_add_test (tc_chain, test_matrix_vertex_scale); tcase_add_test (tc_chain, test_matrix_vertex_scale);
tcase_add_test (tc_chain, test_matrix_vertex_translate);
tcase_add_test (tc_chain, test_matrix_vertex_y_invert); tcase_add_test (tc_chain, test_matrix_vertex_y_invert);
return s; return s;