]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.h
Renderer no longer needs to track vertex array changes
[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         const Buffer *element_buffer;
95         std::set<const Renderable *> excluded;
96
97 public:
98         Renderer(const Camera *);
99         ~Renderer();
100
101         MatrixStack &matrix_stack() { return mtx_stack; }
102
103         const Camera *get_camera() const { return camera; }
104
105         void set_texture(const Texture *);
106         void set_texturing(const Texturing *);
107         void set_material(const Material *);
108
109         /** Sets the shader program to use.  An initial set of data can be set as
110         well, with the same semantics as add_shader_data. */
111         void set_shader_program(const Program *prog, const ProgramData *data = 0);
112
113         /** Adds another set of data to be use with shader programs.  The data is
114         independent of any shader program changes and remains in effect until the
115         Renderer state is popped. */
116         void add_shader_data(const ProgramData &data);
117
118         void set_vertex_array(const VertexArray *);
119         void set_element_buffer(const Buffer *);
120         void set_winding_test(const WindingTest *);
121
122         /** Saves the current state so it can be restored later. */
123         void push_state();
124
125         /** Restores a previously saved state.  Must be matched with an earlier
126         push_state call. */
127         void pop_state();
128
129         /** Prepares for temporarily bypassing the Renderer by synchronizing the
130         current state with GL.  No additional call is necessary to resume using the
131         Renderer. */
132         void escape();
133
134         void exclude(const Renderable &);
135         void include(const Renderable &);
136
137         void render(const Renderable &, const Tag & = Tag());
138         void draw(const Batch &);
139
140 private:
141         void apply_state();
142 };
143
144 } // namespace GL
145 } // namespace Msp
146
147 #endif