]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.h
Lots of comment updates
[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         void set_shader(const Program *, const ProgramData *);
89         void add_shader_data(const ProgramData *);
90         void set_vertex_array(const VertexArray *);
91         void set_element_buffer(const Buffer *);
92         void set_winding_test(const WindingTest *);
93
94         void push_state();
95         void pop_state();
96
97         /** Prepares for temporarily bypassing the Renderer by synchronizing the
98         current state with GL.  No additional call is necessary to resume using the
99         Renderer. */
100         void escape();
101
102         void draw(const Batch &);
103
104 private:
105         void apply_state();
106 };
107
108 } // namespace GL
109 } // namespace Msp
110
111 #endif