This is my attempt at creating a mass-spring cloth simulation. There were many moving pieces to this project, but lets start with a demo of the results (resolution 100 x 100 points):

#### Step 1. Create cloth

Instead of just animating a 3D plane mesh, I wanted to create and render the mesh in my engine. This would give me the capability to easily set some parameters to increase or decrease to resolution of the cloth.

For my engine, I use EBO’s (element buffer objects) to render triangles in OpenGL’s glDrawElements function. So I decided to create the triangles for the mesh in a z-order chain:

- Black lines represent the order of vertex indices in the ebo object
- Green circles represent the start of each chain of vertices
- Red circles represent the end of a chain
- Gray dashes represent places where GL_TRIANGLE_STRIP infers triangle boundaries
- Chains ends are read by OpenGL by inserting the hexadecimal
*0xffffff*in the buffer as an extra indices between a beginning and end indices

#### Step 2. Choose Integration method

There are many possible integration methods that can drive a mass-spring system. In particular, I choose to use the 2nd-order midpoint method. For my cloth, there were springs connecting all adjacent vertices in a star-like pattern.

Even with the midpoint method, I needed to run the simulation in my compute shader 100 times per frame to avoid explosion. I set a fairly high spring constant to keep the cloth stiff and introduced a bit of dampening so that the cloth would settle down over time.

If I could redo this, I’d try to implement RK4 (Runge-Kutta) to see if it reduces the number of iterations I need to maintain stability in the simulation.

#### Step 3. Render

The process from start to finish:

- Create cloth vertices and UVs on the cpu for vertex array objects and compute shader simulation
- Iterate thru cloth’s vertices in compute shader using midpoint method and compute forces from:
- Gravity
- Wind
- Object collision

- Compute normals in a 2nd compute shader based in final resting position
- Texture and render using phong illumination in fragment shader

#### Final result: