]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.h
Avoid some brain damage from Windows headers
[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 WindingTest;
25
26 /**
27 A class for supervising the rendering process.  While many Renderables (in
28 particular, Objects and Scenes) can by rendered without a Renderer, using one
29 will often be more efficient.  This is especially true for ObjectInstances.
30
31 The Renderer works by deferring GL state changes until something is actually
32 being drawn.  This avoids many unnecessary GL calls if consecutive renderables
33 use the same resources.
34
35 A state stack is provided to help with state scoping.  Typically a Renderable
36 will push the current state on entry, set whatever state it requires, render
37 itself, and pop the state when it's done.  An RAII helper class is provided for
38 the push/pop operation.
39 */
40 class Renderer
41 {
42 public:
43         class Push
44         {
45         private:
46                 Renderer &renderer;
47
48         public:
49                 Push(Renderer &r): renderer(r) { renderer.push_state(); }
50                 ~Push() { renderer.pop_state(); }
51         };
52
53         class Exclude
54         {
55         private:
56                 Renderer &renderer;
57                 const Renderable &renderable;
58
59         public:
60                 Exclude(Renderer &r, const Renderable &e): renderer(r), renderable(e) { renderer.exclude(renderable); }
61                 ~Exclude() { renderer.include(renderable); }
62         };
63
64 private:
65         struct State
66         {
67                 const Camera *camera;
68                 Matrix modelview_matrix;
69                 const Texture *texture;
70                 const Texturing *texturing;
71                 unsigned lowest_effect_texunit;
72                 const Material *material;
73                 const Lighting *lighting;
74                 Matrix lighting_matrix;
75                 const Clipping *clipping;
76                 Matrix clipping_matrix;
77                 const Program *shprog;
78                 unsigned shdata_count;
79                 const Mesh *mesh;
80                 const WindingTest *winding_test;
81                 bool reverse_winding;
82
83                 State();
84         };
85
86         enum ChangeMask
87         {
88                 LEGACY_MATRIX = 1,
89                 MODERN_MATRIX = 2,
90                 MATRIX = LEGACY_MATRIX|MODERN_MATRIX,
91                 LEGACY_LIGHTING = 4,
92                 LEGACY_CLIPPING = 8,
93                 SHADER_DATA = 16,
94                 MATERIAL_SHDATA = 32,
95                 STANDARD_SHDATA = 64,
96                 LEGACY_PROJECTION = 128
97         };
98
99         const Camera *default_camera;
100         unsigned char changed;
101         bool matrices_loaded;
102         std::vector<State> state_stack;
103         State *state;
104         ProgramData standard_shdata;
105         std::vector<const ProgramData *> shdata_stack;
106         std::set<const Renderable *> excluded;
107
108 public:
109         Renderer(const Camera *);
110         ~Renderer();
111
112         /** Resets all internal state and restarts rendering.  There must be no
113         unpopped state in the stack.  It is permissible to call begin() multiple
114         times without an intervening end().
115
116         Deprecated; use end() and set_camera() instead.*/
117         void begin(const Camera *);
118
119         /** Sets the camera to render from.  The modelview matrix is reset to the
120         camera's view matrix. */
121         void set_camera(const Camera &);
122
123         const Camera *get_camera() const { return state->camera; }
124
125         /** Replaces the Renderer's modelview matrix. */
126         void set_matrix(const Matrix &);
127
128         /** Applies a transform to the Renderer's modelview matrix. */
129         void transform(const Matrix &);
130
131         /** Returns the current modelview matrix. */
132         const Matrix &get_matrix() const { return state->modelview_matrix; }
133
134         void set_texture(const Texture *);
135         void set_texturing(const Texturing *);
136         unsigned allocate_effect_texunit();
137         void set_material(const Material *);
138
139         void set_lighting(const Lighting *);
140         void set_clipping(const Clipping *);
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         /** 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
173 private:
174         void apply_state();
175 };
176
177 } // namespace GL
178 } // namespace Msp
179
180 #endif