/soupsim

cs 184 final project

Primary LanguageC++

<style> body { padding: 100px; width: 1000px; margin: auto; text-align: left; font-weight: 300; font-family: 'Open Sans', sans-serif; color: #121212; } h1, h2, h3, h4 { font-family: 'Source Sans Pro', sans-serif; } </style> <title>CS 184 Cloth Sim</title>

CS 184: Computer Graphics and Imaging, Spring 2021

Project 4: Cloth Simulator

Katelyn Biesiadecki and Amy Liu



Overview

In this project, we use a mass-spring based grid system to simulate the movement of cloth. We will incorporate factors such as collisions and lighting to create a more realistic rendering of cloth.


Part I: Masses and springs

To set the foundation for the project, we started by building an evenly spaced grid of masses and springs. While the mass grid was a simple assingment of values spaced out by a delta ( (max - min)/num_points for width and height), the spring vector was slightly more complicated because we had to account for edge cases for each spring constraint (SHEARING, BENDING, and STRUCTURAL). For example, a constraint between a mass on an edge and a mass to the left of that edge should be ignored. (In 2D-array space, going to the previous index does not account for edges, e.g. index 16 to 17 could be width and width + 1).

pinned2.json - closeup pinned2.json - full view

Here's what the constraints look like:

pinned2.json - no shearing constraints pinned2.json - only shearing constraints
pinned2.json - all constraints

As you can see in the images above, the shearing constraints appear as connecting diagonal elements. Structural and bending constraints appear as a grid because they connect adjacent (non-diagonal) elements.


Part II: Simulation via numerical integration

In part 2, we first began by computing the total force acting on each point mass by incorporating external accelerationd and Hooke's Law. We then compute the change in positions using Verlet integration, ultimately implementing the equation



Finally, we add constraints, noting that a spring length can't be 10% greater than its rest length. Throughout the whole process, we take into account whether a point is pinned, at which point we make no modifications to the position of the point.

density 1 density 1000
k = 1 k = 100000
high damping low damping

Higher ks give a much stiffer cloth and falls with less movement, whereas low ks makes a lot of free flowing wiggling as it falls. High density makes the cloth much ehavier and solid, so that whne it falls doesn't have many wrinkles or folds. At the same time, it seems pretty flexible because when it falls onto the sphere, it's completely smooth. In contrast, low density makes the cloth light and wrinkly, but also somewhat stiff as it doesn't wrap itself smoothly around the sphere. With high damping, the cloth falls very slowly and immediately settles into its resting position. With low damping, the cloth falls very quickly and bounces a lot until it settles.

final resting state

Part III: Handling collisions with other objects

In this part, we handled collisions of cloth with a sphere and with a plane. For the sphere collision, we computed the tangent point of the surface intersection as well as a correciton vector to apply to the point mass in order to bring it to the tangent point. We do roughly the same thing for the plane.

Flat on plane

As you can see in the sphere image, as the spring constant increases from 500 to 50000, the fabric appears to get more and more stiff. This is because springs with a higher spring constant are stiffer. You can imagine this as the difference between silk and denim.

sphere.json - 500 ks sphere.json - 5,000 ks (default)
sphere.json - 50,000 ks


Part IV: Handling self-collisions

In this section, we implemented self-collisions. First, we computed a hash to partition the point masses into a 3D box with dimensions w * h * t = 3 * width / num_width_points x 3 * height / num_height_points x max(w, h) . We then do a simple division between the x, y, and z coordinates with w, h, and t, respectively, and floor the result to bin the point masses. Then, we construct our spatial map and use that in our Cloth::self_collide() function. This way, we don't have to iterate through the entire grid every time we want to test a candidate point mass. Using the point masses in map at a computed hash, we then check whether the distance between a point mass and its candidate point masses are within 2*thickness and if so, compute a correction vector to offset the point mass such that it is no longer too close to the candidate.


Unfortunately, we were not able to get this part completely working. Here is a screenshot of it as is:

Part 4 as is

Varying the density would have the effect of making the fabric thinner, causing it to fold over more. As in the other parts, increasing the spring constant makes the fabric stiffer and less flexible when it folds on itself, causing it to be more rigid.


Part V: Shaders

Background

Shaders are programs that can run individually and can compute information that helps render lighting, color, etc. Vertex shaders modify geometic properties of vertices, which are then passed into fragment shaders to help compute color. Blinn-Phong shading expands upon the Lambertian shading model and incorporates viewer angle and specular lighting to create a more accurate lighting model.

Implementation

In this part, we added shading and texture to the cloth. We first started by incorporating diffuse lighting, following the formula:

We then moved onto the Blinn-Phong shading model, following the formula:

Blinn-Phong ambient component Blinn-Phong diffuse component
Blinn-Phong specular component

For the next section, we simply called the built-in texture method to add a texture's color to our cloth.

Custom texture

Here are the screenshots of bump mapping on the cloth and the sphere.

Bump mapping on sphere Bump mapping on cloth
Displacement mapping on sphere Displacement mapping on cloth
Low contrast High contrast
coarseness = 16 coarseness = 128

Here are screenshots of the mirror on the sphere and on the cloth.

Mirror on cloth Mirror on sphere

For 5.4, we adjusted our normal vectors by first computing our local space normals with

then taking those normals and multiplying it with the TBN matrix to find our displaced model space normal. Using this normal in our color calculations gives our items the illusion of textured material. In 5.5, we expanded upon this texture by recalculating some of the positions within our vertices using

Updating our positions then gives our materials not only makes the lighting look like its textured, but also physically changes our cloth so that its position is displaced and giving it an actual physical texture. All in all, bump texture looks like it has texture, but the cloth shape is still smooth, whereas displacement texture has both textured lighting and the physical cloth itself has changed positions and texture. When coarseness is changed, it’s very hard to see anything different zoomed out. When zoomed in, the 128 coarseness is just slightly more high contrast and textured than the 16 coarseness sphere.
Finally, for part 5.5, we simply reflected the outgoing eye-ray across our surface normal to generate the mirrored effect, using the texture that we sampled from the environment map.
Shaders are programs that can run individually and can compute information that helps render lighting, color, etc. Vertex shaders modify geometric properties of vertices, which are then passed into fragment shaders to help compute color. Blinn-Phong shading expands upon the Lambertian shading model and incorporates viewer angle and specular lighting to create a more accurate lighting model.


Addendum: Working together

Overall, working on this project was no different than the others. We find that allowing one person to write the first pass of the code and having the other person help debug it is a good way to do it. This way, each person gets the space to do their own thinking without the pressure of another person waiting on them. By the time the structure of the code is written, its easier to attack it together and make the slight modifications like changing formulas. Unfortunately, we both have a bad case of senioritis which is why we're turning the project in two days late.