Skip to content

Commit f3e3c74

Browse files
authored
Merge pull request #49 from miyanyan/qt-opengl
qt viswidget: use glad to init This makes mplot/qt/viswidget.h load GL headers with GLAD (in a similar way that viswidget_mx.h does for multi-context capable GL).
2 parents 4743771 + c66d453 commit f3e3c74

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

examples/qt/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ include_directories(${qtmorph_SOURCE_DIR})
2929
include_directories(BEFORE ${PROJECT_SOURCE_DIR})
3030

3131
# Ensure our programs will get the correct links
32-
set(MORPH_LIBS_GL OpenGL::GL glfw Freetype::Freetype)
32+
set(MORPH_LIBS_GL OpenGL::GL Freetype::Freetype)
3333
set(MORPH_LIBS_CORE ${HDF5_C_LIBRARIES})
3434

3535
# Just about the simplest possible Qt morphologica program - draws the

examples/qt/fps/mainwindow.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define MAINWINDOW_H
33

44
#include <QMainWindow>
5-
#include <QOpenGLWidget>
65
QT_BEGIN_NAMESPACE
76
namespace Ui { class MainWindow; }
87
QT_END_NAMESPACE
@@ -12,6 +11,8 @@ QT_END_NAMESPACE
1211
#include <sm/vvec>
1312
#include <sm/hexgrid>
1413

14+
class QOpenGLWidget;
15+
1516
class MainWindow : public QMainWindow
1617
{
1718
Q_OBJECT

examples/qt/hexgrid/mainwindow.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define MAINWINDOW_H
33

44
#include <QMainWindow>
5-
#include <QOpenGLWidget>
65
QT_BEGIN_NAMESPACE
76
namespace Ui { class MainWindow; }
87
QT_END_NAMESPACE
@@ -12,6 +11,8 @@ QT_END_NAMESPACE
1211
#include <sm/vvec>
1312
#include <sm/hexgrid>
1413

14+
class QOpenGLWidget;
15+
1516
class MainWindow : public QMainWindow
1617
{
1718
Q_OBJECT

mplot/VisualOwnableNoMX.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,13 @@ namespace mplot {
281281
}
282282

283283
public:
284-
#ifdef GLAD_GL // Only define if GL was included with GLAD
285284
void init_glad (GLADloadfunc procaddressfn)
286285
{
287286
this->glfn_version = gladLoadGL (procaddressfn);
288287
if (this->glfn_version == 0) {
289288
throw std::runtime_error ("Failed to initialize GLAD GL context");
290289
}
291290
}
292-
#endif
293291

294292
//! Add a label _text to the scene at position _toffset. Font features are
295293
//! defined by the tfeatures. Return geometry of the text.

mplot/qt/viswidget.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
#include <iostream>
44
#include <functional>
55

6-
#include <QtWidgets/QOpenGLWidget>
7-
#include <QOpenGLFunctions_4_1_Core> // problem is that this loads GL headers, so we don't load glad, further on
8-
#include <QSurfaceFormat>
9-
#include <QMouseEvent>
10-
#include <QWheelEvent>
6+
class QOpenGLWidget;
117

128
// VisualOwnable is going to be owned by the QOpenGLWidget
139
// Define mplot::win_t before #including mplot/VisualOwnableNoMX.h
1410
namespace mplot { using win_t = QOpenGLWidget; }
1511
#include <mplot/VisualOwnableNoMX.h>
12+
13+
#include <QtWidgets/QOpenGLWidget>
14+
#include <QOpenGLContext>
15+
#include <QSurfaceFormat>
16+
#include <QMouseEvent>
17+
#include <QWheelEvent>
18+
1619
// We need to be able to convert from Qt keycodes to mplot keycodes
1720
#include <mplot/qt/keycodes.h>
1821

@@ -22,8 +25,16 @@ namespace mplot {
2225
// This must match the QOpenGLFunctions_4_1_Core class you derive from
2326
constexpr int gl_version = mplot::gl::version_4_1;
2427

28+
struct OpenGLProcAddressHelper {
29+
inline static QOpenGLContext *ctx;
30+
31+
static QFunctionPointer getProcAddress(const char *name) {
32+
return ctx->getProcAddress(name);
33+
}
34+
};
35+
2536
// A mplot::VisualOwnable-based widget
26-
struct viswidget : public QOpenGLWidget, protected QOpenGLFunctions_4_1_Core
37+
struct viswidget : public QOpenGLWidget
2738
{
2839
// Unlike the GLFW or mplot-in-a-QWindow schemes, we hold the mplot::VisualOwnable
2940
// inside the widget.
@@ -59,7 +70,8 @@ namespace mplot {
5970
void initializeGL() override
6071
{
6172
// Make sure we can call gl functions
62-
initializeOpenGLFunctions();
73+
OpenGLProcAddressHelper::ctx = context();
74+
v.init_glad(OpenGLProcAddressHelper::getProcAddress);
6375
// Switch on multisampling anti-aliasing (with the num samples set in constructor)
6476
glEnable (GL_MULTISAMPLE);
6577
// Initialise mplot::VisualOwnable

0 commit comments

Comments
 (0)