Skip to content

Commit ace779d

Browse files
authored
Merge pull request #27 from kintel/refactor
Backport changes from OpenSCAD
2 parents 0aa3955 + a5e3b20 commit ace779d

25 files changed

Lines changed: 347 additions & 200 deletions

‎CMakeLists.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ set(SRCS
116116
src/system-gl.cc
117117
src/OpenGLContext.cc
118118
src/OffscreenContext.cc
119+
src/OffscreenContextNULL.cc
119120
src/OffscreenContextFactory.cc
120121
src/FBO.cc
121122
src/render_immediate.cc
@@ -137,10 +138,13 @@ add_test(NAME default_run COMMAND offscreen)
137138
add_test(NAME fails_on_empty_context_arg COMMAND offscreen --context)
138139
set_property(TEST fails_on_empty_context_arg PROPERTY WILL_FAIL true)
139140

141+
add_test(NAME nullgl_test COMMAND offscreen --context nullgl)
142+
140143
add_test(NAME will_save_framebuffer COMMAND offscreen -o out.png)
141144
add_test(NAME check_file_exists COMMAND ${CMAKE_COMMAND} -E cat out.png)
142145
set_tests_properties(check_file_exists PROPERTIES DEPENDS will_save_framebuffer)
143146

147+
144148
if(APPLE)
145149
add_test(NAME cgl_opengl2_immediate COMMAND offscreen --context cgl --opengl 2 --mode immediate)
146150
add_test(NAME cgl_opengl2_modern COMMAND offscreen --context cgl --opengl 2 --mode modern)

‎src/FBO.cc‎

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
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

810
namespace {
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+

‎src/FBO.h‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,34 @@
88

99
class FBO
1010
{
11-
bool useEXT;
12-
GLuint fbo_id = 0;
13-
GLuint old_fbo_id = 0;
14-
GLuint renderbuf_id = 0;
15-
GLuint depthbuf_id = 0;
16-
bool complete = false;
17-
1811
public:
1912
FBO(int width, int height, bool useEXT);
2013
~FBO() { destroy(); };
21-
bool isComplete() { return this->complete; }
14+
15+
int width() const { return this->width_; }
16+
int height() const { return this->height_; }
17+
bool isComplete() const { return this->complete_; }
18+
2219
bool resize(size_t width, size_t height);
2320
GLuint bind();
2421
void unbind();
22+
23+
private:
2524
void destroy();
25+
26+
int width_ = 0;
27+
int height_ = 0;
28+
bool use_ext_ = false;
29+
GLuint fbo_id_ = 0;
30+
GLuint old_fbo_id_ = 0;
31+
GLuint renderbuf_id_ = 0;
32+
GLuint depthbuf_id_ = 0;
33+
bool complete_ = false;
2634
};
2735

28-
std::unique_ptr<FBO> createFBO(const OpenGLContext &ctx);
36+
std::unique_ptr<FBO> createFBO(int width, int height);
37+
38+
inline std::unique_ptr<FBO> createFBO(const OpenGLContext &ctx) {
39+
return createFBO(ctx.width(), ctx.height());
40+
}
41+

‎src/GLFWContext.cc‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ std::shared_ptr<GLFWContext> CreateGLFWContext(size_t width, size_t height,
4040

4141
int fbWidth, fbHeight;
4242
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
43-
auto context = std::make_shared<GLFWContext>(window, fbWidth, fbHeight);
43+
auto context = std::make_shared<GLFWContext>(window, static_cast<uint32_t>(fbWidth), static_cast<uint32_t>(fbHeight));
4444

4545
return context;
4646
}
4747

48-
GLFWContext::GLFWContext(GLFWwindow* window, int width, int height) : OpenGLContext(width, height), window(window) {
48+
GLFWContext::GLFWContext(GLFWwindow* window, uint32_t width, uint32_t height) : OpenGLContext(width, height), window(window) {
4949
}
5050

51+
5152
void GLFWContext::loop(std::function<void()> render)
5253
{
5354
while (!glfwWindowShouldClose(this->window)) {

‎src/GLFWContext.h‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22

3+
#include <cstddef>
4+
#include <cstdint>
35
#include <memory>
6+
#include <string>
47
#include <functional>
58
#include "system-gl.h"
69
#include <GLFW/glfw3.h>
@@ -10,12 +13,21 @@
1013
class GLFWContext : public OpenGLContext {
1114
public:
1215
GLFWwindow* window;
13-
public:
14-
GLFWContext(GLFWwindow* window, int width, int height);
16+
17+
GLFWContext(GLFWwindow* window, uint32_t width, uint32_t height);
18+
~GLFWContext() override {
19+
if (this->window) {
20+
glfwDestroyWindow(this->window);
21+
}
22+
}
1523

1624
bool isOffscreen() const override { return false; }
1725

18-
bool makeCurrent() override {
26+
std::string getInfo() const override {
27+
return "GL context creator: GLFW\n";
28+
}
29+
30+
bool makeCurrent() const override {
1931
glfwMakeContextCurrent(this->window);
2032
return true;
2133
}
@@ -24,4 +36,5 @@ class GLFWContext : public OpenGLContext {
2436
};
2537

2638
std::shared_ptr<GLFWContext> CreateGLFWContext(size_t width, size_t height,
27-
size_t majorGLVersion, size_t minorGLVersion, bool invisible);
39+
size_t majorGLVersion, size_t minorGLVersion, bool invisible);
40+

‎src/OffscreenContext.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#pragma once
22

3-
3+
#include <cstdint>
44
#include "OpenGLContext.h"
55

66
class OffscreenContext : public OpenGLContext {
77
public:
8-
OffscreenContext(int width, int height) : OpenGLContext(width, height) {}
9-
virtual bool destroy() {return false;}
10-
bool isOffscreen() const override { return true; }
8+
OffscreenContext(uint32_t width, uint32_t height) : OpenGLContext(width, height) {}
119
};
10+

0 commit comments

Comments
 (0)