<p>The first piece of deforming a mesh is the mesh itself. A very simple mesh with very few vertices can be deformed but the results will be limited, there is only so much that can be done with 8 vertices. (The code posted in the question refers to a box, usually those have only 8 unique vertices which is a good simple object to use when setting up/learning graphics). But to do any interesting deformations a mesh that is nicely tessellated is needed. This can be done by adding tessellation shaders, which aren't to hard to setup but can be confusing for someone just learning GLSL/OpenGL. The other choice is to replace the box with something that has more triangles, a few hundred usually is enough to start getting interesting results.</p>
<p>The code to do deformations come in so many forms that I think there is literally an infinite number of possibilities. I will list a few simple techniques to hopefully get you started.</p>
<p>The first &quot;class&quot; of deformations are simple matrix deformations that can be created by adding an extra matrix into the usual MVP, and the simplest form I can think of is a non-symmetric scale such as:</p>
<p><span class="math-container">\begin{equation*}
D_scale = 
\begin{bmatrix}
x &amp; 0 &amp; 0 &amp; 0 \\
0 &amp; y &amp; 0 &amp; 0 \\
0 &amp; 0 &amp; z &amp; 0 \\
0 &amp; 0 &amp; 0 &amp; 1 \\
\end{bmatrix}
\end{equation*}</span></p>
<p>Where x,y and z are not equal to each other. Something like <span class="math-container">$\{0.6, 1.3, 2.6\}$</span>.</p>
<p>A shear/skew matrix along a specific axes is of the form:
<span class="math-container">\begin{equation*}
D_skew = 
\begin{bmatrix}
1 &amp; tan(\theta) &amp; 0 &amp; 0 \\
0 &amp; 1 &amp; 0 &amp; 0 \\
0 &amp; 0 &amp; 1 &amp; 0 \\
0 &amp; 0 &amp; 0 &amp; 1 \\
\end{bmatrix}
\end{equation*}</span></p>
<p>Combine skews and non-uniform scales by multiplying them together.</p>
<pre><code>D_combined = D_skew * D_scale; // order matters here, swap them around for different effects.
</code></pre>
<p>The vertex shader can move/wiggle vertices around with a simple noise generator. Keep the movement very small or the mesh will just look a mess of unconnected triangles. Here is a nice intro to <a href="https://thebookofshaders.com/11/" rel="nofollow noreferrer">noise generators</a>.</p>
<pre><code>// to use the generated noise do something similar to the below:
vec4 generated_noise = MakeNoise( vertex_input_position);
float scale = 0.02;  // scale the noise up or down
vec4 new_vertex_position = vertex_input_position + generated_noise * scale;

// now process the new vertex position as usual...
vec4 output_vertex_position = MVP * new_vertex_position;
</code></pre>
<p>Another form of mesh deformation are simple functions that move the vertices around using math formulas. A personal favorite is to move all the vertices along a straight line out to some limit. The line is made by creating a vector from a point (usually at the center of the mesh) to the vertex that is being processed, normalize it, multiply by a scale factor, then clamp. This effectively creates a sphere and is very easy to animate by passing the current time to the shader through a uniform variable. Use <span class="math-container">$cos(time)*scale$</span> to make the object &quot;throb&quot;.</p>
<p>This post has gotten a lot longer then I expected so I won't post any code for this last one. Anyway, hopefully this post illustrates that the possibilities for deformations are limited only by the coders imagination.</p>

CC BY-SA 4.0
2022-03-22T14:57:22.383Z
2022-03-22T14:57:22.383Z
pmw1234
<p>I am a beginner in glsl and processing. I have a cube, can I know how to deform it using mathematical or progammatical functions?</p>
<pre><code> void setup(){
 size(500,500,P3D); 
 lightShader = loadShader(&quot;phFragmentShader.glsl&quot;, &quot;phVertexShader.glsl&quot;);

}

