11#include " FBO.h"
22
3- #include " system-gl.h"
4-
3+ #include < cstddef>
54#include < iostream>
65#include < memory>
6+ #include < string>
7+
8+ #include " system-gl.h"
79
810namespace {
911
@@ -13,34 +15,33 @@ bool checkFBOStatus() {
1315 const char *statusString = nullptr ;
1416 switch (status) {
1517 case GL_FRAMEBUFFER_COMPLETE :
16- return true ;
17- break ;
18+ return true ;
1819 case GL_FRAMEBUFFER_UNDEFINED :
19- statusString = " GL_FRAMEBUFFER_UNDEFINED" ;
20- break ;
20+ statusString = " GL_FRAMEBUFFER_UNDEFINED" ;
21+ break ;
2122 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT :
22- statusString = " GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT" ;
23- break ;
23+ statusString = " GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT" ;
24+ break ;
2425 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT :
25- statusString = " GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT" ;
26- break ;
26+ statusString = " GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT" ;
27+ break ;
2728 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER :
28- statusString = " GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER" ;
29- break ;
29+ statusString = " GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER" ;
30+ break ;
3031 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER :
31- statusString = " GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER" ;
32- break ;
32+ statusString = " GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER" ;
33+ break ;
3334 case GL_FRAMEBUFFER_UNSUPPORTED :
34- statusString = " GL_FRAMEBUFFER_UNSUPPORTED" ;
35- break ;
35+ statusString = " GL_FRAMEBUFFER_UNSUPPORTED" ;
36+ break ;
3637 case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE :
37- statusString = " GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE" ;
38- break ;
38+ statusString = " GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE" ;
39+ break ;
3940 case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS :
40- statusString = " GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS" ;
41- break ;
41+ statusString = " GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS" ;
42+ break ;
4243 default :
43- break ;
44+ break ;
4445 }
4546
4647 std::cerr << " glCheckFramebufferStatus(): " ;
@@ -52,106 +53,105 @@ bool checkFBOStatus() {
5253
5354} // namespace
5455
55- std::unique_ptr<FBO > createFBO (const OpenGLContext& ctx ) {
56- if (ctx. majorVersion () >= 3 || ctx. isGLES () || hasGLExtension (GL_ARB_framebuffer_object )) {
57- return std::make_unique<FBO >(ctx. width (), ctx. height () , /* useEXT*/ false );
58- } else if (hasGLExtension (GL_EXT_framebuffer_object )) {
59- return std::make_unique<FBO >(ctx. width (), ctx. height () , /* useEXT*/ true );
56+ std::unique_ptr<FBO > createFBO (int width, int height ) {
57+ if (hasGLExtension (ARB_framebuffer_object )) {
58+ return std::make_unique<FBO >(width, height, /* useEXT*/ false );
59+ } else if (hasGLExtension (EXT_framebuffer_object )) {
60+ return std::make_unique<FBO >(width, height, /* useEXT*/ true );
6061 } else {
6162 std::cerr << " Framebuffer Objects not supported" << std::endl;
6263 return nullptr ;
6364 }
6465}
6566
66- FBO ::FBO (int width, int height, bool useEXT) : useEXT (useEXT) {
67+ FBO ::FBO (int width, int height, bool useEXT) : width_(width), height_(height), use_ext_ (useEXT) {
6768 // Generate and bind FBO
68- GL_CHECK (glGenFramebuffers (1 , &this ->fbo_id ));
69+ GL_CHECK (glGenFramebuffers (1 , &this ->fbo_id_ ));
6970 this ->bind ();
7071
7172 // Generate depth and render buffers
72- GL_CHECK (glGenRenderbuffers (1 , &this ->depthbuf_id ));
73- GL_CHECK (glGenRenderbuffers (1 , &this ->renderbuf_id ));
73+ GL_CHECK (glGenRenderbuffers (1 , &this ->depthbuf_id_ ));
74+ GL_CHECK (glGenRenderbuffers (1 , &this ->renderbuf_id_ ));
7475
7576 // Create buffers with correct size
7677 if (!this ->resize (width, height)) return ;
7778
7879 // Attach render and depth buffers
7980 GL_CHECK (glFramebufferRenderbuffer (GL_FRAMEBUFFER , GL_COLOR_ATTACHMENT0 ,
80- GL_RENDERBUFFER , this ->renderbuf_id ));
81+ GL_RENDERBUFFER , this ->renderbuf_id_ ));
8182
8283 if (!checkFBOStatus ()) {
8384 std::cerr << " Problem with OpenGL framebuffer after specifying color render buffer.\n " ;
8485 return ;
8586 }
8687
87- // glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
8888 // to prevent Mesa's software renderer from crashing, do this in two stages.
8989 // ie. instead of using GL_DEPTH_STENCIL_ATTACHMENT, do DEPTH then STENCIL.
9090 GL_CHECK (glFramebufferRenderbuffer (GL_FRAMEBUFFER , GL_DEPTH_ATTACHMENT ,
91- GL_RENDERBUFFER , this ->depthbuf_id ));
91+ GL_RENDERBUFFER , this ->depthbuf_id_ ));
9292 GL_CHECK (glFramebufferRenderbuffer (GL_FRAMEBUFFER , GL_STENCIL_ATTACHMENT ,
93- GL_RENDERBUFFER , this ->depthbuf_id ));
93+ GL_RENDERBUFFER , this ->depthbuf_id_ ));
9494
9595 if (!checkFBOStatus ()) {
9696 std::cerr << " Problem with OpenGL framebuffer after specifying depth render buffer.\n " ;
9797 return ;
9898 }
9999
100- this ->complete = true ;
100+ this ->complete_ = true ;
101101}
102102
103- bool FBO::resize (size_t width, size_t height)
104- {
105- if (this ->useEXT ) {
106- GL_CHECK (glBindRenderbufferEXT (GL_RENDERBUFFER , this ->renderbuf_id ));
103+ bool FBO::resize (size_t width, size_t height) {
104+ if (this ->use_ext_ ) {
105+ GL_CHECK (glBindRenderbufferEXT (GL_RENDERBUFFER , this ->renderbuf_id_ ));
107106 } else {
108- GL_CHECK (glBindRenderbuffer (GL_RENDERBUFFER , this ->renderbuf_id ));
107+ GL_CHECK (glBindRenderbuffer (GL_RENDERBUFFER , this ->renderbuf_id_ ));
109108 }
110109 GL_CHECK (glRenderbufferStorage (GL_RENDERBUFFER , GL_RGBA8 , width, height));
111- if (this ->useEXT ) {
112- GL_CHECK (glBindRenderbufferEXT (GL_RENDERBUFFER , this ->depthbuf_id ));
110+ if (this ->use_ext_ ) {
111+ GL_CHECK (glBindRenderbufferEXT (GL_RENDERBUFFER , this ->depthbuf_id_ ));
113112 } else {
114- GL_CHECK (glBindRenderbuffer (GL_RENDERBUFFER , this ->depthbuf_id ));
113+ GL_CHECK (glBindRenderbuffer (GL_RENDERBUFFER , this ->depthbuf_id_ ));
115114 }
116115 GL_CHECK (glRenderbufferStorage (GL_RENDERBUFFER , GL_DEPTH24_STENCIL8 , width, height));
117116
117+ this ->width_ = width;
118+ this ->height_ = height;
119+
118120 return true ;
119121}
120122
121- GLuint FBO::bind ()
122- {
123- glGetIntegerv (GL_FRAMEBUFFER_BINDING , reinterpret_cast <GLint *>(&this ->old_fbo_id ));
124- if (this ->useEXT ) {
125- GL_CHECK (glBindFramebufferEXT (GL_FRAMEBUFFER , this ->fbo_id ));
123+ GLuint FBO::bind () {
124+ glGetIntegerv (GL_FRAMEBUFFER_BINDING , reinterpret_cast <GLint *>(&this ->old_fbo_id_ ));
125+ if (this ->use_ext_ ) {
126+ GL_CHECK (glBindFramebufferEXT (GL_FRAMEBUFFER , this ->fbo_id_ ));
126127 } else {
127- GL_CHECK (glBindFramebuffer (GL_FRAMEBUFFER , this ->fbo_id ));
128+ GL_CHECK (glBindFramebuffer (GL_FRAMEBUFFER , this ->fbo_id_ ));
128129 }
129- return this ->old_fbo_id ;
130+ return this ->old_fbo_id_ ;
130131}
131132
132- void FBO::unbind ()
133- {
134- if (this ->useEXT ) {
135- GL_CHECK (glBindFramebufferEXT (GL_FRAMEBUFFER , this ->old_fbo_id ));
133+ void FBO::unbind () {
134+ if (this ->use_ext_ ) {
135+ GL_CHECK (glBindFramebufferEXT (GL_FRAMEBUFFER , this ->old_fbo_id_ ));
136136 } else {
137- GL_CHECK (glBindFramebuffer (GL_FRAMEBUFFER , this ->old_fbo_id ));
137+ GL_CHECK (glBindFramebuffer (GL_FRAMEBUFFER , this ->old_fbo_id_ ));
138138 }
139- this ->old_fbo_id = 0 ;
139+ this ->old_fbo_id_ = 0 ;
140140}
141141
142- void FBO::destroy ()
143- {
142+ void FBO::destroy () {
144143 this ->unbind ();
145- if (this ->depthbuf_id != 0 ) {
146- GL_CHECK (glDeleteRenderbuffers (1 , &this ->depthbuf_id ));
147- this ->depthbuf_id = 0 ;
144+ if (this ->depthbuf_id_ != 0 ) {
145+ GL_CHECK (glDeleteRenderbuffers (1 , &this ->depthbuf_id_ ));
146+ this ->depthbuf_id_ = 0 ;
148147 }
149- if (this ->renderbuf_id != 0 ) {
150- GL_CHECK (glDeleteRenderbuffers (1 , &this ->renderbuf_id ));
151- this ->renderbuf_id = 0 ;
148+ if (this ->renderbuf_id_ != 0 ) {
149+ GL_CHECK (glDeleteRenderbuffers (1 , &this ->renderbuf_id_ ));
150+ this ->renderbuf_id_ = 0 ;
152151 }
153- if (this ->fbo_id != 0 ) {
154- GL_CHECK (glDeleteFramebuffers (1 , &this ->fbo_id ));
155- this ->fbo_id = 0 ;
152+ if (this ->fbo_id_ != 0 ) {
153+ GL_CHECK (glDeleteFramebuffers (1 , &this ->fbo_id_ ));
154+ this ->fbo_id_ = 0 ;
156155 }
157156}
157+
0 commit comments