Skip to content

Commit

Permalink
Fix emscripten compilation (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
bchapuis authored Nov 13, 2024
1 parent a586dcf commit 5e95e0e
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 19 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,33 @@ jobs:
run: |
cmake . -B build -DTHREEPP_BUILD_EXAMPLES=OFF -DTHREEPP_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE="Release"
cmake --build build
linux-emscripten:
runs-on: ${{ matrix.os }}
env:
CC: gcc-${{ matrix.compiler_version }}
CXX: g++-${{ matrix.compiler_version }}
strategy:
fail-fast: false
matrix:
os: [ ubuntu-22.04 ]
compiler_version: [ 11 ]

steps:
- uses: actions/checkout@v3

- name: Install prerequisites
run: |
sudo apt-get update && sudo apt-get install -y \
libxinerama-dev \
libxcursor-dev \
xorg-dev \
libglu1-mesa-dev \
pkg-config
- uses: mymindstorm/setup-emsdk@v14

- name: Configure and build
run: |
cmake . -B build -DCMAKE_TOOLCHAIN_FILE=${{env.EM_CACHE_FOLDER}}/libexec/cmake/Modules/Platform/Emscripten.cmake -DTHREEPP_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE="Release"
cmake --build build
2 changes: 1 addition & 1 deletion examples/AddExample.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function(add_example)
set(LINK_FLAGS " --bind -sUSE_GLFW=3 -sGL_DEBUG=1 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFULL_ES3 -sASSERTIONS -sALLOW_MEMORY_GROWTH -sNO_DISABLE_EXCEPTION_CATCHING -sWASM=1")
if (arg_WEB_EMBED)
foreach (path ${arg_WEB_EMBED})
set(LINK_FLAGS "${LINK_FLAGS} --embed-file ${path}")
set(LINK_FLAGS "${LINK_FLAGS} --embed-file \"${path}\"")
endforeach ()
endif ()

Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include(LibConfig.cmake)
add_subdirectory(libs)

add_example(NAME "demo" LINK_IMGUI WEB WEB_EMBED
"../data/fonts@data/fonts"
"${PROJECT_SOURCE_DIR}/data/fonts@data/fonts"
)

add_subdirectory(animation)
Expand Down
2 changes: 1 addition & 1 deletion examples/controls/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

add_example(NAME "drag" WEB)
add_example(NAME "fly" WEB WEB_EMBED
"../data/textures/planets@data/textures/planets"
"${PROJECT_SOURCE_DIR}/data/textures/planets@data/textures/planets"
)
2 changes: 1 addition & 1 deletion examples/extras/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

add_example(NAME "fonts" LINK_IMGUI WEB WEB_EMBED
"../data/fonts@data/fonts"
"${PROJECT_SOURCE_DIR}/data/fonts@data/fonts"
)
2 changes: 1 addition & 1 deletion examples/geometries/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

add_example(NAME "geometries" WEB WEB_EMBED
"../data/textures/uv_grid_opengl.jpg@/data/textures/uv_grid_opengl.jpg"
"${PROJECT_SOURCE_DIR}/data/textures/uv_grid_opengl.jpg@/data/textures/uv_grid_opengl.jpg"
)
add_example(NAME "lathe_geometry" WEB)
add_example(NAME "tube_geometry" WEB)
Expand Down
6 changes: 3 additions & 3 deletions examples/loaders/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

add_example(NAME "stl_loader" WEB WEB_EMBED
"../data/models/stl/pr2_head_pan.stl@data/models/stl/pr2_head_pan.stl"
"${PROJECT_SOURCE_DIR}/data/models/stl/pr2_head_pan.stl@data/models/stl/pr2_head_pan.stl"
)
add_example(NAME "obj_loader" WEB WEB_EMBED
"../data/models/obj/female02@data/models/obj/female02"
"../data/textures/uv_grid_opengl.jpg@data/textures/uv_grid_opengl.jpg"
"${PROJECT_SOURCE_DIR}/data/models/obj/female02@data/models/obj/female02"
"${PROJECT_SOURCE_DIR}/data/textures/uv_grid_opengl.jpg@data/textures/uv_grid_opengl.jpg"
)
add_example(NAME "assimp_loader" LINK_ASSIMP)
add_example(NAME "urdf_loader" LINK_ASSIMP LINK_IMGUI)
Expand Down
6 changes: 3 additions & 3 deletions examples/objects/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@

add_example(NAME "instancing" LINK_IMGUI WEB WEB_EMBED
"../data/fonts@data/fonts"
"${PROJECT_SOURCE_DIR}/data/fonts@data/fonts"
)
if (TARGET "instancing")
target_include_directories("instancing" PRIVATE "${PROJECT_SOURCE_DIR}/examples/libs")
endif ()

add_example(NAME "sprite")
add_example(NAME "lod" WEBWEB_EMBED
"../data/fonts@data/fonts"
"${PROJECT_SOURCE_DIR}/data/fonts@data/fonts"
)
add_example(NAME "points" WEB)

add_example(NAME "bones" LINK_IMGUI)
add_example(NAME "decal" LINK_IMGUI LINK_ASSIMP)
add_example(NAME "water" LINK_IMGUI WEB WEB_EMBED
"../data/textures/waternormals.jpg@data/textures/waternormals.jpg"
"${PROJECT_SOURCE_DIR}/data/textures/waternormals.jpg@data/textures/waternormals.jpg"
)

add_example(NAME "particle_system" LINK_IMGUI)
4 changes: 2 additions & 2 deletions examples/projects/Crane3R/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

add_example(NAME "crane3R" SOURCES main.cpp Crane3R.cpp LINK_IMGUI WEB WEB_EMBED
"../data/fonts@data/fonts"
"../data/models/obj/Crane3R@data/models/obj/Crane3R"
"${PROJECT_SOURCE_DIR}/data/fonts@data/fonts"
"${PROJECT_SOURCE_DIR}/data/models/obj/Crane3R@data/models/obj/Crane3R"
)
if (TARGET "crane3R")
target_link_libraries("crane3R" PRIVATE kine)
Expand Down
10 changes: 5 additions & 5 deletions examples/textures/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
add_example(NAME "cubemap")
add_example(NAME "texture3d")
add_example(NAME "texture2d" WEB WEB_EMBED
"../data/textures/crate.gif@data/textures/crate.gif"
"../data/textures/checker.png@data/textures/checker.png"
"../data/textures/brick_bump.jpg@data/textures/brick_bump.jpg"
"${PROJECT_SOURCE_DIR}/data/textures/crate.gif@data/textures/crate.gif"
"${PROJECT_SOURCE_DIR}/data/textures/checker.png@data/textures/checker.png"
"${PROJECT_SOURCE_DIR}/data/textures/brick_bump.jpg@data/textures/brick_bump.jpg"
)
add_example(NAME "data_texture" WEB WEB_EMBED
"../data/textures/checker.png@data/textures/checker.png"
"../data/textures/crate.gif@data/textures/crate.gif"
"${PROJECT_SOURCE_DIR}/data/textures/checker.png@data/textures/checker.png"
"${PROJECT_SOURCE_DIR}/data/textures/crate.gif@data/textures/crate.gif"
)
add_example(NAME "imgui_framebuffer" LINK_IMGUI WEB)
8 changes: 7 additions & 1 deletion src/threepp/renderers/GLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1128,16 +1128,22 @@ struct GLRenderer::Impl {
}

void copyTextureToImage(Texture& texture) {

#ifdef __EMSCRIPTEN__
std::cerr << "[GLRenderer] copyTextureToImage not available with Emscripten" << std::endl;
return;
#endif
textures.setTexture2D(texture, 0);

auto& image = texture.image();
auto& data = image.data();
const auto newSize = image.width * image.height * (texture.format == Format::RGB || texture.format == Format::BGR ? 3 : 4);
data.resize(newSize);


// Only run this on desktop OpenGL, not in WebGL
glGetTexImage(GL_TEXTURE_2D, 0, gl::toGLFormat(texture.format), gl::toGLType(texture.type), data.data());


state.unbindTexture();
}

Expand Down

0 comments on commit 5e95e0e

Please sign in to comment.