3 This file is part of libmspgl
4 Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
12 #include "framebuffer.h"
15 #include "postprocessor.h"
16 #include "renderbuffer.h"
18 #include "texture2d.h"
25 Pipeline::Pipeline(unsigned w, unsigned h, bool d):
42 void Pipeline::set_camera(const Camera *c)
47 PipelinePass &Pipeline::add_pass(const Tag &tag)
50 throw KeyError("Pass already exists");
52 PipelinePass &pass=passes[tag];
53 pass_order.push_back(tag);
57 PipelinePass &Pipeline::get_pass(const Tag &tag)
59 PassMap::iterator i=passes.find(tag);
61 throw KeyError("Unknown pass");
65 const PipelinePass &Pipeline::get_pass(const Tag &tag) const
67 PassMap::const_iterator i=passes.find(tag);
69 throw KeyError("Unknown pass");
73 void Pipeline::add_renderable(const Renderable &r)
75 renderables.push_back(&r);
78 void Pipeline::add_effect(Effect &e)
80 effects.push_back(&e);
83 void Pipeline::add_postprocessor(PostProcessor &pp)
85 postproc.push_back(&pp);
89 color_buf=new Texture2D;
90 color_buf->set_min_filter(NEAREST);
91 color_buf->set_mag_filter(NEAREST);
92 color_buf->storage((hdr ? RGB16F : RGB), width, height, 0);
93 color_buf->image(0, RGB, UNSIGNED_BYTE, 0);
94 fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0);
95 depth_buf=new Renderbuffer;
96 depth_buf->storage(DEPTH_COMPONENT, width, height);
97 fbo->attach(DEPTH_ATTACHMENT, *depth_buf);
98 Framebuffer::unbind();
102 void Pipeline::render(const Tag &tag) const
104 const PipelinePass &pass=get_pass(tag);
106 Bind bind_depth_test(pass.depth_test);
107 Bind bind_blend(pass.blend);
108 Bind bind_lighting(pass.lighting);
110 for(vector<Effect *>::const_iterator i=pass.effects.begin(); i!=pass.effects.end(); ++i)
113 for(vector<const Renderable *>::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
116 for(vector<Effect *>::const_iterator i=pass.effects.end(); i!=pass.effects.begin();)
120 void Pipeline::render_all() const
128 clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
131 for(vector<Effect *>::const_iterator i=effects.begin(); i!=effects.end(); ++i)
134 for(vector<Tag>::const_iterator i=pass_order.begin(); i!=pass_order.end(); ++i)
137 for(vector<Effect *>::const_iterator i=effects.end(); i!=effects.begin();)
141 Framebuffer::unbind();
143 // XXX Need two color buffer textures to handle multiple post-processors correctly
144 for(vector<PostProcessor *>::const_iterator i=postproc.begin(); i!=postproc.end(); ++i)
145 (*i)->render(*color_buf);