matrix: fix transform/project_points() APIs

Both cogl_matrix_transform_points and _project_points take points_in and
points_out arguments and explicitly allow pointing to the same array
(i.e. to transform in-place) The implementation of the various internal
transform functions though were not handling this possability and so it
was possible the reference partially transformed vertex values as if
they were original input values leading to incorrect results. This patch
ensures we take a temporary copy of the current input point when
transforming.
This commit is contained in:
Robert Bragg 2011-01-18 16:37:01 +00:00
parent 23ed01d4e9
commit e289e85397

View File

@ -444,17 +444,17 @@ _cogl_matrix_transform_points_f2_packed (const CoglMatrix *matrix,
void *points_out, void *points_out,
int n_points) int n_points)
{ {
Point2f *p = points_in;
Point3f *o = points_out; Point3f *o = points_out;
int i; int i;
for (i = 0; i < n_points; i++) for (i = 0; i < n_points; i++)
{ {
o[i].x = matrix->xx * p[i].x + matrix->xy * p[i].y + Point2f p = ((Point2f *)points_in)[i];
o[i].x = matrix->xx * p.x + matrix->xy * p.y +
matrix->xw; matrix->xw;
o[i].y = matrix->yx * p[i].x + matrix->yy * p[i].y + o[i].y = matrix->yx * p.x + matrix->yy * p.y +
matrix->yw; matrix->yw;
o[i].z = matrix->zx * p[i].x + matrix->zy * p[i].y + o[i].z = matrix->zx * p.x + matrix->zy * p.y +
matrix->zw; matrix->zw;
} }
} }
@ -471,12 +471,12 @@ _cogl_matrix_transform_points_f2_strided (const CoglMatrix *matrix,
for (i = 0; i < n_points; i++) for (i = 0; i < n_points; i++)
{ {
Point2f *p = (Point2f *)((guint8 *)points_in + i * stride_in); Point2f p = *(Point2f *)((guint8 *)points_in + i * stride_in);
Point3f *o = (Point3f *)((guint8 *)points_out + i * stride_out); Point3f *o = (Point3f *)((guint8 *)points_out + i * stride_out);
o->x = matrix->xx * p->x + matrix->xy * p->y + matrix->xw; o->x = matrix->xx * p.x + matrix->xy * p.y + matrix->xw;
o->y = matrix->yx * p->x + matrix->yy * p->y + matrix->yw; o->y = matrix->yx * p.x + matrix->yy * p.y + matrix->yw;
o->z = matrix->zx * p->x + matrix->zy * p->y + matrix->zw; o->z = matrix->zx * p.x + matrix->zy * p.y + matrix->zw;
} }
} }
@ -486,19 +486,19 @@ _cogl_matrix_project_points_f2_packed (const CoglMatrix *matrix,
void *points_out, void *points_out,
int n_points) int n_points)
{ {
Point2f *p = points_in;
Point4f *o = points_out; Point4f *o = points_out;
int i; int i;
for (i = 0; i < n_points; i++) for (i = 0; i < n_points; i++)
{ {
o[i].x = matrix->xx * p[i].x + matrix->xy * p[i].y + Point2f p = ((Point2f *)points_in)[i];
o[i].x = matrix->xx * p.x + matrix->xy * p.y +
matrix->xw; matrix->xw;
o[i].y = matrix->yx * p[i].x + matrix->yy * p[i].y + o[i].y = matrix->yx * p.x + matrix->yy * p.y +
matrix->yw; matrix->yw;
o[i].z = matrix->zx * p[i].x + matrix->zy * p[i].y + o[i].z = matrix->zx * p.x + matrix->zy * p.y +
matrix->zw; matrix->zw;
o[i].w = matrix->wx * p[i].x + matrix->wy * p[i].y + o[i].w = matrix->wx * p.x + matrix->wy * p.y +
matrix->ww; matrix->ww;
} }
} }
@ -515,13 +515,13 @@ _cogl_matrix_project_points_f2_strided (const CoglMatrix *matrix,
for (i = 0; i < n_points; i++) for (i = 0; i < n_points; i++)
{ {
Point2f *p = (Point2f *)((guint8 *)points_in + i * stride_in); Point2f p = *(Point2f *)((guint8 *)points_in + i * stride_in);
Point4f *o = (Point4f *)((guint8 *)points_out + i * stride_out); Point4f *o = (Point4f *)((guint8 *)points_out + i * stride_out);
o->x = matrix->xx * p->x + matrix->xy * p->y + matrix->xw; o->x = matrix->xx * p.x + matrix->xy * p.y + matrix->xw;
o->y = matrix->yx * p->x + matrix->yy * p->y + matrix->yw; o->y = matrix->yx * p.x + matrix->yy * p.y + matrix->yw;
o->z = matrix->zx * p->x + matrix->zy * p->y + matrix->zw; o->z = matrix->zx * p.x + matrix->zy * p.y + matrix->zw;
o->w = matrix->wx * p->x + matrix->wy * p->y + matrix->ww; o->w = matrix->wx * p.x + matrix->wy * p.y + matrix->ww;
} }
} }
@ -531,18 +531,18 @@ _cogl_matrix_transform_points_f3_packed (const CoglMatrix *matrix,
void *points_out, void *points_out,
int n_points) int n_points)
{ {
Point3f *p = points_in;
Point3f *o = points_out; Point3f *o = points_out;
int i; int i;
for (i = 0; i < n_points; i++) for (i = 0; i < n_points; i++)
{ {
o[i].x = matrix->xx * p[i].x + matrix->xy * p[i].y + Point3f p = ((Point3f *)points_in)[i];
matrix->xz * p[i].z + matrix->xw; o[i].x = matrix->xx * p.x + matrix->xy * p.y +
o[i].y = matrix->yx * p[i].x + matrix->yy * p[i].y + matrix->xz * p.z + matrix->xw;
matrix->yz * p[i].z + matrix->yw; o[i].y = matrix->yx * p.x + matrix->yy * p.y +
o[i].z = matrix->zx * p[i].x + matrix->zy * p[i].y + matrix->yz * p.z + matrix->yw;
matrix->zz * p[i].z + matrix->zw; o[i].z = matrix->zx * p.x + matrix->zy * p.y +
matrix->zz * p.z + matrix->zw;
} }
} }
@ -558,15 +558,15 @@ _cogl_matrix_transform_points_f3_strided (const CoglMatrix *matrix,
for (i = 0; i < n_points; i++) for (i = 0; i < n_points; i++)
{ {
Point3f *p = (Point3f *)((guint8 *)points_in + i * stride_in); Point3f p = *(Point3f *)((guint8 *)points_in + i * stride_in);
Point3f *o = (Point3f *)((guint8 *)points_out + i * stride_out); Point3f *o = (Point3f *)((guint8 *)points_out + i * stride_out);
o->x = matrix->xx * p->x + matrix->xy * p->y + o->x = matrix->xx * p.x + matrix->xy * p.y +
matrix->xz * p->z + matrix->xw; matrix->xz * p.z + matrix->xw;
o->y = matrix->yx * p->x + matrix->yy * p->y + o->y = matrix->yx * p.x + matrix->yy * p.y +
matrix->yz * p->z + matrix->yw; matrix->yz * p.z + matrix->yw;
o->z = matrix->zx * p->x + matrix->zy * p->y + o->z = matrix->zx * p.x + matrix->zy * p.y +
matrix->zz * p->z + matrix->zw; matrix->zz * p.z + matrix->zw;
} }
} }
@ -576,20 +576,20 @@ _cogl_matrix_project_points_f3_packed (const CoglMatrix *matrix,
void *points_out, void *points_out,
int n_points) int n_points)
{ {
Point3f *p = points_in;
Point4f *o = points_out; Point4f *o = points_out;
int i; int i;
for (i = 0; i < n_points; i++) for (i = 0; i < n_points; i++)
{ {
o[i].x = matrix->xx * p[i].x + matrix->xy * p[i].y + Point3f p = ((Point3f *)points_in)[i];
matrix->xz * p[i].z + matrix->xw; o[i].x = matrix->xx * p.x + matrix->xy * p.y +
o[i].y = matrix->yx * p[i].x + matrix->yy * p[i].y + matrix->xz * p.z + matrix->xw;
matrix->yz * p[i].z + matrix->yw; o[i].y = matrix->yx * p.x + matrix->yy * p.y +
o[i].z = matrix->zx * p[i].x + matrix->zy * p[i].y + matrix->yz * p.z + matrix->yw;
matrix->zz * p[i].z + matrix->zw; o[i].z = matrix->zx * p.x + matrix->zy * p.y +
o[i].w = matrix->wx * p[i].x + matrix->wy * p[i].y + matrix->zz * p.z + matrix->zw;
matrix->wz * p[i].z + matrix->ww; o[i].w = matrix->wx * p.x + matrix->wy * p.y +
matrix->wz * p.z + matrix->ww;
} }
} }
@ -605,17 +605,17 @@ _cogl_matrix_project_points_f3_strided (const CoglMatrix *matrix,
for (i = 0; i < n_points; i++) for (i = 0; i < n_points; i++)
{ {
Point3f *p = (Point3f *)((guint8 *)points_in + i * stride_in); Point3f p = *(Point3f *)((guint8 *)points_in + i * stride_in);
Point4f *o = (Point4f *)((guint8 *)points_out + i * stride_out); Point4f *o = (Point4f *)((guint8 *)points_out + i * stride_out);
o->x = matrix->xx * p->x + matrix->xy * p->y + o->x = matrix->xx * p.x + matrix->xy * p.y +
matrix->xz * p->z + matrix->xw; matrix->xz * p.z + matrix->xw;
o->y = matrix->yx * p->x + matrix->yy * p->y + o->y = matrix->yx * p.x + matrix->yy * p.y +
matrix->yz * p->z + matrix->yw; matrix->yz * p.z + matrix->yw;
o->z = matrix->zx * p->x + matrix->zy * p->y + o->z = matrix->zx * p.x + matrix->zy * p.y +
matrix->zz * p->z + matrix->zw; matrix->zz * p.z + matrix->zw;
o->w = matrix->wx * p->x + matrix->wy * p->y + o->w = matrix->wx * p.x + matrix->wy * p.y +
matrix->wz * p->z + matrix->ww; matrix->wz * p.z + matrix->ww;
} }
} }
@ -625,20 +625,20 @@ _cogl_matrix_project_points_f4_packed (const CoglMatrix *matrix,
void *points_out, void *points_out,
int n_points) int n_points)
{ {
Point4f *p = points_in;
Point4f *o = points_out; Point4f *o = points_out;
int i; int i;
for (i = 0; i < n_points; i++) for (i = 0; i < n_points; i++)
{ {
o[i].x = matrix->xx * p[i].x + matrix->xy * p[i].y + Point4f p = ((Point4f *)points_in)[i];
matrix->xz * p[i].z + matrix->xw * p[i].w; o[i].x = matrix->xx * p.x + matrix->xy * p.y +
o[i].y = matrix->yx * p[i].x + matrix->yy * p[i].y + matrix->xz * p.z + matrix->xw * p.w;
matrix->yz * p[i].z + matrix->yw * p[i].w; o[i].y = matrix->yx * p.x + matrix->yy * p.y +
o[i].z = matrix->zx * p[i].x + matrix->zy * p[i].y + matrix->yz * p.z + matrix->yw * p.w;
matrix->zz * p[i].z + matrix->zw * p[i].w; o[i].z = matrix->zx * p.x + matrix->zy * p.y +
o[i].w = matrix->wx * p[i].x + matrix->wy * p[i].y + matrix->zz * p.z + matrix->zw * p.w;
matrix->wz * p[i].z + matrix->ww * p[i].w; o[i].w = matrix->wx * p.x + matrix->wy * p.y +
matrix->wz * p.z + matrix->ww * p.w;
} }
} }
@ -654,17 +654,17 @@ _cogl_matrix_project_points_f4_strided (const CoglMatrix *matrix,
for (i = 0; i < n_points; i++) for (i = 0; i < n_points; i++)
{ {
Point4f *p = (Point4f *)((guint8 *)points_in + i * stride_in); Point4f p = *(Point4f *)((guint8 *)points_in + i * stride_in);
Point4f *o = (Point4f *)((guint8 *)points_out + i * stride_out); Point4f *o = (Point4f *)((guint8 *)points_out + i * stride_out);
o->x = matrix->xx * p->x + matrix->xy * p->y + o->x = matrix->xx * p.x + matrix->xy * p.y +
matrix->xz * p->z + matrix->xw * p->w; matrix->xz * p.z + matrix->xw * p.w;
o->y = matrix->yx * p->x + matrix->yy * p->y + o->y = matrix->yx * p.x + matrix->yy * p.y +
matrix->yz * p->z + matrix->yw * p->w; matrix->yz * p.z + matrix->yw * p.w;
o->z = matrix->zx * p->x + matrix->zy * p->y + o->z = matrix->zx * p.x + matrix->zy * p.y +
matrix->zz * p->z + matrix->zw * p->w; matrix->zz * p.z + matrix->zw * p.w;
o->w = matrix->wx * p->x + matrix->wy * p->y + o->w = matrix->wx * p.x + matrix->wy * p.y +
matrix->wz * p->z + matrix->ww * p->w; matrix->wz * p.z + matrix->ww * p.w;
} }
} }