-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathframebuffer.h
49 lines (38 loc) · 1.52 KB
/
framebuffer.h
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
#include <GL/glew.h>
#include "defines.h"
struct Framebuffer {
void create(uint32 width, uint32 height) {
glGenFramebuffers(1, &fbo);
glGenTextures(2, textures);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, textures[1]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, width, height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
bind();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, textures[1], 0);
unbind();
}
void destroy() {
glDeleteFramebuffers(1, &fbo);
// This line was missing in the original implementation in the framebuffer video
glDeleteTextures(2, textures);
}
void bind() {
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
}
GLuint getTextureId() {
return textures[0];
}
void unbind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
private:
GLuint fbo;
GLuint textures[2];
};