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)
41 throw KeyError("Pass already exists");
43 PipelinePass &pass=passes[tag];
44 pass_order.push_back(tag);
48 PipelinePass &Pipeline::get_pass(const Tag &tag)
50 PassMap::iterator i=passes.find(tag);
52 throw KeyError("Unknown pass");
56 const PipelinePass &Pipeline::get_pass(const Tag &tag) const
58 PassMap::const_iterator i=passes.find(tag);
60 throw KeyError("Unknown pass");
64 void Pipeline::add_renderable(const Renderable &r)
66 renderables.push_back(&r);
69 void Pipeline::add_effect(Effect &e)
71 effects.push_back(&e);
74 void Pipeline::add_postprocessor(PostProcessor &pp)
76 postproc.push_back(&pp);
80 color_buf=new Texture2D;
81 color_buf->set_min_filter(NEAREST);
82 color_buf->set_mag_filter(NEAREST);
83 color_buf->storage((hdr ? RGB16F : RGB), width, height, 0);
84 color_buf->image(0, RGB, UNSIGNED_BYTE, 0);
85 fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
86 depth_buf=new Renderbuffer;
87 depth_buf->storage(DEPTH_COMPONENT, width, height);
88 fbo->attach(DEPTH_ATTACHMENT, *depth_buf);
89 Framebuffer::unbind();
93 void Pipeline::render(const Tag &tag) const
95 const PipelinePass &pass=get_pass(tag);
97 pass.lighting->bind();
98 for(vector<Effect *>::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i)
100 for(vector<const Renderable *>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
102 for(vector<Effect *>::const_iterator i=pass.effects.end(); i--!=pass.effects.begin();)
108 void Pipeline::render_all() const
113 clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
115 for(vector<Effect *>::const_iterator i=effects.begin(); i!=effects.end(); ++i)
117 for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
119 for(vector<Effect *>::const_iterator i=effects.end(); i--!=effects.begin();)
122 Framebuffer::unbind();
123 // XXX Need two color buffer textures to handle multiple post-processors correctly
124 for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
125 (*i)->render(*color_buf);