]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.h
Better tracking of Renderer matrix changes
[libs/gl.git] / source / renderer.h
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_GL_RENDERER_H_
9 #define MSP_GL_RENDERER_H_
10
11 #include <vector>
12 #include "matrix.h"
13
14 namespace Msp {
15 namespace GL {
16
17 class Batch;
18 class Buffer;
19 class Camera;
20 class Material;
21 class Program;
22 class ProgramData;
23 class Texture;
24 class Texturing;
25 class VertexArray;
26
27 /**
28 A class for supervising the rendering process.  While many Renderables (in
29 particular, Objects and Scenes) can by rendered without a Renderer, using one
30 will often be more efficient.  This is especially true for ObjectInstances.
31
32 The Renderer works by deferring GL state changes until something is actually
33 being drawn.  This avoids many unnecessary GL calls. */
34 class Renderer
35 {
36 public:
37         class Push
38         {
39         private:
40                 Renderer &renderer;
41
42         public:
43                 Push(Renderer &r): renderer(r) { renderer.push_state(); }
44                 ~Push() { renderer.pop_state(); }
45         };
46
47 private:
48         struct State
49         {
50                 const Texture *texture;
51                 const Texturing *texturing;
52                 const Material *material;
53                 const Program *shprog;
54                 std::vector<const ProgramData *> shdata;
55
56                 State();
57         };
58
59         class MtxStack: public MatrixStack
60         {
61         private:
62                 Renderer &renderer;
63
64         public:
65                 MtxStack(Renderer &);
66         private:        
67                 virtual void update();
68         };
69
70         MtxStack mtx_stack;
71         bool mtx_changed;
72         const Camera *camera;
73         std::list<State> state_stack;
74         State *state;
75         const VertexArray *vertex_array;
76         bool vertex_array_changed;
77         const Buffer *element_buffer;
78
79 public:
80         Renderer(const Camera *);
81         ~Renderer();
82
83         MatrixStack &matrix_stack() { return mtx_stack; }
84
85         const Camera *get_camera() const { return camera; }
86
87         void set_texture(const Texture *);
88         void set_texturing(const Texturing *);
89         void set_material(const Material *);
90         void set_shader(const Program *, const ProgramData *);
91         void add_shader_data(const ProgramData *);
92         void set_vertex_array(const VertexArray *);
93         void set_element_buffer(const Buffer *);
94
95         void push_state();
96         void pop_state();
97
98         /** Prepares for temporarily bypassing the Renderer. */
99         void escape();
100
101         void draw(const Batch &);
102
103 private:
104         void apply_state();
105 };
106
107 } // namespace GL
108 } // namespace Msp
109
110 #endif