diff --git a/CMakeLists.txt b/CMakeLists.txt index d6516cf..f6ec9f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) option(QTK_DEBUG "Enable debugger" ON) option(BUILD_SHARED_LIBS "Build shared library" ON) -option(ASSIMP_NEW_INTERFACE "Use assimp::assimp as target instead of assimp" OFF) +option(BUILD_ASSIMP "Add and build assimp as an external project" ON) ################################################################################ # External Libraries @@ -37,11 +37,45 @@ if (NOT Qt6_FOUND) message(FATAL_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() -# Find Assimp -# https://github.com/assimp/assimp/commit/6ac8279977c3a54118551e549d77329497116f66 -find_package(assimp REQUIRED) +# If no ASSIMP_DIR was provided via -DASSIMP_DIR=... +# + Add assimp as external project +# + If assimp is already available system-wide, we can just set -DBUILD_ASSIMP=OFF +if (BUILD_ASSIMP AND NOT ASSIMP_DIR) + # Add Assimp as an external project + include(ExternalProject) + + set(ASSIMP_PREFIX "${CMAKE_BINARY_DIR}/extern/assimp") + set(ASSIMP_INSTALL_DIR "${ASSIMP_PREFIX}/install") + set(ASSIMP_INCLUDE_DIR "${ASSIMP_INSTALL_DIR}/include") + set(ASSIMP_LIB_DIR "${ASSIMP_INSTALL_DIR}/lib") + set(ASSIMP_LIB "${CMAKE_SHARED_LIBRARY_PREFIX}assimp${CMAKE_SHARED_LIBRARY_SUFFIX}") + file(MAKE_DIRECTORY "${ASSIMP_INCLUDE_DIR}") + + ExternalProject_Add( + assimp-extern + PREFIX "${ASSIMP_PREFIX}" + INSTALL_DIR "${ASSIMP_INSTALL}" + CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${ASSIMP_INSTALL_DIR}" + GIT_REPOSITORY https://github.com/assimp/assimp.git + GIT_PROGRESS ON + GIT_TAG v5.2.4 + ) + + add_library(assimp STATIC IMPORTED) + set_target_properties( + assimp + PROPERTIES + IMPORTED_LOCATION "${ASSIMP_INSTALL_DIR}/lib/${ASSIMP_LIB}" + ) + add_dependencies(assimp assimp-extern) + target_include_directories(assimp INTERFACE "${ASSIMP_INCLUDE_DIR}") +else() + if (ASSIMP_DIR) + message(STATUS "ASSIMP_DIR: ${ASSIMP_DIR}") + endif() +endif() + -include(GenerateExportHeader) ################################################################################ # Qtk @@ -75,6 +109,8 @@ set(SOURCE_FILES src/texture.cpp src/transform3D.cpp ) + +include(GenerateExportHeader) qt_add_library(qtk-widget STATIC ${PUBLIC_HEADERS} ${SOURCE_FILES}) target_include_directories(qtk-widget PRIVATE src/ app/) generate_export_header(qtk-widget) @@ -84,24 +120,8 @@ set_target_properties(qtk-widget PROPERTIES VERSION ${PROJECT_VERSION} ) -target_include_directories(qtk-widget PUBLIC $) -if(BUILD_SHARED_LIBS) - target_compile_definitions(qtk-widget PRIVATE QTK_EXPORT PUBLIC QTK_SHARED) - target_link_libraries(qtk-widget PRIVATE Qt6::OpenGLWidgets) - if(ASSIMP_NEW_INTERFACE) - target_link_libraries(qtk-widget PUBLIC assimp::assimp) - else() - target_link_libraries(qtk-widget PUBLIC assimp) - endif() -else() - target_compile_definitions(qtk-widget PUBLIC QTK_STATIC) - target_link_libraries(qtk-widget PUBLIC Qt6::OpenGLWidgets) - if(ASSIMP_NEW_INTERFACE) - target_link_libraries(qtk-widget PUBLIC assimp::assimp) - else() - target_link_libraries(qtk-widget PUBLIC assimp) - endif() -endif() +target_link_libraries(qtk-widget PUBLIC Qt6::OpenGLWidgets) +target_link_libraries(qtk-widget PRIVATE assimp) if(QTK_DEBUG) target_compile_definitions(qtk-widget PUBLIC QTK_DEBUG) @@ -148,19 +168,19 @@ set(QTK_APP_SOURCES app/main.cpp ) qt6_add_big_resources(QTK_APP_SOURCES resources.qrc) -qt_add_executable(qtk-app ${QTK_APP_SOURCES}) -target_include_directories(qtk-app PRIVATE src/ app/) +qt_add_executable(qtk-main ${QTK_APP_SOURCES}) +target_include_directories(qtk-main PRIVATE src/ app/) -# Link qtk-app executable to main qtk-widget library -target_link_libraries(qtk-app PRIVATE Qt6::OpenGLWidgets qtk-widget) +# Link qtk-main executable to main qtk-widget library +target_link_libraries(qtk-main PRIVATE qtk-widget) -set_target_properties(qtk-app PROPERTIES +set_target_properties(qtk-main PROPERTIES WIN32_EXECUTABLE TRUE MACOSX_BUNDLE TRUE MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} ) -install(TARGETS qtk-app +install(TARGETS qtk-main BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ) @@ -170,16 +190,16 @@ if(WIN32) execute_process(COMMAND "${_qt6_qmake_location}" -query QT_INSTALL_PREFIX RESULT_VARIABLE return_code OUTPUT_VARIABLE qt6_install_prefix OUTPUT_STRIP_TRAILING_WHITESPACE) file(TO_NATIVE_PATH "${qt6_install_prefix}/bin" qt6_install_prefix) if(TARGET Qt6::windeployqt) - add_custom_command(TARGET qtk-app + add_custom_command(TARGET qtk-main POST_BUILD COMMAND set PATH=%PATH%$${qt6_install_prefix} - COMMAND Qt6::windeployqt --dir "${CMAKE_BINARY_DIR}/windeployqt" "$/$" + COMMAND Qt6::windeployqt --dir "${CMAKE_BINARY_DIR}/windeployqt" "$/$" ) install(DIRECTORY "${CMAKE_BINARY_DIR}/windeployqt/" DESTINATION bin) endif() if(MSVC AND TARGET Qt6::qmake) - set(VSUSER_FILE ${CMAKE_CURRENT_BINARY_DIR}/qtk-app.vcxproj.user) - file(TO_NATIVE_PATH "${assimp_DIR}/bin" assimp_bin) + set(VSUSER_FILE ${CMAKE_CURRENT_BINARY_DIR}/qtk-main.vcxproj.user) + file(TO_NATIVE_PATH "${ASSIMP_INSTALL_DIR}/bin" assimp_bin) file(WRITE ${VSUSER_FILE} "\n") file(APPEND ${VSUSER_FILE} "\n") file(APPEND ${VSUSER_FILE} " \n") diff --git a/app/mainwindow.ui b/app/mainwindow.ui index 13f9903..938ff7d 100644 --- a/app/mainwindow.ui +++ b/app/mainwindow.ui @@ -14,7 +14,7 @@ Qtk - MainWindow - + 10 @@ -94,8 +94,8 @@ - MainWidget - QOpenGLWidget + Qtk::MainWidget + QWidget
mainwidget.h
diff --git a/app/scene.cpp b/app/scene.cpp index 9b0b91d..f509769 100644 --- a/app/scene.cpp +++ b/app/scene.cpp @@ -602,11 +602,7 @@ void ExampleScene::init() void ExampleScene::draw() { - mSkybox->draw(); - - for (auto & model : mModels) model->draw(); - - for (const auto &mesh : mMeshes) mesh->draw(); + Scene::draw(); mTestPhong->mProgram.bind(); mTestPhong->setUniform("uModelInverseTransposed", diff --git a/app/scene.h b/app/scene.h index 016c6c2..d589694 100644 --- a/app/scene.h +++ b/app/scene.h @@ -17,12 +17,13 @@ class ExampleScene : public Qtk::Scene { + public: ExampleScene(); ~ExampleScene(); virtual void init(); - virtual void draw(); + virtual void draw() override; virtual void update(); private: diff --git a/src/abstractscene.h b/src/abstractscene.h index 1072b60..1fd596d 100644 --- a/src/abstractscene.h +++ b/src/abstractscene.h @@ -20,12 +20,13 @@ namespace Qtk { class Scene : protected QOpenGLFunctions { friend class MainWidget; + public: Scene(); ~Scene(); virtual void init() = 0; - virtual void draw() = 0; + virtual void draw() { privDraw(); }; virtual void update() = 0; static Camera3D & Camera() { return mCamera;} diff --git a/src/mainwidget.cpp b/src/mainwidget.cpp index 1170cbb..bf982a5 100644 --- a/src/mainwidget.cpp +++ b/src/mainwidget.cpp @@ -65,7 +65,7 @@ void MainWidget::paintGL() glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); // Draw the scene first, since it handles drawing our skybox - if (mScene != Q_NULLPTR) mScene->privDraw(); + if (mScene != Q_NULLPTR) mScene->draw(); } void MainWidget::initializeGL() diff --git a/src/skybox.cpp b/src/skybox.cpp index 09afa03..c0febd7 100644 --- a/src/skybox.cpp +++ b/src/skybox.cpp @@ -15,14 +15,16 @@ using namespace Qtk; Skybox::Skybox(std::string right, std::string top, std::string front, std::string left, std::string bottom, std::string back, const std::string & name) - : mCubeMap(Texture::initCubeMap( - QImage(right.c_str()).mirrored(), QImage(top.c_str()), - QImage(front.c_str()), QImage(left.c_str()), - QImage(bottom.c_str()), QImage(back.c_str()))), - mVBO(QOpenGLBuffer::VertexBuffer), + : mVBO(QOpenGLBuffer::VertexBuffer), mVertices(Cube(QTK_DRAW_ELEMENTS).vertices()), mIndices(Cube(QTK_DRAW_ELEMENTS).indices()) -{ init();} +{ + init(); + mCubeMap = Texture::initCubeMap( + QImage(right.c_str()).mirrored(), QImage(top.c_str()), + QImage(front.c_str()), QImage(left.c_str()), + QImage(bottom.c_str()), QImage(back.c_str())); +} Skybox::Skybox(std::string name) : Skybox(":/right.png", ":/top.png", ":/front.png", @@ -87,13 +89,11 @@ void Skybox::init() mVBO.setUsagePattern(QOpenGLBuffer::StaticDraw); mVBO.bind(); // Allocate vertex positions into VBO - mVBO.allocate(mVertices.data(), - mVertices.size() * sizeof(mVertices[0])); + mVBO.allocate(mVertices.data(), mVertices.size() * sizeof(mVertices[0])); // Enable attribute array for vertex positions mProgram.enableAttributeArray(0); - mProgram.setAttributeBuffer(0, GL_FLOAT, 0, - 3, sizeof(QVector3D)); + mProgram.setAttributeBuffer(0, GL_FLOAT, 0, 3, sizeof(QVector3D)); // Set shader texture unit to 0 mProgram.setUniformValue("uTexture", 0);