1+ #include < GL/glew.h>
2+
3+ #include " defines.h"
4+
5+ struct FrameBuffer {
6+ void create (uint32 width, uint32 height) {
7+ glGenFramebuffers (1 , &fbo);
8+
9+ unsigned int textures[2 ];
10+ glGenTextures (2 , textures);
11+
12+ glBindTexture (GL_TEXTURE_2D, textures[0 ]);
13+ glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGBA, width, height, 0 , GL_RGBA, GL_UNSIGNED_BYTE, NULL );
14+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
15+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
16+
17+ glBindTexture (GL_TEXTURE_2D, textures[1 ]);
18+ glTexImage2D (GL_TEXTURE_2D, 0 , GL_DEPTH24_STENCIL8, width, height, 0 , GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL );
19+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
20+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
21+ glBindTexture (GL_TEXTURE_2D, 0 );
22+
23+ bind ();
24+ glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0 ], 0 );
25+ glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, textures[1 ], 0 );
26+ unbind ();
27+ }
28+
29+ void destroy () {
30+ glDeleteFramebuffers (1 , &fbo);
31+ }
32+
33+ void bind () {
34+ glBindFramebuffer (GL_FRAMEBUFFER, fbo);
35+ }
36+
37+ void unbind () {
38+ glBindFramebuffer (GL_FRAMEBUFFER, 0 );
39+ }
40+
41+ private:
42+ GLuint fbo;
43+ };
0 commit comments