In this project, I built a physically based cloth simulation using a mass-spring model with structural, shearing, and bending constraints. I experimented with parameters like spring constant, density, and damping to observe their effects on cloth behavior, capturing how stiffness and weight impact motion and settling. I implemented collisions with spheres and planes, as well as self-collisions using spatial hashing, and tested how different values affect folding and interaction. On the rendering side, I used shaders to apply Blinn-Phong lighting, texture mapping, bump and displacement mapping, and mirror effects. This project deepened my understanding of both simulation stability and shader-based visual effects.
|
|
|
Here I experimented with some the parameters in the simulation.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Here is a picture of the 4 pinned corners shaded cloth in its final resting state.
It checks if the point mass is inside the sphere by comparing the squared distance from the sphere's center with the squared radius. If the point is inside, it is projected outwards to the sphere's surface, using the appropriate direction vector with friction applied to dampen the movement.
The algorithm checks if the point mass is below the plane (using the dot product with the normal). If so, it moves it along the plane normal to just above the plane (with a SURFACE_OFFSET) and adjusts the movement based on friction, ensuring the point mass doesn't "stick" unrealistically.
|
|
Here are a few screenshots that document how my cloth falls and folds on itself, starting with an early, initial self-collision and ending with the cloth at a more restful state.
|
|
|
High ks and low density are similar, and same vice versa.
|
|
|
|
|
|
|
|
As we vary the density and the ks of the cloth, we observe distinct differences in how the cloth behaves as it falls and interacts with itself. For low density and high ks values, the cloth appears smooth and rigid. The low density means the cloth has less mass pulling it downward due to gravity, and the high stiffness resists deformation, so the cloth maintains its shape more effectively. As a result, the fabric falls gently and drapes with minimal wrinkles, almost like a lightweight sheet of stiff paper. Conversely, for high density and low ks values, the cloth appears crumpled or 'crunchy'. The higher density increases the gravitational force acting on the cloth, pulling it down more aggressively, while the low stiffness means the springs can't strongly resist deformation. This leads to more pronounced folds, wrinkles, and intersections as the cloth collapses onto itself, resembling a soft and heavy fabric like velvet or a thick curtain.
A shader program is a set of instructions executed on the graphics processing unit (GPU) to control the rendering of objects on the screen, often written in a shading language like GLSL. The program is typically divided into two primary sections that work together:
This stage operates on individual vertices. Its core responsibility is to take the raw vertex data (such as position, normal vectors, and texture coordinates) and transform it into a coordinate system suitable for display—typically converting the data to normalized device coordinates. Essentially, the vertex shader lays the groundwork by positioning each vertex within the 3D space and passing along data necessary for later stages.
After the vertices have been processed and positioned, the fragment shader takes over to determine the final color and appearance of each pixel (or fragment) that will be displayed. It uses interpolated data from the vertex shader—such as texture coordinates and color—and applies material properties and lighting effects (like specular highlights or diffuse reflections) to create realistic imagery. This process efficiently simulates detailed material and lighting effects.
Together, the vertex and fragment shaders form a powerful rendering pipeline. By combining geometric transformations with color and lighting computations, shader programs deliver realistic material appearances in real time without the high computational cost of techniques like exhaustive ray tracing.
The Blinn-Phong shading model simulates how light interacts with surfaces by combining three lighting components: ambient, diffuse, and specular.
|
|
|
|
|
|
Here is a screenshot of bump mapping on the cloth and on the sphere and a screenshot of displacement mapping on the sphere using a texture image of Berkeley's Campanile.
|
|
Bump mapping and displacement mapping are both techniques for simulating surface detail, but they work in fundamentally different ways. With bump mapping, detailed surface features are simulated by perturbing the surface normals used in lighting calculations without altering the actual geometry. In contrast, displacement mapping modifies the actual positions of the vertices based on a texture. This means that the surface geometry itself is altered, creating real, physical depth. We can clearly see this phenomenon above with the displacement sphere's geometry actually warped.
We can also change the sphere mesh's coarseness by using -o 16 -a 16 and then -o 128 -a 128. Visually, bump mapping seems to perform better than displacement mapping for this low sampling rate but as we increase the sample rate displacement mapping while a more computationally expensive task, it can better capture the texture associated perturbations.
|
|
|
|
|
|
I also decided to make an extra cartoon style shader where the diffuse light is calculated using the dot product between the normalized normal and the light direction, then quantized by multiplying by a preset number of levels, flooring the result, and finally dividing back. A small ambient factor (set here to 0.1) is added to prevent areas from becoming completely dark, and the final color is computed by multiplying the base color with the combined lighting which can be changed using the color wheel.
|
|