1 #ifndef MSP_GL_RENDERER_H_
2 #define MSP_GL_RENDERER_H_
7 #include "programdata.h"
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.
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.
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.
51 Push(Renderer &r): renderer(r) { renderer.push_state(); }
52 ~Push() { renderer.pop_state(); }
59 const Renderable &renderable;
62 Exclude(Renderer &r, const Renderable &e): renderer(r), renderable(e) { renderer.exclude(renderable); }
63 ~Exclude() { renderer.include(renderable); }
71 const Texture *texture;
72 const Sampler *sampler;
78 struct BoundProgramData
80 const ProgramData *shdata;
81 mutable unsigned generation;
83 BoundProgramData(const ProgramData *);
90 unsigned texture_count;
91 unsigned lowest_effect_texunit;
92 const Material *material;
93 const Lighting *lighting;
94 const Clipping *clipping;
95 const Program *shprog;
96 unsigned shdata_count;
97 const VertexSetup *vertex_setup;
98 const WindingTest *winding_test;
100 unsigned object_lod_bias;
109 MATERIAL_SHDATA = 32,
113 const Camera *default_camera;
114 unsigned char changed;
115 std::vector<State> state_stack;
117 std::vector<BoundTexture> texture_stack;
118 ProgramData standard_shdata;
119 std::vector<BoundProgramData> shdata_stack;
120 std::set<const Renderable *> excluded;
124 DEPRECATED Renderer(const Camera *);
130 /** Sets the camera to render from. The model matrix is reset to identity. */
131 void set_camera(const Camera &);
133 const Camera *get_camera() const { return state->camera; }
135 /** Replaces the Renderer's model matrix. */
136 void set_matrix(const Matrix &);
138 /** Applies a transform to the Renderer's model matrix. */
139 void transform(const Matrix &);
141 /** Returns the current model matrix. */
142 const Matrix &get_matrix() const { return state->model_matrix; }
144 void set_texture(Tag, const Texture *, const Sampler * = 0);
146 void set_texture(Tag, int, const Texture *, const Sampler *);
147 void flush_textures();
149 #pragma GCC diagnostic push
150 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
151 DEPRECATED void set_texture(const Texture *, const Sampler * = 0);
152 DEPRECATED void set_texturing(const Texturing *);
153 DEPRECATED unsigned allocate_effect_texunit();
154 #pragma GCC diagnostic pop
155 void set_material(const Material *);
157 void set_lighting(const Lighting *);
158 void set_clipping(const Clipping *);
160 /** Sets the shader program to use. An initial set of data can be set as
161 well, with the same semantics as add_shader_data. */
162 void set_shader_program(const Program *prog, const ProgramData *data = 0);
164 /** Adds another set of data to be use with shader programs. The data is
165 independent of any shader program changes and remains in effect until the
166 Renderer state is popped. */
167 void add_shader_data(const ProgramData &data);
169 void flush_shader_data();
171 void set_vertex_setup(const VertexSetup *);
172 void set_winding_test(const WindingTest *);
173 void set_reverse_winding(bool);
175 void set_object_lod_bias(unsigned);
176 unsigned get_object_lod_bias() const { return state->object_lod_bias; }
178 /** Saves the current state so it can be restored later. */
181 /** Restores a previously saved state. Must be matched with an earlier
185 /** Unbinds all objects and resets related state. There must be no unpopped
186 state in the stack. The Renderer remains valid and may be reused for
187 further rendering. */
190 void exclude(const Renderable &);
191 void include(const Renderable &);
193 void render(const Renderable &, Tag = Tag());
194 void draw(const Batch &);
195 void draw_instanced(const Batch &, unsigned);