1 #ifndef MSP_GL_RENDERER_H_
2 #define MSP_GL_RENDERER_H_
7 #include "framebuffer.h"
9 #include "pipelinestate.h"
10 #include "programdata.h"
30 A class for supervising the rendering process. While many Renderables (in
31 particular, Objects and Scenes) can by rendered without a Renderer, using one
32 will often be more efficient. This is especially true for ObjectInstances.
34 The Renderer works by deferring GL state changes until something is actually
35 being drawn. This avoids many unnecessary GL calls if consecutive renderables
36 use the same resources.
38 A state stack is provided to help with state scoping. Typically a Renderable
39 will push the current state on entry, set whatever state it requires, render
40 itself, and pop the state when it's done. An RAII helper class is provided for
41 the push/pop operation.
52 Push(Renderer &r): renderer(r) { renderer.push_state(); }
53 ~Push() { renderer.pop_state(); }
60 const Renderable &renderable;
63 Exclude(Renderer &r, const Renderable &e): renderer(r), renderable(e) { renderer.exclude(renderable); }
64 ~Exclude() { renderer.include(renderable); }
72 const Texture *texture;
73 const Sampler *sampler;
79 struct BoundProgramData
81 const ProgramData *shdata;
82 mutable unsigned generation;
84 BoundProgramData(const ProgramData *);
91 const Framebuffer *framebuffer;
94 unsigned texture_count;
95 const Clipping *clipping;
96 const Program *shprog;
97 unsigned shdata_count;
98 const VertexSetup *vertex_setup;
99 FaceWinding front_face;
101 const DepthTest *depth_test;
102 const StencilTest *stencil_test;
104 unsigned object_lod_bias;
115 unsigned char changed;
116 std::vector<State> state_stack;
118 std::vector<BoundTexture> texture_stack;
119 ProgramData standard_shdata;
120 std::vector<BoundProgramData> shdata_stack;
121 std::set<const Renderable *> excluded;
122 PipelineState pipeline_state;
129 /** Sets the camera to render from. The model matrix is reset to identity. */
130 void set_camera(const Camera &);
132 const Camera *get_camera() const { return state->camera; }
134 /** Replaces the Renderer's model matrix. */
135 void set_matrix(const Matrix &);
137 /** Applies a transform to the Renderer's model matrix. */
138 void transform(const Matrix &);
140 /** Returns the current model matrix. */
141 const Matrix &get_matrix() const { return state->model_matrix; }
143 void set_framebuffer(const Framebuffer *);
144 void set_viewport(const Rect *);
145 void set_scissor(const Rect *);
147 const Framebuffer *get_framebuffer() const { return state->framebuffer; }
149 void set_texture(Tag, const Texture *, const Sampler * = 0);
151 void flush_textures();
153 DEPRECATED void set_material(const Material *);
155 DEPRECATED void set_lighting(const Lighting *);
156 void set_clipping(const Clipping *);
158 /** Sets the shader program to use. An initial set of data can be set as
159 well, with the same semantics as add_shader_data. */
160 void set_shader_program(const Program *prog, const ProgramData *data = 0);
162 /** Adds another set of data to be use with shader programs. The data is
163 independent of any shader program changes and remains in effect until the
164 Renderer state is popped. */
165 void add_shader_data(const ProgramData &data);
167 DEPRECATED void flush_shader_data() { flush_shader_data_(); }
169 void flush_shader_data_();
172 void set_vertex_setup(const VertexSetup *);
173 void set_front_face(FaceWinding);
174 void set_face_cull(CullMode);
176 void set_depth_test(const DepthTest *);
177 void set_stencil_test(const StencilTest *);
178 void set_blend(const Blend *);
180 void set_object_lod_bias(unsigned);
181 unsigned get_object_lod_bias() const { return state->object_lod_bias; }
183 /** Saves the current state so it can be restored later. */
186 /** Restores a previously saved state. Must be matched with an earlier
190 /** Unbinds all objects and resets related state. There must be no unpopped
191 state in the stack. The Renderer remains valid and may be reused for
192 further rendering. */
195 void exclude(const Renderable &);
196 void include(const Renderable &);
199 void clear(BufferBits);
201 void render(const Renderable &, Tag = Tag());
202 void draw(const Batch &);
203 void draw_instanced(const Batch &, unsigned);
205 void resolve_multisample(Framebuffer &, BufferBits);