cogl/matrix: Make it row-major

This commit is contained in:
Georges Basile Stavracas Neto 2019-03-01 21:44:20 -03:00
parent 2ed596088f
commit a9d32be61e
No known key found for this signature in database
GPG Key ID: 886C17EE170D1385
4 changed files with 77 additions and 81 deletions

View File

@ -532,7 +532,7 @@ _cogl_util_get_eye_planes_for_screen_poly (float *polygon,
* frustum; coordinates range from [-Wc,Wc] left to right on the * frustum; coordinates range from [-Wc,Wc] left to right on the
* x-axis and [Wc,-Wc] top to bottom on the y-axis. * x-axis and [Wc,-Wc] top to bottom on the y-axis.
*/ */
Wc = DEPTH * projection->wz + projection->ww; Wc = DEPTH * projection->zw + projection->ww;
#define CLIP_X(X) ((((float)X - viewport[0]) * (2.0 / viewport[2])) - 1) * Wc #define CLIP_X(X) ((((float)X - viewport[0]) * (2.0 / viewport[2])) - 1) * Wc
#define CLIP_Y(Y) ((((float)Y - viewport[1]) * (2.0 / viewport[3])) - 1) * -Wc #define CLIP_Y(Y) ((((float)Y - viewport[1]) * (2.0 / viewport[3])) - 1) * -Wc
@ -545,7 +545,7 @@ _cogl_util_get_eye_planes_for_screen_poly (float *polygon,
tmp_poly[i].w = Wc; tmp_poly[i].w = Wc;
} }
Wc = DEPTH * 2 * projection->wz + projection->ww; Wc = DEPTH * 2 * projection->zw + projection->ww;
/* FIXME: technically we don't need to project all of the points /* FIXME: technically we don't need to project all of the points
* twice, it would be enough project every other point since * twice, it would be enough project every other point since

View File

@ -108,9 +108,6 @@ cogl_matrix_multiply (CoglMatrix *result,
cogl_matrix_to_graphene_matrix (a, &m1); cogl_matrix_to_graphene_matrix (a, &m1);
cogl_matrix_to_graphene_matrix (b, &m2); cogl_matrix_to_graphene_matrix (b, &m2);
/* AxB on a column-major matrix (CoglMatrix) is equal
* to BxA on a row-major matrix (graphene_matrix_t)
*/
graphene_matrix_multiply (&m2, &m1, &res); graphene_matrix_multiply (&m2, &m1, &res);
graphene_matrix_to_cogl_matrix (&res, result); graphene_matrix_to_cogl_matrix (&res, result);
@ -124,7 +121,7 @@ _cogl_matrix_prefix_print (const char *prefix, const CoglMatrix *matrix)
int i; int i;
for (i = 0;i < 4; i++) for (i = 0;i < 4; i++)
g_print ("%s\t%f %f %f %f\n", prefix, m[i], m[4+i], m[8+i], m[12+i] ); g_print ("%s\t%f %f %f %f\n", prefix, m[i*4], m[i*4+1], m[i*4+2], m[i*4+3] );
} }
/* /*
@ -163,7 +160,6 @@ cogl_matrix_rotate (CoglMatrix *matrix,
graphene_vec3_t r; graphene_vec3_t r;
cogl_matrix_to_graphene_matrix (matrix, &m); cogl_matrix_to_graphene_matrix (matrix, &m);
graphene_matrix_transpose (&m, &m);
graphene_matrix_init_rotate (&rotation, angle, graphene_vec3_init (&r, x, y, z)); graphene_matrix_init_rotate (&rotation, angle, graphene_vec3_init (&r, x, y, z));
graphene_matrix_multiply (&rotation, &m, &m); graphene_matrix_multiply (&rotation, &m, &m);
@ -207,8 +203,6 @@ cogl_matrix_frustum (CoglMatrix *matrix,
graphene_matrix_init_frustum (&frustum, left, right, bottom, top, z_near, z_far); graphene_matrix_init_frustum (&frustum, left, right, bottom, top, z_near, z_far);
cogl_matrix_to_graphene_matrix (matrix, &m); cogl_matrix_to_graphene_matrix (matrix, &m);
graphene_matrix_transpose (&m, &m);
graphene_matrix_multiply (&m, &frustum, &m); graphene_matrix_multiply (&m, &frustum, &m);
graphene_matrix_to_cogl_matrix (&m, matrix); graphene_matrix_to_cogl_matrix (&m, matrix);
@ -227,8 +221,6 @@ cogl_matrix_perspective (CoglMatrix *matrix,
graphene_matrix_init_perspective (&perspective, fov_y, aspect, z_near, z_far); graphene_matrix_init_perspective (&perspective, fov_y, aspect, z_near, z_far);
cogl_matrix_to_graphene_matrix (matrix, &m); cogl_matrix_to_graphene_matrix (matrix, &m);
graphene_matrix_transpose (&m, &m);
graphene_matrix_multiply (&m, &perspective, &m); graphene_matrix_multiply (&m, &perspective, &m);
graphene_matrix_to_cogl_matrix (&m, matrix); graphene_matrix_to_cogl_matrix (&m, matrix);
@ -249,8 +241,6 @@ cogl_matrix_orthographic (CoglMatrix *matrix,
graphene_matrix_init_ortho (&ortho, x_1, x_2, y_2, y_1, near, far); graphene_matrix_init_ortho (&ortho, x_1, x_2, y_2, y_1, near, far);
cogl_matrix_to_graphene_matrix (matrix, &m); cogl_matrix_to_graphene_matrix (matrix, &m);
graphene_matrix_transpose (&m, &m);
graphene_matrix_multiply (&m, &ortho, &m); graphene_matrix_multiply (&m, &ortho, &m);
graphene_matrix_to_cogl_matrix (&m, matrix); graphene_matrix_to_cogl_matrix (&m, matrix);
@ -264,15 +254,24 @@ cogl_matrix_scale (CoglMatrix *matrix,
float sz) float sz)
{ {
graphene_matrix_t m; graphene_matrix_t m;
CoglMatrix old;
cogl_matrix_to_graphene_matrix (matrix, &m); cogl_matrix_to_graphene_matrix (matrix, &m);
graphene_matrix_transpose (&m, &m); /* Cogl moves the matrix to 0, scales it, then moves back
graphene_matrix_scale (&m, sx, sy, sz); * to the previous transform point. To ease transition,
graphene_matrix_transpose (&m, &m); * preserve that behavior.
*/
old = *matrix;
graphene_matrix_scale (&m, sx, sy, sz);
graphene_matrix_to_cogl_matrix (&m, matrix); graphene_matrix_to_cogl_matrix (&m, matrix);
matrix->wx = old.wx;
matrix->wy = old.wy;
matrix->wz = old.wz;
matrix->ww = old.ww;
_COGL_MATRIX_DEBUG_PRINT (matrix); _COGL_MATRIX_DEBUG_PRINT (matrix);
} }
@ -399,10 +398,9 @@ cogl_matrix_view_2d_in_frustum (CoglMatrix *matrix,
float width_scale = width_2d_start / width_2d; float width_scale = width_2d_start / width_2d;
float height_scale = height_2d_start / height_2d; float height_scale = height_2d_start / height_2d;
cogl_matrix_scale (matrix, width_scale, -height_scale, width_scale);
cogl_matrix_translate (matrix, cogl_matrix_translate (matrix,
left_2d_plane, top_2d_plane, -z_2d); left_2d_plane, top_2d_plane, -z_2d);
cogl_matrix_scale (matrix, width_scale, -height_scale, width_scale);
} }
/* Assuming a symmetric perspective matrix is being used for your /* Assuming a symmetric perspective matrix is being used for your
@ -479,10 +477,10 @@ cogl_matrix_transform_point (const CoglMatrix *matrix,
{ {
float _x = *x, _y = *y, _z = *z, _w = *w; float _x = *x, _y = *y, _z = *z, _w = *w;
*x = matrix->xx * _x + matrix->xy * _y + matrix->xz * _z + matrix->xw * _w; *x = matrix->xx * _x + matrix->yx * _y + matrix->zx * _z + matrix->wx * _w;
*y = matrix->yx * _x + matrix->yy * _y + matrix->yz * _z + matrix->yw * _w; *y = matrix->xy * _x + matrix->yy * _y + matrix->zy * _z + matrix->wy * _w;
*z = matrix->zx * _x + matrix->zy * _y + matrix->zz * _z + matrix->zw * _w; *z = matrix->xz * _x + matrix->yz * _y + matrix->zz * _z + matrix->wz * _w;
*w = matrix->wx * _x + matrix->wy * _y + matrix->wz * _z + matrix->ww * _w; *w = matrix->xw * _x + matrix->yw * _y + matrix->zw * _z + matrix->ww * _w;
} }
typedef struct _Point2f typedef struct _Point2f
@ -521,9 +519,9 @@ _cogl_matrix_transform_points_f2 (const CoglMatrix *matrix,
Point2f p = *(Point2f *)((uint8_t *)points_in + i * stride_in); Point2f p = *(Point2f *)((uint8_t *)points_in + i * stride_in);
Point3f *o = (Point3f *)((uint8_t *)points_out + i * stride_out); Point3f *o = (Point3f *)((uint8_t *)points_out + i * stride_out);
o->x = matrix->xx * p.x + matrix->xy * p.y + matrix->xw; o->x = matrix->xx * p.x + matrix->yx * p.y + matrix->wx;
o->y = matrix->yx * p.x + matrix->yy * p.y + matrix->yw; o->y = matrix->xy * p.x + matrix->yy * p.y + matrix->wy;
o->z = matrix->zx * p.x + matrix->zy * p.y + matrix->zw; o->z = matrix->xz * p.x + matrix->yz * p.y + matrix->wz;
} }
} }
@ -542,10 +540,10 @@ _cogl_matrix_project_points_f2 (const CoglMatrix *matrix,
Point2f p = *(Point2f *)((uint8_t *)points_in + i * stride_in); Point2f p = *(Point2f *)((uint8_t *)points_in + i * stride_in);
Point4f *o = (Point4f *)((uint8_t *)points_out + i * stride_out); Point4f *o = (Point4f *)((uint8_t *)points_out + i * stride_out);
o->x = matrix->xx * p.x + matrix->xy * p.y + matrix->xw; o->x = matrix->xx * p.x + matrix->yx * p.y + matrix->wx;
o->y = matrix->yx * p.x + matrix->yy * p.y + matrix->yw; o->y = matrix->xy * p.x + matrix->yy * p.y + matrix->wy;
o->z = matrix->zx * p.x + matrix->zy * p.y + matrix->zw; o->z = matrix->xz * p.x + matrix->yz * p.y + matrix->wz;
o->w = matrix->wx * p.x + matrix->wy * p.y + matrix->ww; o->w = matrix->xw * p.x + matrix->yw * p.y + matrix->ww;
} }
} }
@ -564,12 +562,12 @@ _cogl_matrix_transform_points_f3 (const CoglMatrix *matrix,
Point3f p = *(Point3f *)((uint8_t *)points_in + i * stride_in); Point3f p = *(Point3f *)((uint8_t *)points_in + i * stride_in);
Point3f *o = (Point3f *)((uint8_t *)points_out + i * stride_out); Point3f *o = (Point3f *)((uint8_t *)points_out + i * stride_out);
o->x = matrix->xx * p.x + matrix->xy * p.y + o->x = matrix->xx * p.x + matrix->yx * p.y +
matrix->xz * p.z + matrix->xw; matrix->zx * p.z + matrix->wx;
o->y = matrix->yx * p.x + matrix->yy * p.y + o->y = matrix->xy * p.x + matrix->yy * p.y +
matrix->yz * p.z + matrix->yw; matrix->zy * p.z + matrix->wy;
o->z = matrix->zx * p.x + matrix->zy * p.y + o->z = matrix->xz * p.x + matrix->yz * p.y +
matrix->zz * p.z + matrix->zw; matrix->zz * p.z + matrix->wz;
} }
} }
@ -588,14 +586,14 @@ _cogl_matrix_project_points_f3 (const CoglMatrix *matrix,
Point3f p = *(Point3f *)((uint8_t *)points_in + i * stride_in); Point3f p = *(Point3f *)((uint8_t *)points_in + i * stride_in);
Point4f *o = (Point4f *)((uint8_t *)points_out + i * stride_out); Point4f *o = (Point4f *)((uint8_t *)points_out + i * stride_out);
o->x = matrix->xx * p.x + matrix->xy * p.y + o->x = matrix->xx * p.x + matrix->yx * p.y +
matrix->xz * p.z + matrix->xw; matrix->zx * p.z + matrix->wx;
o->y = matrix->yx * p.x + matrix->yy * p.y + o->y = matrix->xy * p.x + matrix->yy * p.y +
matrix->yz * p.z + matrix->yw; matrix->zy * p.z + matrix->wy;
o->z = matrix->zx * p.x + matrix->zy * p.y + o->z = matrix->xz * p.x + matrix->yz * p.y +
matrix->zz * p.z + matrix->zw; matrix->zz * p.z + matrix->wz;
o->w = matrix->wx * p.x + matrix->wy * p.y + o->w = matrix->xw * p.x + matrix->yw * p.y +
matrix->wz * p.z + matrix->ww; matrix->zw * p.z + matrix->ww;
} }
} }
@ -614,14 +612,14 @@ _cogl_matrix_project_points_f4 (const CoglMatrix *matrix,
Point4f p = *(Point4f *)((uint8_t *)points_in + i * stride_in); Point4f p = *(Point4f *)((uint8_t *)points_in + i * stride_in);
Point4f *o = (Point4f *)((uint8_t *)points_out + i * stride_out); Point4f *o = (Point4f *)((uint8_t *)points_out + i * stride_out);
o->x = matrix->xx * p.x + matrix->xy * p.y + o->x = matrix->xx * p.x + matrix->yx * p.y +
matrix->xz * p.z + matrix->xw * p.w; matrix->zx * p.z + matrix->wx * p.w;
o->y = matrix->yx * p.x + matrix->yy * p.y + o->y = matrix->xy * p.x + matrix->yy * p.y +
matrix->yz * p.z + matrix->yw * p.w; matrix->zy * p.z + matrix->wy * p.w;
o->z = matrix->zx * p.x + matrix->zy * p.y + o->z = matrix->xz * p.x + matrix->yz * p.y +
matrix->zz * p.z + matrix->zw * p.w; matrix->zz * p.z + matrix->wz * p.w;
o->w = matrix->wx * p.x + matrix->wy * p.y + o->w = matrix->xw * p.x + matrix->yw * p.y +
matrix->wz * p.z + matrix->ww * p.w; matrix->zw * p.z + matrix->ww * p.w;
} }
} }
@ -710,8 +708,6 @@ cogl_matrix_look_at (CoglMatrix *matrix,
graphene_matrix_init_look_at (&look_at, &eye, &center, &up); graphene_matrix_init_look_at (&look_at, &eye, &center, &up);
cogl_matrix_to_graphene_matrix (matrix, &m); cogl_matrix_to_graphene_matrix (matrix, &m);
graphene_matrix_transpose (&m, &m);
graphene_matrix_translate (&m, &GRAPHENE_POINT3D_INIT (-eye_position_x, graphene_matrix_translate (&m, &GRAPHENE_POINT3D_INIT (-eye_position_x,
-eye_position_y, -eye_position_y,
-eye_position_z)); -eye_position_z));

View File

@ -60,7 +60,7 @@ G_BEGIN_DECLS
* CoglMatrix: * CoglMatrix:
* *
* A CoglMatrix holds a 4x4 transform matrix. This is a single precision, * A CoglMatrix holds a 4x4 transform matrix. This is a single precision,
* column-major matrix which means it is compatible with what OpenGL expects. * row-major matrix.
* *
* A CoglMatrix can represent transforms such as, rotations, scaling, * A CoglMatrix can represent transforms such as, rotations, scaling,
* translation, sheering, and linear projections. You can combine these * translation, sheering, and linear projections. You can combine these
@ -86,28 +86,28 @@ G_BEGIN_DECLS
*/ */
struct _CoglMatrix struct _CoglMatrix
{ {
/* column 0 */ /* row 0 */
float xx; float xx;
float yx;
float zx;
float wx;
/* column 1 */
float xy; float xy;
float yy;
float zy;
float wy;
/* column 2 */
float xz; float xz;
float yz;
float zz;
float wz;
/* column 3 */
float xw; float xw;
/* row 1 */
float yx;
float yy;
float yz;
float yw; float yw;
/* row 2 */
float zx;
float zy;
float zz;
float zw; float zw;
/* row 3 */
float wx;
float wy;
float wz;
float ww; float ww;
}; };
COGL_STRUCT_SIZE_ASSERT (CoglMatrix, 64); COGL_STRUCT_SIZE_ASSERT (CoglMatrix, 64);
@ -139,10 +139,10 @@ cogl_matrix_init_identity (CoglMatrix *matrix);
* Resets matrix to the (tx, ty, tz) translation matrix: * Resets matrix to the (tx, ty, tz) translation matrix:
* *
* |[ * |[
* .xx=1; .xy=0; .xz=0; .xw=tx; * .xx=1; .xy=0; .xz=0; .xw=0;
* .yx=0; .yy=1; .yz=0; .yw=ty; * .yx=0; .yy=1; .yz=0; .yw=0;
* .zx=0; .zy=0; .zz=1; .zw=tz; * .zx=0; .zy=0; .zz=1; .zw=0;
* .wx=0; .wy=0; .wz=0; .ww=1; * .wx=tx; .wy=ty; .wz=tz; .ww=1;
* ]| * ]|
* *
* Since: 2.0 * Since: 2.0

