Skip to content

Commit 34fbbf6

Browse files
committed
(Mesh) Added mesh optimizer.
1 parent 6fb1304 commit 34fbbf6

7 files changed

Lines changed: 108 additions & 1 deletion

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@
5858
[submodule "libs/GameNetworkingSockets"]
5959
path = libs/GameNetworkingSockets
6060
url = https://github.com/SpaRcle-Studio/GameNetworkingSockets.git
61+
[submodule "libs/meshoptimizer"]
62+
path = libs/meshoptimizer
63+
url = https://github.com/SpaRcle-Studio/meshoptimizer

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ option(SR_COMMON_MYHTML "" OFF)
3737
option(SR_COMMON_LITEHTML "" OFF)
3838
option(SR_COMMON_ASIO "" OFF)
3939
option(SR_COMMON_GNS "" OFF)
40+
option(SR_COMMON_MESHOPTIMIZER "" ON)
4041

4142
option(SR_COMMON_DLL_EXPORTS "" ON)
4243
option(SR_COMMON_GIT_METADATA "" ON)
@@ -163,6 +164,19 @@ endif()
163164

164165
add_subdirectory(libs/glm)
165166

167+
if (SR_COMMON_MESHOPTIMIZER)
168+
message(STATUS "SRCommon will be using meshoptimizer")
169+
170+
add_compile_definitions(SR_COMMON_MESHOPTIMIZER)
171+
172+
set(MESHOPT_BUILD_DEMO OFF CACHE INTERNAL "" FORCE)
173+
set(MESHOPT_BUILD_GLTFPACK OFF CACHE INTERNAL "" FORCE)
174+
set(MESHOPT_BUILD_SHARED_LIBS OFF CACHE INTERNAL "" FORCE)
175+
set(MESHOPT_INSTALL OFF CACHE INTERNAL "" FORCE)
176+
177+
add_subdirectory(libs/meshoptimizer)
178+
endif()
179+
166180
if (SR_UTILS_FMT_HEADER_ONLY)
167181
add_definitions(
168182
-DFMT_HEADER_ONLY

cmake/SRCommonIncludes.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ if (SR_COMMON_JSON)
1919
SR_UTILS_INCLUDE_DIRECTORIES_APPEND(${CMAKE_CURRENT_SOURCE_DIR}/libs/json/include)
2020
endif()
2121

22+
if (SR_COMMON_MESHOPTIMIZER)
23+
SR_UTILS_INCLUDE_DIRECTORIES_APPEND(${CMAKE_CURRENT_SOURCE_DIR}/libs/meshoptimizer/src)
24+
endif()
25+
2226
if (SR_COMMON_OPENSSL)
2327
SR_UTILS_INCLUDE_DIRECTORIES_APPEND(${CMAKE_CURRENT_SOURCE_DIR}/libs/openssl/include)
2428
endif()

cmake/SRCommonLinking.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ if (SR_COMMON_SDL)
2222
target_link_libraries(Utils SDL3::SDL3)
2323
endif()
2424

25+
if (SR_COMMON_MESHOPTIMIZER)
26+
target_link_libraries(Utils meshoptimizer)
27+
endif()
28+
2529
if (SR_COMMON_ZLIB)
2630
target_link_libraries(Utils zlibstatic)
2731
endif()

inc/Utils/Common/Vertices.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#ifndef SR_ENGINE_SKYBOXCONSTANTS_H
66
#define SR_ENGINE_SKYBOXCONSTANTS_H
77

8-
#include <Utils/stdInclude.h>
8+
#include <Utils/Types/FastMemoryArray.h>
99

1010
namespace SR_UTILS_NS {
1111
struct Vec2 {
@@ -57,6 +57,37 @@ namespace SR_UTILS_NS {
5757

5858
std::vector<Vertex> ComputeConvexHull(const std::vector<Vertex>& vertices);
5959

60+
namespace Details {
61+
bool OptimizeVerticesImpl(
62+
const void* vertices,
63+
uint64_t vertexSize,
64+
uint64_t verticesCount,
65+
const SR_HTYPES_NS::FastMemoryArray<uint32_t>& indices,
66+
uint64_t targetIndicesCount,
67+
float_t targetError,
68+
SR_HTYPES_NS::FastMemoryArray<uint32_t>& outSimplifiedIndices
69+
);
70+
}
71+
72+
template<typename T> bool OptimizeVertices(
73+
const SR_HTYPES_NS::FastMemoryArray<T>& vertices,
74+
const SR_HTYPES_NS::FastMemoryArray<uint32_t>& indices,
75+
uint64_t targetIndicesCount,
76+
float_t targetError,
77+
SR_HTYPES_NS::FastMemoryArray<uint32_t>& outSimplifiedIndices
78+
) {
79+
SR_TRACY_ZONE;
80+
return Details::OptimizeVerticesImpl(
81+
vertices.data(),
82+
sizeof(T),
83+
vertices.size(),
84+
indices,
85+
targetIndicesCount,
86+
targetError,
87+
outSimplifiedIndices
88+
);
89+
}
90+
6091
template<typename T> static std::vector<T> IndexedVerticesToNonIndexed(
6192
const std::vector<T>& vertices,
6293
const std::vector<uint32_t>& indices)

libs/meshoptimizer

Submodule meshoptimizer added at edbbfba

src/Utils/Common/Vertices.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,57 @@
88
// #include <Hull/Coordinate.h>
99
// #include <QuickHull/FastQuickHull.h>
1010

11+
#ifdef SR_COMMON_MESHOPTIMIZER
12+
#include <meshoptimizer.h>
13+
#endif
14+
1115
namespace SR_UTILS_NS {
16+
namespace Details {
17+
bool OptimizeVerticesImpl(
18+
const void* vertices,
19+
uint64_t vertexSize,
20+
uint64_t verticesCount,
21+
const SR_HTYPES_NS::FastMemoryArray<uint32_t>& indices,
22+
uint64_t targetIndicesCount,
23+
float_t targetError,
24+
SR_HTYPES_NS::FastMemoryArray<uint32_t>& outSimplifiedIndices
25+
) {
26+
SR_TRACY_ZONE;
27+
28+
outSimplifiedIndices.resize(indices.size());
29+
30+
#ifdef SR_COMMON_MESHOPTIMIZER
31+
float resultError = 0.0f;
32+
uint32_t options = 0;
33+
34+
size_t resultCount = meshopt_simplify(
35+
outSimplifiedIndices.data(),
36+
indices.data(),
37+
indices.size(),
38+
(float*)vertices,
39+
verticesCount,
40+
vertexSize,
41+
targetIndicesCount,
42+
targetError,
43+
options,
44+
&resultError
45+
);
46+
47+
if (resultCount == 0) {
48+
SR_ERROR("SR_UTILS_NS::Details::OptimizeVerticesImpl() : mesh simplification failed!");
49+
outSimplifiedIndices.clear();
50+
return false;
51+
}
52+
53+
outSimplifiedIndices.resize(resultCount);
54+
return true;
55+
#else
56+
memcpy(outSimplifiedIndices.data(), indices.data(), indices.size() * sizeof(uint32_t));
57+
return true;
58+
#endif
59+
}
60+
}
61+
1262
std::vector<Vertex> ComputeConvexHull(const std::vector<Vertex>& vertices) {
1363
/*std::vector<hull::Coordinate> coordinates;
1464
coordinates.reserve(vertices.size());

0 commit comments

Comments
 (0)