1 #ifndef MSP_GL_RENDERER_H_
2 #define MSP_GL_RENDERER_H_
7 #include "programdata.h"
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.
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.
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.
49 Push(Renderer &r): renderer(r) { renderer.push_state(); }
50 ~Push() { renderer.pop_state(); }
57 const Renderable &renderable;
60 Exclude(Renderer &r, const Renderable &e): renderer(r), renderable(e) { renderer.exclude(renderable); }
61 ~Exclude() { renderer.include(renderable); }
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;
80 const WindingTest *winding_test;
90 MATRIX = LEGACY_MATRIX|MODERN_MATRIX,
96 LEGACY_PROJECTION = 128
99 unsigned char changed;
100 bool matrices_loaded;
101 std::vector<State> state_stack;
103 ProgramData standard_shdata;
104 std::vector<const ProgramData *> shdata_stack;
105 std::set<const Renderable *> excluded;
108 Renderer(const Camera *);
111 /** Resets all internal state and restarts rendering. There must be no
112 unpopped state in the stack. It is permissible to call begin() multiple
113 times without an intervening end().
115 Deprecated; use end() and set_camera() instead.*/
116 void begin(const Camera *);
118 /** Sets the camera to render from. The modelview matrix is reset to the
119 camera's view matrix. */
120 void set_camera(const Camera &);
122 const Camera *get_camera() const { return state->camera; }
124 /** Replaces the Renderer's modelview matrix. */
125 void set_matrix(const Matrix &);
127 /** Applies a transform to the Renderer's modelview matrix. */
128 void transform(const Matrix &);
130 /** Returns the current modelview matrix. */
131 const Matrix &get_matrix() const { return state->modelview_matrix; }
133 void set_texture(const Texture *);
134 void set_texturing(const Texturing *);
135 unsigned allocate_effect_texunit();
136 void set_material(const Material *);
138 void set_lighting(const Lighting *);
139 void set_clipping(const Clipping *);
141 /** Sets the shader program to use. An initial set of data can be set as
142 well, with the same semantics as add_shader_data. */
143 void set_shader_program(const Program *prog, const ProgramData *data = 0);
145 /** Adds another set of data to be use with shader programs. The data is
146 independent of any shader program changes and remains in effect until the
147 Renderer state is popped. */
148 void add_shader_data(const ProgramData &data);
150 void set_mesh(const Mesh *);
151 void set_winding_test(const WindingTest *);
152 void set_reverse_winding(bool);
154 /** Saves the current state so it can be restored later. */
157 /** Restores a previously saved state. Must be matched with an earlier
161 /** Unbinds all objects and resets related state. There must be no unpopped
162 state in the stack. The Renderer remains valid and may be reused for
163 further rendering. */
166 void exclude(const Renderable &);
167 void include(const Renderable &);
169 void render(const Renderable &, const Tag & = Tag());
170 void draw(const Batch &);