thebookofshaders/08/README.md

102 lines
5.5 KiB
Markdown
Raw Normal View History

2015-06-01 21:23:56 +00:00
## 2D Matrices
2015-03-15 15:35:14 +00:00
### Translate
2015-06-09 11:07:40 +00:00
![](translate.jpg)
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.
![](translate-space.jpg)
This is easier to see than to explain, so to see for yourself:
2015-06-01 21:23:56 +00:00
* Uncomment line 35 of the code below to see how the space itself moves around.
2015-03-15 15:35:14 +00:00
<div class="codeAndCanvas" data="cross-translate.frag"></div>
2015-06-01 21:23:56 +00:00
Now try the following exercise:
2015-03-15 15:35:14 +00:00
2015-06-01 21:23:56 +00:00
* 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.
2015-03-15 15:35:14 +00:00
### Rotations
2015-06-01 21:23:56 +00:00
To rotate objects we also need to move the entire space system. For that we are going to use a [matrix](http://en.wikipedia.org/wiki/Matrix_%28mathematics%29). 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.
2015-03-15 15:35:14 +00:00
![](matrixes.png)
2015-06-02 10:29:49 +00:00
GLSL has native support for two, three and four dimensional matrices: [```mat2```](../glossary/index.html#mat2.md) (2x2), [```mat3```](../glossary/index.html#mat3.md) (3x3) and [```mat4```](../glossary/index.html#mat4.md) (4x4). GLSL also supports matrix multiplication (```*```) and a matrix specific function ([```matrixCompMult()```](../glossary/index.html#matrixCompMult.md)).
2015-03-15 15:35:14 +00:00
2015-06-01 21:23:56 +00:00
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:
2015-03-15 15:35:14 +00:00
![](3dtransmat.png)
2015-06-01 21:23:56 +00:00
More interestingly, we can use a matrix to rotate the coordinate system:
2015-03-15 15:35:14 +00:00
![](2drotmat.png)
2015-06-01 21:23:56 +00:00
Take a look at the following code for a function that constructs a 2D rotation matrix. This function follows the above [formula](http://en.wikipedia.org/wiki/Rotation_matrix) to rotate the coordinates around the ```vec2(0.0)``` point.
2015-03-15 15:35:14 +00:00
```glsl
mat2 rotate2d(float _angle){
return mat2(cos(_angle),-sin(_angle),
sin(_angle),cos(_angle));
}
```
2015-06-11 10:57:22 +00:00
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.
2015-06-09 11:07:40 +00:00
![](rotate.jpg)
2015-06-11 10:57:22 +00:00
That will looks like the following code:
2015-06-01 21:23:56 +00:00
2015-03-15 15:35:14 +00:00
<div class="codeAndCanvas" data="cross-rotate.frag"></div>
2015-06-01 21:23:56 +00:00
Try the following exercises:
2015-03-15 15:35:14 +00:00
2015-06-01 21:23:56 +00:00
* Uncomment line 42 of above code and pay attention to what happens.
2015-03-15 15:35:14 +00:00
2015-06-01 21:23:56 +00:00
* Comment the translations before and after the rotation, on lines 37 and 39, and observe the consequences.
2015-03-15 15:35:14 +00:00
2015-06-01 21:23:56 +00:00
* Use rotations to improve the animation you simulated in the translation exercise.
2015-03-15 15:35:14 +00:00
### Scale
2015-06-01 21:23:56 +00:00
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.
2015-03-15 15:35:14 +00:00
![](3dscalemat.png)
2015-06-01 21:23:56 +00:00
Following the previous formula, we can figure out how to make a 2D scaling matrix:
2015-03-15 15:35:14 +00:00
```glsl
mat2 scale(vec2 _scale){
return mat2(_scale.x,0.0,
0.0,_scale.y);
}
```
<div class="codeAndCanvas" data="cross-scale.frag"></div>
2015-06-01 21:23:56 +00:00
Try the following exercises to understand more deeply how this works.
2015-03-15 15:35:14 +00:00
2015-06-01 21:23:56 +00:00
* Uncomment line 42 of above code to see the space coordinate being scaled.
2015-03-15 15:35:14 +00:00
2015-06-01 21:23:56 +00:00
* See what happens when you comment the translations before and after the scaling on lines 37 and 39.
2015-03-15 15:35:14 +00:00
2015-06-01 21:23:56 +00:00
* 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.
2015-03-15 15:35:14 +00:00
2015-06-01 21:23:56 +00:00
* 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)](https://www.pinterest.com/patriciogonzv/huds/). Use the following ShaderToy example by [Ndel](https://www.shadertoy.com/user/ndel) for inspiration and reference.
2015-03-15 15:35:14 +00:00
<iframe width="800" height="450" frameborder="0" src="https://www.shadertoy.com/embed/4s2SRt?gui=true&t=10&paused=true" allowfullscreen></iframe>
2015-06-01 21:23:56 +00:00
### Other uses for matrices: YUV color
[YUV](http://en.wikipedia.org/wiki/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.
2015-03-15 15:35:14 +00:00
2015-06-01 21:23:56 +00:00
The following code is an interesting opportunity to use matrix operations in GLSL to transform colors from one mode to another.
2015-03-15 15:35:14 +00:00
<div class="codeAndCanvas" data="yuv.frag"></div>
2015-06-01 21:23:56 +00:00
As you can see we are treating colors as vectors by multiplying them with matrices. In that way we “move” the values around.
2015-06-02 10:29:49 +00:00
In this chapter we have learn about how to use matrix transformation to move, rotate and scale vectors. This will be esential to compose shapes like the once we learn to do in the previus chapter. On our next step we will apply all what we have learn to make beatiful procedural patterns. You will find that coding repetition and variation can be an exciting practice.