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(){
 lightShader = loadShader("phFragmentShader.glsl", "phVertexShader.glsl");


void draw(){
   pointLight(255, 0, 0, 159, 272, 200);
  // pointLight(255, 0, 0, mouseX, mouseY, 200);





1 Answer 1


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.

// 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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.