View File

@ -284,9 +284,9 @@ get_paint_level (int width, int height)
u0 = width / 2.; u0 = width / 2.;
v0 = height / 2.; v0 = height / 2.;
xc = pm.xx * u0 + pm.xy * v0 + pm.xw; xc = pm.xx * u0 + pm.yx * v0 + pm.wx;
yc = pm.yx * u0 + pm.yy * v0 + pm.yw; yc = pm.xy * u0 + pm.yy * v0 + pm.wy;
wc = pm.wx * u0 + pm.wy * v0 + pm.ww; wc = pm.xw * u0 + pm.yw * v0 + pm.ww;
/* We'll simplify the equations below for a bit of micro-optimization. /* We'll simplify the equations below for a bit of micro-optimization.
* The commented out code is the unsimplified version. * The commented out code is the unsimplified version.
@ -321,10 +321,10 @@ get_paint_level (int width, int height)
*/ */
/* dxdu * wc, etc */ /* dxdu * wc, etc */
dxdu_ = 0.5 * viewport_width * (pm.xx - pm.wx * (xc/wc)); dxdu_ = 0.5 * viewport_width * (pm.xx - pm.xw * (xc/wc));
dxdv_ = 0.5 * viewport_width * (pm.xy - pm.wy * (xc/wc)); dxdv_ = 0.5 * viewport_width * (pm.yx - pm.yw * (xc/wc));
dydu_ = 0.5 * viewport_height * (pm.yx - pm.wx * (yc/wc)); dydu_ = 0.5 * viewport_height * (pm.xy - pm.xw * (yc/wc));
dydv_ = 0.5 * viewport_height * (pm.yy - pm.wy * (yc/wc)); dydv_ = 0.5 * viewport_height * (pm.yy - pm.yw * (yc/wc));
/* det * wc^2 */ /* det * wc^2 */
det_ = dxdu_ * dydv_ - dxdv_ * dydu_; det_ = dxdu_ * dydv_ - dxdv_ * dydu_;