]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.h
Remove bind mechanics from Mesh and VertexArray
[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 Texture;
23 class Texturing;
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 State
67         {
68                 const Camera *camera;
69                 Matrix modelview_matrix;
70                 const Texture *texture;
71                 const Texturing *texturing;
72                 unsigned lowest_effect_texunit;
73                 const Material *material;
74                 const Lighting *lighting;
75                 Matrix lighting_matrix;
76                 const Clipping *clipping;
77                 Matrix clipping_matrix;
78                 const Program *shprog;
79                 unsigned shdata_count;
80                 const VertexSetup *vertex_setup;
81                 const WindingTest *winding_test;
82                 bool reverse_winding;
83                 unsigned object_lod_bias;
84
85                 State();
86         };
87
88         enum ChangeMask
89         {
90                 MATRIX = 2,
91                 SHADER_DATA = 16,
92                 MATERIAL_SHDATA = 32,
93                 STANDARD_SHDATA = 64
94         };
95
96         const Camera *default_camera;
97         unsigned char changed;
98         std::vector<State> state_stack;
99         State *state;
100         ProgramData standard_shdata;
101         std::vector<const ProgramData *> shdata_stack;
102         std::set<const Renderable *> excluded;
103
104 public:
105         Renderer(const Camera *);
106         ~Renderer();
107
108         /** Resets all internal state and restarts rendering.  There must be no
109         unpopped state in the stack.  It is permissible to call begin() multiple
110         times without an intervening end().
111
112         Deprecated; use end() and set_camera() instead.*/
113         void begin(const Camera *);
114
115         /** Sets the camera to render from.  The modelview matrix is reset to the
116         camera's view matrix. */
117         void set_camera(const Camera &);
118
119         const Camera *get_camera() const { return state->camera; }
120
121         /** Replaces the Renderer's modelview matrix. */
122         void set_matrix(const Matrix &);
123
124         /** Applies a transform to the Renderer's modelview matrix. */
125         void transform(const Matrix &);
126
127         /** Returns the current modelview matrix. */
128         const Matrix &get_matrix() const { return state->modelview_matrix; }
129
130         void set_texture(const Texture *);
131         void set_texturing(const Texturing *);
132         unsigned allocate_effect_texunit();
133         void set_material(const Material *);
134
135         void set_lighting(const Lighting *);
136         void set_clipping(const Clipping *);
137
138         /** Sets the shader program to use.  An initial set of data can be set as
139         well, with the same semantics as add_shader_data. */
140         void set_shader_program(const Program *prog, const ProgramData *data = 0);
141
142         /** Adds another set of data to be use with shader programs.  The data is
143         independent of any shader program changes and remains in effect until the
144         Renderer state is popped. */
145         void add_shader_data(const ProgramData &data);
146
147         void flush_shader_data();
148
149         void set_vertex_setup(const VertexSetup *);
150         void set_winding_test(const WindingTest *);
151         void set_reverse_winding(bool);
152
153         void set_object_lod_bias(unsigned);
154         unsigned get_object_lod_bias() const { return state->object_lod_bias; }
155
156         /** Saves the current state so it can be restored later. */
157         void push_state();
158
159         /** Restores a previously saved state.  Must be matched with an earlier
160         push_state call. */
161         void pop_state();
162
163         /** Unbinds all objects and resets related state.  There must be no unpopped
164         state in the stack.  The Renderer remains valid and may be reused for
165         further rendering. */
166         void end();
167
168         void exclude(const Renderable &);
169         void include(const Renderable &);
170
171         void render(const Renderable &, const Tag & = Tag());
172         void draw(const Batch &);
173         void draw_instanced(const Batch &, unsigned);
174
175 private:
176         void apply_state();
177 };
178
179 } // namespace GL
180 } // namespace Msp
181
182 #endif