reprap: Deleted example board
[simavr] / examples / shared / libc3 / src / c3context.h
1 /*
2         c3context.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 #ifndef __C3CONTEXT_H___
24 #define __C3CONTEXT_H___
25
26 #include "c3algebra.h"
27 #include "c3geometry.h"
28 #include "c3pixels.h"
29 #include "c3program.h"
30 #include "c3camera.h"
31
32 enum {
33         C3_CONTEXT_VIEW_NONE = 0,
34         C3_CONTEXT_VIEW_EYE,
35         C3_CONTEXT_VIEW_LIGHT
36 };
37
38 typedef struct c3context_view_t {
39         int                     type : 4,                       // C3_CONTEXT_VIEW_EYE...
40                                 dirty : 1,
41                                 index : 4;                      // index in context array
42         c3apiobject_t   bid;                    // buffer id (fbo, texture...)
43         c3vec2          size;                           // in pixels. for fbo/textures/window
44         c3cam_t         cam;
45         c3mat4          projection;                     // projection matrix
46
47         c3geometry_array_t      projected;
48         struct {
49                 c3f min, max;
50         } z;
51 } c3context_view_t, *c3context_view_p;
52
53 DECLARE_C_ARRAY(c3context_view_t, c3context_view_array, 4);
54
55 //! c3context_t is a container for a 'scene' to be drawn
56 /*!
57  * A c3context_t holds a root object, a list of already cached projected
58  * version of the geometry, and a driver that can be customized to draw it.
59  *
60  * This is a wrapper around a "top level object", the list of projected
61  * geometries is kept, purged and resorted if the root object becomes
62  * dirty
63  */
64 typedef struct c3context_t {
65         int     current;
66         c3context_view_array_t  views;
67
68         struct c3object_t * root;       // root object
69
70         c3pixels_array_t        pixels; // pixels, textures...
71         c3program_array_t       programs;       // fragment, vertex, geometry shaders
72
73         const struct c3driver_context_t ** driver;
74 } c3context_t, *c3context_p;
75
76 //! Allocates a new context of size w=width, h=height
77 c3context_p
78 c3context_new(
79                 int w,
80                 int h);
81
82 //! Initializes a new context 'c' of size w=width, h=height
83 c3context_p
84 c3context_init(
85                 c3context_p c,
86                 int w,
87                 int h);
88
89 //! Disposes the context, and everything underneath
90 void
91 c3context_dispose(
92                 c3context_p c);
93
94 //! Reproject geometry for dirty objects
95 int
96 c3context_project(
97                 c3context_p c);
98 //! Draws the context
99 void
100 c3context_draw(
101                 c3context_p c);
102
103 IMPLEMENT_C_ARRAY(c3context_view_array);
104
105 /*
106  * Set and get the current view, this is done
107  * before projecting and drawing
108  */
109 static inline c3context_view_p
110 c3context_view_get(
111                 c3context_p c )
112 {
113         return &c->views.e[c->current];
114 }
115
116 static inline c3context_view_p
117 c3context_view_get_at(
118                 c3context_p c,
119                 int view)
120 {
121         if (view < c->views.count)
122                 return &c->views.e[view];
123         return NULL;
124 }
125
126 static inline void
127 c3context_view_set(
128                 c3context_p c,
129                 int view)
130 {
131         if (view < c->views.count)
132                 c->current = view;
133 }
134
135 #endif /* __C3CONTEXT_H___ */