forked from gongjianbo/OpenGLwithQtWidgets
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
100 lines (92 loc) · 3.33 KB
/
main.cpp
File metadata and controls
100 lines (92 loc) · 3.33 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
#include <QApplication>
#include <QSurfaceFormat>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QOffscreenSurface>
#include <QDebug>
#include "mainwindow.h"
//解析字符串中的opengl版本号
bool parseOpenGLVersion(const QByteArray &versionString, int &major, int &minor)
{
bool majorOk = false;
bool minorOk = false;
QList<QByteArray> parts = versionString.split(' ');
if (versionString.startsWith(QByteArrayLiteral("OpenGL ES"))) {
if (parts.size() >= 3) {
QList<QByteArray> versionParts = parts.at(2).split('.');
if (versionParts.size() >= 2) {
major = versionParts.at(0).toInt(&majorOk);
minor = versionParts.at(1).toInt(&minorOk);
// Nexus 6 has "OpenGL ES 3.0V@95.0 (GIT@I86da836d38)"
if (!minorOk)
if (int idx = versionParts.at(1).indexOf('V'))
minor = versionParts.at(1).left(idx).toInt(&minorOk);
} else {
qWarning("Unrecognized OpenGL ES version");
}
} else {
// If < 3 parts to the name, it is an unrecognised OpenGL ES
qWarning("Unrecognised OpenGL ES version");
}
} else {
// Not OpenGL ES, but regular OpenGL, the version numbers are first in the string
QList<QByteArray> versionParts = parts.at(0).split('.');
if (versionParts.size() >= 2) {
major = versionParts.at(0).toInt(&majorOk);
minor = versionParts.at(1).toInt(&minorOk);
} else {
qWarning("Unrecognized OpenGL version");
}
}
if (!majorOk || !minorOk)
qWarning("Unrecognized OpenGL version");
return (majorOk && minorOk);
}
bool checkVersion(int minMajor, int minMinor)
{
int major = 0;
int minor = 0;
//一般需要先构造一个QGuiApplication有些函数才能调用
QScreen *screen = qApp->primaryScreen();
if(screen){
QOffscreenSurface surface(screen);
surface.create();
QOpenGLContext context;
context.create();
context.makeCurrent(&surface);
const GLubyte *glstr = context.functions()->glGetString(GL_VERSION);
if(glstr){
QByteArray bytestr = QByteArray(reinterpret_cast<const char*>(glstr));
qDebug() << "glGetString" << bytestr;
if(!parseOpenGLVersion(bytestr,major,minor)){
major = 0;
minor = 0;
}
}
context.doneCurrent();
surface.destroy();
}
qDebug() << "Native OpenGL version:" << major << minor;
return (major*10+minor>=minMajor*10+minMinor);
}
int main(int argc, char *argv[])
{
QGuiApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
QApplication app(argc, argv);
//《OpenGL编程指南(原书第九版)》以OpenGL4.5为基础
qDebug() << "Need OpenGL4.5 desktop context";
if(!checkVersion(4, 5))
return -1;
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
//在Qt6设置后显示黑色窗口,或者切换tab崩溃
QSurfaceFormat fmt;
fmt.setRenderableType(QSurfaceFormat::OpenGL);
fmt.setVersion(4, 5);
fmt.setProfile(QSurfaceFormat::CompatibilityProfile);
//fmt.setSamples(8);//多重采样
QSurfaceFormat::setDefaultFormat(fmt);
#endif
MainWindow w;
w.show();
return app.exec();
}