Optimize meshes with meshoptimizer library if available
parent
746b9e3ef3
commit
f4bd55991f
|
@ -245,6 +245,14 @@ endif()
|
||||||
find_package(Freetype REQUIRED)
|
find_package(Freetype REQUIRED)
|
||||||
link_libraries(Freetype::Freetype)
|
link_libraries(Freetype::Freetype)
|
||||||
|
|
||||||
|
find_package(meshoptimizer CONFIG QUIET)
|
||||||
|
if(meshoptimizer_FOUND)
|
||||||
|
message(STATUS "Found meshoptimizer library")
|
||||||
|
set(HAVE_MESHOPTIMIZER 1)
|
||||||
|
else()
|
||||||
|
message(STATUS "meshoptimizer library is missing")
|
||||||
|
endif()
|
||||||
|
|
||||||
#[[
|
#[[
|
||||||
get_cmake_property(_variableNames VARIABLES)
|
get_cmake_property(_variableNames VARIABLES)
|
||||||
list (SORT _variableNames)
|
list (SORT _variableNames)
|
||||||
|
|
|
@ -3,4 +3,5 @@
|
||||||
#cmakedefine HAVE_FLOAT_CHARCONV
|
#cmakedefine HAVE_FLOAT_CHARCONV
|
||||||
#cmakedefine HAVE_STD_FILESYSTEM
|
#cmakedefine HAVE_STD_FILESYSTEM
|
||||||
#cmakedefine HAVE_WORDEXP
|
#cmakedefine HAVE_WORDEXP
|
||||||
|
#cmakedefine HAVE_MESHOPTIMIZER
|
||||||
#cmakedefine WORDS_BIGENDIAN
|
#cmakedefine WORDS_BIGENDIAN
|
||||||
|
|
|
@ -80,6 +80,10 @@ if(ENABLE_FFMPEG)
|
||||||
target_link_libraries(celestia ${FFMPEG_LIBRARIES})
|
target_link_libraries(celestia ${FFMPEG_LIBRARIES})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if (HAVE_MESHOPTIMIZER)
|
||||||
|
target_link_libraries(celestia PRIVATE meshoptimizer::meshoptimizer)
|
||||||
|
endif()
|
||||||
|
|
||||||
install(TARGETS celestia LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} NAMELINK_SKIP)
|
install(TARGETS celestia LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} NAMELINK_SKIP)
|
||||||
|
|
||||||
add_subdirectory(glut)
|
add_subdirectory(glut)
|
||||||
|
|
|
@ -16,6 +16,10 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <celutil/logger.h>
|
#include <celutil/logger.h>
|
||||||
|
|
||||||
|
#ifdef HAVE_MESHOPTIMIZER
|
||||||
|
#include <meshoptimizer.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "mesh.h"
|
#include "mesh.h"
|
||||||
|
|
||||||
using celestia::util::GetLogger;
|
using celestia::util::GetLogger;
|
||||||
|
@ -455,6 +459,21 @@ Mesh::mergePrimitiveGroups()
|
||||||
groups = std::move(newGroups);
|
groups = std::move(newGroups);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Mesh::optimize()
|
||||||
|
{
|
||||||
|
#ifdef HAVE_MESHOPTIMIZER
|
||||||
|
if (groups.size() > 1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto &g = groups.front();
|
||||||
|
|
||||||
|
meshopt_optimizeVertexCache(g.indices.data(), g.indices.data(), g.indices.size(), nVertices);
|
||||||
|
meshopt_optimizeOverdraw(g.indices.data(), g.indices.data(), g.indices.size(), reinterpret_cast<float*>(vertices.data()), nVertices, vertexDesc.strideBytes, 1.05f);
|
||||||
|
meshopt_optimizeVertexFetch(vertices.data(), g.indices.data(), g.indices.size(), vertices.data(), nVertices, vertexDesc.strideBytes);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
Mesh::pick(const Eigen::Vector3d& rayOrigin, const Eigen::Vector3d& rayDirection, PickResult* result) const
|
Mesh::pick(const Eigen::Vector3d& rayOrigin, const Eigen::Vector3d& rayDirection, PickResult* result) const
|
||||||
{
|
{
|
||||||
|
@ -769,4 +788,5 @@ Mesh::canMerge(const Mesh &other, const std::vector<Material> &materials) const
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // end namespace cmod
|
} // end namespace cmod
|
||||||
|
|
|
@ -239,6 +239,7 @@ class Mesh
|
||||||
|
|
||||||
void merge(const Mesh&);
|
void merge(const Mesh&);
|
||||||
bool canMerge(const Mesh&, const std::vector<Material> &materials) const;
|
bool canMerge(const Mesh&, const std::vector<Material> &materials) const;
|
||||||
|
void optimize();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PrimitiveGroup createLinePrimitiveGroup(bool lineStrip, const std::vector<Index32>& indices);
|
PrimitiveGroup createLinePrimitiveGroup(bool lineStrip, const std::vector<Index32>& indices);
|
||||||
|
|
|
@ -318,7 +318,6 @@ Model::usesTextureType(TextureSemantic t) const
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
Model::OpacityComparator::operator()(const Mesh& a, const Mesh& b) const
|
Model::OpacityComparator::operator()(const Mesh& a, const Mesh& b) const
|
||||||
{
|
{
|
||||||
|
@ -353,6 +352,9 @@ Model::sortMeshes(const MeshComparator& comparator)
|
||||||
p.merge(meshes[i]);
|
p.merge(meshes[i]);
|
||||||
}
|
}
|
||||||
GetLogger()->info("Merged similar meshes: {} -> {}.\n", meshes.size(), newMeshes.size());
|
GetLogger()->info("Merged similar meshes: {} -> {}.\n", meshes.size(), newMeshes.size());
|
||||||
|
|
||||||
|
for (auto &mesh : newMeshes)
|
||||||
|
mesh.optimize();
|
||||||
meshes = std::move(newMeshes);
|
meshes = std::move(newMeshes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue