Last active
December 13, 2018 11:54
-
-
Save hombit/cdfd303659c5461a309e6a0d25b1241d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.0.0) | |
cmake_policy(SET CMP0048 NEW) | |
project(qtopengl LANGUAGES C CXX VERSION 3.2.2) | |
set(CMAKE_INCLUDE_CURRENT_DIR ON) | |
set(CMAKE_AUTOMOC ON) | |
set(CMAKE_CXX_STANDARD 11) | |
find_package(Qt5Core REQUIRED) | |
find_package(Qt5Widgets REQUIRED) | |
find_package(Qt5Test REQUIRED) | |
if(Qt5Widgets_VERSION VERSION_LESS 5.5) | |
message(FATAL_ERROR "Qt5 5.5 or later is required") | |
else(Qt5Widgets_VERSION VERSION_LESS 5.5) | |
message(STATUS "Found Qt ${Qt5Widgets_VERSION}") | |
endif(Qt5Widgets_VERSION VERSION_LESS 5.5) | |
include_directories ("${PROJECT_SOURCE_DIR}/include" "${CMAKE_CURRENT_BINARY_DIR}") | |
set(SOURCES main.cpp main.h) | |
set(TARGET qtopengl) | |
add_executable(${TARGET} ${SOURCES}) | |
target_link_libraries(${TARGET} Qt5::Widgets) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QDebug> | |
#include <QString> | |
#include <QOpenGLContext> | |
#include <QOpenGLFunctions> | |
#include <QSurfaceFormat> | |
#include <main.h> | |
OpenGLWidget::OpenGLWidget(): QOpenGLWidget() { | |
} | |
OpenGLWidget::~OpenGLWidget() = default; | |
void OpenGLWidget::initializeGL() { | |
QOpenGLWidget::initializeGL(); | |
qDebug() << format().version() << format().renderableType(); | |
auto context = this->context(); | |
auto functions = context->functions(); | |
const QString vendor = reinterpret_cast<const char*>(functions->glGetString(GL_VENDOR)); | |
const QString renderer = reinterpret_cast<const char*>(functions->glGetString(GL_RENDERER)); | |
const QString version = reinterpret_cast<const char*>(functions->glGetString(GL_VERSION)); | |
qDebug() << "OpenGL vendor: " << vendor << " " | |
<< "renderer: " << renderer << " " | |
<< "version: " << version; | |
QCoreApplication::quit(); | |
} | |
MainWindow::MainWindow(): QMainWindow() { | |
setCentralWidget(&widget_); | |
} | |
MainWindow::~MainWindow() = default; | |
Application::Application(int& argc, char **argv): QApplication(argc, argv) { | |
main_.show(); | |
} | |
Application::~Application() = default; | |
int main(int argc, char** argv) { | |
if (argc < 3) | |
return 1; | |
QSurfaceFormat format; | |
format.setVersion(atoi(argv[1]), atoi(argv[2])); | |
format.setProfile(QSurfaceFormat::CoreProfile); | |
QSurfaceFormat::setDefaultFormat(format); | |
Application app(argc, argv); | |
return app.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _MAIN_H | |
#define _MAIN_H | |
#include <QApplication> | |
#include <QMainWindow> | |
#include <QOpenGLWidget> | |
class OpenGLWidget: public QOpenGLWidget { | |
private: | |
public: | |
OpenGLWidget(); | |
virtual ~OpenGLWidget() override; | |
virtual void initializeGL() override; | |
}; | |
class MainWindow: | |
public QMainWindow { | |
Q_OBJECT | |
private: | |
OpenGLWidget widget_; | |
public: | |
MainWindow(); | |
virtual ~MainWindow() override; | |
}; | |
class Application: public QApplication { | |
Q_OBJECT | |
private: | |
MainWindow main_; | |
public: | |
Application(int& argc, char **argv); | |
virtual ~Application() override; | |
}; | |
#endif // _MAIN_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment