This program provides an interactive real time cloth simulation by using the mass-spring model. It is written in Java and uses LWJGL to access OpenGL. For performance reasons, the calculation of the model is done on the GPU in a compute shader allowing the simulation to use smaller time steps.
- Features
- System Requirements
- Build
- Controls
- Information about the code and program flow
- Results
- References
- Cloth simulated with the mass-spring model considering internal forces (springs) and external forces (gravity, viscous damping (friction), viscous interaction (fluid like mediums, wind))
- Relaxation to ensure length constraints of the joints (joints will not become disproportionately extended) and to make the model more stable
- Phong shading and normal mapping
- Java Version 14 or higher (otherwise small rewrites are necessary to undo new language features)
- OpenGL 4.3 or higher (support for compute shaders)
- Maven
I have tested the program on a Linux system. In the maven configuration file (pom.xml) Windows and MacOS are configured as well and might work.
- Compile with
mvn compile
. - Execute with
mvn exec:java -Dexec.mainClass="massspringcloth.MassSpringClothRender"
.
(Or use a Java IDE with Maven support...)
- User Interface (top left corner):
- Play/Pause button: Start or stop the simulation.
- Scene buttons: Switch or restart the selected scene. Left to right: hanging cloth (wind on/off button below, only enabled for this scene), cloth hanging in the xz-plane from 3 points, cloth falling on a sphere, flag in wind
- Movement and Camera:
- W,A,S,D,Shift,Space: Move the camera forward,left,backward,right,down,up.
- Left mouse button (press and hold) and dragging the mouse: Rotate the camera.
- Mouse wheel: Zoom the camera.
- Other:
- T (press and hold): Render cloth as wireframe (only edges of the mesh visible).
- The package
src/main/java/massspringcloth/
contains all the classes directly related to the construction and simulation of the cloth model. - The package
src/main/java/renderengine/
contains the classes needed to communicate with and access OpenGL. - The resource folder
src/main/resources/shaders/
contains the vertex, fragment and compute shaders for the program. Especially:cloth_compute.glsl
is the compute shader where the main calculation of the new positions of the mass spring model happens.cloth_vert.glsl
andcloth_frag.glsl
are the vertex and fragment shader to render the result.
A few words about the program flow:
- When a
massspringcloth/scene/IScene
is created by themassspringcloth/simulation/SimulationController.java
the initial positions, velocities and locked points are defined. - They are passed to the
massspringcloth/cloth/MassSpringModel.java
where the vertices, texture coordinates and indices are calculated. - After that in the
massspringcloth/cloth/MassSpringCloth.java
the OpenGL buffers (vertex buffer objects (vbo)) for the vertex shader are created as well as the input and output buffers of the compute shader. - When the simulation starts the compute shader is executed multiple times in parallel for each point. It computes the new positions from the positions in the input buffer and writes the updated data to the output buffer as well as to the position vbo of the vertex shader. After each compute shader execution, the input and output buffer of the compute shader are swapped so that it gets the updated data as its new input. Normals and tangents are calculated in the compute shader as well for lighting and normal mapping. Depending on the settings, the compute shader either calculates the new positions due to acting internal and external forces or does relaxation (adjusting the positions of the points to bring two points closer to another) to prevent disproportional extented joints. For each iteration both compute shader "stages" are executed successively.
- After the compute shader has run multiple times, the updated data in the vertex positions buffer is rendered by the vertex and fragment shader.
- Step four and five repeat until the simulation is stopped or the scene is changed.
Cloth hanging from two points.
Cloth hanging from two points with wind.
Cloth hanging in the xz-plane from 3 points.
Cloth falling on a sphere.
Flag (cloth) with wind.
Main OpenGL and LWJGL references that I have used:
- https://learnopengl.com/
- Tutorial series "OpenGL 3D Game Tutorial" by https://www.youtube.com/user/ThinMatrix
- https://www.lwjgl.org/guide
- https://github.com/LWJGL/lwjgl3-wiki/wiki/2.6.1.-Ray-tracing-with-OpenGL-Compute-Shaders-%28Part-I%29 (compute shaders in LWJGL)
Mass-Spring Model for cloth simulation reference:
- X. Provot, “Deformation constraints in a mass-spring model to describe rigid cloth behavior,” in IN GRAPHICS INTERFACE, 1995, pp. 147–154. [Online]. Available: https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.84.1732