Fix integration with Qt Creator

Add cmake build options in format of `QTK_*`
+ `QTK_UPDATE_SUBMODULES` to include Assimp as a git submodule
+ `QTK_BUILD_SHARED_LIBS` to toggle building Qtk as Shared / Static
This commit is contained in:
Shaun Reed 2022-08-21 15:37:42 -04:00
parent 6cdf906065
commit 83756327ce
14 changed files with 162 additions and 82 deletions

View File

@ -22,19 +22,32 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
add_compile_options(/wd4131 /wd4127)
endif()
message(STATUS "[Qtk]: Compiling with ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "[Qtk] Compiling with ${CMAKE_CXX_COMPILER_ID}")
# Qtk build options
option(QTK_DEBUG "Enable debugger" ON)
option(BUILD_SHARED_LIBS "Build shared library" ON)
message(STATUS "[Qtk] Compiling with QTK_DEBUG=${QTK_DEBUG}")
option(QTK_BUILD_SHARED_LIBS "Build shared library" ON)
message(
STATUS
"[Qtk] Compiling with QTK_BUILD_SHARED_LIBS=${QTK_BUILD_SHARED_LIBS}"
)
option(QTK_UPDATE_SUBMODULES "Update external project (assimp) submodule" ON)
message(
STATUS
"[Qtk] Compiling with QTK_UPDATE_SUBMODULES=${QTK_UPDATE_SUBMODULES}"
)
# Qt options
set(QT_DIR "$ENV{HOME}/Code/Clones/Qt/6.3.1/gcc_64/" CACHE PATH "Path to Qt6")
# Options for bringing your own assimp installation; Otherwise not needed
# + If assimp is available system-wide we can just set UPDATE_SUBMODULES OFF
option(UPDATE_SUBMODULES "Update external project (assimp) git submodule" ON)
# + If assimp is available system-wide we can just set QTK_UPDATE_SUBMODULES OFF
option(ASSIMP_NEW_INTERFACE "Use the assimp::assimp interface (WIN / OSX)" OFF)
message(
STATUS
"[Qtk] Compiling with ASSIMP_NEW_INTERFACE=${ASSIMP_NEW_INTERFACE}"
)
################################################################################
# External Libraries
@ -47,15 +60,23 @@ list(APPEND CMAKE_PREFIX_PATH "${QT_DIR}")
# Find Qt
find_package(Qt6 COMPONENTS OpenGLWidgets)
if (NOT Qt6_FOUND)
message(SEND_ERROR "[Qtk] Error: Unable to find Qt6 at CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
message(FATAL_ERROR "[Qtk] Error: Specify path to Qt6 with `cmake -DCMAKE_PREFIX_PATH=/path/to/Qt/6.x.x/gcc_64 -S /path/to/qtk -B /path/to/qtk/build && cmake --build /path/to/qtk/build -j $(nprocs)`")
message(
SEND_ERROR "[Qtk] Error: Unable to find Qt6 at CMAKE_PREFIX_PATH: "
"${CMAKE_PREFIX_PATH}"
)
message(
FATAL_ERROR
"[Qtk] Error: Specify path to Qt6 with `cmake "
"-DCMAKE_PREFIX_PATH=/path/to/Qt/6.x.x/gcc_64 -S /path/to/qtk -B "
"/path/to/qtk/build && cmake --build /path/to/qtk/build -j $(nprocs)`"
)
endif()
if (UPDATE_SUBMODULES)
message(STATUS "[Qtk]: Updating submodules...")
if (QTK_UPDATE_SUBMODULES)
message(STATUS "[Qtk] Updating submodules...")
include("${CMAKE_SOURCE_DIR}/cmake/include/git_submodule.cmake")
submodule_update(extern/assimp/assimp/)
add_subdirectory(extern/assimp/assimp)
submodule_update("${CMAKE_CURRENT_SOURCE_DIR}/extern/assimp/assimp/")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/extern/assimp/assimp/")
endif()
@ -65,7 +86,7 @@ endif()
set(
PUBLIC_HEADERS
src/mainwidget.h
src/qtkwidget.h
src/abstractscene.h
src/camera3d.h
src/mesh.h
@ -79,7 +100,7 @@ set(
set(
SOURCE_FILES
src/mainwidget.cpp
src/qtkwidget.cpp
src/abstractscene.cpp
src/camera3d.cpp
src/input.cpp
@ -95,7 +116,11 @@ set(
)
include(GenerateExportHeader)
qt_add_library(qtk-widget STATIC ${PUBLIC_HEADERS} ${SOURCE_FILES})
if (QTK_BUILD_SHARED_LIBS)
add_library(qtk-widget SHARED ${PUBLIC_HEADERS} ${SOURCE_FILES})
else()
add_library(qtk-widget STATIC ${PUBLIC_HEADERS} ${SOURCE_FILES})
endif()
target_include_directories(qtk-widget PRIVATE src/ app/)
set_target_properties(qtk-widget PROPERTIES
@ -104,15 +129,16 @@ set_target_properties(qtk-widget PROPERTIES
)
target_link_libraries(qtk-widget PUBLIC Qt6::OpenGLWidgets)
target_link_libraries(qtk-widget PUBLIC Qt6::Widgets)
if (UPDATE_SUBMODULES OR NOT ASSIMP_NEW_INTERFACE)
if (QTK_UPDATE_SUBMODULES OR NOT ASSIMP_NEW_INTERFACE)
target_link_libraries(qtk-widget PUBLIC assimp)
elseif(ASSIMP_NEW_INTERFACE)
target_link_libraries(qtk-widget PUBLIC assimp::assimp)
endif()
if(QTK_DEBUG)
message(STATUS "[Qtk]: Building with QTK_DEBUG=${QTK_DEBUG}")
message(STATUS "[Qtk] Building with QTK_DEBUG=${QTK_DEBUG}")
target_compile_definitions(qtk-widget PUBLIC QTK_DEBUG)
endif()
@ -122,7 +148,6 @@ if(WIN32)
target_link_libraries(qtk-widget PUBLIC OpenGL::GL)
endif()
generate_export_header(qtk-widget)
# Install files
install(TARGETS qtk-widget
# Associate qtk-widget target with qtk-export
@ -156,8 +181,8 @@ configure_file(
# Add our Qt resources.qrc file to our application
set(QTK_APP_SOURCES app/main.cpp
app/examplescene.cpp app/examplescene.h
app/mainwindow.cpp app/mainwindow.h app/mainwindow.ui
app/scene.cpp app/scene.h
app/resourcemanager.h
src/qtkresources.h.in
)
@ -167,7 +192,7 @@ qt_add_executable(qtk-main ${QTK_APP_SOURCES})
target_include_directories(qtk-main PRIVATE src/ app/)
# Link qtk-main executable to main qtk-widget library
target_link_libraries(qtk-main PRIVATE qtk-widget)
target_link_libraries(qtk-main PUBLIC qtk-widget)
set_target_properties(qtk-main PROPERTIES
WIN32_EXECUTABLE TRUE
@ -180,6 +205,7 @@ install(TARGETS qtk-main
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
generate_export_header(qtk-widget)
if(WIN32)
get_target_property(_qt6_qmake_location Qt6::qmake IMPORTED_LOCATION)
execute_process(COMMAND "${_qt6_qmake_location}" -query QT_INSTALL_PREFIX RESULT_VARIABLE return_code OUTPUT_VARIABLE qt6_install_prefix OUTPUT_STRIP_TRAILING_WHITESPACE)

View File

@ -3,18 +3,18 @@
Practice project for learning about using OpenGL in Qt widget applications.
Model loader using [Assimp](https://assimp.org/) within a Qt widget application.
You can import your own models within `mainwdget.cpp`, inside the
`MainWidget::initObjects()` function. I've commented throughout the code there
to explain which model or example I'm modifying. Rotations and translations
happen in `MainWidget::update()`, to get textures loading on models look into
[material files](http://www.paulbourke.net/dataformats/mtl/) and see some
examples in the `resources/models/` directory. For more in-depth examples, see
`scene.h` and `scene.cpp`
You can import your own models within `app/examplescene.cpp`, inside the
`ExampleScene::init()` function. Rotations and translations
happen in `ExampleScene::update()`.
Can be built with cmake manually or using
[Qt Creator](https://github.com/qt-creator/qt-creator).
For the build to be successful, I've found through testing on VMs that the system requires around 6GB of RAM.
This is mostly due to the large .obj files that are built into the project using [Qt Resource System](https://doc.qt.io/qt-6/resources.html)
To get textures loading on models look into [material files](http://www.paulbourke.net/dataformats/mtl/)
and see some examples in the `resources/models/` directory.
### Building
Builds are configured for CLion or [Qt Creator](https://github.com/qt-creator/qt-creator).
Simply open the root `CMakeLists.txt` with either of these editors and configurations will be loaded.
This project has been ported to Qt6, which is not yet available in Ubuntu apt repositories.
To run this project, you will *need* to install [Qt6 Open Source Binaries](https://www.qt.io/download-qt-installer) for your system.
@ -24,8 +24,21 @@ Once Qt6 is installed, to build and run `qtk` on Ubuntu -
```bash
sudo apt update -y && sudo apt install freeglut3-dev libassimp-dev cmake build-essential git
git clone https://gitlab.com/shaunrd0/qtk
cmake -DCMAKE_PREFIX_PATH=$HOME/Qt/6.3.1/gcc_64 -S qtk/ -B qtk/build/ && cmake --build qtk/build/ -j $(nprocs)
./qtk/build/qtk
cmake -DCMAKE_PREFIX_PATH=$HOME/Qt/6.3.1/gcc_64 -S qtk/ -B qtk/build/ && cmake --build qtk/build/ -j $(nproc --ignore=2) --target qtk-main
./qtk/build/qtk-main
```
By default, the build will initialize Assimp as a git submodule and build from source.
We can turn this off by setting the `-DQTK_UPDATE_SUBMODULES=OFF` flag when running CMake.
This will greatly increase build speed, but we will need to make sure Assimp is available either system-wide or using a custom `CMAKE_PREFIX_PATH`.
Using `-DQTK_UPDATE_SUBMODULES=ON` supports providing assimp on cross-platform builds (Windows / Mac / Linux) and may be easier to configure.
```bash
sudo apt update -y && sudo apt install freeglut3-dev libassimp-dev cmake build-essential git
git clone https://gitlab.com/shaunrd0/qtk
cmake -DQTK_UPDATE_SUBMODULES=OFF -DCMAKE_PREFIX_PATH=$HOME/Qt/6.3.1/gcc_64 -S qtk/ -B qtk/build/ && cmake --build qtk/build/ -j $(nproc --ignore=2) --target qtk-main
#cmake -DQTK_UPDATE_SUBMODULES=OFF -DCMAKE_PREFIX_PATH=$HOME/Qt/6.3.1/gcc_64;/path/to/assimp/dir -S qtk/ -B qtk/build/ && cmake --build qtk/build/ -j $(nproc --ignore=2) --target qtk-main
./qtk/build/qtk-main
```
You can fly around the scene if you hold the right mouse button and use WASD.
@ -43,8 +56,30 @@ Spartan with normals -
![](resources/spartan-normals.png)
## Model Artists
### QtkWidget in Qt Creator
The `QtkWidget` class is exported as a shared library for use in Qt Creator's design mode.
We can add more QtkWidgets to view and render the scene from multiple perspectives.
There is still some work to be done here, so there isn't a builtin way to add an additional view within the application.
![](resources/qtk-views.png)
After building Qtk, we can drag and drop an `OpenGL Widget` onto the `mainwindow.ui`.
Then right-click the new OpenGLWidget and `Promote To->QtkWidget` to add a second view.
![](resources/qtk-views-setup.png)
If we demote or delete all widgets in `mainwindow.ui` and rebuild the project, Qt Creator will drop `QtkWidget` from the list of possible promoted widgets.
Add an `OpenGL Widget` to the UI, right-click it and navigate to `Promote Widget...` and enter the information below.
![](resources/qtk-reference.png)
After you fill out the `New Promoted Class` form, click `Add` *and*`Promote`, then rebuild.
After following these steps Qt Creator will list `QtkWidget` as an option to promote `OpenGL Widgets` again.
## Model Artists
"Alien Hominid" (https://skfb.ly/onStx) by Nwilly_art is licensed under Creative Commons Attribution (http://creativecommons.org/licenses/by/4.0/).
@ -59,4 +94,3 @@ Modified (learnopengl.com) material assignment (Joey de Vries) for easier load i
"Terror-bird (NHMW-Geo 2012/0007/0001)" (https://skfb.ly/onAWy) by Natural History Museum Vienna is licensed under Creative Commons Attribution-NonCommercial (http://creativecommons.org/licenses/by-nc/4.0/).
"Golden Lion Sitting OBJ Low Poly FREE" (https://skfb.ly/onZAH) by LordSamueliSolo is licensed under Creative Commons Attribution (http://creativecommons.org/licenses/by/4.0/).

View File

@ -11,7 +11,7 @@
#include <meshrenderer.h>
#include <model.h>
#include <resourcemanager.h>
#include <scene.h>
#include <examplescene.h>
#include <texture.h>
using namespace Qtk;

View File

@ -17,7 +17,6 @@
class ExampleScene : public Qtk::Scene {
public:
ExampleScene();
~ExampleScene();

View File

@ -9,7 +9,7 @@
#include <QApplication>
#include <QLabel>
#include <mainwidget.h>
#include <qtkwidget.h>
#include <mainwindow.h>

View File

@ -1,13 +1,24 @@
#include <mainwindow.h>
#include <ui_mainwindow.h>
#include <qtkwidget.h>
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mScene = new ExampleScene();
ui->openGLWidget->setScene(mScene);
// For use in design mode using Qt Creator
// + We can use the `ui` member to access nested widgets by name
for (const auto widget : ui->qWidget->children()) {
auto qtkWidget = dynamic_cast<Qtk::QtkWidget*>(widget);
if (qtkWidget != nullptr) {
std::string key = qtkWidget->objectName().toStdString();
if (mScenes[key] == nullptr) {
mScenes[qtkWidget->objectName().toStdString()] = new ExampleScene();
}
qtkWidget->setScene(mScenes[qtkWidget->objectName().toStdString()]);
}
}
setWindowIcon(QIcon("../resources/icon.png"));
}

View File

@ -3,8 +3,8 @@
#include <QMainWindow>
#include <qtk-widget_export.h>
#include <scene.h>
#include "qtk-widget_export.h"
#include <examplescene.h>
namespace Ui {
@ -21,7 +21,7 @@ public:
private:
Ui::MainWindow *ui;
Qtk::Scene * mScene;
std::unordered_map<std::string, Qtk::Scene*> mScenes;
};
#endif // MAINWINDOW_H

View File

@ -14,15 +14,25 @@
<string>Qtk - MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="Qtk::MainWidget" name="openGLWidget" native="true">
<widget class="QWidget" name="qWidget" native="true">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>775</width>
<height>550</height>
<x>0</x>
<y>0</y>
<width>801</width>
<height>561</height>
</rect>
</property>
<widget class="Qtk::QtkWidget" name="openGLWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>781</width>
<height>541</height>
</rect>
</property>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
@ -94,9 +104,9 @@
</widget>
<customwidgets>
<customwidget>
<class>Qtk::MainWidget</class>
<extends>QWidget</extends>
<header>mainwidget.h</header>
<class>Qtk::QtkWidget</class>
<extends>QOpenGLWidget</extends>
<header>qtkwidget.h</header>
</customwidget>
</customwidgets>
<resources/>

BIN
resources/qtk-reference.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

BIN
resources/qtk-views.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 263 KiB

View File

@ -13,10 +13,11 @@
#include <Qt>
#include <qtkapi.h>
#include <qtkwidget.h>
namespace Qtk {
class QTKAPI Input {
friend class MainWidget;
friend class Qtk::QtkWidget;
public:
// Possible key states
enum InputState

View File

@ -9,7 +9,7 @@
#include <QKeyEvent>
#include <input.h>
#include <mainwidget.h>
#include <qtkwidget.h>
#include <mesh.h>
#include <abstractscene.h>
@ -19,26 +19,26 @@ using namespace Qtk;
* Constructors, Destructors
******************************************************************************/
MainWidget::MainWidget() : mScene(Q_NULLPTR), mDebugLogger(Q_NULLPTR)
QtkWidget::QtkWidget() : mScene(Q_NULLPTR), mDebugLogger(Q_NULLPTR)
{
initializeWidget();
}
// Constructor for using this widget in QtDesigner
MainWidget::MainWidget(QWidget *parent) : QOpenGLWidget(parent),
QtkWidget::QtkWidget(QWidget *parent) : QOpenGLWidget(parent),
mScene(Q_NULLPTR), mDebugLogger(Q_NULLPTR)
{
initializeWidget();
}
MainWidget::MainWidget(const QSurfaceFormat &format)
QtkWidget::QtkWidget(const QSurfaceFormat &format)
: mScene(Q_NULLPTR), mDebugLogger(Q_NULLPTR)
{
setFormat(format);
setFocusPolicy(Qt::ClickFocus);
}
MainWidget::~MainWidget()
QtkWidget::~QtkWidget()
{
makeCurrent();
teardownGL();
@ -49,7 +49,7 @@ MainWidget::~MainWidget()
* Private Member Functions
******************************************************************************/
void MainWidget::teardownGL()
void QtkWidget::teardownGL()
{
// Nothing to teardown yet...
}
@ -59,7 +59,7 @@ void MainWidget::teardownGL()
* Inherited Virtual Member Functions
******************************************************************************/
void MainWidget::paintGL()
void QtkWidget::paintGL()
{
// Clear buffers
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
@ -68,7 +68,7 @@ void MainWidget::paintGL()
if (mScene != Q_NULLPTR) mScene->draw();
}
void MainWidget::initializeGL()
void QtkWidget::initializeGL()
{
initializeOpenGLFunctions();
// Connect the frameSwapped signal to call the update() function
@ -98,7 +98,7 @@ void MainWidget::initializeGL()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void MainWidget::resizeGL(int width, int height)
void QtkWidget::resizeGL(int width, int height)
{
Scene::Projection().setToIdentity();
Scene::Projection().perspective(45.0f,
@ -111,7 +111,7 @@ void MainWidget::resizeGL(int width, int height)
* Protected Slots
******************************************************************************/
void MainWidget::update()
void QtkWidget::update()
{
updateCameraInput();
@ -120,7 +120,7 @@ void MainWidget::update()
QWidget::update();
}
void MainWidget::messageLogged(const QOpenGLDebugMessage &msg)
void QtkWidget::messageLogged(const QOpenGLDebugMessage &msg)
{
QString error;
@ -185,7 +185,7 @@ void MainWidget::messageLogged(const QOpenGLDebugMessage &msg)
* Protected Helpers
******************************************************************************/
void MainWidget::keyPressEvent(QKeyEvent *event)
void QtkWidget::keyPressEvent(QKeyEvent *event)
{
if (event->isAutoRepeat()) {
// Do not repeat input while a key is held down
@ -195,7 +195,7 @@ void MainWidget::keyPressEvent(QKeyEvent *event)
}
}
void MainWidget::keyReleaseEvent(QKeyEvent *event)
void QtkWidget::keyReleaseEvent(QKeyEvent *event)
{
if (event->isAutoRepeat()) {
event->ignore();
@ -204,12 +204,12 @@ void MainWidget::keyReleaseEvent(QKeyEvent *event)
}
}
void MainWidget::mousePressEvent(QMouseEvent *event)
void QtkWidget::mousePressEvent(QMouseEvent *event)
{
Input::registerMousePress(event->button());
}
void MainWidget::mouseReleaseEvent(QMouseEvent *event)
void QtkWidget::mouseReleaseEvent(QMouseEvent *event)
{
Input::registerMouseRelease(event->button());
}
@ -219,7 +219,7 @@ void MainWidget::mouseReleaseEvent(QMouseEvent *event)
* Private Helpers
******************************************************************************/
void MainWidget::initializeWidget()
void QtkWidget::initializeWidget()
{
QSurfaceFormat format;
format.setRenderableType(QSurfaceFormat::OpenGL);
@ -237,7 +237,7 @@ void MainWidget::initializeWidget()
setFocusPolicy(Qt::ClickFocus);
}
void MainWidget::printContextInformation()
void QtkWidget::printContextInformation()
{
QString glType;
QString glVersion;
@ -273,7 +273,7 @@ void MainWidget::printContextInformation()
}
void MainWidget::updateCameraInput()
void QtkWidget::updateCameraInput()
{
Input::update();
// Camera Transformation

View File

@ -5,8 +5,8 @@
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#ifndef QTK_MAINWIDGET_H
#define QTK_MAINWIDGET_H
#ifndef QTK_QTKWIDGET_H
#define QTK_QTKWIDGET_H
#include <iostream>
@ -18,18 +18,17 @@
#include <qtkapi.h>
#include <abstractscene.h>
namespace Qtk {
class QTKAPI MainWidget : public QOpenGLWidget,
protected QOpenGLFunctions {
class QTKAPI QtkWidget : public QOpenGLWidget,
protected QOpenGLFunctions {
Q_OBJECT;
public:
// Constructors
MainWidget();
explicit MainWidget(QWidget *parent);
explicit MainWidget(const QSurfaceFormat &format);
~MainWidget() override;
QtkWidget();
explicit QtkWidget(QWidget *parent);
explicit QtkWidget(const QSurfaceFormat &format);
~QtkWidget() override;
private:
void teardownGL();
@ -40,8 +39,8 @@ namespace Qtk {
void initializeGL() override;
void resizeGL(int width, int height) override;
inline Scene * getScene() {return mScene;}
inline void setScene(Scene * scene) {
inline Qtk::Scene * getScene() {return mScene;}
inline void setScene(Qtk::Scene * scene) {
if (mScene != Q_NULLPTR) delete mScene;
mScene = scene;
}
@ -64,7 +63,7 @@ namespace Qtk {
void initializeWidget();
void updateCameraInput();
Scene * mScene;
Qtk::Scene * mScene;
#ifdef QTK_DEBUG
void printContextInformation();
QOpenGLDebugLogger * mDebugLogger;
@ -72,4 +71,4 @@ namespace Qtk {
};
}
#endif // QTK_MAINWIDGET_H
#endif // QTK_QTKWIDGET_H