NvCloth Cloth Simulation Library

repository·master·Indexed 43 days ago

https://github.com/nvidiagameworks/nvcloth

A high-performance, low-level cloth simulation library for real-time interactive applications and games. It provides robust simulation and collision response for animated characters with minimal integration overhead. Includes support for PhysX compatibility (NvCloth 1.1.6 with PhysX 4.0, or NvCloth 1.1.5 with PhysX 3.4) and Android cross-compilation via android-cmake.

Tokens
4K
Snippets
10
Records
21
Agent score
55%

What's inside NvCloth

  1. Overview of NvCloth

    master
    NvCloth is a low-level cloth solver library designed for real-time interactive applications, such as games. It provides fast and robust cloth simulation, collision detection, and response mechanisms optimized for animated characters. The library is designed with a low-level interface to ensure minimal overhead and easy integration into existing engines.
  2. Optimize ImGui for large lists using clipping

    master

    While ImGui is highly performant, submitting thousands of items (e.g., 1000+) to the UI can incur overhead because ImGui must still calculate the size of every item even if it is not visible.

    To maintain high performance when browsing very large datasets:

    • Manual Clipping: Perform clipping in your own code so you only submit the items currently visible on screen to ImGui.
    • ImGuiListClipper: Use the built-in ImGuiListClipper helper to assist with calculating which items should be rendered based on scroll position.
  3. Ensure PhysX / PxShared compatibility

    master

    When integrating NvCloth with PhysX, ensure the versions of PxShared match the requirements of the NvCloth version being used:

    • For NvCloth 1.1.6: Use the version of PxShared shipped with PhysX 4.0.
    • For PhysX 3.4: You must use NvCloth 1.1.5 instead of 1.1.6.
  4. Manage large font textures and memory

    master

    If you have a very large number of glyphs or multiple fonts, you may hit graphics driver texture size limits. To mitigate this:

    1. Specify texture width: Set io.Fonts.TexDesiredWidth to control the aspect ratio (minimizing height).
    2. Reduce oversampling: Lower config.OversampleH to 2 or 1.
    3. Limit glyph ranges: Only bake the specific character ranges required for your application.
  5. Identify Android architecture and features in CMakeLists.txt

    master

    When writing CMakeLists.txt files intended to be used with android-cmake, you can use the ANDROID variable to trigger platform-specific logic. To detect specific architectures, check the CMAKE_SYSTEM_PROCESSOR variable:

    ValueArchitecture/ABI
    armv5tearmeabi
    armv6armeabi-v6 with VFP
    armv7-aarmeabi-v7a variants
    aarch64arm64-v8a
    i686x86
    x86_64x86_64
    mipsmips
    mips64mips64

    Other useful variables:

    • NEON: Set if the target ABI supports Neon.
    • ANDROID_NATIVE_API_LEVEL: The target API level.
    • ANDROID_NDK_RELEASE: The NDK version.
    • ANDROID_RTTI: Set if RTTI is enabled by the selected STL.
    • ANDROID_EXCEPTIONS: Set if exceptions are enabled by the selected STL.
    if (ANDROID)
        message(STATUS "Hello from Android build!")
        if (CMAKE_SYSTEM_PROCESSOR STREQUAL "armv7-a")
            # ARMv7 specific logic
        endif()
    endif()
  6. Handle UTF-8 encoding and codepoint remapping

    master

    ImGui requires all strings to use UTF-8 encoding.

    • Avoid local code pages: Using local code pages (like CP-923 or CP-1251) in source literals will not work.
    • C++11 Solution: Use the u8"string" syntax for UTF-8 literals.
    • Manual Conversion: Convert text data to UTF-8 before loading or use font->AddRemapChar() to map local codepage characters to Unicode codepoints.
  7. Build android-cmake on Windows

    master

    To build android-cmake natively on Windows, do not use cygwin as it is not supported. It is highly recommended to use Ninja for fast, multithreaded native builds.

    Using Ninja:

    1. Ensure CMake version is newer than 2.8.9.
    2. Download the latest Ninja from GitHub.
    3. Add ninja.exe to your system PATH.
    4. Run CMake with the -GNinja generator flag.

    Using GNU Make (Alternative): If you prefer using a Windows port of GNU Make (such as make.exe from the Android NDK or mingw-make):

    1. Add the path to your make.exe to your system PATH.
    2. Run CMake using the -G"MinGW Makefiles" generator (note: use MinGW Makefiles even if your make is not MinGW's).
    3. Pass -DCMAKE_MAKE_PROGRAM="<full/path/to/make.exe>" to CMake.
    4. Build using make.exe or cmake --build ..
    # Recommended: Using Ninja
    cmake -GNinja <other_args>
    
    # Alternative: Using MinGW Makefiles
    cmake -G"MinGW Makefiles" -DCMAKE_MAKE_PROGRAM="C:/path/to/make.exe" <other_args>
  8. Integrate dear imgui (ImGui) into your project

    master

    ImGui is a bloat-free, renderer-agnostic C++ GUI library designed for fast iteration in real-time 3D applications, tools, and debuggers. It is self-contained and does not require a specific build process; you can simply add its source files directly to your project.

    Core Files to Include

    To integrate ImGui, copy the following files into your project:

    • imgui.cpp
    • imgui.h
    • imgui_demo.cpp
    • imgui_draw.cpp
    • imgui_internal.h
    • imconfig.h (user-editable configuration)
    • stb_rect_pack.h
    • stb_textedit.h
    • stb_truetype.h

    Integration Workflow

    1. Input Handling: Your application must pass mouse and keyboard inputs to ImGui.
    2. Rendering: ImGui outputs vertex buffers and simple command-lists. You are responsible for rendering these buffers using your application's existing 3D pipeline (e.g., OpenGL, DirectX, Vulkan).
    3. Execution: Because ImGui does not touch graphics state directly, you can call ImGui commands anywhere in your code, such as during an algorithm execution or within your own rendering loop.
  9. Build a C/C++ project for Android using android-cmake

    master

    To compile native code for Android using the android-cmake scripts, use the android.toolchain.cmake file with the CMake command line. You must specify the NDK path, the build type (either Release or Debug), and the target ABI.

    Requirements:

    • Android NDK (>= r5)
    • CMake (>= v2.6.3, v2.8.9 recommended)

    NDK Discovery Order:

    1. ANDROID_NDK CMake variable
    2. ANDROID_NDK environment variable
    3. Paths in ANDROID_NDK_SEARCH_PATHS CMake variable
    4. Platform-specific locations (e.g., home folder, Windows "Program Files")
    # Configure the project
    cmake -DCMAKE_TOOLCHAIN_FILE=android.toolchain.cmake \
          -DANDROID_NDK=<ndk_path>                       \
          -DCMAKE_BUILD_TYPE=Release                     \
          -DANDROID_ABI="armeabi-v7a with NEON"          \
          <source_path>
    
    # Build the project
    cmake --build .
  10. Merge icon fonts into a main font

    master

    To use icons (like FontAwesome) alongside your main text, use MergeMode = true in an ImFontConfig. This combines the icon glyphs into the existing font atlas.

    Important: The character ranges array passed to AddFont* functions is used lazily and is not copied. You must ensure the ranges array remains in scope for the entire duration of the font usage.

    // Load a first font
    ImGuiIO& io = ImGui::GetIO();
    io.Fonts->AddFontDefault();
    
    // Define ranges (must stay in scope)
    static const ImWchar icons_ranges[] = { 0xf000, 0xf3ff, 0 };
    
    ImFontConfig config;
    config.MergeMode = true;
    
    // Add the icon font using MergeMode
    io.Fonts->AddFontFromFileTTF("fontawesome-webfont.ttf", 18.0f, &config, icons_ranges);
  11. Access NvCloth documentation and API reference

    master

    Detailed information for NvCloth is organized into several locations within the repository:

    • Release Notes, API User Guide, and Compiling Instructions: Located at ./NvCloth/docs/documentation/index.html.
    • API Documentation (Doxygen): Located at ./NvCloth/docs/doxy/index.html.
    • Changes and Platform Support: See ./NvCloth/ReleaseNotes.txt.
  12. Correctly append compiler flags in Android builds

    master

    When adding custom flags to your C or C++ targets, always append them to the existing flags rather than overwriting them. android-cmake prepopulates CMAKE_C_FLAGS and CMAKE_CXX_FLAGS with critical Android-specific flags.

    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${my_c_flags}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${my_cxx_flags}")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${my_cxx_flags}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${my_cxx_flags}")