74 lines
2.0 KiB
C++
74 lines
2.0 KiB
C++
#include "fbo.h"
|
|
|
|
#include "demo_data.h"
|
|
#include <memory>
|
|
|
|
BEGIN_NAMESPACE
|
|
|
|
fbo::fbo(int w, int h)
|
|
: width(w),
|
|
height(h) {
|
|
|
|
glGenFramebuffers(1, &frame_buffer_id);
|
|
glGenTextures(1, &color_buffer_id);
|
|
glGenRenderbuffers(1, &depth_buffer_id);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer_id);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, color_buffer_id);
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE,
|
|
4, // number of samples for multisampling
|
|
GL_RGBA8,
|
|
width, height,
|
|
false);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, color_buffer_id,
|
|
0);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, depth_buffer_id);
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
|
|
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_buffer_id);
|
|
}
|
|
|
|
int fbo::get_width() const {
|
|
return width;
|
|
}
|
|
|
|
int fbo::get_height() const {
|
|
return height;
|
|
}
|
|
|
|
GLuint fbo::get_color_buffer_id() const {
|
|
return color_buffer_id;
|
|
}
|
|
|
|
GLuint fbo::get_depth_buffer_id() const {
|
|
return depth_buffer_id;
|
|
}
|
|
|
|
void fbo::use() {
|
|
old_width = demo_data::screen_width;
|
|
old_height = demo_data::screen_height;
|
|
|
|
demo_data::screen_height = width;
|
|
demo_data::screen_height = height;
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer_id);
|
|
glViewport(0, 0, width, height);
|
|
}
|
|
|
|
void fbo::unuse() const {
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
glViewport(0, 0, old_width, old_height);
|
|
|
|
demo_data::screen_width = old_width;
|
|
demo_data::screen_height = old_height;
|
|
}
|
|
|
|
std::shared_ptr<texture> fbo::get_texture() {
|
|
return std::make_shared<texture>(color_buffer_id, width, height);
|
|
}
|
|
|
|
END_NAMESPACE |