[cogl/wip/color-mask: 1/2] matrix: Add cogl_matrix_init_looking_at API



commit 7f392a891a96b0aad8c40851d4572b21ec41e929
Author: Robert Bragg <robert linux intel com>
Date:   Sat Jul 9 02:43:17 2011 +0100

    matrix: Add cogl_matrix_init_looking_at API

 cogl/cogl-matrix.c |   59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 cogl/cogl-matrix.h |   38 +++++++++++++++++++++++++++++++++
 2 files changed, 97 insertions(+), 0 deletions(-)
---
diff --git a/cogl/cogl-matrix.c b/cogl/cogl-matrix.c
index 4ceece7..76b1581 100644
--- a/cogl/cogl-matrix.c
+++ b/cogl/cogl-matrix.c
@@ -2051,3 +2051,62 @@ cogl_matrix_is_identity (const CoglMatrix *matrix)
   else
     return memcmp (matrix, identity, sizeof (float) * 16) == 0;
 }
+
+void
+cogl_matrix_init_looking_at (CoglMatrix *matrix,
+                             float eye_position_x,
+                             float eye_position_y,
+                             float eye_position_z,
+                             float object_x,
+                             float object_y,
+                             float object_z,
+                             float world_up_x,
+                             float world_up_y,
+                             float world_up_z)
+{
+  CoglVector3 forward;
+  CoglVector3 side;
+  CoglVector3 up;
+
+  /* Get a unit viewing direction vector */
+  cogl_vector3_init (&forward,
+                     object_x - eye_position_x,
+                     object_y - eye_position_y,
+                     object_z - eye_position_z);
+  cogl_vector3_normalize (&forward);
+
+  cogl_vector3_init (&up, world_up_x, world_up_y, world_up_z);
+
+  /* Take the sideways direction as being perpendicular to the viewing
+   * direction and the word up vector. */
+  cogl_vector3_cross_product (&side, &forward, &up);
+  cogl_vector3_normalize (&side);
+
+  /* Now we have unit sideways and forward-direction vectors calculate
+   * a new mutually perpendicular up vector. */
+  cogl_vector3_cross_product (&up, &side, &forward);
+
+  matrix->xx = side.x;
+  matrix->yx = side.y;
+  matrix->zx = side.z;
+  matrix->wx = 0;
+
+  matrix->xy = up.x;
+  matrix->yy = up.y;
+  matrix->zy = up.z;
+  matrix->wy = 0;
+
+  matrix->xz = -forward.x;
+  matrix->yz = -forward.y;
+  matrix->zz = -forward.z;
+  matrix->wz = 0;
+
+  matrix->xw = 0;
+  matrix->yw = 0;
+  matrix->zw = 0;
+  matrix->ww = 1;
+
+  cogl_matrix_translate (matrix, -eye_position_x, -eye_position_y, -eye_position_z);
+
+  matrix->flags = (MAT_FLAG_GENERAL_3D | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE);
+}
diff --git a/cogl/cogl-matrix.h b/cogl/cogl-matrix.h
index a2c2385..71ec3b1 100644
--- a/cogl/cogl-matrix.h
+++ b/cogl/cogl-matrix.h
@@ -194,6 +194,44 @@ cogl_matrix_scale (CoglMatrix *matrix,
 		   float sz);
 
 /**
+ * cogl_matrix_init_looking_at:
+ * @matrix: A 4x4 transformation matrix
+ * @eye_position_x: The X coordinate to look from
+ * @eye_position_y: The Y coordinate to look from
+ * @eye_position_z: The Z coordinate to look from
+ * @object_x: The X coordinate of the object to look at
+ * @object_y: The Y coordinate of the object to look at
+ * @object_z: The Z coordinate of the object to look at
+ * @world_up_x: The X component of the world's up direction vector
+ * @world_up_y: The Y component of the world's up direction vector
+ * @world_up_z: The Z component of the world's up direction vector
+ *
+ * Initializes a view transform @matrix that positions the camera at
+ * the coordinate (@eye_position_x, @eye_position_y, @eye_position_z)
+ * looking towards an object at the coordinate (@object_x, @object_y,
+ * @object_z). The top of the camera is aligned to the given world up
+ * vector, which is normally simply (0, 1, 0) to map up to the
+ * positive direction of the y axis.
+ *
+ * <note>You should never look directly along the world-up
+ * vector</note>
+ *
+ * <note>It is assumed you are using a typical projection matrix where
+ * your origin maps to the center of your viewport.</note>
+ */
+void
+cogl_matrix_init_looking_at (CoglMatrix *matrix,
+                             float eye_position_x,
+                             float eye_position_y,
+                             float eye_position_z,
+                             float object_x,
+                             float object_y,
+                             float object_z,
+                             float world_up_x,
+                             float world_up_y,
+                             float world_up_z);
+
+/**
  * cogl_matrix_frustum:
  * @matrix: A 4x4 transformation matrix
  * @left: coord of left vertical clipping plane



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]