diff --git a/CMakeLists.txt b/CMakeLists.txt index 593611d..ca1b64c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,82 +48,33 @@ find_package(assimp REQUIRED) ################################################################################ # 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 PRIVATE ${OPENGL_INCLUDE_DIR}) target_link_libraries(main-widget PRIVATE ${OPENGL_LIBRARIES}) - -# Input -add_library(input src/input.cpp src/input.h) -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) - +target_link_libraries(main-widget PRIVATE assimp) +target_link_libraries(main-widget PUBLIC Qt${QT_VERSION_MAJOR}::Widgets) +target_link_libraries(main-widget PUBLIC Qt${QT_VERSION_MAJOR}::OpenGLWidgets) ################################################################################ # Final Application ################################################################################ -target_link_libraries(main-widget PRIVATE mesh) -target_link_libraries(main-widget PUBLIC scene) - # Link qtk executable to main main-widget library target_link_libraries(qtk PUBLIC main-widget) @@ -131,3 +82,5 @@ set_target_properties(qtk PROPERTIES WIN32_EXECUTABLE TRUE MACOSX_BUNDLE TRUE ) + +generate_export_header(main-widget) diff --git a/app/main.cpp b/app/main.cpp index 38e9b78..15f56c9 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -10,6 +10,7 @@ #include #include +#include int main(int argc, char *argv[]) @@ -24,14 +25,15 @@ int main(int argc, char *argv[]) format.setVersion(4,5); // Set the number of samples used for glEnable(GL_MULTISAMPLING) 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); #ifdef QTK_DEBUG format.setOption(QSurfaceFormat::DebugContext); #endif // QTK_DEBUG - // Set the widget up using a custom format - MainWidget widget(format); + // Create window for Qt application using custom mainwindow.h + MainWindow w; + w.show(); return a.exec(); } diff --git a/resources/icon.png b/resources/icon.png new file mode 100644 index 0000000..67d11c4 Binary files /dev/null and b/resources/icon.png differ diff --git a/src/mainwidget.cpp b/src/mainwidget.cpp index dc4ab83..e1a82cf 100644 --- a/src/mainwidget.cpp +++ b/src/mainwidget.cpp @@ -22,23 +22,20 @@ MainWidget::MainWidget() : mDebugLogger(Q_NULLPTR) { - QSurfaceFormat format; - format.setRenderableType(QSurfaceFormat::OpenGL); - format.setProfile(QSurfaceFormat::CoreProfile); - format.setVersion(4, 5); - format.setDepthBufferSize(16); - // If QTK_DEBUG is set, enable debug context -#ifdef QTK_DEBUG - format.setOption(QSurfaceFormat::DebugContext); -#endif + initializeWidget(); +} + +// Constructor for using this widget in QtDesigner +MainWidget::MainWidget(QWidget *parent) : QOpenGLWidget(parent), mDebugLogger(Q_NULLPTR) +{ + initializeWidget(); } MainWidget::MainWidget(const QSurfaceFormat &format) : mDebugLogger(Q_NULLPTR) { setFormat(format); - resize(QSize(800, 600)); - show(); + setFocusPolicy(Qt::ClickFocus); } MainWidget::~MainWidget() @@ -89,9 +86,9 @@ void MainWidget::initObjects() mObject->mProgram.enableAttributeArray(0); mObject->mProgram.setAttributeBuffer(0, GL_FLOAT, 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("uAmbientStrength", 0.2f); + mObject->mProgram.setUniformValue("uAmbientStrength", 0.75f); mObject->mVBO.release(); mObject->mVAO.release(); @@ -248,6 +245,7 @@ void MainWidget::messageLogged(const QOpenGLDebugMessage &msg) void MainWidget::keyPressEvent(QKeyEvent *event) { if (event->isAutoRepeat()) { + // Do not repeat input while a key is held down event->ignore(); } else { Input::registerKeyPress(event->key()); @@ -278,6 +276,24 @@ void MainWidget::mouseReleaseEvent(QMouseEvent *event) * 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() { QString glType; diff --git a/src/mainwidget.h b/src/mainwidget.h index 10093a7..ff00232 100644 --- a/src/mainwidget.h +++ b/src/mainwidget.h @@ -31,6 +31,7 @@ Q_OBJECT; public: // Constructors MainWidget(); + explicit MainWidget(QWidget *parent); explicit MainWidget(const QSurfaceFormat &format); ~MainWidget() override; @@ -57,6 +58,7 @@ protected: private: // Private helpers + void initializeWidget(); void printContextInformation(); void updateCameraInput(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp new file mode 100644 index 0000000..6ee1f0b --- /dev/null +++ b/src/mainwindow.cpp @@ -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; +} diff --git a/src/mainwindow.h b/src/mainwindow.h new file mode 100644 index 0000000..9efee87 --- /dev/null +++ b/src/mainwindow.h @@ -0,0 +1,24 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +#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 diff --git a/src/mainwindow.ui b/src/mainwindow.ui new file mode 100644 index 0000000..83f5142 --- /dev/null +++ b/src/mainwindow.ui @@ -0,0 +1,104 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + Qtk - MainWindow + + + + + + 10 + 10 + 775 + 550 + + + + + + + + 0 + 0 + 800 + 22 + + + + + File + + + + + + + + + View + + + + + + + + + + Save + + + + + Save + + + + + Open... + + + + + Save + + + + + Save as... + + + + + Exit + + + + + true + + + Show Console + + + + + + MainWidget + QOpenGLWidget +
mainwidget.h
+
+
+ + +
diff --git a/src/scene.cpp b/src/scene.cpp index ce972eb..70bf76f 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -24,7 +24,10 @@ QMatrix4x4 Scene::mProjection; Scene::Scene() { - init(); + mCamera.transform().setTranslation(0.0f, 0.0f, 20.0f); + mCamera.transform().setRotation(-5.0f, 0.0f, 1.0f, 0.0f); + + init(); } Scene::~Scene() @@ -44,10 +47,6 @@ Scene::~Scene() 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 mTestPhong = new MeshRenderer("phong", Cube()); mTestPhong->mTransform.setTranslation(3.0f, 0.0f, -2.0f);