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