Add assimp as external dependency

This commit is contained in:
Shaun Reed 2022-08-14 17:02:50 -04:00
parent 249a2b4446
commit f3406ee749
7 changed files with 72 additions and 54 deletions

View File

@ -21,7 +21,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
option(QTK_DEBUG "Enable debugger" ON) option(QTK_DEBUG "Enable debugger" ON)
option(BUILD_SHARED_LIBS "Build shared library" 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 # 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)`") 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() endif()
# Find Assimp # If no ASSIMP_DIR was provided via -DASSIMP_DIR=...
# https://github.com/assimp/assimp/commit/6ac8279977c3a54118551e549d77329497116f66 # + Add assimp as external project
find_package(assimp REQUIRED) # + 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 # Qtk
@ -75,6 +109,8 @@ set(SOURCE_FILES
src/texture.cpp src/texture.cpp
src/transform3D.cpp src/transform3D.cpp
) )
include(GenerateExportHeader)
qt_add_library(qtk-widget STATIC ${PUBLIC_HEADERS} ${SOURCE_FILES}) qt_add_library(qtk-widget STATIC ${PUBLIC_HEADERS} ${SOURCE_FILES})
target_include_directories(qtk-widget PRIVATE src/ app/) target_include_directories(qtk-widget PRIVATE src/ app/)
generate_export_header(qtk-widget) generate_export_header(qtk-widget)
@ -84,24 +120,8 @@ set_target_properties(qtk-widget PROPERTIES
VERSION ${PROJECT_VERSION} VERSION ${PROJECT_VERSION}
) )
target_include_directories(qtk-widget PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
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) target_link_libraries(qtk-widget PUBLIC Qt6::OpenGLWidgets)
if(ASSIMP_NEW_INTERFACE) target_link_libraries(qtk-widget PRIVATE assimp)
target_link_libraries(qtk-widget PUBLIC assimp::assimp)
else()
target_link_libraries(qtk-widget PUBLIC assimp)
endif()
endif()
if(QTK_DEBUG) if(QTK_DEBUG)
target_compile_definitions(qtk-widget PUBLIC 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) qt6_add_big_resources(QTK_APP_SOURCES resources.qrc)
qt_add_executable(qtk-app ${QTK_APP_SOURCES}) qt_add_executable(qtk-main ${QTK_APP_SOURCES})
target_include_directories(qtk-app PRIVATE src/ app/) target_include_directories(qtk-main PRIVATE src/ app/)
# Link qtk-app executable to main qtk-widget library # Link qtk-main executable to main qtk-widget library
target_link_libraries(qtk-app PRIVATE Qt6::OpenGLWidgets qtk-widget) target_link_libraries(qtk-main PRIVATE qtk-widget)
set_target_properties(qtk-app PROPERTIES set_target_properties(qtk-main PROPERTIES
WIN32_EXECUTABLE TRUE WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE MACOSX_BUNDLE TRUE
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
) )
install(TARGETS qtk-app install(TARGETS qtk-main
BUNDLE DESTINATION . BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 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) 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) file(TO_NATIVE_PATH "${qt6_install_prefix}/bin" qt6_install_prefix)
if(TARGET Qt6::windeployqt) if(TARGET Qt6::windeployqt)
add_custom_command(TARGET qtk-app add_custom_command(TARGET qtk-main
POST_BUILD POST_BUILD
COMMAND set PATH=%PATH%$<SEMICOLON>${qt6_install_prefix} COMMAND set PATH=%PATH%$<SEMICOLON>${qt6_install_prefix}
COMMAND Qt6::windeployqt --dir "${CMAKE_BINARY_DIR}/windeployqt" "$<TARGET_FILE_DIR:qtk-app>/$<TARGET_FILE_NAME:qtk-app>" COMMAND Qt6::windeployqt --dir "${CMAKE_BINARY_DIR}/windeployqt" "$<TARGET_FILE_DIR:qtk-main>/$<TARGET_FILE_NAME:qtk-main>"
) )
install(DIRECTORY "${CMAKE_BINARY_DIR}/windeployqt/" DESTINATION bin) install(DIRECTORY "${CMAKE_BINARY_DIR}/windeployqt/" DESTINATION bin)
endif() endif()
if(MSVC AND TARGET Qt6::qmake) if(MSVC AND TARGET Qt6::qmake)
set(VSUSER_FILE ${CMAKE_CURRENT_BINARY_DIR}/qtk-app.vcxproj.user) set(VSUSER_FILE ${CMAKE_CURRENT_BINARY_DIR}/qtk-main.vcxproj.user)
file(TO_NATIVE_PATH "${assimp_DIR}/bin" assimp_bin) file(TO_NATIVE_PATH "${ASSIMP_INSTALL_DIR}/bin" assimp_bin)
file(WRITE ${VSUSER_FILE} "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n") file(WRITE ${VSUSER_FILE} "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
file(APPEND ${VSUSER_FILE} "<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n") file(APPEND ${VSUSER_FILE} "<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n")
file(APPEND ${VSUSER_FILE} " <PropertyGroup>\n") file(APPEND ${VSUSER_FILE} " <PropertyGroup>\n")

View File

@ -14,7 +14,7 @@
<string>Qtk - MainWindow</string> <string>Qtk - MainWindow</string>
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<widget class="Qtk::MainWidget" name="openGLWidget"> <widget class="Qtk::MainWidget" name="openGLWidget" native="true">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>10</x> <x>10</x>
@ -94,8 +94,8 @@
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>
<class>MainWidget</class> <class>Qtk::MainWidget</class>
<extends>QOpenGLWidget</extends> <extends>QWidget</extends>
<header>mainwidget.h</header> <header>mainwidget.h</header>
</customwidget> </customwidget>
</customwidgets> </customwidgets>

View File

@ -602,11 +602,7 @@ void ExampleScene::init()
void ExampleScene::draw() void ExampleScene::draw()
{ {
mSkybox->draw(); Scene::draw();
for (auto & model : mModels) model->draw();
for (const auto &mesh : mMeshes) mesh->draw();
mTestPhong->mProgram.bind(); mTestPhong->mProgram.bind();
mTestPhong->setUniform("uModelInverseTransposed", mTestPhong->setUniform("uModelInverseTransposed",

View File

@ -17,12 +17,13 @@
class ExampleScene : public Qtk::Scene { class ExampleScene : public Qtk::Scene {
public: public:
ExampleScene(); ExampleScene();
~ExampleScene(); ~ExampleScene();
virtual void init(); virtual void init();
virtual void draw(); virtual void draw() override;
virtual void update(); virtual void update();
private: private:

View File

@ -20,12 +20,13 @@
namespace Qtk { namespace Qtk {
class Scene : protected QOpenGLFunctions { class Scene : protected QOpenGLFunctions {
friend class MainWidget; friend class MainWidget;
public: public:
Scene(); Scene();
~Scene(); ~Scene();
virtual void init() = 0; virtual void init() = 0;
virtual void draw() = 0; virtual void draw() { privDraw(); };
virtual void update() = 0; virtual void update() = 0;
static Camera3D & Camera() { return mCamera;} static Camera3D & Camera() { return mCamera;}

View File

@ -65,7 +65,7 @@ void MainWidget::paintGL()
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
// Draw the scene first, since it handles drawing our skybox // 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() void MainWidget::initializeGL()

View File

@ -15,14 +15,16 @@ using namespace Qtk;
Skybox::Skybox(std::string right, std::string top, std::string front, Skybox::Skybox(std::string right, std::string top, std::string front,
std::string left, std::string bottom, std::string back, std::string left, std::string bottom, std::string back,
const std::string & name) const std::string & name)
: mCubeMap(Texture::initCubeMap( : mVBO(QOpenGLBuffer::VertexBuffer),
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),
mVertices(Cube(QTK_DRAW_ELEMENTS).vertices()), mVertices(Cube(QTK_DRAW_ELEMENTS).vertices()),
mIndices(Cube(QTK_DRAW_ELEMENTS).indices()) 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::Skybox(std::string name)
: Skybox(":/right.png", ":/top.png", ":/front.png", : Skybox(":/right.png", ":/top.png", ":/front.png",
@ -87,13 +89,11 @@ void Skybox::init()
mVBO.setUsagePattern(QOpenGLBuffer::StaticDraw); mVBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
mVBO.bind(); mVBO.bind();
// Allocate vertex positions into VBO // Allocate vertex positions into VBO
mVBO.allocate(mVertices.data(), mVBO.allocate(mVertices.data(), mVertices.size() * sizeof(mVertices[0]));
mVertices.size() * sizeof(mVertices[0]));
// Enable attribute array for vertex positions // Enable attribute array for vertex positions
mProgram.enableAttributeArray(0); mProgram.enableAttributeArray(0);
mProgram.setAttributeBuffer(0, GL_FLOAT, 0, mProgram.setAttributeBuffer(0, GL_FLOAT, 0, 3, sizeof(QVector3D));
3, sizeof(QVector3D));
// Set shader texture unit to 0 // Set shader texture unit to 0
mProgram.setUniformValue("uTexture", 0); mProgram.setUniformValue("uTexture", 0);