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;
81 Matrix modelview_matrix;
82 unsigned texture_count;
83 unsigned lowest_effect_texunit;
84 const Material *material;
85 const Lighting *lighting;
86 Matrix lighting_matrix;
87 const Clipping *clipping;
88 Matrix clipping_matrix;
89 const Program *shprog;
90 unsigned shdata_count;
91 const VertexSetup *vertex_setup;
92 const WindingTest *winding_test;
94 unsigned object_lod_bias;
103 MATERIAL_SHDATA = 32,
107 const Camera *default_camera;
108 unsigned char changed;
109 std::vector<State> state_stack;
111 std::vector<BoundTexture> texture_stack;
112 ProgramData standard_shdata;
113 std::vector<const ProgramData *> shdata_stack;
114 std::set<const Renderable *> excluded;
118 DEPRECATED Renderer(const Camera *);
124 /** Sets the camera to render from. The modelview matrix is reset to the
125 camera's view matrix. */
126 void set_camera(const Camera &);
128 const Camera *get_camera() const { return state->camera; }
130 /** Replaces the Renderer's modelview matrix. */
131 void set_matrix(const Matrix &);
133 /** Applies a transform to the Renderer's modelview matrix. */
134 void transform(const Matrix &);
136 /** Returns the current modelview matrix. */
137 const Matrix &get_matrix() const { return state->modelview_matrix; }
139 void set_texture(Tag, const Texture *, const Sampler * = 0);
141 void set_texture(Tag, int, const Texture *, const Sampler *);
142 void flush_textures();
144 #pragma GCC diagnostic push
145 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
146 DEPRECATED void set_texture(const Texture *, const Sampler * = 0);
147 DEPRECATED void set_texturing(const Texturing *);
148 DEPRECATED unsigned allocate_effect_texunit();
149 #pragma GCC diagnostic pop
150 void set_material(const Material *);
152 void set_lighting(const Lighting *);
153 void set_clipping(const Clipping *);
155 /** Sets the shader program to use. An initial set of data can be set as
156 well, with the same semantics as add_shader_data. */
157 void set_shader_program(const Program *prog, const ProgramData *data = 0);
159 /** Adds another set of data to be use with shader programs. The data is
160 independent of any shader program changes and remains in effect until the
161 Renderer state is popped. */
162 void add_shader_data(const ProgramData &data);
164 void flush_shader_data();
166 void set_vertex_setup(const VertexSetup *);
167 void set_winding_test(const WindingTest *);
168 void set_reverse_winding(bool);
170 void set_object_lod_bias(unsigned);
171 unsigned get_object_lod_bias() const { return state->object_lod_bias; }
173 /** Saves the current state so it can be restored later. */
176 /** Restores a previously saved state. Must be matched with an earlier
180 /** Unbinds all objects and resets related state. There must be no unpopped
181 state in the stack. The Renderer remains valid and may be reused for
182 further rendering. */
185 void exclude(const Renderable &);
186 void include(const Renderable &);
188 void render(const Renderable &, Tag = Tag());
189 void draw(const Batch &);
190 void draw_instanced(const Batch &, unsigned);