6. How Shaders Work in OpenGL
6. How Shaders Work in OpenGL
Rif[!notes] Default shader
If you have followed the first five videos , then run the program you will see a triangle is generated.This is because if you don’t set a shader of yourself , the driver will use the default shader.
1. Shader
Kind 1 : vertex shader
primary purpose:
- Provide the positions of the vertexes , so that we can have and use these positions to make some transformations
[!Example]
As we all know, there is a camera in the world of the game world. If we just set the vertex’s screen positions. when the camera moves , these vertexs won’t be rendered properly.
Kind 2 : fragment/pixel shader
==Color filler==
Useage:
- Will run for each pixel to perform rasterization
- Have the pixels ==colored==
[!note] rasterization
Let’s take drawing a triangle as an example
- Draw the three points
- Connect the points
- Fill the space in the close area == rasterization
Differences
- In drawing a triangle , the vertex shader is used by three times and the fragment shader is used by thousands of times
More about optimazation
- Since the fragment shader is called much often than the vertex shader, it is worth thinking that we do more calculations on vertex shader and then pass data from the vertex shader to the fragment shader.
[!Example] The lighting
The fragment shader can be time-consumed in this case. We need to consider the position of the lighting , the move of the object , the veins and so on . Just to get the correct color of one pixel , this work is huge.
The pipeline in this process
- Draw pen
- Vertex shader
- Fragment shader
- Show on screen