thebookofshaders/08
Patricio Gonzalez Vivo 24e33093ac adding images
2016-05-05 10:36:31 -04:00
..
tmp adding barcode 2015-10-07 14:20:15 -04:00
2drotmat.png matrix chapter 2015-06-12 12:04:30 -04:00
3drotmat.png matrix chapter 2015-06-12 12:04:30 -04:00
3dtransmat.png matrix chapter 2015-06-12 12:04:30 -04:00
cross-animated.frag credits and fixes 2015-06-22 06:53:39 -04:00
cross-animated.png updating 2016-05-05 10:08:15 -04:00
cross-rotate.frag credits and fixes 2015-06-22 06:53:39 -04:00
cross-rotate.png adding images 2016-05-05 10:36:31 -04:00
cross-scale.frag credits and fixes 2015-06-22 06:53:39 -04:00
cross-scale.png adding images 2016-05-05 10:36:31 -04:00
cross-translate.frag credits and fixes 2015-06-22 06:53:39 -04:00
cross-translate.png adding images 2016-05-05 10:36:31 -04:00
cross.frag credits and fixes 2015-06-22 06:53:39 -04:00
cross.png adding subnails 2016-04-23 14:13:44 -04:00
index.php adding files and main README-JP.md 2015-08-05 08:39:27 -03:00
matrices.frag adding fractals 2015-09-10 12:49:53 -04:00
matrices.png adding subnails 2016-04-23 14:13:44 -04:00
matrix.frag adding matrix animate 2015-06-12 12:42:20 -04:00
matrix.png updating 2016-05-05 10:08:15 -04:00
matrixes.png matrix chapter 2015-06-12 12:04:30 -04:00
README-ch.md finish 6 chapters 2015-12-30 01:46:15 +08:00
README-jp.md minor re-wording 2015-12-27 14:58:30 -08:00
README.md relative paths as variables, glossary querry, code style 2015-07-17 16:24:23 -04:00
rotate.jpg Changed DPI from 300 to 96 for all JPEGs that were not being resized properly in the resultant pdf. 2015-07-27 19:04:58 -04:00
rotmat.png matrix chapter 2015-06-12 12:04:30 -04:00
scale.png matrix chapter 2015-06-12 12:04:30 -04:00
SUMMARY.md adding subnails 2016-04-23 14:13:44 -04:00
TITLE.md adding subnails 2016-04-23 14:13:44 -04:00
translate.jpg Changed DPI from 300 to 96 for all JPEGs that were not being resized properly in the resultant pdf. 2015-07-27 19:04:58 -04:00
yuv.frag matrix chapter 2015-06-12 12:04:30 -04:00
yuv.png adding thumbs 2016-04-03 17:25:06 -04:00

2D Matrices

Translate

In the previous chapter we learned how to make some shapes - the trick to moving those shapes is to move the coordinate system itself. We can achieve that by simply adding a vector to the st variable that contains the location of each fragment. This causes the whole space coordinate system to move.

This is easier to see than to explain, so to see for yourself:

  • Uncomment line 35 of the code below to see how the space itself moves around.

Now try the following exercise:

  • Using u_time together with the shaping functions move the small cross around in an interesting way. Search for a specific quality of motion you are interested in and try to make the cross move in the same way. Recording something from the "real world" first might be useful - it could be the coming and going of waves, a pendulum movement, a bouncing ball, a car accelerating, a bicycle stopping.

Rotations

To rotate objects we also need to move the entire space system. For that we are going to use a matrix. A matrix is an organized set of numbers in columns and rows. Vectors are multiplied by matrices following a precise set of rules in order to modify the values of the vector in a particular way.

Wikipedia entry for Matrix (mathematics)

GLSL has native support for two, three and four dimensional matrices: mat2 (2x2), mat3 (3x3) and mat4 (4x4). GLSL also supports matrix multiplication (*) and a matrix specific function (matrixCompMult()).

Based on how matrices behave it's possible to construct matrices to produce specific behaviors. For example we can use a matrix to translate a vector:

More interestingly, we can use a matrix to rotate the coordinate system:

Take a look at the following code for a function that constructs a 2D rotation matrix. This function follows the above formula for two dimentional vectors to rotate the coordinates around the vec2(0.0) point.

mat2 rotate2d(float _angle){
    return mat2(cos(_angle),-sin(_angle),
                sin(_angle),cos(_angle));
}

According to the way we've been drawing shapes, this is not exactly what we want. Our cross shape is drawn in the center of the canvas which corresponds to the position vec2(0.5). So, before we rotate the space we need to move shape from the center to the vec2(0.0) coordinate, rotate the space, then finally move it back to the original place.

That looks like the following code:

Try the following exercises:

  • Uncomment line 45 of above code and pay attention to what happens.

  • Comment the translations before and after the rotation, on lines 37 and 39, and observe the consequences.

  • Use rotations to improve the animation you simulated in the translation exercise.

Scale

We've seen how matrices are used to translate and rotate objects in space. (Or more precisely to transform the coordinate system to rotate and move the objects.) If you've used 3D modeling software or the push and pop matrix functions in Processing, you will know that matrices can also be used to scale the size of an object.

Following the previous formula, we can figure out how to make a 2D scaling matrix:

mat2 scale(vec2 _scale){
    return mat2(_scale.x,0.0,
                0.0,_scale.y);
}

Try the following exercises to understand more deeply how this works.

  • Uncomment line 42 of above code to see the space coordinate being scaled.

  • See what happens when you comment the translations before and after the scaling on lines 37 and 39.

  • Try combining a rotation matrix together with a scale matrix. Be aware that the order matters. Multiply by the matrix first and then multiply the vectors.

  • Now that you know how to draw different shapes, and move, rotate and scale them, it's time to make a nice composition. Design and construct a fake UI or HUD (heads up display). Use the following ShaderToy example by Ndel for inspiration and reference.

Other uses for matrices: YUV color

YUV is a color space used for analog encoding of photos and videos that takes into account the range of human perception to reduce the bandwidth of chrominance components.

The following code is an interesting opportunity to use matrix operations in GLSL to transform colors from one mode to another.

As you can see we are treating colors as vectors by multiplying them with matrices. In that way we “move” the values around.

In this chapter we've learned how to use matrix transformations to move, rotate and scale vectors. These transformations will be essential for making compositions out of the shapes we learned about in the previous chapter. In the next chapter we'll apply all we've learned to make beautiful procedural patterns. You will find that coding repetition and variation can be an exciting practice.