Skip to content

Commit c764f8c

Browse files
committed
Replace TinyGLTF with tinygltf and redesign fallback behavior for missing library.
1 parent 0ae3fa8 commit c764f8c

File tree

2 files changed

+53
-32
lines changed

2 files changed

+53
-32
lines changed
Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ find_path(TinyGLTF_INCLUDE_DIR
3232
)
3333

3434
include(FindPackageHandleStandardArgs)
35-
find_package_handle_standard_args(TinyGLTF
35+
find_package_handle_standard_args(tinygltf
3636
REQUIRED_VARS TinyGLTF_INCLUDE_DIR
3737
FAIL_MESSAGE "" # Suppress the error message to allow our fallback
3838
)
@@ -51,41 +51,62 @@ if(TinyGLTF_FOUND)
5151
endif()
5252
endif()
5353
else()
54-
# If not found, use FetchContent to download and build
55-
include(FetchContent)
54+
# If not found, create a custom tinygltf implementation
55+
message(STATUS "TinyGLTF not found, creating a custom implementation...")
5656

57-
message(STATUS "TinyGLTF not found, fetching from GitHub...")
58-
FetchContent_Declare(
59-
tinygltf
60-
GIT_REPOSITORY https://github.com/syoyo/tinygltf.git
61-
GIT_TAG v2.8.18 # Use a specific tag for stability
57+
# Create a directory for our custom tinygltf implementation
58+
set(TINYGLTF_DIR "${CMAKE_CURRENT_BINARY_DIR}/tinygltf")
59+
file(REMOVE_RECURSE "${TINYGLTF_DIR}")
60+
file(MAKE_DIRECTORY "${TINYGLTF_DIR}")
61+
62+
# Download the necessary files directly
63+
file(DOWNLOAD
64+
"https://raw.githubusercontent.com/syoyo/tinygltf/v2.8.18/tiny_gltf.h"
65+
"${TINYGLTF_DIR}/tiny_gltf.h"
66+
SHOW_PROGRESS
6267
)
6368

64-
# Configure tinygltf before making it available
65-
FetchContent_GetProperties(tinygltf)
66-
if(NOT tinygltf_POPULATED)
67-
FetchContent_Populate(tinygltf)
68-
69-
# Update the minimum required CMake version to avoid deprecation warning
70-
file(READ "${tinygltf_SOURCE_DIR}/CMakeLists.txt" TINYGLTF_CMAKE_CONTENT)
71-
string(REPLACE "cmake_minimum_required(VERSION 3.6)"
72-
"cmake_minimum_required(VERSION 3.10)"
73-
TINYGLTF_CMAKE_CONTENT "${TINYGLTF_CMAKE_CONTENT}")
74-
file(WRITE "${tinygltf_SOURCE_DIR}/CMakeLists.txt" "${TINYGLTF_CMAKE_CONTENT}")
75-
76-
# Create a symbolic link to make nlohmann/json.hpp available
77-
if(EXISTS "${tinygltf_SOURCE_DIR}/json.hpp")
78-
file(MAKE_DIRECTORY "${tinygltf_SOURCE_DIR}/nlohmann")
79-
file(CREATE_LINK "${tinygltf_SOURCE_DIR}/json.hpp" "${tinygltf_SOURCE_DIR}/nlohmann/json.hpp" SYMBOLIC)
80-
endif()
69+
file(DOWNLOAD
70+
"https://raw.githubusercontent.com/syoyo/tinygltf/v2.8.18/json.hpp"
71+
"${TINYGLTF_DIR}/json.hpp"
72+
SHOW_PROGRESS
73+
)
8174

82-
# Set tinygltf to header-only mode
83-
set(TINYGLTF_HEADER_ONLY ON CACHE BOOL "Use header only version" FORCE)
84-
set(TINYGLTF_INSTALL OFF CACHE BOOL "Do not install tinygltf" FORCE)
75+
file(DOWNLOAD
76+
"https://raw.githubusercontent.com/syoyo/tinygltf/v2.8.18/stb_image.h"
77+
"${TINYGLTF_DIR}/stb_image.h"
78+
SHOW_PROGRESS
79+
)
8580

86-
# Add the subdirectory after modifying the CMakeLists.txt
87-
add_subdirectory(${tinygltf_SOURCE_DIR} ${tinygltf_BINARY_DIR})
88-
endif()
81+
file(DOWNLOAD
82+
"https://raw.githubusercontent.com/syoyo/tinygltf/v2.8.18/stb_image_write.h"
83+
"${TINYGLTF_DIR}/stb_image_write.h"
84+
SHOW_PROGRESS
85+
)
86+
87+
# Create a symbolic link to make nlohmann/json.hpp available
88+
file(MAKE_DIRECTORY "${TINYGLTF_DIR}/nlohmann")
89+
file(CREATE_LINK "${TINYGLTF_DIR}/json.hpp" "${TINYGLTF_DIR}/nlohmann/json.hpp" SYMBOLIC)
90+
91+
# Create a simple CMakeLists.txt file
92+
file(WRITE "${TINYGLTF_DIR}/CMakeLists.txt" "
93+
cmake_minimum_required(VERSION 3.10...3.29)
94+
project(tinygltf)
95+
96+
if(NOT TARGET tinygltf)
97+
add_library(tinygltf INTERFACE)
98+
target_include_directories(tinygltf INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
99+
target_compile_definitions(tinygltf INTERFACE
100+
TINYGLTF_IMPLEMENTATION
101+
TINYGLTF_NO_EXTERNAL_IMAGE
102+
TINYGLTF_NO_STB_IMAGE
103+
TINYGLTF_NO_STB_IMAGE_WRITE
104+
)
105+
endif()
106+
")
107+
108+
# Add the subdirectory
109+
add_subdirectory(${TINYGLTF_DIR} ${CMAKE_CURRENT_BINARY_DIR}/tinygltf-build)
89110

90111
# Create an alias for the tinygltf target
91112
if(NOT TARGET tinygltf::tinygltf)

attachments/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export namespace vk {
194194
endif()
195195

196196
find_package (tinyobjloader REQUIRED)
197-
find_package (TinyGLTF REQUIRED)
197+
find_package (tinygltf REQUIRED)
198198
find_package (KTX REQUIRED)
199199

200200
# set up Vulkan C++ module

0 commit comments

Comments
 (0)