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