Embed Qtk widget into QMainWindow application

+ Fix keyboard input focus to bind on mouse click
+ Fix missing call to `setFormat` in `MainWidget` ctor
+ Add placeholder toolbar options
This commit is contained in:
Shaun Reed 2022-03-13 18:00:00 -04:00
parent 2ed1dc800f
commit 3497b97f6e
9 changed files with 204 additions and 89 deletions

View File

@ -48,82 +48,33 @@ find_package(assimp REQUIRED)
################################################################################ ################################################################################
# Mainwidget # Mainwidget
add_library(main-widget src/mainwidget.cpp src/mainwidget.h) include(GenerateExportHeader)
add_library(main-widget SHARED
src/mainwidget.cpp src/mainwidget.h
src/mainwindow.cpp src/mainwindow.h src/mainwindow.ui
src/input.cpp src/input.h
src/mesh.cpp src/mesh.h
src/texture.cpp src/texture.h
src/object.cpp src/object.h
src/meshrenderer.cpp src/meshrenderer.h
src/camera3d.cpp src/camera3d.h
src/skybox.cpp src/skybox.h
src/transform3D.cpp src/transform3D.h
src/model.cpp src/model.h
src/scene.cpp src/scene.h
)
target_include_directories(main-widget PUBLIC src/) target_include_directories(main-widget PUBLIC src/)
target_include_directories(main-widget PRIVATE ${OPENGL_INCLUDE_DIR}) target_include_directories(main-widget PRIVATE ${OPENGL_INCLUDE_DIR})
target_link_libraries(main-widget PRIVATE ${OPENGL_LIBRARIES}) target_link_libraries(main-widget PRIVATE ${OPENGL_LIBRARIES})
target_link_libraries(main-widget PRIVATE assimp)
# Input target_link_libraries(main-widget PUBLIC Qt${QT_VERSION_MAJOR}::Widgets)
add_library(input src/input.cpp src/input.h) target_link_libraries(main-widget PUBLIC Qt${QT_VERSION_MAJOR}::OpenGLWidgets)
target_include_directories(input PUBLIC src/)
target_link_libraries(input PUBLIC Qt${QT_VERSION_MAJOR}::Widgets)
# Mesh
add_library(mesh src/mesh.cpp src/mesh.h)
target_include_directories(mesh PUBLIC src/)
target_link_libraries(mesh PUBLIC Qt${QT_VERSION_MAJOR}::OpenGLWidgets)
# Texture
add_library(texture src/texture.cpp src/texture.h)
target_include_directories(texture PUBLIC src/)
target_link_libraries(texture PUBLIC Qt${QT_VERSION_MAJOR}::OpenGLWidgets)
# Object
add_library(object src/object.cpp src/object.h)
target_include_directories(object PUBLIC src/)
target_link_libraries(object INTERFACE mesh)
target_link_libraries(object PUBLIC Qt${QT_VERSION_MAJOR}::OpenGL)
target_link_libraries(object PUBLIC Qt${QT_VERSION_MAJOR}::OpenGLWidgets)
# MeshRenderer
add_library(meshrenderer src/meshrenderer.cpp src/meshrenderer.h)
target_include_directories(meshrenderer PUBLIC src/)
target_link_libraries(meshrenderer PUBLIC object)
# Camera3D
add_library(camera3d src/camera3d.cpp src/camera3d.h)
target_include_directories(camera3d PUBLIC src/)
target_link_libraries(camera3d INTERFACE input)
target_link_libraries(camera3d PUBLIC Qt${QT_VERSION_MAJOR}::Widgets)
# Skybox
add_library(skybox src/skybox.cpp src/skybox.h)
# Skybox needs Mesh, Camera3D, and Qt6::Widgets
target_link_libraries(skybox PRIVATE mesh)
target_link_libraries(skybox PRIVATE camera3d)
target_link_libraries(skybox PUBLIC Qt${QT_VERSION_MAJOR}::OpenGLWidgets)
# Transform3D
add_library(transform3d src/transform3D.cpp src/transform3D.h)
target_include_directories(transform3d PUBLIC src/)
target_link_libraries(transform3d PUBLIC Qt${QT_VERSION_MAJOR}::Widgets)
# Model
add_library(model src/model.cpp src/model.h)
target_include_directories(model PUBLIC src/)
target_link_libraries(model PRIVATE assimp)
target_link_libraries(model PUBLIC Qt${QT_VERSION_MAJOR}::OpenGL)
# Model library requires transform3d and Qt6::Widgets
target_link_libraries(model PUBLIC transform3d)
target_link_libraries(model PUBLIC Qt${QT_VERSION_MAJOR}::OpenGLWidgets)
# Scene
add_library(scene src/scene.cpp src/scene.h)
target_include_directories(scene PUBLIC src/)
target_link_libraries(scene PUBLIC model)
target_link_libraries(scene PUBLIC meshrenderer)
target_link_libraries(scene PUBLIC skybox)
target_link_libraries(scene PUBLIC texture)
target_link_libraries(scene PUBLIC Qt${QT_VERSION_MAJOR}::OpenGLWidgets)
################################################################################ ################################################################################
# Final Application # Final Application
################################################################################ ################################################################################
target_link_libraries(main-widget PRIVATE mesh)
target_link_libraries(main-widget PUBLIC scene)
# Link qtk executable to main main-widget library # Link qtk executable to main main-widget library
target_link_libraries(qtk PUBLIC main-widget) target_link_libraries(qtk PUBLIC main-widget)
@ -131,3 +82,5 @@ set_target_properties(qtk PROPERTIES
WIN32_EXECUTABLE TRUE WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE MACOSX_BUNDLE TRUE
) )
generate_export_header(main-widget)

