1#include "managed_frame_buffer.h"
6managed_frame_buffer::managed_frame_buffer() {
12managed_frame_buffer::~managed_frame_buffer() {
17void managed_frame_buffer::destruct(
const context& ctx) {
22 for(
auto it = attachments.begin(); it != attachments.end(); ++it) {
23 attachment& a = (*it).second;
28ivec2 managed_frame_buffer::get_size() {
39 int max_render_buffer_size = 12288;
42 if(max_render_buffer_size > 0) {
43 if(size.x() > max_render_buffer_size || size.y() > max_render_buffer_size) {
44 this->size =
ivec2(-1);
52void managed_frame_buffer::add_attachment(
const std::string& name,
const std::string& format,
TextureFilter tf,
TextureWrap tw,
bool attach) {
60 if(a.is_depth_attachment()) {
63 a.index = index_counter;
67 attachments.insert(std::make_pair(name, a));
70bool managed_frame_buffer::enable_attachment(context& ctx,
const std::string& name,
int tex_unit) {
72 auto elem = attachments.find(name);
73 if(elem != attachments.end()) {
74 attachment& a = (*elem).second;
75 return a.tex.enable(ctx, tex_unit);
81bool managed_frame_buffer::disable_attachment(context& ctx,
const std::string& name) {
83 auto elem = attachments.find(name);
84 if(elem != attachments.end()) {
85 attachment& a = (*elem).second;
86 return a.tex.disable(ctx);
92texture* managed_frame_buffer::attachment_texture_ptr(
const std::string& name) {
94 auto elem = attachments.find(name);
95 if(elem != attachments.end()) {
96 attachment& a = (*elem).second;
103bool managed_frame_buffer::ensure(context& ctx) {
105 ivec2 actual_size = get_actual_size(ctx);
109 if(!create_and_validate(ctx, actual_size)) {
110 std::cerr <<
"Error: fbo not complete" << std::endl;
119bool managed_frame_buffer::enable(context& ctx) {
121 bool success = fb.
enable(ctx);
126bool managed_frame_buffer::disable(context& ctx) {
132bool managed_frame_buffer::create_and_validate(context& ctx,
const ivec2& size) {
134 fb.
create(ctx, size.x(), size.y());
136 for(
auto it = attachments.begin(); it != attachments.end(); ++it) {
137 attachment& a = (*it).second;
139 unsigned filter_specifier = (unsigned)a.tf;
142 TextureFilter mag_filter = (filter_specifier & 1) ? TF_LINEAR : TF_NEAREST;
144 bool use_mipmaps = filter_specifier > 1;
146 a.tex = texture(a.format, mag_filter, a.tf, a.tw, a.tw);
147 a.tex.create(ctx, TT_2D, size.x(), size.y());
150 a.tex.generate_mipmaps(ctx);
152 if(a.is_depth_attachment()) {
156 fb.
attach(ctx, a.tex, 0, a.index);
164ivec2 managed_frame_buffer::get_actual_size(context& ctx) {
166 ivec2 actual_size(size);
168 actual_size.x() = ctx.get_width();
170 actual_size.y() = ctx.get_height();
bool create(const context &ctx, int _width=-1, int _height=-1)
create framebuffer if extension is supported, otherwise return false.
void push_viewport(context &ctx, const dvec2 &depth_range=dvec2(0, 1))
push a new window transformation to cover the fbo onto the window transformation stack
bool is_complete(const context &ctx) const
check for completeness, if not complete, get the reason in last_error
int get_width() const
return the width
void pop_viewport(context &ctx)
recover the window transformation array active before the last call to push_viewport
void destruct(const context &ctx)
destruct the framebuffer objext
bool attach(const context &ctx, const render_buffer &rb, int i=0)
attach render buffer to depth buffer if it is a depth buffer, to stencil if it is a stencil buffer or...
bool enable(context &ctx, int i0=-1, int i1=-1, int i2=-1, int i3=-1, int i4=-1, int i5=-1, int i6=-1, int i7=-1, int i8=-1, int i9=-1, int i10=-1, int i11=-1, int i12=-1, int i13=-1, int i14=-1, int i15=-1)
enable the framebuffer either with all color attachments if no arguments are given or if arguments ar...
bool disable(context &ctx)
disable the framebuffer object
int get_height() const
return the height
bool set_size(const ivec2 &size)
Sets the size of the framebuffer renderbuffers.
virtual bool is_created() const
return whether component has been created
TextureFilter
different texture filter
TextureWrap
different texture wrap modes
cgv::math::fvec< int32_t, 2 > ivec2
declare type of 2d 32 bit integer vectors