3 This file is part of libmspgl
4 Copyright © 2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include "framebuffer.h"
13 #include "postprocessor.h"
14 #include "renderbuffer.h"
15 #include "texture2d.h"
22 Pipeline::Pipeline(unsigned w, unsigned h, bool d):
38 PipelinePass &Pipeline::add_pass(const Tag &tag)
40 if(passes.count(tag.id))
41 throw KeyError("Pass already exists");
43 PipelinePass &pass=passes[tag.id];
44 pass_order.push_back(tag);
48 PipelinePass &Pipeline::get_pass(const Tag &tag)
50 map<unsigned, PipelinePass>::iterator i=passes.find(tag.id);
52 throw KeyError("Unknown pass");
56 const PipelinePass &Pipeline::get_pass(const Tag &tag) const
58 map<unsigned, PipelinePass>::const_iterator i=passes.find(tag.id);
60 throw KeyError("Unknown pass");
64 bool Pipeline::has_pass(const Tag &tag) const
66 return passes.count(tag.id);
69 void Pipeline::add_renderable(const Renderable &r)
71 renderables.push_back(&r);
74 void Pipeline::add_effect(Effect &e)
76 effects.push_back(&e);
79 void Pipeline::add_postprocessor(PostProcessor &pp)
81 postproc.push_back(&pp);
85 color_buf=new Texture2D;
86 color_buf->set_min_filter(NEAREST);
87 color_buf->set_mag_filter(NEAREST);
88 color_buf->storage((hdr ? RGB16F : RGB), width, height, 0);
89 color_buf->image(0, RGB, UNSIGNED_BYTE, 0);
90 fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
91 depth_buf=new Renderbuffer;
92 depth_buf->storage(DEPTH_COMPONENT, width, height);
93 fbo->attach(DEPTH_ATTACHMENT, *depth_buf);
94 Framebuffer::unbind();
98 void Pipeline::render(const Tag &tag) const
100 const PipelinePass &pass=get_pass(tag);
102 pass.lighting->bind();
103 for(vector<Effect *>::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i)
105 for(vector<const Renderable *>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
107 for(vector<Effect *>::const_iterator i=pass.effects.end(); i--!=pass.effects.begin();)
113 void Pipeline::render_all() const
118 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
120 for(vector<Effect *>::const_iterator i=effects.begin(); i!=effects.end(); ++i)
122 for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
124 for(vector<Effect *>::const_iterator i=effects.end(); i--!=effects.begin();)
127 Framebuffer::unbind();
128 // XXX Need two color buffer textures to handle multiple post-processors correctly
129 for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
130 (*i)->render(*color_buf);