]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.h
Add a new transform API to Renderer.
[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 Material;
17 class Mesh;
18 class Lighting;
19 class Program;
20 class Renderable;
21 class Texture;
22 class Texturing;
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                 unsigned lowest_effect_texunit;
69                 const Material *material;
70                 const Lighting *lighting;
71                 Matrix lighting_matrix;
72                 const Program *shprog;
73                 unsigned shdata_count;
74                 const Mesh *mesh;
75                 const WindingTest *winding_test;
76                 bool reverse_winding;
77
78                 State();
79         };
80
81         class MtxStack: public MatrixStack
82         {
83         private:
84                 Renderer &renderer;
85
86         public:
87                 MtxStack(Renderer &);
88         private:
89                 virtual void update();
90         };
91
92         enum ChangeMask
93         {
94                 LEGACY_MATRIX = 1,
95                 MODERN_MATRIX = 2,
96                 MATRIX = LEGACY_MATRIX|MODERN_MATRIX,
97                 LIGHTING = 4,
98                 SHADER_DATA = 8
99         };
100
101         MtxStack mtx_stack;
102         unsigned char changed;
103         bool matrices_loaded;
104         unsigned shdata_applied;
105         const Camera *camera;
106         std::vector<State> state_stack;
107         State *state;
108         ProgramData standard_shdata;
109         std::vector<const ProgramData *> shdata_stack;
110         std::set<const Renderable *> excluded;
111
112 public:
113         Renderer(const Camera *);
114         ~Renderer();
115
116         /** Resets all internal state and restarts rendering.  There must be no
117         unpopped state in the stack.  It is permissible to call begin() multiple
118         times without an intervening end(). */
119         void begin(const Camera *);
120
121         /** Deprecated as unsafe.  Use set_matrix() or transform() instead. */
122         MatrixStack &matrix_stack() { return mtx_stack; }
123
124         /** Replaces the Renderer's modelview matrix. */
125         void set_matrix(const Matrix &);
126
127         /** Applies a transform to the Renderer's modelview matrix. */
128         void transform(const Matrix &);
129
130         /** Returns the current modelview matrix. */
131         const Matrix &get_matrix() const { return mtx_stack.top(); }
132
133         const Camera *get_camera() const { return camera; }
134
135         void set_texture(const Texture *);
136         void set_texturing(const Texturing *);
137         unsigned allocate_effect_texunit();
138         void set_material(const Material *);
139
140         void set_lighting(const Lighting *);
141
142         /** Sets the shader program to use.  An initial set of data can be set as
143         well, with the same semantics as add_shader_data. */
144         void set_shader_program(const Program *prog, const ProgramData *data = 0);
145
146         /** Adds another set of data to be use with shader programs.  The data is
147         independent of any shader program changes and remains in effect until the
148         Renderer state is popped. */
149         void add_shader_data(const ProgramData &data);
150
151         void set_mesh(const Mesh *);
152         void set_winding_test(const WindingTest *);
153         void set_reverse_winding(bool);
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         /** Prepares for temporarily bypassing the Renderer by synchronizing the
163         current state with GL.  No additional call is necessary to resume using the
164         Renderer.  DEPRECATED. */
165         void escape();
166
167         /** Unbinds all objects and resets related state.  There must be no unpopped
168         state in the stack.  Rendering with the same camera can be restarted without
169         an explicit begin() call. */
170         void end();
171
172         void exclude(const Renderable &);
173         void include(const Renderable &);
174
175         void render(const Renderable &, const Tag & = Tag());
176         void draw(const Batch &);
177
178 private:
179         void apply_state();
180         void reset_state();
181 };
182
183 } // namespace GL
184 } // namespace Msp
185
186 #endif