]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.h
Route rendering calls through Renderer and add an exclusion system
[libs/gl.git] / source / renderer.h
1 #ifndef MSP_GL_RENDERER_H_
2 #define MSP_GL_RENDERER_H_
3
4 #include <set>
5 #include <vector>
6 #include "matrix.h"
7 #include "tag.h"
8
9 namespace Msp {
10 namespace GL {
11
12 class Batch;
13 class Buffer;
14 class Camera;
15 class Material;
16 class Program;
17 class ProgramData;
18 class Renderable;
19 class Texture;
20 class Texturing;
21 class VertexArray;
22 class WindingTest;
23
24 /**
25 A class for supervising the rendering process.  While many Renderables (in
26 particular, Objects and Scenes) can by rendered without a Renderer, using one
27 will often be more efficient.  This is especially true for ObjectInstances.
28
29 The Renderer works by deferring GL state changes until something is actually
30 being drawn.  This avoids many unnecessary GL calls if consecutive renderables
31 use the same resources.
32
33 A state stack is provided to help with state scoping.  Typically a Renderable
34 will push the current state on entry, set whatever state it requires, render
35 itself, and pop the state when it's done.  An RAII helper class is provided for
36 the push/pop operation.
37 */
38 class Renderer
39 {
40 public:
41         class Push
42         {
43         private:
44                 Renderer &renderer;
45
46         public:
47                 Push(Renderer &r): renderer(r) { renderer.push_state(); }
48                 ~Push() { renderer.pop_state(); }
49         };
50
51         class Exclude
52         {
53         private:
54                 Renderer &renderer;
55                 const Renderable &renderable;
56
57         public:
58                 Exclude(Renderer &r, const Renderable &e): renderer(r), renderable(e) { renderer.exclude(renderable); }
59                 ~Exclude() { renderer.include(renderable); }
60         };
61
62 private:
63         struct State
64         {
65                 const Texture *texture;
66                 const Texturing *texturing;
67                 const Material *material;
68                 const Program *shprog;
69                 unsigned shdata_count;
70                 const WindingTest *winding_test;
71
72                 State();
73         };
74
75         class MtxStack: public MatrixStack
76         {
77         private:
78                 Renderer &renderer;
79
80         public:
81                 MtxStack(Renderer &);
82         private:        
83                 virtual void update();
84         };
85
86         MtxStack mtx_stack;
87         bool mtx_changed;
88         const Camera *camera;
89         std::vector<State> state_stack;
90         State *state;
91         std::vector<const ProgramData *> shdata_stack;
92         bool shdata_changed;
93         const VertexArray *vertex_array;
94         bool vertex_array_changed;
95         const Buffer *element_buffer;
96         std::set<const Renderable *> excluded;
97
98 public:
99         Renderer(const Camera *);
100         ~Renderer();
101
102         MatrixStack &matrix_stack() { return mtx_stack; }
103
104         const Camera *get_camera() const { return camera; }
105
106         void set_texture(const Texture *);
107         void set_texturing(const Texturing *);
108         void set_material(const Material *);
109
110         /** Sets the shader program to use.  An initial set of data can be set as
111         well, with the same semantics as add_shader_data. */
112         void set_shader_program(const Program *prog, const ProgramData *data = 0);
113
114         /** Adds another set of data to be use with shader programs.  The data is
115         independent of any shader program changes and remains in effect until the
116         Renderer state is popped. */
117         void add_shader_data(const ProgramData &data);
118
119         void set_vertex_array(const VertexArray *);
120         void set_element_buffer(const Buffer *);
121         void set_winding_test(const WindingTest *);
122
123         /** Saves the current state so it can be restored later. */
124         void push_state();
125
126         /** Restores a previously saved state.  Must be matched with an earlier
127         push_state call. */
128         void pop_state();
129
130         /** Prepares for temporarily bypassing the Renderer by synchronizing the
131         current state with GL.  No additional call is necessary to resume using the
132         Renderer. */
133         void escape();
134
135         void exclude(const Renderable &);
136         void include(const Renderable &);
137
138         void render(const Renderable &, const Tag & = Tag());
139         void draw(const Batch &);
140
141 private:
142         void apply_state();
143 };
144
145 } // namespace GL
146 } // namespace Msp
147
148 #endif