1#include "managed_frame_buffer.h"
6managed_frame_buffer::managed_frame_buffer() {
12void managed_frame_buffer::destruct(
const context& ctx) {
17 for(
auto it = attachments.begin(); it != attachments.end(); ++it) {
18 attachment& a = (*it).second;
23ivec2 managed_frame_buffer::get_size() {
33void managed_frame_buffer::add_attachment(
const std::string& name,
const std::string& format,
TextureFilter tf,
TextureWrap tw,
bool attach) {
41 if(a.is_depth_attachment()) {
44 a.index = index_counter;
48 attachments.insert(std::make_pair(name, a));
51bool managed_frame_buffer::enable_attachment(context& ctx,
const std::string& name,
int tex_unit) {
53 auto elem = attachments.find(name);
54 if(elem != attachments.end()) {
55 attachment& a = (*elem).second;
56 return a.tex.enable(ctx, tex_unit);
62bool managed_frame_buffer::disable_attachment(context& ctx,
const std::string& name) {
64 auto elem = attachments.find(name);
65 if(elem != attachments.end()) {
66 attachment& a = (*elem).second;
67 return a.tex.disable(ctx);
73texture* managed_frame_buffer::attachment_texture_ptr(
const std::string& name) {
75 auto elem = attachments.find(name);
76 if(elem != attachments.end()) {
77 attachment& a = (*elem).second;
86 int max_size = ctx.get_device_capabilities().max_render_buffer_size;
87 if(max_size > 0 && size.
x() > max_size || size.
y() > max_size) {
88 std::cerr <<
"Error: managed_framebuffer::ensure: requested size exceeds maximum supported size" << std::endl;
92 ivec2 actual_size = get_actual_size(ctx);
96 if(!create_and_validate(ctx, actual_size))
97 std::cerr <<
"Error: managed_framebuffer::ensure: framebuffer not complete" << std::endl;
103bool managed_frame_buffer::enable(
context& ctx,
bool push_viewport) {
106 bool success = fb.
enable(ctx);
110bool managed_frame_buffer::disable(context& ctx,
bool pop_viewport) {
117bool managed_frame_buffer::create_and_validate(context& ctx,
const ivec2& size) {
119 fb.
create(ctx, size.x(), size.y());
121 for(
auto it = attachments.begin(); it != attachments.end(); ++it) {
122 attachment& a = (*it).second;
124 unsigned filter_specifier = (unsigned)a.tf;
127 TextureFilter mag_filter = (filter_specifier & 1) ? TF_LINEAR : TF_NEAREST;
129 bool use_mipmaps = filter_specifier > 1;
131 a.tex = texture(a.format, mag_filter, a.tf, a.tw, a.tw);
132 a.tex.create(ctx, TT_2D, size.x(), size.y());
135 a.tex.generate_mipmaps(ctx);
137 if(a.is_depth_attachment()) {
141 fb.
attach(ctx, a.tex, 0, a.index);
149ivec2 managed_frame_buffer::get_actual_size(context& ctx) {
151 ivec2 actual_size(size);
153 actual_size.x() = ctx.get_width();
155 actual_size.y() = ctx.get_height();
base class for all drawables, which is independent of the used rendering API.
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 ensure(context &ctx)
Ensure the framebuffer is constructed with the specified size.
void set_size(const ivec2 &size)
Set the size of the framebuffer attachments.
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