trlsmax / imgui-vtk

test on how to integrate vtk into glfw + imgui project
MIT License
107 stars 26 forks source link

CPM support #17

Open triuk opened 1 year ago

triuk commented 1 year ago

Hi, this piece of code is precious. I really love it exists. But the setup is not that simple, because it links to other repositories when downloaded with git and if you already use imgui in the project, it means you'll end up linking all things manually and probably have target conflicts. So to ease this integration, I made a CMakeLists.txt that uses CPM to download and build imgui-vtk. The benefit is obvious - the CPM checks for new version of imgui-vtk every time you configure your project and run cmake on it. That means it is always up to date without effort. When you add other's project to yours, you definitely do not want to do it manually. I'll be happy, if you comment or improve this idea :)

Usage: download just main.cpp from the imgui-vtk project. Add my CMakeFiles.txt code to the same folder (so the working dir has only two files: main.cpp and this CMakeFiles.txt. Configure and build the executable.

cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR)

# #####################
# # DOWNLOAD SECTION ##
# #####################
set(CPM_DOWNLOAD_VERSION 0.36.0)

if(CPM_SOURCE_CACHE)
  set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
  set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
  set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

if(NOT(EXISTS ${CPM_DOWNLOAD_LOCATION}))
  message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
  file(DOWNLOAD
    https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
    ${CPM_DOWNLOAD_LOCATION}
  )
endif()

include(${CPM_DOWNLOAD_LOCATION})

# # ImGUI VTK ########
find_package(imvtk QUIET)

if(NOT imvtk_FOUND)
  CPMAddPackage(
    NAME imvtk
    GITHUB_REPOSITORY trlsmax/imgui-vtk
    GIT_TAG origin/master
  )
endif()

# ###### END OF DOWNLOAD SECTION ########################
# #######################################################
project(imgui-vtk)

set(CMAKE_CXX_STANDARD 11)
set(EXEC_NAME imvtkCPM)
set(PROGRAM_SRC main.cpp)
add_executable(${EXEC_NAME} ${PROGRAM_SRC})

# VTK
find_package(VTK COMPONENTS
  CommonCore
  CommonColor
  CommonDataModel
  FiltersCore
  InteractionStyle
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
  QUIET
)

if(NOT VTK_FOUND)
  message(FATAL_ERROR "VTK not found!")
  return()
endif()

if(VTK_VERSION VERSION_LESS "9.0.0")
  include(${VTK_USE_FILE})
else()
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ${EXEC_NAME}
    MODULES ${VTK_LIBRARIES}
  )
endif()

target_link_libraries(${EXEC_NAME} imgui_vtk_viewer)

Successfully tried on Ubuntu 22.04 with VS Code

rajkundu commented 1 year ago

I definitely hear you; bundling gl3w, glfw, and ImGui as Git submodules is nice for demonstration purposes (e.g., reliably cloning, building, and running this repo), but it is not so great when integrating imgui-vtk into larger CMake projects. However, I think most of that is because of ImGui's design. Since it's a lightweight, cross-platform, cross-toolchain library, it doesn't have any official CMake integration (at least as far as I know). There are a lot of files in this repo, but I think all you really need are VtkViewer.h and VtkViewer.cpp if you already have ImGui & VTK.

Personally, I'm not familiar with CPM, but thank you very much for sharing your CMakeLists.txt. I'm glad you're able to make good use of imgui-vtk! :)