libc3: Few more updates
[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 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  * 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 "c3algebra.h"
33 #include "c_utils.h"
34
35 typedef c3vec3 c3vertex, *c3vertex_p;
36 typedef c3vec4 c3colorf, *c3colorf_p;
37 typedef c3vec2 c3tex, *c3tex_p;
38
39 struct c3object_t;
40 struct c3pixels_t;
41 struct c3program_t;
42
43 DECLARE_C_ARRAY(c3vertex, c3vertex_array, 16, uint32_t bid);
44 DECLARE_C_ARRAY(c3tex, c3tex_array, 16, uint32_t bid);
45 DECLARE_C_ARRAY(c3colorf, c3colorf_array, 16, uint32_t bid);
46
47 //! Geometry material. TODO: Beef up. Add vertex/fragment programs..
48 typedef struct c3material_t {
49         c3colorf        color;
50         struct c3pixels_t * texture;
51         struct c3program_t * program;
52         struct {
53                 uint32_t src, dst;
54         } blend;
55 } c3material_t;
56
57 //! Bounding box. TODO: Move to a separate file?
58 typedef struct c3bbox_t {
59         c3vec3  min, max;
60 } c3bbox_t;
61
62 //! Generic geometry type
63 enum {
64         C3_RAW_TYPE = 0,
65         C3_LINES_TYPE,
66         C3_TRIANGLE_TYPE,
67         C3_TEXTURE_TYPE,
68 };
69
70 /*!
71  * geometry type.
72  * The type is used as non-opengl description of what the geometry
73  * contains, like "texture", and the subtype can be used to store the
74  * real format of the vertices. like GL_LINES etc
75  */
76 typedef union c3geometry_type_t {
77         struct  { uint32_t type : 16, subtype : 16; };
78         uint32_t value;
79 } c3geometry_type_t;
80
81 /*!
82  * Geometry object. Describes a set of vertices, texture coordinates,
83  * normals, colors and material
84  * The projection is not set here, a geometry is always attached to a
85  * c3object that has the projection
86  */
87 typedef struct c3geometry_t {
88         c3geometry_type_t       type;   // C3_TRIANGLE_TYPE, GL_LINES etc
89         int                                     dirty : 1,
90                                                 custom : 1;             // has a custom driver
91         str_p                           name;   // optional
92         c3material_t            mat;
93         struct c3object_t * object;
94         const struct c3driver_geometry_t ** driver;
95
96         c3vertex_array_t        vertice;
97         c3tex_array_t           textures;
98         c3colorf_array_t        colorf;
99         c3vertex_array_t        normals;
100
101         // projected version of the vertice
102         c3vertex_array_t        projected;
103         c3bbox_t                        bbox;
104
105         /*
106          * Some shared attributes
107          */
108         union {
109                 struct {
110                         float width;
111                 } line;
112         };
113 } c3geometry_t, *c3geometry_p;
114
115 DECLARE_C_ARRAY(c3geometry_p, c3geometry_array, 4);
116
117 //! Allocates a new geometry, init it, and attached it to parent 'o' (optional)
118 c3geometry_p
119 c3geometry_new(
120                 c3geometry_type_t type,
121                 struct c3object_t * o /* = NULL */);
122 //! Init an existing new geometry, and attached it to parent 'o' (optional)
123 c3geometry_p
124 c3geometry_init(
125                 c3geometry_p g,
126                 c3geometry_type_t type,
127                 struct c3object_t * o /* = NULL */);
128 //! Disposes (via the driver interface) the geometry
129 void
130 c3geometry_dispose(
131                 c3geometry_p g);
132
133 //! Prepares a geometry. 
134 /*!
135  * The project phase is called only when the container object is 'dirty'
136  * for example if it's projection has changed.
137  * The project call is responsible for reprojecting the geometry and that
138  * sort of things
139  */
140 void
141 c3geometry_project(
142                 c3geometry_p g,
143                 c3mat4p m);
144
145 //! Draw the geometry
146 /*
147  * Called when drawing the context. Typicaly this calls the geometry 
148  * driver, which in turn will call the 'context' draw method, and the 
149  * application to draw this particular geometry
150  */
151 void
152 c3geometry_draw(
153                 c3geometry_p g );
154
155
156 //! allocate (if not there) and return a custom driver for this geometry
157 /*!
158  * Geometries come with a default, read only driver stack.. It is a constant
159  * global to save memory for each of the 'generic' object.
160  * This call will duplicate that stack and allocate (if not there) a read/write
161  * empty driver that the application can use to put their own, per object,
162  * callback. For example you can add your own project() or draw() function
163  * and have it called first
164  */
165 struct c3driver_geometry_t *
166 c3geometry_get_custom(
167                 c3geometry_p g );
168
169 IMPLEMENT_C_ARRAY(c3geometry_array);
170 IMPLEMENT_C_ARRAY(c3vertex_array);
171 IMPLEMENT_C_ARRAY(c3tex_array);
172 IMPLEMENT_C_ARRAY(c3colorf_array);
173
174 static inline c3geometry_type_t
175 c3geometry_type(int type, int subtype)
176 {
177         c3geometry_type_t r;// = { .type = type, .subtype = subtype }; // older gcc <4.6 doesn't like this
178         r.type = type; r.subtype = subtype;
179         return r;
180 }
181
182 #endif /* __C3GEOMETRY_H___ */