]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.h
ef17b3e62043ad847969f4b7e6102854e52fc55c
[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 class WindingTest;
27
28 /**
29 A class for supervising the rendering process.  While many Renderables (in
30 particular, Objects and Scenes) can by rendered without a Renderer, using one
31 will often be more efficient.  This is especially true for ObjectInstances.
32
33 The Renderer works by deferring GL state changes until something is actually
34 being drawn.  This avoids many unnecessary GL calls. */
35 class Renderer
36 {
37 public:
38         class Push
39         {
40         private:
41                 Renderer &renderer;
42
43         public:
44                 Push(Renderer &r): renderer(r) { renderer.push_state(); }
45                 ~Push() { renderer.pop_state(); }
46         };
47
48 private:
49         struct State
50         {
51                 const Texture *texture;
52                 const Texturing *texturing;
53                 const Material *material;
54                 const Program *shprog;
55                 std::vector<const ProgramData *> shdata;
56                 const WindingTest *winding_test;
57
58                 State();
59         };
60
61         class MtxStack: public MatrixStack
62         {
63         private:
64                 Renderer &renderer;
65
66         public:
67                 MtxStack(Renderer &);
68         private:        
69                 virtual void update();
70         };
71
72         MtxStack mtx_stack;
73         bool mtx_changed;
74         const Camera *camera;
75         std::list<State> state_stack;
76         State *state;
77         const VertexArray *vertex_array;
78         bool vertex_array_changed;
79         const Buffer *element_buffer;
80
81 public:
82         Renderer(const Camera *);
83         ~Renderer();
84
85         MatrixStack &matrix_stack() { return mtx_stack; }
86
87         const Camera *get_camera() const { return camera; }
88
89         void set_texture(const Texture *);
90         void set_texturing(const Texturing *);
91         void set_material(const Material *);
92         void set_shader(const Program *, const ProgramData *);
93         void add_shader_data(const ProgramData *);
94         void set_vertex_array(const VertexArray *);
95         void set_element_buffer(const Buffer *);
96         void set_winding_test(const WindingTest *);
97
98         void push_state();
99         void pop_state();
100
101         /** Prepares for temporarily bypassing the Renderer. */
102         void escape();
103
104         void draw(const Batch &);
105
106 private:
107         void apply_state();
108 };
109
110 } // namespace GL
111 } // namespace Msp
112
113 #endif