-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenGLWindow.cpp
More file actions
149 lines (113 loc) · 2.89 KB
/
OpenGLWindow.cpp
File metadata and controls
149 lines (113 loc) · 2.89 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "OpenGLWindow.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLPaintDevice>
#include <QtGui/QPainter>
OpenGLWindow::OpenGLWindow(QWindow *parent) :
QWindow(parent),
mContext(nullptr),
mDebugLogger(nullptr)
{
setSurfaceType(QWindow::OpenGLSurface);
}
void OpenGLWindow::renderLater() {
requestUpdate();
}
void OpenGLWindow::renderNow() {
if (!isExposed())
return;
if (mContext == nullptr)
initOpenGL();
mContext->makeCurrent(this);
paintGL();
mContext->swapBuffers(this);
}
bool OpenGLWindow::event(QEvent *event) {
switch (event->type()) {
case QEvent::UpdateRequest:
renderNow();
return true;
default:
return QWindow::event(event);
}
}
void OpenGLWindow::exposeEvent(QExposeEvent * /*event*/) {
renderNow();
}
void OpenGLWindow::resizeEvent(QResizeEvent * event) {
QWindow::resizeEvent(event);
if (mContext == nullptr)
initOpenGL();
resizeGL(width(), height());
}
void OpenGLWindow::onMessageLogged(const QOpenGLDebugMessage &msg) {
QString prefix;
switch (msg.severity()) {
case QOpenGLDebugMessage::NotificationSeverity:
prefix += "++++";
break;
case QOpenGLDebugMessage::HighSeverity:
prefix += "+++";
break;
case QOpenGLDebugMessage::MediumSeverity:
prefix += "++";
break;
case QOpenGLDebugMessage::LowSeverity:
prefix += "+";
break;
}
prefix += " [";
#define CASE(c) case QOpenGLDebugMessage::c: prefix += #c; break
switch (msg.source())
{
CASE(APISource);
CASE(WindowSystemSource);
CASE(ShaderCompilerSource);
CASE(ThirdPartySource);
CASE(ApplicationSource);
CASE(OtherSource);
CASE(InvalidSource);
}
#undef CASE
prefix += ":";
// Format based on type
#define CASE(c) case QOpenGLDebugMessage::c: prefix += #c; break
switch (msg.type())
{
CASE(ErrorType);
CASE(DeprecatedBehaviorType);
CASE(UndefinedBehaviorType);
CASE(PortabilityType);
CASE(PerformanceType);
CASE(OtherType);
CASE(MarkerType);
CASE(GroupPushType);
CASE(GroupPopType);
}
#undef CASE
prefix += "] ";
qDebug().noquote().nospace() << prefix << msg.message() << "\n";
}
void OpenGLWindow::initOpenGL() {
Q_ASSERT(mContext == nullptr);
mContext = new QOpenGLContext(this);
mContext->setFormat(requestedFormat());
mContext->create();
mContext->makeCurrent(this);
Q_ASSERT(mContext->isValid());
initializeOpenGLFunctions();
#ifdef GL_DEBUG
if (mContext->hasExtension(QByteArrayLiteral("GL_KHR_debug")))
qDebug() << "GL_KHR_debug extension available";
else
qWarning() << "GL_KHR_debug extension *not* available";
mDebugLogger = new QOpenGLDebugLogger(this);
if (mDebugLogger->initialize()) {
qDebug() << "Debug Logger initialized\n";
connect(mDebugLogger, SIGNAL(messageLogged(QOpenGLDebugMessage)), this, SLOT(onMessageLogged(QOpenGLDebugMessage)));
mDebugLogger->startLogging();
}
#endif // GL_DEBUG
initializeGL(); // call user code
}