]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.h
Remove vertex array tracking from Renderer
[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 Lighting;
17 class Program;
18 class ProgramData;
19 class Renderable;
20 class Texture;
21 class Texturing;
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 Lighting *lighting;
69                 Matrix lighting_matrix;
70                 const Program *shprog;
71                 unsigned shdata_count;
72                 const WindingTest *winding_test;
73                 bool reverse_winding;
74
75                 State();
76         };
77
78         class MtxStack: public MatrixStack
79         {
80         private:
81                 Renderer &renderer;
82
83         public:
84                 MtxStack(Renderer &);
85         private:
86                 virtual void update();
87         };
88
89         MtxStack mtx_stack;
90         bool mtx_changed;
91         const Camera *camera;
92         std::vector<State> state_stack;
93         State *state;
94         bool lighting_changed;
95         std::vector<const ProgramData *> shdata_stack;
96         bool shdata_changed;
97         const Buffer *element_buffer;
98         std::set<const Renderable *> excluded;
99
100 public:
101         Renderer(const Camera *);
102         ~Renderer();
103
104         MatrixStack &matrix_stack() { return mtx_stack; }
105
106         const Camera *get_camera() const { return camera; }
107
108         void set_texture(const Texture *);
109         void set_texturing(const Texturing *);
110         void set_material(const Material *);
111
112         void set_lighting(const Lighting *);
113
114         /** Sets the shader program to use.  An initial set of data can be set as
115         well, with the same semantics as add_shader_data. */
116         void set_shader_program(const Program *prog, const ProgramData *data = 0);
117
118         /** Adds another set of data to be use with shader programs.  The data is
119         independent of any shader program changes and remains in effect until the
120         Renderer state is popped. */
121         void add_shader_data(const ProgramData &data);
122
123         void set_element_buffer(const Buffer *);
124         void set_winding_test(const WindingTest *);
125         void set_reverse_winding(bool);
126
127         /** Saves the current state so it can be restored later. */
128         void push_state();
129
130         /** Restores a previously saved state.  Must be matched with an earlier
131         push_state call. */
132         void pop_state();
133
134         /** Prepares for temporarily bypassing the Renderer by synchronizing the
135         current state with GL.  No additional call is necessary to resume using the
136         Renderer. */
137         void escape();
138
139         void exclude(const Renderable &);
140         void include(const Renderable &);
141
142         void render(const Renderable &, const Tag & = Tag());
143         void draw(const Batch &);
144
145 private:
146         void apply_state();
147 };
148
149 } // namespace GL
150 } // namespace Msp
151
152 #endif