View File

@ -10,6 +10,7 @@
#include <QLabel> #include <QLabel>
#include <mainwidget.h> #include <mainwidget.h>
#include <mainwindow.h>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -24,14 +25,15 @@ int main(int argc, char *argv[])
format.setVersion(4,5); format.setVersion(4,5);
// Set the number of samples used for glEnable(GL_MULTISAMPLING) // Set the number of samples used for glEnable(GL_MULTISAMPLING)
format.setSamples(4); format.setSamples(4);
// Set the size of teh depth bufer for glEnable(GL_DEPTH_TEST) // Set the size of the depth bufer for glEnable(GL_DEPTH_TEST)
format.setDepthBufferSize(16); format.setDepthBufferSize(16);
#ifdef QTK_DEBUG #ifdef QTK_DEBUG
format.setOption(QSurfaceFormat::DebugContext); format.setOption(QSurfaceFormat::DebugContext);
#endif // QTK_DEBUG #endif // QTK_DEBUG
// Set the widget up using a custom format // Create window for Qt application using custom mainwindow.h
MainWidget widget(format); MainWindow w;
w.show();
return a.exec(); return a.exec();
} }

BIN
resources/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

View File

@ -22,23 +22,20 @@
MainWidget::MainWidget() : mDebugLogger(Q_NULLPTR) MainWidget::MainWidget() : mDebugLogger(Q_NULLPTR)
{ {
QSurfaceFormat format; initializeWidget();
format.setRenderableType(QSurfaceFormat::OpenGL); }
format.setProfile(QSurfaceFormat::CoreProfile);
format.setVersion(4, 5); // Constructor for using this widget in QtDesigner
format.setDepthBufferSize(16); MainWidget::MainWidget(QWidget *parent) : QOpenGLWidget(parent), mDebugLogger(Q_NULLPTR)
// If QTK_DEBUG is set, enable debug context {
#ifdef QTK_DEBUG initializeWidget();
format.setOption(QSurfaceFormat::DebugContext);
#endif
} }
MainWidget::MainWidget(const QSurfaceFormat &format) MainWidget::MainWidget(const QSurfaceFormat &format)
: mDebugLogger(Q_NULLPTR) : mDebugLogger(Q_NULLPTR)
{ {
setFormat(format); setFormat(format);
resize(QSize(800, 600)); setFocusPolicy(Qt::ClickFocus);
show();
} }
MainWidget::~MainWidget() MainWidget::~MainWidget()
@ -89,9 +86,9 @@ void MainWidget::initObjects()
mObject->mProgram.enableAttributeArray(0); mObject->mProgram.enableAttributeArray(0);
mObject->mProgram.setAttributeBuffer(0, GL_FLOAT, 0, mObject->mProgram.setAttributeBuffer(0, GL_FLOAT, 0,
3, sizeof(mObject->vertices()[0])); 3, sizeof(mObject->vertices()[0]));
mObject->mProgram.setUniformValue("uColor", QVector3D(0.0f, 0.25f, 0.0f)); mObject->mProgram.setUniformValue("uColor", QVector3D(1.0f, 0.0f, 0.0f));
mObject->mProgram.setUniformValue("uLightColor", WHITE); mObject->mProgram.setUniformValue("uLightColor", WHITE);
mObject->mProgram.setUniformValue("uAmbientStrength", 0.2f); mObject->mProgram.setUniformValue("uAmbientStrength", 0.75f);
mObject->mVBO.release(); mObject->mVBO.release();
mObject->mVAO.release(); mObject->mVAO.release();
@ -248,6 +245,7 @@ void MainWidget::messageLogged(const QOpenGLDebugMessage &msg)
void MainWidget::keyPressEvent(QKeyEvent *event) void MainWidget::keyPressEvent(QKeyEvent *event)
{ {
if (event->isAutoRepeat()) { if (event->isAutoRepeat()) {
// Do not repeat input while a key is held down
event->ignore(); event->ignore();
} else { } else {
Input::registerKeyPress(event->key()); Input::registerKeyPress(event->key());
@ -278,6 +276,24 @@ void MainWidget::mouseReleaseEvent(QMouseEvent *event)
* Private Helpers * Private Helpers
******************************************************************************/ ******************************************************************************/
void MainWidget::initializeWidget()
{
QSurfaceFormat format;
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setVersion(4, 6);
// Set the number of samples used for glEnable(GL_MULTISAMPLING)
format.setSamples(4);
// Set the size of the depth bufer for glEnable(GL_DEPTH_TEST)
format.setDepthBufferSize(16);
// If QTK_DEBUG is set, enable debug context
#ifdef QTK_DEBUG
format.setOption(QSurfaceFormat::DebugContext);
#endif
setFormat(format);
setFocusPolicy(Qt::ClickFocus);
}
void MainWidget::printContextInformation() void MainWidget::printContextInformation()
{ {
QString glType; QString glType;

View File

@ -31,6 +31,7 @@ Q_OBJECT;
public: public:
// Constructors // Constructors
MainWidget(); MainWidget();
explicit MainWidget(QWidget *parent);
explicit MainWidget(const QSurfaceFormat &format); explicit MainWidget(const QSurfaceFormat &format);
~MainWidget() override; ~MainWidget() override;
@ -57,6 +58,7 @@ protected:
private: private:
// Private helpers // Private helpers
void initializeWidget();
void printContextInformation(); void printContextInformation();
void updateCameraInput(); void updateCameraInput();

15
src/mainwindow.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowIcon(QIcon("../resources/icon.png"));
}
MainWindow::~MainWindow()
{
delete ui;
}

24
src/mainwindow.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "main-widget_export.h"
namespace Ui {
class MainWindow;
}
class MAIN_WIDGET_EXPORT MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

104
src/mainwindow.ui Normal file
View File

@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Qtk - MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="MainWidget" name="openGLWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>775</width>
<height>550</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menuTest">
<property name="title">
<string>File</string>
</property>
<addaction name="actionOpen"/>
<addaction name="actionSave_2"/>
<addaction name="actionSave_as"/>
<addaction name="actionExit"/>
</widget>
<widget class="QMenu" name="menuView">
<property name="title">
<string>View</string>
</property>
<addaction name="actionShow_Console"/>
</widget>
<addaction name="menuTest"/>
<addaction name="menuView"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionOtherTest">
<property name="text">
<string>Save</string>
</property>
</action>
<action name="actionSave">
<property name="text">
<string>Save</string>
</property>
</action>
<action name="actionOpen">
<property name="text">
<string>Open...</string>
</property>
</action>
<action name="actionSave_2">
<property name="text">
<string>Save</string>
</property>
</action>
<action name="actionSave_as">
<property name="text">
<string>Save as...</string>
</property>
</action>
<action name="actionExit">
<property name="text">
<string>Exit</string>
</property>
</action>
<action name="actionShow_Console">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Show Console</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
<class>MainWidget</class>
<extends>QOpenGLWidget</extends>
<header>mainwidget.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -24,6 +24,9 @@ QMatrix4x4 Scene::mProjection;
Scene::Scene() Scene::Scene()
{ {
mCamera.transform().setTranslation(0.0f, 0.0f, 20.0f);
mCamera.transform().setRotation(-5.0f, 0.0f, 1.0f, 0.0f);
init(); init();
} }
@ -44,10 +47,6 @@ Scene::~Scene()
void Scene::init() void Scene::init()
{ {
mCamera.transform().setTranslation(0.0f, 0.0f, 5.0f);
mCamera.transform().setRotation(180.0f, 0.0f, 1.0f, 0.0f);
// Initialize Phong example cube // Initialize Phong example cube
mTestPhong = new MeshRenderer("phong", Cube()); mTestPhong = new MeshRenderer("phong", Cube());
mTestPhong->mTransform.setTranslation(3.0f, 0.0f, -2.0f); mTestPhong->mTransform.setTranslation(3.0f, 0.0f, -2.0f);