1ee1d9c27821310ee5de2963d1143a2452b8934f
[simavr] / examples / board_reprap / src / c3 / c3object.h
1 /*
2         c3object.h
3
4         Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #ifndef __C3OBJECT_H___
24 #define __C3OBJECT_H___
25
26 #include <stdbool.h>
27 #include "c3/c3transform.h"
28 #include "c3/c3geometry.h"
29
30 struct c3object_t;
31
32 DECLARE_C_ARRAY(struct c3object_t*, c3object_array, 4);
33
34 //! c3object is a container for child object, and geometry
35 /*!
36  * a c3object is a node in a c3object tree, it contains sub-objects and/or
37  * geometry. It also contains it's own list of transform matrices, so can
38  * be seen as a "anchor" that can be moved around and where you can
39  * attach other objects or geometry.
40  *
41  * An object has a notion of 'dirty bit' -- something that signals that
42  * something has changed and deserved reprojection. the dirty bit
43  * is propagated upward when 1 (up to the root object) and downward when 0
44  * (to allow clearing the bit on a subtree)
45  */
46 typedef struct c3object_t {
47         str_p                           name;   //! optional name
48         int                                     dirty : 1, 
49                                                 visible : 1 /* TODO: Implement visible */;
50         struct c3context_t * context; //! context this object is attached to
51         struct c3object_t * parent;             //! Parent object
52         const struct c3driver_object_t ** driver;       //! Driver stack
53         c3transform_array_t     transform;
54         c3object_array_t        objects;        //! child object list
55         c3geometry_array_t      geometry;       //! Object geometri(es)
56 } c3object_t, *c3object_p;
57
58 //! Allocates and initialize an emty object, attaches it to parent 'o'
59 c3object_p
60 c3object_new(
61                 c3object_p o /* = NULL */);
62 //! Disposes of everything under this object
63 void
64 c3object_dispose(
65                 c3object_p o);
66 //! Clears every sub-object, geometry, and transform, but do not dispose of o
67 void
68 c3object_clear(
69                 c3object_p o);
70 //! Initializes 'o' as a new object, attaches it to parent (optional)
71 c3object_p
72 c3object_init(
73                 c3object_p o,
74                 c3object_p parent /* = NULL */);
75 //! sets the dirty bit for 'o' and related tree
76 /*!
77  * When dirty is 1, sets the dirty bit of this object and all the parent
78  * objects up to the root object.
79  * When dirty is 0, clear the dirty bit of this object, and all the
80  * sub objects.
81  */
82 void
83 c3object_set_dirty(
84                 c3object_p o,
85                 bool dirty);
86 //! Adds a new geometry g to object o
87 void
88 c3object_add_geometry(
89                 c3object_p o,
90                 c3geometry_p g);
91 //! Adds a new sub-object sub to object o
92 void
93 c3object_add_object(
94                 c3object_p o,
95                 c3object_p sub);
96 //! Adds a new transform matrix, initialized as identity
97 c3transform_p
98 c3object_add_transform(
99                 c3object_p o );
100 //! Iterates all the sub-objects and collects all the geometries
101 /*!
102  * This call iterates the sub-objects and collects all their 'projected'
103  * geometry, and add them to the array
104  */
105 void
106 c3object_get_geometry(
107                 c3object_p o,
108                 c3geometry_array_p array );
109 //! Project object 'o' using it's own transformations, relative to matrix 'm'
110 /*!
111  * Multiply this objects transformation(s) to matrix 'm' and calls
112  * reprojects the geometries using that matrix as an anchor. also call
113  * recursively to sub-objects to follow the projection down.
114  */
115 void
116 c3object_project(
117                 c3object_p o,
118                 const c3mat4p m);
119
120 IMPLEMENT_C_ARRAY(c3object_array);
121
122 #endif /* __C3OBJECT_H___ */