]> git.tdb.fi Git - libs/gl.git/blob - source/pipeline.cpp
Add a Camera class
[libs/gl.git] / source / pipeline.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "camera.h"
9 #include "effect.h"
10 #include "except.h"
11 #include "framebuffer.h"
12 #include "lighting.h"
13 #include "pipeline.h"
14 #include "postprocessor.h"
15 #include "renderbuffer.h"
16 #include "texture2d.h"
17
18 using namespace std;
19
20 namespace Msp {
21 namespace GL {
22
23 Pipeline::Pipeline(unsigned w, unsigned h, bool d):
24         camera(0),
25         width(w),
26         height(h),
27         hdr(d),
28         fbo(0),
29         color_buf(0),
30         depth_buf(0)
31 { }
32
33 Pipeline::~Pipeline()
34 {
35         delete fbo;
36         delete color_buf;
37         delete depth_buf;
38 }
39
40 void Pipeline::set_camera(const Camera *c)
41 {
42         camera = c;
43 }
44
45 PipelinePass &Pipeline::add_pass(const Tag &tag)
46 {
47         if(passes.count(tag))
48                 throw KeyError("Pass already exists");
49
50         PipelinePass &pass=passes[tag];
51         pass_order.push_back(tag);
52         return pass;
53 }
54
55 PipelinePass &Pipeline::get_pass(const Tag &tag)
56 {
57         PassMap::iterator i=passes.find(tag);
58         if(i==passes.end())
59                 throw KeyError("Unknown pass");
60         return i->second;
61 }
62
63 const PipelinePass &Pipeline::get_pass(const Tag &tag) const
64 {
65         PassMap::const_iterator i=passes.find(tag);
66         if(i==passes.end())
67                 throw KeyError("Unknown pass");
68         return i->second;
69 }
70
71 void Pipeline::add_renderable(const Renderable &r)
72 {
73         renderables.push_back(&r);
74 }
75
76 void Pipeline::add_effect(Effect &e)
77 {
78         effects.push_back(&e);
79 }
80
81 void Pipeline::add_postprocessor(PostProcessor &pp)
82 {
83         postproc.push_back(&pp);
84         if(!fbo)
85         {
86                 fbo=new Framebuffer;
87                 color_buf=new Texture2D;
88                 color_buf->set_min_filter(NEAREST);
89                 color_buf->set_mag_filter(NEAREST);
90                 color_buf->storage((hdr ? RGB16F : RGB), width, height, 0);
91                 color_buf->image(0, RGB, UNSIGNED_BYTE, 0);
92                 fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
93                 depth_buf=new Renderbuffer;
94                 depth_buf->storage(DEPTH_COMPONENT, width, height);
95                 fbo->attach(DEPTH_ATTACHMENT, *depth_buf);
96                 Framebuffer::unbind();
97         }
98 }
99
100 void Pipeline::render(const Tag &tag) const
101 {
102         const PipelinePass &pass=get_pass(tag);
103         if(pass.lighting)
104                 pass.lighting->bind();
105         for(vector<Effect *>::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i)
106                 (*i)->prepare();
107         for(vector<const Renderable *>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
108                 (*i)->render(tag);
109         for(vector<Effect *>::const_iterator i=pass.effects.end(); i--!=pass.effects.begin();)
110                 (*i)->cleanup();
111         if(pass.lighting)
112                 Lighting::unbind();
113 }
114
115 void Pipeline::render_all() const
116 {
117         if(camera)
118                 camera->apply();
119
120         if(fbo)
121         {
122                 fbo->bind();
123                 clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
124         }
125
126         for(vector<Effect *>::const_iterator i=effects.begin(); i!=effects.end(); ++i)
127                 (*i)->prepare();
128         for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
129                 render(*i);
130         for(vector<Effect *>::const_iterator i=effects.end(); i--!=effects.begin();)
131                 (*i)->cleanup();
132
133         if(fbo)
134                 Framebuffer::unbind();
135
136         // XXX Need two color buffer textures to handle multiple post-processors correctly
137         for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
138                 (*i)->render(*color_buf);
139 }
140
141 } // namespace GL
142 } // namespace Msp