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); }
67 const Texture *texture;
68 const Texturing *texturing;
69 unsigned lowest_effect_texunit;
70 const Material *material;
71 const Lighting *lighting;
72 Matrix lighting_matrix;
73 const Clipping *clipping;
74 Matrix clipping_matrix;
75 const Program *shprog;
76 unsigned shdata_count;
78 const WindingTest *winding_test;
84 class MtxStack: public MatrixStack
92 virtual void update();
99 MATRIX = LEGACY_MATRIX|MODERN_MATRIX,
106 unsigned char changed;
107 bool matrices_loaded;
108 unsigned shdata_applied;
109 const Camera *camera;
110 std::vector<State> state_stack;
112 ProgramData standard_shdata;
113 std::vector<const ProgramData *> shdata_stack;
114 std::set<const Renderable *> excluded;
117 Renderer(const Camera *);
120 /** Resets all internal state and restarts rendering. There must be no
121 unpopped state in the stack. It is permissible to call begin() multiple
122 times without an intervening end(). */
123 void begin(const Camera *);
125 /** Deprecated as unsafe. Use set_matrix() or transform() instead. */
126 MatrixStack &matrix_stack() { return mtx_stack; }
128 /** Replaces the Renderer's modelview matrix. */
129 void set_matrix(const Matrix &);
131 /** Applies a transform to the Renderer's modelview matrix. */
132 void transform(const Matrix &);
134 /** Returns the current modelview matrix. */
135 const Matrix &get_matrix() const { return mtx_stack.top(); }
137 const Camera *get_camera() const { return camera; }
139 void set_texture(const Texture *);
140 void set_texturing(const Texturing *);
141 unsigned allocate_effect_texunit();
142 void set_material(const Material *);
144 void set_lighting(const Lighting *);
145 void set_clipping(const Clipping *);
147 /** Sets the shader program to use. An initial set of data can be set as
148 well, with the same semantics as add_shader_data. */
149 void set_shader_program(const Program *prog, const ProgramData *data = 0);
151 /** Adds another set of data to be use with shader programs. The data is
152 independent of any shader program changes and remains in effect until the
153 Renderer state is popped. */
154 void add_shader_data(const ProgramData &data);
156 void set_mesh(const Mesh *);
157 void set_winding_test(const WindingTest *);
158 void set_reverse_winding(bool);
160 /** Saves the current state so it can be restored later. */
163 /** Restores a previously saved state. Must be matched with an earlier
167 /** Prepares for temporarily bypassing the Renderer by synchronizing the
168 current state with GL. No additional call is necessary to resume using the
169 Renderer. DEPRECATED. */
172 /** Unbinds all objects and resets related state. There must be no unpopped
173 state in the stack. Rendering with the same camera can be restarted without
174 an explicit begin() call. */
177 void exclude(const Renderable &);
178 void include(const Renderable &);
180 void render(const Renderable &, const Tag & = Tag());
181 void draw(const Batch &);