[Cogl] [PATCH] matrix: Add a init_translation() contructor

Damien Lespiau damien.lespiau at gmail.com
Thu May 3 06:15:04 PDT 2012


From: Damien Lespiau <damien.lespiau at intel.com>

This allows people to initialize a matrix with a translation
transformation. The options to do it at the moment were:

* init_from_array() but it give cogl no information about the type of
  matrix.
* init_indentity() and then translate() but it means doing a lot of
  computations for no reason.
---
 cogl/cogl-matrix.c                                 |   34 ++++++++++++++++++++
 cogl/cogl-matrix.h                                 |   24 ++++++++++++++
 .../cogl-2.0-experimental-sections.txt             |    1 +
 3 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/cogl/cogl-matrix.c b/cogl/cogl-matrix.c
index a346a5c..1a569c6 100644
--- a/cogl/cogl-matrix.c
+++ b/cogl/cogl-matrix.c
@@ -1596,6 +1596,40 @@ cogl_matrix_init_identity (CoglMatrix *matrix)
   _COGL_MATRIX_DEBUG_PRINT (matrix);
 }
 
+/*
+ * Set a matrix to the (tx, ty, tz) translation matrix.
+ *
+ * @matix matrix.
+ * @tx x coordinate of the translation vector
+ * @ty y coordinate of the translation vector
+ * @tz z coordinate of the translation vector
+ */
+static void
+_cogl_matrix_init_translation (CoglMatrix *matrix,
+                               float       tx,
+                               float       ty,
+                               float       tz)
+{
+  memcpy (matrix, identity, 16 * sizeof (float));
+
+  matrix->xw = tx;
+  matrix->yw = ty;
+  matrix->yw = tz;
+
+  matrix->type = COGL_MATRIX_TYPE_3D;
+  matrix->flags = MAT_FLAG_TRANSLATION | MAT_DIRTY_INVERSE;
+}
+
+void
+cogl_matrix_init_translation (CoglMatrix *matrix,
+                              float       tx,
+                              float       ty,
+                              float       tz)
+{
+  _cogl_matrix_init_translation (matrix, tx, ty, tz);
+  _COGL_MATRIX_DEBUG_PRINT (matrix);
+}
+
 #if 0
 /*
  * Test if the given matrix preserves vector lengths.
diff --git a/cogl/cogl-matrix.h b/cogl/cogl-matrix.h
index cca1823..b207e01 100644
--- a/cogl/cogl-matrix.h
+++ b/cogl/cogl-matrix.h
@@ -127,6 +127,30 @@ void
 cogl_matrix_init_identity (CoglMatrix *matrix);
 
 /**
+ * cogl_matrix_init_translation:
+ * @matrix: A 4x4 transformation matrix
+ * @tx x coordinate of the translation vector
+ * @ty y coordinate of the translation vector
+ * @tz z coordinate of the translation vector
+ *
+ * Resets matrix to the (tx, ty, tz) translation matrix:
+ *
+ * |[
+ *   .xx=1; .xy=0; .xz=0; .xw=tx;
+ *   .yx=0; .yy=1; .yz=0; .yw=ty;
+ *   .zx=0; .zy=0; .zz=1; .zw=tz;
+ *   .wx=0; .wy=0; .wz=0; .ww=1;
+ * ]|
+ *
+ * Since: 2.0
+ */
+void
+cogl_matrix_init_translation (CoglMatrix *matrix,
+                              float       tx,
+                              float       ty,
+                              float       tz);
+
+/**
  * cogl_matrix_multiply:
  * @result: The address of a 4x4 matrix to store the result in
  * @a: A 4x4 transformation matrix
diff --git a/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-sections.txt b/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-sections.txt
index ca3340d..c9af2fb 100644
--- a/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-sections.txt
+++ b/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-sections.txt
@@ -534,6 +534,7 @@ cogl_color_equal
 CoglMatrix
 cogl_matrix_init_identity
 cogl_matrix_init_from_array
+cogl_matrix_init_translation
 cogl_matrix_copy
 cogl_matrix_equal
 cogl_matrix_free
-- 
1.7.7.5



More information about the Cogl mailing list