-
Notifications
You must be signed in to change notification settings - Fork 1
/
Framebuffer.hpp
94 lines (69 loc) · 2.01 KB
/
Framebuffer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once
#include "incgraphics.h"
#include "Debug.hpp"
#include "Logger.hpp"
#include "Renderbuffer.hpp"
namespace gl {
struct Framebuffer {
GLuint fbo = 0;
std::vector<GLuint> textures;
size_t w=0, h=0;
Framebuffer()
{}
auto id() const {
return fbo;
}
static void init(GLuint &fbo) {
glGenFramebuffers(1, &fbo); GLERROR
}
static void init(Framebuffer &fb) {
fb.init();
}
void init() {
gl::Framebuffer::init(fbo);
}
static void bind(GLuint fbo) {
glBindFramebuffer(GL_FRAMEBUFFER, fbo); GLERROR
}
static void bind(Framebuffer &fb) {
fb.bind();
}
void bind() {
gl::Framebuffer::bind(fbo);
}
static void unbind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0); GLERROR
}
template <GLenum Attachment>
void attach_texture(int mipmap_level=0) {
gl::Framebuffer::bind();
GLuint tex = 0;
glGenTextures(1, &tex); GLERROR
glBindTexture(GL_TEXTURE_2D, tex); GLERROR
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); GLERROR
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); GLERROR
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); GLERROR
glFramebufferTexture2D(GL_FRAMEBUFFER, Attachment, GL_TEXTURE_2D, tex, mipmap_level); GLERROR
textures.push_back(tex);
gl::Framebuffer::unbind();
}
template <GLenum AttachmentT, GLenum StorageT>
void attach_renderbuffer(gl::Renderbuffer<StorageT> &rb) {
bind();
gl::Renderbuffer<StorageT>::bind(rb);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, AttachmentT, GL_RENDERBUFFER, rb.id()); GLERROR
gl::Renderbuffer<StorageT>::unbind();
gl::Framebuffer::unbind();
}
static bool is_complete() {
return glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
}
static void clear(gl::Framebuffer &fb) {
glDeleteTextures(fb.textures.size(), fb.textures.data()); GLERROR
glDeleteFramebuffers(1, &fb.fbo); GLERROR
}
void clear() {
gl::Framebuffer::clear(*this);
}
};
}