-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFramebuffer.cpp
More file actions
79 lines (60 loc) · 2.18 KB
/
Framebuffer.cpp
File metadata and controls
79 lines (60 loc) · 2.18 KB
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
#include "Framebuffer.h"
using namespace std;
Framebuffer::Framebuffer(unsigned width, unsigned height) {
this->width = width;
this->height = height;
glGenTextures(1, &depthTexId);
glBindTexture(GL_TEXTURE_2D, depthTexId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height,
0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glGenTextures(1, &colorTexId);
glBindTexture(GL_TEXTURE_2D, colorTexId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, width, height,
0, GL_RGBA, GL_FLOAT, 0);
glGenFramebuffersEXT(1, &frameBufferId);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferId);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_TEXTURE_2D, depthTexId, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, colorTexId, 0);
if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT) {
cerr << "Invalid framebuffer configuration" << endl;
exit(-1);
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
Framebuffer::~Framebuffer() {
glDeleteFramebuffersEXT(1, &frameBufferId);
glDeleteTextures(1, &depthTexId);
glDeleteTextures(1, &colorTexId);
}
GLuint Framebuffer::colorTextureId() {
return colorTexId;
}
GLuint Framebuffer::depthTextureId() {
return depthTexId;
}
GLuint Framebuffer::getWidth() {
return width;
}
GLuint Framebuffer::getHeight() {
return height;
}
void Framebuffer::bind() {
glPushAttrib(GL_VIEWPORT_BIT);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferId);
glViewport(0, 0, width, height);
}
void Framebuffer::unbind() {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glPopAttrib();
}