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