1 /*
2  * Cogl
3  *
4  * A Low Level GPU Graphics and Utilities API
5  *
6  * Copyright (C) 2009,2010,2012 Intel Corporation.
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use, copy,
12  * modify, merge, publish, distribute, sublicense, and/or sell copies
13  * of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  *
28  *
29  *
30  * Authors:
31  *   Havoc Pennington <hp@pobox.com> for litl
32  *   Robert Bragg <robert@linux.intel.com>
33  */
34 
35 #ifndef _COGL_MATRIX_STACK_H_
36 #define _COGL_MATRIX_STACK_H_
37 
38 #if !defined(__COGL_H_INSIDE__) && !defined(COGL_COMPILATION)
39 #error "Only <cogl/cogl.h> can be included directly."
40 #endif
41 
42 #include "cogl-matrix.h"
43 #include "cogl-context.h"
44 
45 
46 /**
47  * SECTION:cogl-matrix-stack
48  * @short_description: Functions for efficiently tracking many
49  *                     related transformations
50  *
51  * Matrices can be used (for example) to describe the model-view
52  * transforms of objects, texture transforms, and projective
53  * transforms.
54  *
55  * The #CoglMatrix api provides a good way to manipulate individual
56  * matrices representing a single transformation but if you need to
57  * track many-many such transformations for many objects that are
58  * organized in a scenegraph for example then using a separate
59  * #CoglMatrix for each object may not be the most efficient way.
60  *
61  * A #CoglMatrixStack enables applications to track lots of
62  * transformations that are related to each other in some kind of
63  * hierarchy.  In a scenegraph for example if you want to know how to
64  * transform a particular node then you usually have to walk up
65  * through the ancestors and accumulate their transforms before
66  * finally applying the transform of the node itself. In this model
67  * things are grouped together spatially according to their ancestry
68  * and all siblings with the same parent share the same initial
69  * transformation. The #CoglMatrixStack API is suited to tracking lots
70  * of transformations that fit this kind of model.
71  *
72  * Compared to using the #CoglMatrix api directly to track many
73  * related transforms, these can be some advantages to using a
74  * #CoglMatrixStack:
75  * <itemizedlist>
76  *   <listitem>Faster equality comparisons of transformations</listitem>
77  *   <listitem>Efficient comparisons of the differences between arbitrary
78  *   transformations</listitem>
79  *   <listitem>Avoid redundant arithmetic related to common transforms
80  *   </listitem>
81  *   <listitem>Can be more space efficient (not always though)</listitem>
82  * </itemizedlist>
83  *
84  * For reference (to give an idea of when a #CoglMatrixStack can
85  * provide a space saving) a #CoglMatrix can be expected to take 72
86  * bytes whereas a single #CoglMatrixEntry in a #CoglMatrixStack is
87  * currently around 32 bytes on a 32bit CPU or 36 bytes on a 64bit
88  * CPU. An entry is needed for each individual operation applied to
89  * the stack (such as rotate, scale, translate) so if most of your
90  * leaf node transformations only need one or two simple operations
91  * relative to their parent then a matrix stack will likely take less
92  * space than having a #CoglMatrix for each node.
93  *
94  * Even without any space saving though the ability to perform fast
95  * comparisons and avoid redundant arithmetic (especially sine and
96  * cosine calculations for rotations) can make using a matrix stack
97  * worthwhile.
98  */
99 
100 /**
101  * CoglMatrixStack:
102  *
103  * Tracks your current position within a hierarchy and lets you build
104  * up a graph of transformations as you traverse through a hierarchy
105  * such as a scenegraph.
106  *
107  * A #CoglMatrixStack always maintains a reference to a single
108  * transformation at any point in time, representing the
109  * transformation at the current position in the hierarchy. You can
110  * get a reference to the current transformation by calling
111  * cogl_matrix_stack_get_entry().
112  *
113  * When a #CoglMatrixStack is first created with
114  * cogl_matrix_stack_new() then it is conceptually positioned at the
115  * root of your hierarchy and the current transformation simply
116  * represents an identity transformation.
117  *
118  * As you traverse your object hierarchy (your scenegraph) then you
119  * should call cogl_matrix_stack_push() whenever you move down one
120  * level and call cogl_matrix_stack_pop() whenever you move back up
121  * one level towards the root.
122  *
123  * At any time you can apply a set of operations, such as "rotate",
124  * "scale", "translate" on top of the current transformation of a
125  * #CoglMatrixStack using functions such as
126  * cogl_matrix_stack_rotate(), cogl_matrix_stack_scale() and
127  * cogl_matrix_stack_translate(). These operations will derive a new
128  * current transformation and will never affect a transformation
129  * that you have referenced using cogl_matrix_stack_get_entry().
130  *
131  * Internally applying operations to a #CoglMatrixStack builds up a
132  * graph of #CoglMatrixEntry structures which each represent a single
133  * immutable transform.
134  */
135 typedef struct _CoglMatrixStack CoglMatrixStack;
136 
137 /**
138  * cogl_matrix_stack_get_gtype:
139  *
140  * Returns: a #GType that can be used with the GLib type system.
141  */
142 GType cogl_matrix_stack_get_gtype (void);
143 
144 /**
145  * CoglMatrixEntry:
146  *
147  * Represents a single immutable transformation that was retrieved
148  * from a #CoglMatrixStack using cogl_matrix_stack_get_entry().
149  *
150  * Internally a #CoglMatrixEntry represents a single matrix
151  * operation (such as "rotate", "scale", "translate") which is applied
152  * to the transform of a single parent entry.
153  *
154  * Using the #CoglMatrixStack api effectively builds up a graph of
155  * these immutable #CoglMatrixEntry structures whereby operations
156  * that can be shared between multiple transformations will result
157  * in shared #CoglMatrixEntry nodes in the graph.
158  *
159  * When a #CoglMatrixStack is first created it references one
160  * #CoglMatrixEntry that represents a single "load identity"
161  * operation. This serves as the root entry and all operations
162  * that are then applied to the stack will extend the graph
163  * starting from this root "load identity" entry.
164  *
165  * Given the typical usage model for a #CoglMatrixStack and the way
166  * the entries are built up while traversing a scenegraph then in most
167  * cases where an application is interested in comparing two
168  * transformations for equality then it is enough to simply compare
169  * two #CoglMatrixEntry pointers directly. Technically this can lead
170  * to false negatives that could be identified with a deeper
171  * comparison but often these false negatives are unlikely and
172  * don't matter anyway so this enables extremely cheap comparisons.
173  *
174  * <note>#CoglMatrixEntry<!-- -->s are reference counted using
175  * cogl_matrix_entry_ref() and cogl_matrix_entry_unref() not with
176  * cogl_object_ref() and cogl_object_unref().</note>
177  */
178 typedef struct _CoglMatrixEntry CoglMatrixEntry;
179 
180 /**
181  * cogl_matrix_entry_get_gtype:
182  *
183  * Returns: a #GType that can be used with the GLib type system.
184  */
185 GType cogl_matrix_entry_get_gtype (void);
186 
187 
188 /**
189  * cogl_matrix_stack_new:
190  * @ctx: A #CoglContext
191  *
192  * Allocates a new #CoglMatrixStack that can be used to build up
193  * transformations relating to objects in a scenegraph like hierarchy.
194  * (See the description of #CoglMatrixStack and #CoglMatrixEntry for
195  * more details of what a matrix stack is best suited for)
196  *
197  * When a #CoglMatrixStack is first allocated it is conceptually
198  * positioned at the root of your scenegraph hierarchy. As you
199  * traverse your scenegraph then you should call
200  * cogl_matrix_stack_push() whenever you move down a level and
201  * cogl_matrix_stack_pop() whenever you move back up a level towards
202  * the root.
203  *
204  * Once you have allocated a #CoglMatrixStack you can get a reference
205  * to the current transformation for the current position in the
206  * hierarchy by calling cogl_matrix_stack_get_entry().
207  *
208  * Once you have allocated a #CoglMatrixStack you can apply operations
209  * such as rotate, scale and translate to modify the current transform
210  * for the current position in the hierarchy by calling
211  * cogl_matrix_stack_rotate(), cogl_matrix_stack_scale() and
212  * cogl_matrix_stack_translate().
213  *
214  * Return value: (transfer full): A newly allocated #CoglMatrixStack
215  */
216 CoglMatrixStack *
217 cogl_matrix_stack_new (CoglContext *ctx);
218 
219 /**
220  * cogl_matrix_stack_push:
221  * @stack: A #CoglMatrixStack
222  *
223  * Saves the current transform and starts a new transform that derives
224  * from the current transform.
225  *
226  * This is usually called while traversing a scenegraph whenever you
227  * traverse one level deeper. cogl_matrix_stack_pop() can then be
228  * called when going back up one layer to restore the previous
229  * transform of an ancestor.
230  */
231 void
232 cogl_matrix_stack_push (CoglMatrixStack *stack);
233 
234 /**
235  * cogl_matrix_stack_pop:
236  * @stack: A #CoglMatrixStack
237  *
238  * Restores the previous transform that was last saved by calling
239  * cogl_matrix_stack_push().
240  *
241  * This is usually called while traversing a scenegraph whenever you
242  * return up one level in the graph towards the root node.
243  */
244 void
245 cogl_matrix_stack_pop (CoglMatrixStack *stack);
246 
247 /**
248  * cogl_matrix_stack_load_identity:
249  * @stack: A #CoglMatrixStack
250  *
251  * Resets the current matrix to the identity matrix.
252  */
253 void
254 cogl_matrix_stack_load_identity (CoglMatrixStack *stack);
255 
256 /**
257  * cogl_matrix_stack_scale:
258  * @stack: A #CoglMatrixStack
259  * @x: Amount to scale along the x-axis
260  * @y: Amount to scale along the y-axis
261  * @z: Amount to scale along the z-axis
262  *
263  * Multiplies the current matrix by one that scales the x, y and z
264  * axes by the given values.
265  */
266 void
267 cogl_matrix_stack_scale (CoglMatrixStack *stack,
268                          float x,
269                          float y,
270                          float z);
271 
272 /**
273  * cogl_matrix_stack_translate:
274  * @stack: A #CoglMatrixStack
275  * @x: Distance to translate along the x-axis
276  * @y: Distance to translate along the y-axis
277  * @z: Distance to translate along the z-axis
278  *
279  * Multiplies the current matrix by one that translates along all
280  * three axes according to the given values.
281  */
282 void
283 cogl_matrix_stack_translate (CoglMatrixStack *stack,
284                              float x,
285                              float y,
286                              float z);
287 
288 /**
289  * cogl_matrix_stack_rotate:
290  * @stack: A #CoglMatrixStack
291  * @angle: Angle in degrees to rotate.
292  * @x: X-component of vertex to rotate around.
293  * @y: Y-component of vertex to rotate around.
294  * @z: Z-component of vertex to rotate around.
295  *
296  * Multiplies the current matrix by one that rotates the around the
297  * axis-vector specified by @x, @y and @z. The rotation follows the
298  * right-hand thumb rule so for example rotating by 10 degrees about
299  * the axis-vector (0, 0, 1) causes a small counter-clockwise
300  * rotation.
301  */
302 void
303 cogl_matrix_stack_rotate (CoglMatrixStack *stack,
304                           float angle,
305                           float x,
306                           float y,
307                           float z);
308 
309 /**
310  * cogl_matrix_stack_rotate_quaternion:
311  * @stack: A #CoglMatrixStack
312  * @quaternion: A #CoglQuaternion
313  *
314  * Multiplies the current matrix by one that rotates according to the
315  * rotation described by @quaternion.
316  */
317 void
318 cogl_matrix_stack_rotate_quaternion (CoglMatrixStack *stack,
319                                      const CoglQuaternion *quaternion);
320 
321 /**
322  * cogl_matrix_stack_rotate_euler:
323  * @stack: A #CoglMatrixStack
324  * @euler: A #CoglEuler
325  *
326  * Multiplies the current matrix by one that rotates according to the
327  * rotation described by @euler.
328  */
329 void
330 cogl_matrix_stack_rotate_euler (CoglMatrixStack *stack,
331                                 const CoglEuler *euler);
332 
333 /**
334  * cogl_matrix_stack_multiply:
335  * @stack: A #CoglMatrixStack
336  * @matrix: the matrix to multiply with the current model-view
337  *
338  * Multiplies the current matrix by the given matrix.
339  */
340 void
341 cogl_matrix_stack_multiply (CoglMatrixStack *stack,
342                             const CoglMatrix *matrix);
343 
344 /**
345  * cogl_matrix_stack_frustum:
346  * @stack: A #CoglMatrixStack
347  * @left: X position of the left clipping plane where it
348  *   intersects the near clipping plane
349  * @right: X position of the right clipping plane where it
350  *   intersects the near clipping plane
351  * @bottom: Y position of the bottom clipping plane where it
352  *   intersects the near clipping plane
353  * @top: Y position of the top clipping plane where it intersects
354  *   the near clipping plane
355  * @z_near: The distance to the near clipping plane (Must be positive)
356  * @z_far: The distance to the far clipping plane (Must be positive)
357  *
358  * Replaces the current matrix with a perspective matrix for a given
359  * viewing frustum defined by 4 side clip planes that all cross
360  * through the origin and 2 near and far clip planes.
361  */
362 void
363 cogl_matrix_stack_frustum (CoglMatrixStack *stack,
364                            float left,
365                            float right,
366                            float bottom,
367                            float top,
368                            float z_near,
369                            float z_far);
370 
371 /**
372  * cogl_matrix_stack_perspective:
373  * @stack: A #CoglMatrixStack
374  * @fov_y: Vertical field of view angle in degrees.
375  * @aspect: The (width over height) aspect ratio for display
376  * @z_near: The distance to the near clipping plane (Must be positive,
377  *   and must not be 0)
378  * @z_far: The distance to the far clipping plane (Must be positive)
379  *
380  * Replaces the current matrix with a perspective matrix based on the
381  * provided values.
382  *
383  * <note>You should be careful not to have too great a @z_far / @z_near
384  * ratio since that will reduce the effectiveness of depth testing
385  * since there wont be enough precision to identify the depth of
386  * objects near to each other.</note>
387  */
388 void
389 cogl_matrix_stack_perspective (CoglMatrixStack *stack,
390                                float fov_y,
391                                float aspect,
392                                float z_near,
393                                float z_far);
394 
395 /**
396  * cogl_matrix_stack_orthographic:
397  * @stack: A #CoglMatrixStack
398  * @x_1: The x coordinate for the first vertical clipping plane
399  * @y_1: The y coordinate for the first horizontal clipping plane
400  * @x_2: The x coordinate for the second vertical clipping plane
401  * @y_2: The y coordinate for the second horizontal clipping plane
402  * @near: The <emphasis>distance</emphasis> to the near clipping
403  *   plane (will be <emphasis>negative</emphasis> if the plane is
404  *   behind the viewer)
405  * @far: The <emphasis>distance</emphasis> to the far clipping
406  *   plane (will be <emphasis>negative</emphasis> if the plane is
407  *   behind the viewer)
408  *
409  * Replaces the current matrix with an orthographic projection matrix.
410  */
411 void
412 cogl_matrix_stack_orthographic (CoglMatrixStack *stack,
413                                 float x_1,
414                                 float y_1,
415                                 float x_2,
416                                 float y_2,
417                                 float near,
418                                 float far);
419 
420 /**
421  * cogl_matrix_stack_get_inverse:
422  * @stack: A #CoglMatrixStack
423  * @inverse: (out): The destination for a 4x4 inverse transformation matrix
424  *
425  * Gets the inverse transform of the current matrix and uses it to
426  * initialize a new #CoglMatrix.
427  *
428  * Return value: %TRUE if the inverse was successfully calculated or %FALSE
429  *   for degenerate transformations that can't be inverted (in this case the
430  *   @inverse matrix will simply be initialized with the identity matrix)
431  */
432 CoglBool
433 cogl_matrix_stack_get_inverse (CoglMatrixStack *stack,
434                                CoglMatrix *inverse);
435 
436 /**
437  * cogl_matrix_stack_get_entry:
438  * @stack: A #CoglMatrixStack
439  *
440  * Gets a reference to the current transform represented by a
441  * #CoglMatrixEntry pointer.
442  *
443  * <note>The transform represented by a #CoglMatrixEntry is
444  * immutable.</note>
445  *
446  * <note>#CoglMatrixEntry<!-- -->s are reference counted using
447  * cogl_matrix_entry_ref() and cogl_matrix_entry_unref() and you
448  * should call cogl_matrix_entry_unref() when you are finished with
449  * and entry you get via cogl_matrix_stack_get_entry().</note>
450  *
451  * Return value: (transfer none): A pointer to the #CoglMatrixEntry
452  *               representing the current matrix stack transform.
453  */
454 CoglMatrixEntry *
455 cogl_matrix_stack_get_entry (CoglMatrixStack *stack);
456 
457 /**
458  * cogl_matrix_stack_get:
459  * @stack: A #CoglMatrixStack
460  * @matrix: (out): The potential destination for the current matrix
461  *
462  * Resolves the current @stack transform into a #CoglMatrix by
463  * combining the operations that have been applied to build up the
464  * current transform.
465  *
466  * There are two possible ways that this function may return its
467  * result depending on whether the stack is able to directly point
468  * to an internal #CoglMatrix or whether the result needs to be
469  * composed of multiple operations.
470  *
471  * If an internal matrix contains the required result then this
472  * function will directly return a pointer to that matrix, otherwise
473  * if the function returns %NULL then @matrix will be initialized
474  * to match the current transform of @stack.
475  *
476  * <note>@matrix will be left untouched if a direct pointer is
477  * returned.</note>
478  *
479  * Return value: A direct pointer to the current transform or %NULL
480  *               and in that case @matrix will be initialized with
481  *               the value of the current transform.
482  */
483 CoglMatrix *
484 cogl_matrix_stack_get (CoglMatrixStack *stack,
485                        CoglMatrix *matrix);
486 
487 /**
488  * cogl_matrix_entry_get:
489  * @entry: A #CoglMatrixEntry
490  * @matrix: (out): The potential destination for the transform as
491  *                 a matrix
492  *
493  * Resolves the current @entry transform into a #CoglMatrix by
494  * combining the sequence of operations that have been applied to
495  * build up the current transform.
496  *
497  * There are two possible ways that this function may return its
498  * result depending on whether it's possible to directly point
499  * to an internal #CoglMatrix or whether the result needs to be
500  * composed of multiple operations.
501  *
502  * If an internal matrix contains the required result then this
503  * function will directly return a pointer to that matrix, otherwise
504  * if the function returns %NULL then @matrix will be initialized
505  * to match the transform of @entry.
506  *
507  * <note>@matrix will be left untouched if a direct pointer is
508  * returned.</note>
509  *
510  * Return value: A direct pointer to a #CoglMatrix transform or %NULL
511  *               and in that case @matrix will be initialized with
512  *               the effective transform represented by @entry.
513  */
514 CoglMatrix *
515 cogl_matrix_entry_get (CoglMatrixEntry *entry,
516                        CoglMatrix *matrix);
517 
518 /**
519  * cogl_matrix_stack_set:
520  * @stack: A #CoglMatrixStack
521  * @matrix: A #CoglMatrix replace the current matrix value with
522  *
523  * Replaces the current @stack matrix value with the value of @matrix.
524  * This effectively discards any other operations that were applied
525  * since the last time cogl_matrix_stack_push() was called or since
526  * the stack was initialized.
527  */
528 void
529 cogl_matrix_stack_set (CoglMatrixStack *stack,
530                        const CoglMatrix *matrix);
531 
532 /**
533  * cogl_is_matrix_stack:
534  * @object: a #CoglObject
535  *
536  * Determines if the given #CoglObject refers to a #CoglMatrixStack.
537  *
538  * Return value: %TRUE if @object is a #CoglMatrixStack, otherwise
539  *               %FALSE.
540  */
541 CoglBool
542 cogl_is_matrix_stack (void *object);
543 
544 /**
545  * cogl_matrix_entry_calculate_translation:
546  * @entry0: The first reference transform
547  * @entry1: A second reference transform
548  * @x: (out): The destination for the x-component of the translation
549  * @y: (out): The destination for the y-component of the translation
550  * @z: (out): The destination for the z-component of the translation
551  *
552  * Determines if the only difference between two transforms is a
553  * translation and if so returns what the @x, @y, and @z components of
554  * the translation are.
555  *
556  * If the difference between the two translations involves anything
557  * other than a translation then the function returns %FALSE.
558  *
559  * Return value: %TRUE if the only difference between the transform of
560  *                @entry0 and the transform of @entry1 is a translation,
561  *                otherwise %FALSE.
562  */
563 CoglBool
564 cogl_matrix_entry_calculate_translation (CoglMatrixEntry *entry0,
565                                          CoglMatrixEntry *entry1,
566                                          float *x,
567                                          float *y,
568                                          float *z);
569 
570 /**
571  * cogl_matrix_entry_is_identity:
572  * @entry: A #CoglMatrixEntry
573  *
574  * Determines whether @entry is known to represent an identity
575  * transform.
576  *
577  * If this returns %TRUE then the entry is definitely the identity
578  * matrix. If it returns %FALSE it may or may not be the identity
579  * matrix but no expensive comparison is performed to verify it.
580  *
581  * Return value: %TRUE if @entry is definitely an identity transform,
582  *               otherwise %FALSE.
583  */
584 CoglBool
585 cogl_matrix_entry_is_identity (CoglMatrixEntry *entry);
586 
587 /**
588  * cogl_matrix_entry_equal:
589  * @entry0: The first #CoglMatrixEntry to compare
590  * @entry1: A second #CoglMatrixEntry to compare
591  *
592  * Compares two arbitrary #CoglMatrixEntry transforms for equality
593  * returning %TRUE if they are equal or %FALSE otherwise.
594  *
595  * <note>In many cases it is unnecessary to use this api and instead
596  * direct pointer comparisons of entries are good enough and much
597  * cheaper too.</note>
598  *
599  * Return value: %TRUE if @entry0 represents the same transform as
600  *               @entry1, otherwise %FALSE.
601  */
602 CoglBool
603 cogl_matrix_entry_equal (CoglMatrixEntry *entry0,
604                          CoglMatrixEntry *entry1);
605 
606 /**
607  * cogl_debug_matrix_entry_print:
608  * @entry: A #CoglMatrixEntry
609  *
610  * Allows visualizing the operations that build up the given @entry
611  * for debugging purposes by printing to stdout.
612  */
613 void
614 cogl_debug_matrix_entry_print (CoglMatrixEntry *entry);
615 
616 /**
617  * cogl_matrix_entry_ref:
618  * @entry: A #CoglMatrixEntry
619  *
620  * Takes a reference on the given @entry to ensure the @entry stays
621  * alive and remains valid. When you are finished with the @entry then
622  * you should call cogl_matrix_entry_unref().
623  *
624  * It is an error to pass an @entry pointer to cogl_object_ref() and
625  * cogl_object_unref()
626  */
627 CoglMatrixEntry *
628 cogl_matrix_entry_ref (CoglMatrixEntry *entry);
629 
630 /**
631  * cogl_matrix_entry_unref:
632  * @entry: A #CoglMatrixEntry
633  *
634  * Releases a reference on @entry either taken by calling
635  * cogl_matrix_entry_unref() or to release the reference given when
636  * calling cogl_matrix_stack_get_entry().
637  */
638 void
639 cogl_matrix_entry_unref (CoglMatrixEntry *entry);
640 
641 #endif /* _COGL_MATRIX_STACK_H_ */
642