void draw(){
 
   shader(lightShader);
   fill(125);
   noStroke();
  
  rotateX(0.8);
  rotateY(0.6);
  rotateZ(0.1);
  
  
   pointLight(255, 0, 0, 159, 272, 200);
  // pointLight(255, 0, 0, mouseX, mouseY, 200);

  translate(250,250);

    box(100);


}


}
</code></pre>

<opengl><glsl><processing>
How to deform an object in glsl? (processing)
Tragend
# How to deform an object in glsl? (processing)

(Asked by: username_0 on 2022-03-20)

I am a beginner in glsl and processing. I have a cube, can I know how to deform it using mathematical or progammatical functions?

```
 void setup(){
 size(500,500,P3D); 
 lightShader = loadShader("phFragmentShader.glsl", "phVertexShader.glsl");

}

void draw(){
 
   shader(lightShader);
   fill(125);
   noStroke();
  
  rotateX(0.8);
  rotateY(0.6);
  rotateZ(0.1);
  
  
   pointLight(255, 0, 0, 159, 272, 200);
  // pointLight(255, 0, 0, mouseX, mouseY, 200);

  translate(250,250);

    box(100);

}

}

```

**Tags:** opengl, glsl, processing


---

## 1 answers

### Answer by username_1 on 2022-03-22. Score: 0

The first piece of deforming a mesh is the mesh itself. A very simple mesh with very few vertices can be deformed but the results will be limited, there is only so much that can be done with 8 vertices. (The code posted in the question refers to a box, usually those have only 8 unique vertices which is a good simple object to use when setting up/learning graphics). But to do any interesting deformations a mesh that is nicely tessellated is needed. This can be done by adding tessellation shaders, which aren't to hard to setup but can be confusing for someone just learning GLSL/OpenGL. The other choice is to replace the box with something that has more triangles, a few hundred usually is enough to start getting interesting results.

The code to do deformations come in so many forms that I think there is literally an infinite number of possibilities. I will list a few simple techniques to hopefully get you started.

The first "class" of deformations are simple matrix deformations that can be created by adding an extra matrix into the usual MVP, and the simplest form I can think of is a non-symmetric scale such as:

\begin{equation\*}
D\_scale =
\begin{bmatrix}
x & 0 & 0 & 0 \\
0 & y & 0 & 0 \\
0 & 0 & z & 0 \\
0 & 0 & 0 & 1 \\
\end{bmatrix}
\end{equation\*}

Where x,y and z are not equal to each other. Something like $\{0.6, 1.3, 2.6\}$.

A shear/skew matrix along a specific axes is of the form:
\begin{equation\*}
D\_skew =
\begin{bmatrix}
1 & tan(\theta) & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1 \\
\end{bmatrix}
\end{equation\*}

Combine skews and non-uniform scales by multiplying them together.

```
D_combined = D_skew * D_scale; // order matters here, swap them around for different effects.

```

The vertex shader can move/wiggle vertices around with a simple noise generator. Keep the movement very small or the mesh will just look a mess of unconnected triangles. Here is a nice intro to [noise generators](https://thebookofshaders.com/11/).

```
// to use the generated noise do something similar to the below:
vec4 generated_noise = MakeNoise( vertex_input_position);
float scale = 0.02;  // scale the noise up or down
vec4 new_vertex_position = vertex_input_position + generated_noise * scale;

// now process the new vertex position as usual...
vec4 output_vertex_position = MVP * new_vertex_position;

```

Another form of mesh deformation are simple functions that move the vertices around using math formulas. A personal favorite is to move all the vertices along a straight line out to some limit. The line is made by creating a vector from a point (usually at the center of the mesh) to the vertex that is being processed, normalize it, multiply by a scale factor, then clamp. This effectively creates a sphere and is very easy to animate by passing the current time to the shader through a uniform variable. Use $cos(time)\*scale$ to make the object "throb".

This post has gotten a lot longer then I expected so I won't post any code for this last one. Anyway, hopefully this post illustrates that the possibilities for deformations are limited only by the coders imagination.


---

