3 * Copyright (C) 2014 Matthew Waters <ystreet00@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 #include <gst/gl/gstglutils.c>
26 #undef GST_CAT_DEFAULT
27 #include <gst/check/gstcheck.h>
29 #define EPSILON 0.0001f
30 #define FEQ(a,b) (fabs(a-b) < EPSILON)
33 debug_matrix (const float *m)
36 for (i = 0; i < 4; i++) {
37 GST_DEBUG ("%10.4f %10.4f %10.4f %10.4f", m[i * 4 + 0], m[i * 4 + 1],
38 m[i * 4 + 2], m[i * 4 + 3]);
42 GST_START_TEST (test_matrix_multiply)
69 gst_gl_multiply_matrix4 (A, B, res);
73 for (i = 0; i < G_N_ELEMENTS (res); i++) {
74 fail_unless (FEQ (res[i], C[i]), "value %f at index %u does not match "
75 "expected value %f", res[i], i, C[i]);
81 GST_START_TEST (test_matrix_ndc)
83 GstBuffer *buffer = gst_buffer_new ();
84 GstVideoAffineTransformationMeta *aff_meta;
102 aff_meta = gst_buffer_add_video_affine_transformation_meta (buffer);
104 /* test default identity matrix */
105 gst_gl_get_affine_transformation_meta_as_ndc (aff_meta, res);
106 GST_DEBUG ("result");
109 for (i = 0; i < G_N_ELEMENTS (res); i++) {
110 fail_unless (FEQ (res[i], m[i]), "value %f at index %u does not match "
111 "expected value %f", res[i], i, m[i]);
114 /* test setting and receiving the same values */
115 gst_gl_set_affine_transformation_meta_from_ndc (aff_meta, n);
116 gst_gl_get_affine_transformation_meta_as_ndc (aff_meta, res);
118 GST_DEBUG ("result");
121 for (i = 0; i < G_N_ELEMENTS (res); i++) {
122 fail_unless (FEQ (res[i], n[i]), "value %f at index %u does not match "
123 "expected value %f", res[i], i, n[i]);
126 gst_buffer_unref (buffer);
132 transpose_matrix4 (float *m, float *res)
136 for (i = 0; i < 4; i++) {
137 for (j = 0; j < 4; j++) {
138 int idx = i + (j * 4);
139 int swapped_idx = j + (i * 4);
142 fail_unless (idx == swapped_idx);
144 res[swapped_idx] = m[idx];
150 dot4 (float *v1, float *v2)
152 GST_TRACE ("%.4f * %.4f + %.4f * %.4f + %.4f * %.4f + %.4f * %.4f",
153 v1[0], v2[0], v1[1], v2[1], v1[2], v2[2], v1[3], v2[3]);
154 return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2] + v1[3] * v2[3];
159 _matrix_mult_vertex4 (float *m, float *v, float *res)
161 res[0] = dot4 (&m[0], v);
162 res[1] = dot4 (&m[4], v);
163 res[2] = dot4 (&m[8], v);
164 res[3] = dot4 (&m[12], v);
169 _vertex_mult_matrix4 (float *v, float *m, float *res)
171 float tmp[16] = { 0., };
173 transpose_matrix4 (m, tmp);
174 _matrix_mult_vertex4 (tmp, v, res);
177 GST_START_TEST (test_matrix_vertex_identity)
186 float v[] = { 1., 1., 1., 1. };
187 float res[4] = { 0., };
190 _vertex_mult_matrix4 (v, identity, res);
191 GST_DEBUG ("vertex: %.4f %.4f %.4f %.4f", v[0], v[1], v[2], v[3]);
192 GST_DEBUG ("result: %.4f %.4f %.4f %.4f", res[0], res[1], res[2], res[3]);
194 for (i = 0; i < 4; i++) {
195 fail_unless (FEQ (res[i], v[i]), "value %f at index %u does not match "
196 "expected value %f", res[i], i, v[i]);
199 _matrix_mult_vertex4 (identity, v, res);
200 GST_DEBUG ("vertex: %.4f %.4f %.4f %.4f", v[0], v[1], v[2], v[3]);
201 GST_DEBUG ("result: %.4f %.4f %.4f %.4f", res[0], res[1], res[2], res[3]);
203 for (i = 0; i < 4; i++) {
204 fail_unless (FEQ (res[i], v[i]), "value %f at index %u does not match "
205 "expected value %f", res[i], i, v[i]);
211 GST_START_TEST (test_matrix_vertex_scale)
220 float v[] = { 1., 1., 1., 1. };
221 float expected[] = { 1.5, 2.5, 3., 1. };
222 float res[4] = { 0., };
225 _vertex_mult_matrix4 (v, scale, res);
226 GST_DEBUG ("vertex: %.4f %.4f %.4f %.4f", v[0], v[1], v[2], v[3]);
227 GST_DEBUG ("result: %.4f %.4f %.4f %.4f", res[0], res[1], res[2], res[3]);
229 for (i = 0; i < 4; i++) {
230 fail_unless (FEQ (res[i], expected[i]),
231 "value %f at index %u does not match " "expected value %f", res[i], i,
235 _matrix_mult_vertex4 (scale, v, res);
236 GST_DEBUG ("vertex: %.4f %.4f %.4f %.4f", v[0], v[1], v[2], v[3]);
237 GST_DEBUG ("result: %.4f %.4f %.4f %.4f", res[0], res[1], res[2], res[3]);
239 for (i = 0; i < 4; i++) {
240 fail_unless (FEQ (res[i], expected[i]),
241 "value %f at index %u does not match " "expected value %f", res[i], i,
250 gst_gl_upload_suite (void)
252 Suite *s = suite_create ("GstGLMatrix");
253 TCase *tc_chain = tcase_create ("matrix");
255 suite_add_tcase (s, tc_chain);
256 tcase_add_test (tc_chain, test_matrix_multiply);
257 tcase_add_test (tc_chain, test_matrix_ndc);
262 GST_CHECK_MAIN (gst_gl_upload);