Skip to content

Add open gl example and fix #717 #727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -385,5 +385,5 @@ MigrationBackup/
FodyWeavers.xsd
/ build
/Settings.ini
.vscode/settings.json
.vscode/
/.settings
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ add_subdirectory(autohide)
add_subdirectory(autohidedragndrop)
add_subdirectory(emptydockarea)
add_subdirectory(dockindock)
add_subdirectory(openGL)
1 change: 1 addition & 0 deletions examples/examples.pro
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ SUBDIRS = \
autohidedragndrop \
centralwidget \
simple \
openGL \
hideshow \
sidebar \
deleteonclose \
Expand Down
135 changes: 135 additions & 0 deletions examples/openGL/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
cmake_minimum_required(VERSION 3.5)

project(OpenGLExample VERSION ${VERSION_SHORT})

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

find_package(
QT NAMES Qt6 Qt5
COMPONENTS Core
Gui
Widgets
Charts
OpenGLWidgets
Quick
QuickWidgets
ShaderTools
REQUIRED)

find_package(
Qt${QT_VERSION_MAJOR} 5.5
COMPONENTS Core
Gui
Widgets
Charts
OpenGLWidgets
Quick
QuickWidgets
ShaderTools
REQUIRED)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

qt_add_executable(
${PROJECT_NAME}
WIN32
main.cpp
mainwindow.cpp
mainwindow.h
glwindow.cpp
glwindow.h
glwidget.h
glwidget.cpp
fbitem.cpp
fbitem.h
logo.cpp
logo.h)

target_include_directories(${PROJECT_NAME}
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../src")
target_link_libraries(${PROJECT_NAME}
PRIVATE qtadvanceddocking-qt${QT_VERSION_MAJOR})

qt_add_qml_module(
${PROJECT_NAME}
URI
fbitem
QML_FILES
"test.qml"
RESOURCE_PREFIX
/openGL
NO_RESOURCE_TARGET_PATH)

qt6_add_shaders(
${PROJECT_NAME}
"shaders"
PRECOMPILE
OPTIMIZED
PREFIX
"/openGL"
FILES
"wobble.frag")

# Resources:
set(resource_files "qtlogo.png")

qt_add_resources(${PROJECT_NAME} "OpenGLExample" PREFIX "/" FILES
${resource_files})

target_link_libraries(
${PROJECT_NAME}
PUBLIC Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Gui
Qt${QT_VERSION_MAJOR}::Widgets
Qt${QT_VERSION_MAJOR}::Charts
Qt${QT_VERSION_MAJOR}::OpenGLWidgets
Qt${QT_VERSION_MAJOR}::Quick
Qt${QT_VERSION_MAJOR}::QuickWidgets)

set_target_properties(
${PROJECT_NAME}
PROPERTIES AUTOMOC ON
AUTORCC ON
AUTOUIC ON
CXX_STANDARD 14
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
VERSION ${VERSION_SHORT}
EXPORT_NAME "Qt Advanced Docking System OpenGL Example"
ARCHIVE_OUTPUT_DIRECTORY
"${CMAKE_BINARY_DIR}/${ads_PlatformDir}/lib"
LIBRARY_OUTPUT_DIRECTORY
"${CMAKE_BINARY_DIR}/${ads_PlatformDir}/lib"
RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_BINARY_DIR}/${ads_PlatformDir}/bin")

# Define include directories
target_include_directories(
${PROJECT_NAME}
PRIVATE $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/examples/openGL>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${INSTALL_NAME}>)

install(
TARGETS ${PROJECT_NAME}
BUNDLE DESTINATION .
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

qt_generate_deploy_app_script(TARGET ${PROJECT_NAME} OUTPUT_SCRIPT
deploy_script NO_UNSUPPORTED_PLATFORM_ERROR)

install(SCRIPT ${deploy_script})

qt_generate_deploy_qml_app_script(
TARGET
${PROJECT_NAME}
OUTPUT_SCRIPT
qml_deploy_script
MACOS_BUNDLE_POST_BUILD
NO_UNSUPPORTED_PLATFORM_ERROR
DEPLOY_USER_QML_MODULES_ON_UNSUPPORTED_PLATFORM)

install(SCRIPT ${qml_deploy_script})
239 changes: 239 additions & 0 deletions examples/openGL/fbitem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "fbitem.h"
#include <QOpenGLFramebufferObject>
#include <QOpenGLFunctions>
#include <QMatrix4x4>

FbItem::FbItem(QQuickItem *parent)
: QQuickFramebufferObject(parent),
m_target(0, 0, -1),
m_syncState(AllNeedsSync),
m_multisample(false)
{
}

QQuickFramebufferObject::Renderer *FbItem::createRenderer() const
{
return new FbItemRenderer(m_multisample);
}

void FbItem::setEye(const QVector3D &v)
{
if (m_eye != v) {
m_eye = v;
m_syncState |= CameraNeedsSync;
update();
}
}

void FbItem::setTarget(const QVector3D &v)
{
if (m_target != v) {
m_target = v;
m_syncState |= CameraNeedsSync;
update();
}
}

void FbItem::setRotation(const QVector3D &v)
{
if (m_rotation != v) {
m_rotation = v;
m_syncState |= RotationNeedsSync;
update();
}
}

int FbItem::swapSyncState()
{
int s = m_syncState;
m_syncState = 0;
return s;
}

FbItemRenderer::FbItemRenderer(bool multisample)
: m_inited(false),
m_multisample(multisample),
m_dirty(DirtyAll)
{
m_camera.setToIdentity();
m_baseWorld.setToIdentity();
m_baseWorld.translate(0, 0, -1);
m_world = m_baseWorld;
}

void FbItemRenderer::synchronize(QQuickFramebufferObject *qfbitem)
{
FbItem *item = static_cast<FbItem *>(qfbitem);
int syncState = item->swapSyncState();
if (syncState & FbItem::CameraNeedsSync) {
m_camera.setToIdentity();
m_camera.lookAt(item->eye(), item->eye() + item->target(), QVector3D(0, 1, 0));
m_dirty |= DirtyCamera;
}
if (syncState & FbItem::RotationNeedsSync) {
m_rotation = item->rotation();
m_dirty |= DirtyWorld;
}
}

struct StateBinder
{
StateBinder(FbItemRenderer *r)
: m_r(r) {
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glEnable(GL_DEPTH_TEST);
f->glEnable(GL_CULL_FACE);
f->glDepthMask(GL_TRUE);
f->glDepthFunc(GL_LESS);
f->glFrontFace(GL_CCW);
f->glCullFace(GL_BACK);
m_r->m_program->bind();
}
~StateBinder() {
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
m_r->m_program->release();
f->glDisable(GL_CULL_FACE);
f->glDisable(GL_DEPTH_TEST);
}
FbItemRenderer *m_r;
};

void FbItemRenderer::updateDirtyUniforms()
{
if (m_dirty & DirtyProjection)
m_program->setUniformValue(m_projMatrixLoc, m_proj);

if (m_dirty & DirtyCamera)
m_program->setUniformValue(m_camMatrixLoc, m_camera);

if (m_dirty & DirtyWorld) {
m_program->setUniformValue(m_worldMatrixLoc, m_world);
QMatrix3x3 normalMatrix = m_world.normalMatrix();
m_program->setUniformValue(m_normalMatrixLoc, normalMatrix);
}

if (m_dirty & DirtyLight)
m_program->setUniformValue(m_lightPosLoc, QVector3D(0, 0, 70));

m_dirty = 0;
}

void FbItemRenderer::render()
{
ensureInit();

if (m_vao.isCreated())
m_vao.bind();
else
setupVertexAttribs();

StateBinder state(this);

QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glClearColor(0, 0, 0, 0);
f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (m_dirty & DirtyWorld) {
m_world = m_baseWorld;
m_world.rotate(m_rotation.x(), 1, 0, 0);
m_world.rotate(m_rotation.y(), 0, 1, 0);
m_world.rotate(m_rotation.z(), 0, 0, 1);
}

updateDirtyUniforms();

f->glDrawArrays(GL_TRIANGLES, 0, m_logo.vertexCount());

if (m_vao.isCreated())
m_vao.release();
}

QOpenGLFramebufferObject *FbItemRenderer::createFramebufferObject(const QSize &size)
{
m_dirty |= DirtyProjection;
m_proj.setToIdentity();
m_proj.perspective(45.0f, GLfloat(size.width()) / size.height(), 0.01f, 100.0f);

QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
format.setSamples(m_multisample ? 4 : 0);
return new QOpenGLFramebufferObject(size, format);
}

void FbItemRenderer::ensureInit()
{
if (m_inited)
return;

m_inited = true;

initBuf();
initProgram();
}

void FbItemRenderer::initBuf()
{
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);

m_logoVbo.create();
m_logoVbo.bind();
m_logoVbo.allocate(m_logo.constData(), m_logo.count() * sizeof(GLfloat));

setupVertexAttribs();
}

void FbItemRenderer::setupVertexAttribs()
{
m_logoVbo.bind();
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glEnableVertexAttribArray(0);
f->glEnableVertexAttribArray(1);
f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), nullptr);
f->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), reinterpret_cast<void *>(3 * sizeof(GLfloat)));
m_logoVbo.release();
}

