18d26deeff99e8414f3bf5d2c5849590e4adac6a
[simavr] / examples / shared / libc3 / src / c3geometry.h
1 /*
2         c3geometry.h
3
4         Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
5
6         This file is part of libc3.
7
8         libc3 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         libc3 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 libc3.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 /*
23  * c3geometry is a structure containing one set of vertices and various
24  * bits related to it. Ultimately it contains a pre-cached projected
25  * version of the vertices that the drawing code can use directly.
26  * c3geometry is aways attached to a c3object as a parent.
27  */
28
29 #ifndef __C3GEOMETRY_H___
30 #define __C3GEOMETRY_H___
31
32 #include "c3types.h"
33 #include "c_utils.h"
34
35
36 struct c3object_t;
37 struct c3pixels_t;
38 struct c3program_t;
39
40 /*
41  * Allow per-array storage of an extra buffer object
42  * if 'mutable' is not set (default), the array can be clear()ed after
43  * the buffer is bound.
44  * If the array is mutable, setting the 'dirty' flag will signal
45  * the rendering layer that it needs to update the buffer object
46  */
47 typedef struct c3geometry_buffer_t {
48         c3apiobject_t bid;      // buffer object
49         void * refCon;          // reference constant for application use
50         int mutable : 1, dirty : 1;
51 } c3geometry_buffer_t, * c3geometry_buffer_p;
52
53 DECLARE_C_ARRAY(c3vertex_t, c3vertex_array, 16, c3geometry_buffer_t buffer);
54 DECLARE_C_ARRAY(c3tex_t, c3tex_array, 16, c3geometry_buffer_t buffer);
55 DECLARE_C_ARRAY(c3colorf_t, c3colorf_array, 16, c3geometry_buffer_t buffer);
56 DECLARE_C_ARRAY(c3index_t, c3indices_array, 16, c3geometry_buffer_t buffer);
57
58 //! Geometry material.
59 typedef struct c3material_t {
60         c3colorf_t      color;
61         struct c3pixels_t * texture;
62         struct c3program_t * program;
63         struct {
64                 uint32_t src, dst;
65         } blend;
66 } c3material_t;
67
68 #define C3_TYPE(_a,_b,_c,_d) \
69         (((uint32_t)(_a)<<24)|((uint32_t)(_b)<<16)|((uint32_t)(_c)<<8)|(_d))
70
71 //! Generic geometry type
72 enum {
73         C3_RAW_TYPE = C3_TYPE('r','a','w','g'),
74         C3_TRIANGLE_TYPE = C3_TYPE('t','r','i','a'),
75 };
76
77 /*!
78  * geometry type.
79  * The type is used as non-opengl description of what the geometry
80  * contains, like "texture", and the subtype can be used to store the
81  * real format of the vertices. like GL_LINES etc
82  */
83 typedef struct c3geometry_type_t {
84         uint32_t type;                  // C3_RAW_TYPE etc
85         c3apiobject_t subtype;  // GL_LINES etc
86 } c3geometry_type_t;
87
88 /*!
89  * Geometry object. Describes a set of vertices, texture coordinates,
90  * normals, colors and material
91  * The projection is not set here, a geometry is always attached to a
92  * c3object that has the projection
93  */
94 typedef struct c3geometry_t {
95         c3geometry_type_t       type;   // geometry type
96         int                                     dirty : 1,
97                                                 custom : 1;             // has a custom driver
98         str_p                           name;   // optional
99         c3apiobject_t           bid;    // buffer id for opengl
100
101         c3material_t            mat;
102         struct c3object_t * object;     // parent object
103         const struct c3driver_geometry_t ** driver;
104
105         c3bbox_t                        bbox;   // world aligned bounding box
106         c3vertex_array_t        vertice;
107         c3tex_array_t           textures;       // optional: texture coordinates
108         c3vertex_array_t        normals;        // optional: vertex normals
109         c3indices_array_t       indices;        // optional: vertex indices
110         // could go ?
111         c3colorf_array_t        colorf;         // optional: vertex colors
112
113         /*
114          * Some shared attributes
115          */
116         union {
117                 struct {
118                         float width;
119                 } line;
120         };
121 } c3geometry_t, *c3geometry_p;
122
123 DECLARE_C_ARRAY(c3geometry_p, c3geometry_array, 4);
124
125 //! Allocates a new geometry, init it, and attached it to parent 'o' (optional)
126 c3geometry_p
127 c3geometry_new(
128                 c3geometry_type_t type,
129                 struct c3object_t * o /* = NULL */);
130 //! Init an existing new geometry, and attached it to parent 'o' (optional)
131 c3geometry_p
132 c3geometry_init(
133                 c3geometry_p g,
134                 c3geometry_type_t type,
135                 struct c3object_t * o /* = NULL */);
136 //! Disposes (via the driver interface) the geometry
137 void
138 c3geometry_dispose(
139                 c3geometry_p g);
140
141 //! Prepares a geometry. 
142 /*!
143  * The project phase is called only when the container object is 'dirty'
144  * for example if it's projection has changed.
145  * The project call is responsible for reprojecting the geometry and that
146  * sort of things
147  */
148 void
149 c3geometry_project(
150                 c3geometry_p g,
151                 c3mat4p m);
152
153 //! Draw the geometry
154 /*
155  * Called when drawing the context. Typicaly this calls the geometry 
156  * driver, which in turn will call the 'context' draw method, and the 
157  * application to draw this particular geometry
158  */
159 void
160 c3geometry_draw(
161                 c3geometry_p g );
162
163 /*
164  * if not present, create an index array, and collapses
165  * the vertex array by removing vertices that are within
166  * 'tolerance' (as a distance between vertices considered equals).
167  * If the normals exists, they are also compared
168  * to normaltolerance (in radian) and if both position and normals
169  * are within tolerance, the vertices are collapsed and the normals
170  * are averaged.
171  * This code allows smooth rendering of STL files generated by
172  * CAD programs that generate only triangle normals.
173  */
174 void
175 c3geometry_factor(
176                 c3geometry_p g,
177                 c3f tolerance,
178                 c3f normaltolerance);
179
180 //! allocate (if not there) and return a custom driver for this geometry
181 /*!
182  * Geometries come with a default, read only driver stack.. It is a constant
183  * global to save memory for each of the 'generic' object.
184  * This call will duplicate that stack and allocate (if not there) a read/write
185  * empty driver that the application can use to put their own, per object,
186  * callback. For example you can add your own project() or draw() function
187  * and have it called first
188  */
189 struct c3driver_geometry_t *
190 c3geometry_get_custom(
191                 c3geometry_p g );
192
193 IMPLEMENT_C_ARRAY(c3geometry_array);
194 IMPLEMENT_C_ARRAY(c3vertex_array);
195 IMPLEMENT_C_ARRAY(c3tex_array);
196 IMPLEMENT_C_ARRAY(c3colorf_array);
197 IMPLEMENT_C_ARRAY(c3indices_array);
198
199 static inline c3geometry_type_t
200 c3geometry_type(uint32_t type, int subtype)
201 {
202         c3geometry_type_t r;// = { .type = type, .subtype = subtype }; // older gcc <4.6 doesn't like this
203         r.type = type; r.subtype = (c3apiobject_t)subtype;
204         return r;
205 }
206
207 #endif /* __C3GEOMETRY_H___ */