Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jspanchu committed Dec 4, 2023
1 parent 1ebc6db commit 896b4e6
Show file tree
Hide file tree
Showing 14 changed files with 617 additions and 531 deletions.
25 changes: 9 additions & 16 deletions .github/workflows/build-wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,26 @@ jobs:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
container:
image: jspanchu/vtk-wasm-target:latest
options: -v ${{ github.workspace }}:/project/src
steps:
- name: Checkout repository and submodules
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive
- name: Configure
run: |
emcmake cmake -GNinja -S /project/src -B /project/build -DCMAKE_BUILD_TYPE=Release -DVTK_DIR=$VTK_DIR
- name: Build
run:
cmake --build /project/build
run: |
npm install
npm run build-wasm
- name: Prepare artifacts
run: |
cd /project/build
mkdir /web_content
mv imgui_vtk_app.js /web_content/
mv imgui_vtk_app.wasm /web_content/
mv index.html /web_content/
mkdir web_content
mv build-emscripten/sample/imgui_vtk_app.js web_content/
mv build-emscripten/index.html web_content/
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
path: '/web_content/'
path: './web_content/'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
uses: actions/deploy-pages@v1
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
# Build dirs
out/
.vs/
*.json
compile_commands.json
.cache
build
build*
build*

# node
node_modules/
package-lock.json
226 changes: 17 additions & 209 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,220 +1,28 @@
# SPDX-FileCopyrightText: Copyright 2023 Jaswant Panchumarti
# SPDX-License-Identifier: BSD-3-Clause

cmake_minimum_required (VERSION 3.6)
project (vtkDearImGuiInjector)
include (GenerateExportHeader)

find_package(ParaView
QUIET
)
if (NOT ParaView_FOUND)
find_package(VTK)
if (NOT VTK_FOUND)
message(FATAL_ERROR "Cannot find VTK installation")
return ()
endif()
endif()

message(STATUS "VTK_VERSION: ${VTK_VERSION}")

# For an adobe spectrum look.
option(USE_ADOBE_SPECTRUM_STYLE "Style ImGui with Adobe spectrum look" ON)

if (USE_ADOBE_SPECTRUM_STYLE)
set(_IMGUI_DIR "adobe_imgui_spectrum")
list(APPEND _proj_definitions ADOBE_IMGUI_SPECTRUM)
elseif(IMGUI_DIR) # From cli -DIMGUI_DIR
set(_IMGUI_DIR IMGUI_DIR)
else()
set(_IMGUI_DIR "imgui")
endif()

