]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.h
Tweak the shader functions in Renderer
[libs/gl.git] / source / renderer.h
1 #ifndef MSP_GL_RENDERER_H_
2 #define MSP_GL_RENDERER_H_
3
4 #include <vector>
5 #include "matrix.h"
6
7 namespace Msp {
8 namespace GL {
9
10 class Batch;
11 class Buffer;
12 class Camera;
13 class Material;
14 class Program;
15 class ProgramData;
16 class Texture;
17 class Texturing;
18 class VertexArray;
19 class WindingTest;
20
21 /**
22 A class for supervising the rendering process.  While many Renderables (in
23 particular, Objects and Scenes) can by rendered without a Renderer, using one
24 will often be more efficient.  This is especially true for ObjectInstances.
25
26 The Renderer works by deferring GL state changes until something is actually
27 being drawn.  This avoids many unnecessary GL calls if consecutive renderables
28 use the same resources.
29 */
30 class Renderer
31 {
32 public:
33         class Push
34         {
35         private:
36                 Renderer &renderer;
37
38         public:
39                 Push(Renderer &r): renderer(r) { renderer.push_state(); }
40                 ~Push() { renderer.pop_state(); }
41         };
42
43 private:
44         struct State
45         {
46                 const Texture *texture;
47                 const Texturing *texturing;
48                 const Material *material;
49                 const Program *shprog;
50                 std::vector<const ProgramData *> shdata;
51                 const WindingTest *winding_test;
52
53                 State();
54         };
55
56         class MtxStack: public MatrixStack
57         {
58         private:
59                 Renderer &renderer;
60
61         public:
62                 MtxStack(Renderer &);
63         private:        
64                 virtual void update();
65         };
66
67         MtxStack mtx_stack;
68         bool mtx_changed;
69         const Camera *camera;
70         std::list<State> state_stack;
71         State *state;
72         const VertexArray *vertex_array;
73         bool vertex_array_changed;
74         const Buffer *element_buffer;
75         bool shdata_changed;
76
77 public:
78         Renderer(const Camera *);
79         ~Renderer();
80
81         MatrixStack &matrix_stack() { return mtx_stack; }
82
83         const Camera *get_camera() const { return camera; }
84
85         void set_texture(const Texture *);
86         void set_texturing(const Texturing *);
87         void set_material(const Material *);
88
89         /** Sets the shader program to use.  An initial set of data can be set as
90         well, with the same semantics as add_shader_data. */
91         void set_shader_program(const Program *prog, const ProgramData *data = 0);
92
93         /** Adds another set of data to be use with shader programs.  The data is
94         independent of any shader program changes and remains in effect until the
95         Renderer state is popped. */
96         void add_shader_data(const ProgramData &data);
97
98         void set_vertex_array(const VertexArray *);
99         void set_element_buffer(const Buffer *);
100         void set_winding_test(const WindingTest *);
101
102         void push_state();
103         void pop_state();
104
105         /** Prepares for temporarily bypassing the Renderer by synchronizing the
106         current state with GL.  No additional call is necessary to resume using the
107         Renderer. */
108         void escape();
109
110         void draw(const Batch &);
111
112 private:
113         void apply_state();
114 };
115
116 } // namespace GL
117 } // namespace Msp
118
119 #endif