static const char *vertexShaderSource =
"attribute vec4 vertex;\n"
"attribute vec3 normal;\n"
"varying vec3 vert;\n"
"varying vec3 vertNormal;\n"
"uniform mat4 projMatrix;\n"
"uniform mat4 camMatrix;\n"
"uniform mat4 worldMatrix;\n"
"uniform mat3 normalMatrix;\n"
"void main() {\n"
" vert = vertex.xyz;\n"
" vertNormal = normalMatrix * normal;\n"
" gl_Position = projMatrix * camMatrix * worldMatrix * vertex;\n"
"}\n";

static const char *fragmentShaderSource =
"varying highp vec3 vert;\n"
"varying highp vec3 vertNormal;\n"
"uniform highp vec3 lightPos;\n"
"void main() {\n"
" highp vec3 L = normalize(lightPos - vert);\n"
" highp float NL = max(dot(normalize(vertNormal), L), 0.0);\n"
" highp vec3 color = vec3(0.39, 1.0, 0.0);\n"
" highp vec3 col = clamp(color * 0.2 + color * 0.8 * NL, 0.0, 1.0);\n"
" gl_FragColor = vec4(col, 1.0);\n"
"}\n";

void FbItemRenderer::initProgram()
{
m_program.reset(new QOpenGLShaderProgram);
m_program->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
m_program->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
m_program->bindAttributeLocation("vertex", 0);
m_program->bindAttributeLocation("normal", 1);
m_program->link();
m_projMatrixLoc = m_program->uniformLocation("projMatrix");
m_camMatrixLoc = m_program->uniformLocation("camMatrix");
m_worldMatrixLoc = m_program->uniformLocation("worldMatrix");
m_normalMatrixLoc = m_program->uniformLocation("normalMatrix");
m_lightPosLoc = m_program->uniformLocation("lightPos");
}
Loading