Framebuffer.cpp
1// Copyright 2019 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4
5#include <smk/Color.hpp>
6#include <smk/Drawable.hpp>
7#include <smk/Framebuffer.hpp>
8#include <smk/RenderState.hpp>
9
10namespace smk {
11
12/// @brief Construct a Framebuffer of a given dimensions.
13/// @param width The width of the drawing surface.
14/// @param height The width of the drawing surface.
15Framebuffer::Framebuffer(int width, int height) {
16 Texture::Option option;
17 option.internal_format = GL_RGB;
18 option.format = GL_RGB;
19 option.type = GL_UNSIGNED_BYTE;
20 option.generate_mipmap = false;
21 option.min_filter = GL_LINEAR;
22 option.mag_filter = GL_LINEAR;
23 color_textures_.emplace_back(nullptr, width, height, option);
24
25 Init();
26}
27
28/// @brief Construct a Framebuffer from a list of color textures. Those textures
29/// must be constructed with not mipmap. See Texture::Option::generate_mipma^
30// /and Texture::Option::min_filter.
31/// @param width The width of the drawing surface.
32/// @param height The width of the drawing surface.
33Framebuffer::Framebuffer(std::vector<Texture> color_textures)
34 : color_textures_(std::move(color_textures)) {
35 Init();
36}
37
38void Framebuffer::Init() {
39 width_ = color_texture().width();
40 height_ = color_texture().height();
41
42 // The frame buffer.
43 glGenFramebuffers(1, &frame_buffer_);
44 glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer_);
45
46 // Attach the textures to the framebuffer.
47 for (size_t i = 0; i < color_textures_.size(); ++i) {
48 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
49 GL_TEXTURE_2D, color_textures_[i].id(), 0);
50 }
51
52 // The render buffer.
53 glGenRenderbuffers(1, &render_buffer_);
54 glBindRenderbuffer(GL_RENDERBUFFER, render_buffer_);
55 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width_, height_);
56 glBindRenderbuffer(GL_RENDERBUFFER, 0);
57
58 // Attach it to the framebuffer.
59 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
60 GL_RENDERBUFFER, render_buffer_);
61
62 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
63 std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!"
64 << std::endl;
65 }
66
67 glBindFramebuffer(GL_FRAMEBUFFER, 0);
68
69 InitRenderTarget();
70}
71
72Framebuffer::~Framebuffer() {
73 if (frame_buffer_) {
74 glDeleteFramebuffers(1, &frame_buffer_);
75 frame_buffer_ = 0;
76 }
77
78 if (render_buffer_) {
79 glDeleteRenderbuffers(1, &render_buffer_);
80 render_buffer_ = 0;
81 }
82}
83
84Framebuffer::Framebuffer(Framebuffer&& other) noexcept {
85 this->operator=(std::move(other));
86}
87
88Framebuffer& Framebuffer::operator=(Framebuffer&& other) noexcept {
89 RenderTarget::operator=(std::move(other));
90 std::swap(color_textures_, other.color_textures_); // NOLINT
91 std::swap(render_buffer_, other.render_buffer_); // NOLINT
92 return *this;
93}
94
95smk::Texture& Framebuffer::color_texture() {
96 return color_textures_[0];
97}
98
99} // namespace smk
Framebuffer(int width, int height)
Construct a Framebuffer of a given dimensions.
Definition: Framebuffer.cpp:15
RenderTarget & operator=(RenderTarget &&other) noexcept
Move operator.
int height() const
the height of the surface.
int width() const
the width of the surface.
int height() const
Access the height of the texture.
Definition: Texture.cpp:201
int width() const
Access the width of the texture.
Definition: Texture.cpp:195