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