Home opengl - shaders(vsh, fsh)
Post
Cancel

opengl - shaders(vsh, fsh)

ref

What is a VSH file?

3D shading file used by applications that use the OpenGL graphics library; contains a graphics function used for shading a vertex, which is a point in 3D space; executed by a GPU in order to show 3D shading effects on an object.

ref

Understanding Shaders

Understanding the OpenGL rendering pipeline is important for learning OpenGL.

  1. Prepare vertex data (pass data to OpenGL via Vbo, Vao, and vertex attribute)
  2. Vertex processing (mainly done by vertex shader, which can be seen, it also includes optional tessellation and geometry shader stages)
  3. Vertex post-processing (mainly including clipping, vertex-coordinate normalization, and viewport transformations)
  4. Primitive assembly (e.g. 3 points assembled into a 3-angle shape)
  5. Rasterization into pixels
  6. Use fragment shader to process these pixels
  7. Sampling processing (mainly includes scissor test, Depth test, Blending, stencil test, etc.).

OpenGL Shader Language, abbreviated GLSL, is a language similar to the C language specifically designed for GPUs, which can be run in parallel in the GPU.

OpenGL ES has a shader with .FSH and .Vsh two files. These two files are compiled and linked to generate an executable program to interact with the GPU.

Vsh is a vertex shader, which, with vertex calculations, can understand the position of the control vertex, in which we usually pass in the position of the current vertex, and the coordinates of the texture.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 1 attribute vec4 position; 
 2 attribute vec4 inputTextureCoordinate;
 3 
 4 varying vec2 textureCoordinate;
 5 
 6 precision mediump float;
 7 uniform float overTurn;
 8 
 9 void main()
10 {
11 gl_Position = position;
12     if (overTurn>0.0) {
13         textureCoordinate = vec2(inputTextureCoordinate.x,overTurn-inputTextureCoordinate.y);
14     }
15     else
16         textureCoordinate = vec2(inputTextureCoordinate.x,inputTextureCoordinate.y);
17 }

There are two kinds of variables, one is attribute and the other is varying.

attribute is passed in from outside, each vertex has these two properties, so it is also called vertex attribute (vertex attribute).

Variables of the varying type are used to pass data between vertex shader and fragment shader.

.FSH is a fragment of shader. In this case I can recalculate each pixel point.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1 varying highp vec2 textureCoordinate;
2 precision mediump float;
3 uniform sampler2D videoFrame;
4 
5 vec4 memoryRender(vec4 color)
6 {
7     float gray;
8     gray = color.r*0.3+color.g*0.59+color.b*0.11;
9     color.r = gray;
10     color.g = gray;
11     color.b = gray;
12     
13     color.r += color.r*1.5;
14     color.g = color.g*2.0;
15     
16     if(color.r > 255.0)
17     color.r = 255.0;
18     if(color.g > 255.0)
19     color.g = 255.0;
20     
21     return color;
22 }
23 
24 void main()
25 {
26     vec4 pixelColor;
27 
28     pixelColor = texture2D(videoFrame, textureCoordinate);
29     
30     gl_FragColor = memoryRender(pixelColor);
31 }
This post is licensed under CC BY 4.0 by the author.