1 #ifndef MSP_GL_RENDERER_H_
2 #define MSP_GL_RENDERER_H_
7 #include "programdata.h"
26 A class for supervising the rendering process. While many Renderables (in
27 particular, Objects and Scenes) can by rendered without a Renderer, using one
28 will often be more efficient. This is especially true for ObjectInstances.
30 The Renderer works by deferring GL state changes until something is actually
31 being drawn. This avoids many unnecessary GL calls if consecutive renderables
32 use the same resources.
34 A state stack is provided to help with state scoping. Typically a Renderable
35 will push the current state on entry, set whatever state it requires, render
36 itself, and pop the state when it's done. An RAII helper class is provided for
37 the push/pop operation.
48 Push(Renderer &r): renderer(r) { renderer.push_state(); }
49 ~Push() { renderer.pop_state(); }
56 const Renderable &renderable;
59 Exclude(Renderer &r, const Renderable &e): renderer(r), renderable(e) { renderer.exclude(renderable); }
60 ~Exclude() { renderer.include(renderable); }
66 const Texture *texture;
67 const Texturing *texturing;
68 unsigned lowest_effect_texunit;
69 const Material *material;
70 const Lighting *lighting;
71 Matrix lighting_matrix;
72 const Program *shprog;
73 unsigned shdata_count;
75 const WindingTest *winding_test;
81 class MtxStack: public MatrixStack
89 virtual void update();
96 MATRIX = LEGACY_MATRIX|MODERN_MATRIX,
102 unsigned char changed;
103 bool matrices_loaded;
104 unsigned shdata_applied;
105 const Camera *camera;
106 std::vector<State> state_stack;
108 ProgramData standard_shdata;
109 std::vector<const ProgramData *> shdata_stack;
110 const Buffer *element_buffer;
111 std::set<const Renderable *> excluded;
114 Renderer(const Camera *);
117 /** Resets all internal state and restarts rendering. There must be no
118 unpopped state in the stack. It is permissible to call begin() multiple
119 times without an intervening end(). */
120 void begin(const Camera *);
122 MatrixStack &matrix_stack() { return mtx_stack; }
124 const Camera *get_camera() const { return camera; }
126 void set_texture(const Texture *);
127 void set_texturing(const Texturing *);
128 unsigned allocate_effect_texunit();
129 void set_material(const Material *);
131 void set_lighting(const Lighting *);
133 /** Sets the shader program to use. An initial set of data can be set as
134 well, with the same semantics as add_shader_data. */
135 void set_shader_program(const Program *prog, const ProgramData *data = 0);
137 /** Adds another set of data to be use with shader programs. The data is
138 independent of any shader program changes and remains in effect until the
139 Renderer state is popped. */
140 void add_shader_data(const ProgramData &data);
142 void set_mesh(const Mesh *);
143 void set_element_buffer(const Buffer *);
144 void set_winding_test(const WindingTest *);
145 void set_reverse_winding(bool);
147 /** Saves the current state so it can be restored later. */
150 /** Restores a previously saved state. Must be matched with an earlier
154 /** Prepares for temporarily bypassing the Renderer by synchronizing the
155 current state with GL. No additional call is necessary to resume using the
156 Renderer. DEPRECATED. */
159 /** Unbinds all objects and resets related state. There must be no unpopped
160 state in the stack. Rendering with the same camera can be restarted without
161 an explicit begin() call. */
164 void exclude(const Renderable &);
165 void include(const Renderable &);
167 void render(const Renderable &, const Tag & = Tag());
168 void draw(const Batch &);