]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.h
Remove the deprecated ProgramBuilder class
[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 Clipping;
17 class Material;
18 class Mesh;
19 class Lighting;
20 class Program;
21 class Renderable;
22 class Sampler;
23 class Texture;
24 class Texturing;
25 class VertexSetup;
26 class WindingTest;
27
28 /**
29 A class for supervising the rendering process.  While many Renderables (in
30 particular, Objects and Scenes) can by rendered without a Renderer, using one
31 will often be more efficient.  This is especially true for ObjectInstances.
32
33 The Renderer works by deferring GL state changes until something is actually
34 being drawn.  This avoids many unnecessary GL calls if consecutive renderables
35 use the same resources.
36
37 A state stack is provided to help with state scoping.  Typically a Renderable
38 will push the current state on entry, set whatever state it requires, render
39 itself, and pop the state when it's done.  An RAII helper class is provided for
40 the push/pop operation.
41 */
42 class Renderer
43 {
44 public:
45         class Push
46         {
47         private:
48                 Renderer &renderer;
49
50         public:
51                 Push(Renderer &r): renderer(r) { renderer.push_state(); }
52                 ~Push() { renderer.pop_state(); }
53         };
54
55         class Exclude
56         {
57         private:
58                 Renderer &renderer;
59                 const Renderable &renderable;
60
61         public:
62                 Exclude(Renderer &r, const Renderable &e): renderer(r), renderable(e) { renderer.exclude(renderable); }
63                 ~Exclude() { renderer.include(renderable); }
64         };
65
66 private:
67         struct State
68         {
69                 const Camera *camera;
70                 Matrix modelview_matrix;
71                 const Texture *texture;
72                 const Sampler *sampler;
73                 const Texturing *texturing;
74                 unsigned lowest_effect_texunit;
75                 const Material *material;
76                 const Lighting *lighting;
77                 Matrix lighting_matrix;
78                 const Clipping *clipping;
79                 Matrix clipping_matrix;
80                 const Program *shprog;
81                 unsigned shdata_count;
82                 const VertexSetup *vertex_setup;
83                 const WindingTest *winding_test;
84                 bool reverse_winding;
85                 unsigned object_lod_bias;
86
87                 State();
88         };
89
90         enum ChangeMask
91         {
92                 MATRIX = 2,
93                 SHADER_DATA = 16,
94                 MATERIAL_SHDATA = 32,
95                 STANDARD_SHDATA = 64
96         };
97
98         const Camera *default_camera;
99         unsigned char changed;
100         std::vector<State> state_stack;
101         State *state;
102         ProgramData standard_shdata;
103         std::vector<const ProgramData *> shdata_stack;
104         std::set<const Renderable *> excluded;
105
106 public:
107         Renderer();
108         DEPRECATED Renderer(const Camera *);
109 private:
110         void init();
111 public:
112         ~Renderer();
113
114         /** Sets the camera to render from.  The modelview matrix is reset to the
115         camera's view matrix. */
116         void set_camera(const Camera &);
117
118         const Camera *get_camera() const { return state->camera; }
119
120         /** Replaces the Renderer's modelview matrix. */
121         void set_matrix(const Matrix &);
122
123         /** Applies a transform to the Renderer's modelview matrix. */
124         void transform(const Matrix &);
125
126         /** Returns the current modelview matrix. */
127         const Matrix &get_matrix() const { return state->modelview_matrix; }
128
129         void set_texture(const Texture *, const Sampler * = 0);
130         void set_texturing(const Texturing *);
131         unsigned allocate_effect_texunit();
132         void set_material(const Material *);
133
134         void set_lighting(const Lighting *);
135         void set_clipping(const Clipping *);
136
137         /** Sets the shader program to use.  An initial set of data can be set as
138         well, with the same semantics as add_shader_data. */
139         void set_shader_program(const Program *prog, const ProgramData *data = 0);
140
141         /** Adds another set of data to be use with shader programs.  The data is
142         independent of any shader program changes and remains in effect until the
143         Renderer state is popped. */
144         void add_shader_data(const ProgramData &data);
145
146         void flush_shader_data();
147
148         void set_vertex_setup(const VertexSetup *);
149         void set_winding_test(const WindingTest *);
150         void set_reverse_winding(bool);
151
152         void set_object_lod_bias(unsigned);
153         unsigned get_object_lod_bias() const { return state->object_lod_bias; }
154
155         /** Saves the current state so it can be restored later. */
156         void push_state();
157
158         /** Restores a previously saved state.  Must be matched with an earlier
159         push_state call. */
160         void pop_state();
161
162         /** Unbinds all objects and resets related state.  There must be no unpopped
163         state in the stack.  The Renderer remains valid and may be reused for
164         further rendering. */
165         void end();
166
167         void exclude(const Renderable &);
168         void include(const Renderable &);
169
170         void render(const Renderable &, const Tag & = Tag());
171         void draw(const Batch &);
172         void draw_instanced(const Batch &, unsigned);
173
174 private:
175         void apply_state();
176 };
177
178 } // namespace GL
179 } // namespace Msp
180
181 #endif