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