# sources and headers
file(GLOB IMGUI_SOURCES ${_IMGUI_DIR}/*.cpp)
file(GLOB IMGUI_HEADERS ${_IMGUI_DIR}/*.h)
list(APPEND _proj_headers
${IMGUI_HEADERS}
"${_IMGUI_DIR}/backends/imgui_impl_opengl3.h"
"include/vtkDearImGuiInjector.h"
)
list(APPEND _proj_sources
${IMGUI_SOURCES}
"${_IMGUI_DIR}/backends/imgui_impl_opengl3.cpp"
"src/vtkDearImGuiInjector.cxx"
)

if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
list(APPEND _proj_definitions IMGUI_IMPL_OPENGL_ES3)
endif()

# library target
add_library(${CMAKE_PROJECT_NAME} STATIC ${_proj_sources})
target_compile_definitions(${CMAKE_PROJECT_NAME} PUBLIC ${_proj_definitions})
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/${_IMGUI_DIR}>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/${_IMGUI_DIR}/backends>
$<INSTALL_INTERFACE:include>
find_package(VTK
COMPONENTS
FiltersSources
InteractionStyle
InteractionWidgets
RenderingOpenGL2
RenderingUI
)
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC ${VTK_LIBRARIES})
generate_export_header(${CMAKE_PROJECT_NAME})

# Install targets
install(TARGETS ${CMAKE_PROJECT_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
install(FILES ${CMAKE_PROJECT_NAME}.h ${PROJECT_BINARY_DIR}/${CMAKE_PROJECT_NAME}_export.h DESTINATION include)
install(FILES ${_proj_headers} DESTINATION include)

# -----------------------------------------------------------------------------
# Configure application
# -----------------------------------------------------------------------------

if (NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
message("Skipping WebAssembly example: This needs to run inside an Emscripten build environment")

add_executable(test_imgui_vtk "src/test_imgui_vtk.cxx")

target_link_libraries(test_imgui_vtk PRIVATE ${CMAKE_PROJECT_NAME})
vtk_module_autoinit(
TARGETS test_imgui_vtk
MODULES ${VTK_LIBRARIES}
)
if (NOT VTK_FOUND)
message("Skipping example: ${VTK_NOT_FOUND_MESSAGE}")
return ()
endif ()

message(STATUS "VTK_VERSION: ${VTK_VERSION}")

# -----------------------------------------------------------------------------
# WebAssembly build options
# -----------------------------------------------------------------------------

set(emscripten_options)
list(APPEND emscripten_options
"--bind"
"-g3"
"SHELL:-s EXPORT_NAME=vtkApp"
"SHELL:-s ALLOW_MEMORY_GROWTH=1"
"SHELL:-s DEMANGLE_SUPPORT=1"
"SHELL:-s EMULATE_FUNCTION_POINTER_CASTS=0"
"SHELL:-s ERROR_ON_UNDEFINED_SYMBOLS=0"
"SHELL:-s MODULARIZE=1"
"SHELL:-s USE_PTHREADS=0"
"SHELL:-s WASM=1"
)

# -----------------------------------------------------------------------------
# Build options
# -----------------------------------------------------------------------------

set(OPTIMIZE "SMALLEST_WITH_CLOSURE" CACHE STRING "Emscripten optimization")
set_property(CACHE OPTIMIZE PROPERTY
STRINGS
NO_OPTIMIZATION # -O0
LITTLE # -O1
MORE # -O2
BEST # -O3
SMALL # -Os
SMALLEST # -Oz
SMALLEST_WITH_CLOSURE # -Oz --closure 1
)

if(OPTIMIZE STREQUAL "NO_OPTIMIZATION")
# imgui_vtk.js 659K
# imgui_vtk.wasm 4.9M
# time => 4 minutes 3 seconds
list(APPEND emscripten_options
"-Oz"
)
elseif(OPTIMIZE STREQUAL "LITTLE")
# imgui_vtk.js 529K
# imgui_vtk.wasm 5.9M
list(APPEND emscripten_options
"-O1"
)
elseif(OPTIMIZE STREQUAL "MORE")
# imgui_vtk.js 529K
# imgui_vtk.wasm 5.3M
list(APPEND emscripten_options
"-O2"
)
elseif(OPTIMIZE STREQUAL "BEST")
# imgui_vtk.js 529K
# imgui_vtk.wasm 4.9M
# time => 4 minutes 7 seconds
list(APPEND emscripten_options
"-O3"
)
elseif(OPTIMIZE STREQUAL "SMALL")
# imgui_vtk.js 529K
# imgui_vtk.wasm 4.9M
list(APPEND emscripten_options
"-Os"
)
elseif(OPTIMIZE STREQUAL "SMALLEST")
# imgui_vtk.js 659K
# imgui_vtk.wasm 4.9M
list(APPEND emscripten_options
"-Oz"
)
elseif(OPTIMIZE STREQUAL "SMALLEST_WITH_CLOSURE")
# imgui_vtk.js 659K
# imgui_vtk.wasm 4.9M
list(APPEND emscripten_options
"-Oz"
"SHELL:--closure 1"
)
endif()

# -----------------------------------------------------------------------------
# Compile example code
# -----------------------------------------------------------------------------

add_executable(imgui_vtk_app "src/test_imgui_vtk.cxx")
target_link_libraries(imgui_vtk_app PRIVATE ${CMAKE_PROJECT_NAME} ${VTK_LIBRARIES})

target_link_libraries(imgui_vtk_app
PRIVATE
VTK::CommonColor
VTK::CommonCore
VTK::CommonDataModel
VTK::FiltersCore
VTK::FiltersSources
VTK::InteractionStyle
VTK::InteractionWidgets
VTK::RenderingCore
VTK::RenderingFreeType
VTK::RenderingOpenGL2
)

target_compile_options(imgui_vtk_app
PUBLIC
${emscripten_options}
)

target_link_options(imgui_vtk_app
PUBLIC
${emscripten_options}
)

# -----------------------------------------------------------------------------
# VTK modules initialization
# -----------------------------------------------------------------------------

vtk_module_autoinit(
TARGETS imgui_vtk_app
MODULES ${VTK_LIBRARIES}
)

# -----------------------------------------------------------------------------
# Copy HTML to build directory
# -----------------------------------------------------------------------------

add_custom_command(
TARGET imgui_vtk_app
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_if_different
"${CMAKE_CURRENT_SOURCE_DIR}/index.html"
$<TARGET_FILE_DIR:imgui_vtk_app>
)
# For an adobe spectrum look.
option(USE_ADOBE_SPECTRUM_STYLE "Style ImGui with Adobe spectrum look" ON)

add_subdirectory(src)
add_subdirectory(sample)
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 jaswantp
Copyright 2023 Jaswant Panchumarti

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
78 changes: 42 additions & 36 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
<!-- SPDX-FileCopyrightText: Copyright 2023 Jaswant Panchumarti -->
<!-- SPDX-License-Identifier: BSD-3-Clause -->

<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<canvas id="canvas" style="position: absolute; left: 0; top: 0;" tabindex="0"></canvas>
<script type="text/javascript" src="imgui_vtk_app.js"></script>
<script type='text/javascript'>
var Module = {
canvas: (function() {
var canvas = document.getElementById('canvas');
canvas.addEventListener(
"webglcontextlost",
function(e) {
console.error('WebGL context lost. You will need to reload the page.');
e.preventDefault();
},
false
);
return canvas;
})(),
onRuntimeInitialized: function() {
console.log('initialized');
setTimeout(() => {
window.dispatchEvent(new Event('resize'));
}, 0);
},
};

// Use the export name to instantiate the app
var app = vtkApp(Module);
console.log('App created');
canvas.setAttribute('tabindex', '0'); // needed to put focus on the canvas
canvas.addEventListener('click', () => canvas.focus());
canvas.focus();
</script>
</body>
</html>
<head>
<meta charset="utf-8" />
</head>

<body>
<canvas id="canvas" style="position: absolute; left: 0; top: 0;" tabindex="0"></canvas>
<script type="text/javascript" src="imgui_vtk_app.js"></script>
<script type='text/javascript'>
var Module = {
canvas: (function () {
var canvas = document.getElementById('canvas');
canvas.addEventListener(
"webglcontextlost",
function (e) {
console.error('WebGL context lost. You will need to reload the page.');
e.preventDefault();
},
false
);
return canvas;
})(),
onRuntimeInitialized: function () {
console.log('initialized');
setTimeout(() => {
window.dispatchEvent(new Event('resize'));
}, 0);
},
};

// Use the export name to instantiate the app
var app = vtkApp(Module);
console.log('App created');
canvas.setAttribute('tabindex', '0'); // needed to put focus on the canvas
canvas.addEventListener('click', () => canvas.focus());
canvas.focus();
</script>
</body>

</html>
Loading

0 comments on commit 896b4e6

Please sign in to comment.