Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ OpenGL Superb Learning Notes - Fragment Shader     - Linux security configuration (Linux)

- Linux Firewall Builder shorewall (Linux)

- Build your own Web server under Ubuntu Linux system (Server)

- Docker ecosystem security is gradually maturing (Server)

- Error code: 2013 Lost connection to MySQL server during query (Database)

- Bash Automated Customization Linux belongs to its own CentOS system (Linux)

- CentOS 7 version how to achieve the power to start the graphical interface (Linux)

- 25 Git Usage Tips (Linux)

- JavaScript in null undefined summary (Linux)

- To compile and install Clang 3.5.0 in Linux CentOS (Linux)

- Android Get App version number and version name (Programming)

- Android Custom View step (Programming)

- Java Learning: elegant string (Programming)

- Python: Finding meet the conditions specified in the file directory (Programming)

- Oracle to create an external table (Database)

- Oracle metadata Reconstruction experiments (Database)

- Setting the RedHat9 Intrusion Detection System (Linux)

- Boot-Repair Tool - repair of frequent start-up problems (Linux)

- Three kinds of binary tree traversal recursive and iterative solution (Programming)

- IP configuration under Linux (Linux)

 
         
  OpenGL Superb Learning Notes - Fragment Shader
     
  Add Date : 2017-08-31      
         
         
         
  Image Processing

Image processing is a special kind of processing that is independent of the vertex shader. After rendering the scene without using the fragment shader, the convolution kernel can be applied in various ways.

To keep the shader simple, using hardware acceleration, we limit the size of the total convolution to 3X3.

In the example program, glCopyTexIamge2D is called to copy the framebuffer into the texture. The larger of the texture is less than the maximum N-th power of the window 2 (there is no such restriction in 2.0). Then draw a fragment colored rectangle in the middle of the window, the size of the texture is the same, the texture coordinates from the lower left corner (0,0) to the upper right corner (1,1).

Fragment shader based on texture coordinates, in its core adjacent to the 3X3 texture sampling, and then filtered to get the color of the center point.

blurry

Blur may be the most common filter. It is able to smooth some high frequency characteristics, such as object edge jagged. It is also called a low-pass filter. It allows low frequency characteristics to pass through while retaining high frequency characteristics.

If we only use 3X3 convolution kernel, then in a single sampling will not be too obvious change. We can make multiple samples.

Here is the shader code:

//blur.fs
#version 120
// Sampled texture
Uniform sampler2D sampler0;
// Offset of the sample
Uniform vec2 tc_offset [9];

Void main (void)
{
    Vec4 sampler [9];
    For (int i = 0; i & lt; 9; ++ i)
    {
        // Get the sampled data
        Sampler [i] = texture2D (sampler0, gl_TexCoord [0] .st + tc_offset [i]);
    }}

  // 1 2 1
    // 2 1 2/13
    // 1 2 1
    //Calculation results
    Sampler [0] + sampler [1] * 2.0 + sampler [2] + sampler [3] * 2.0 + sampler [4] + gl_FragColor =
                                     Sampler [5] * 2.0 + sampler [6] + sampler [7] * 2.0 + sampler [8]) / 13.0;
}}
In this process, first we do not use the shader to draw a good graphics, and then enable the shader program, set the sampler0 and tc_offse, copy the frame buffer to the texture. Then set the texture coordinates, draw a square, use the shader to deal with texture. The following are some of the construction code:

Void ChangeSize ()
{
...
 WindowWidth = textureWidth = w;
  WindowHeight = textureHeight = h;
  / / Does not support non-2 n times texture
  If (! GLEE_ARB_texture_non_power_of_two)
  {
    Int n = 1;
    While ((1 < < n) < windowWidth)
    {
      N ++;
    }}
    TextureWidth = (1 < < (n-1));

    N = 1;
    While ((1 < < n) < windowHeight)
    {
      N ++;
    }}
    TextureHeight = (1 < < (n-1));
  }}

  GlViewport (0, 0, w, h);
  TwWindowSize (w, h);
  GLfloat xIn = 1.0f / textureWidth;
  GLfloat yIn = 1.0 f / textureHeight;
  // Construct an offset array
  For (int i = 0; i & lt; 3; ++ i)
  {
    For (int j = 0; j & lt; 3; ++ j)
    {
      Tc_offset [3 * i + j] [0] = (i - 1.0f) * xIn;
      Tc_offset [3 * i + j] [1] = (j - 1.0f) * yIn;
    }}
  }}
}}
Void RenderScene ()
{
...
  // Do not use a shader to draw graphics to the frame buffer
  GlClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  GlUseProgram (0);
  GlLightfv (GL_LIGHT0, GL_POSITION, g_lightPos);
  GlMatrixMode (GL_PROJECTION);
  GlLoadIdentity ();
  GluPerspective (35.0, (GLfloat) windowWidth / (GLfloat) windowHeight, 1.0, 100.0);
  GlMatrixMode (GL_MODELVIEW);
  GlPushMatrix ();
    (CameraPos [0], cameraPos [1], cameraPos [2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    GlTranslatef (xTrans, yTrans, zTrans);
    Float mat [4 * 4];
    ConvertQuaternionToMatrix (g_Rotate, mat);

    GlMultMatrixf (mat);

    DrawGround ();
    DrawObjects ();
  GlPopMatrix ();
  // Opens the fragment shader for blurring
  GlDisable (GL_DEPTH_TEST);
  GlMatrixMode (GL_PROJECTION);
  GlLoadIdentity ();
  GlMatrixMode (GL_MODELVIEW);
  GlLoadIdentity ();
  GlUseProgram (program [whichShader]);

  UniformLoc = glGetUniformLocation (program [whichShader], "sampler0");
  If (uniformLoc! = -1)
  {
    GlUniform1i (uniformLoc, sampler);
  }}
  UniformLoc = glGetUniformLocation (program [which Shader], "tc_offset");
  If (uniformLoc! = -1)
  {
    GlUniform2fv (uniformLoc, 9, & tc_offset [0] [0]);
  }}
  // The number of passes through the shader
  For (int i = 0; i < numPass; ++ i)
  {
    // Reads data from the frame buffer into the texture
    GlCopyTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, windowWidth-textureWidth, windowHeight-textureHeight,
      TextureWidth, textureHeight, 0);
    // Clear the frame buffer
    GlClear (GL_COLOR_BUFFER_BIT);
    GlBegin (GL_QUADS);
      GlTexCoord2f (0.0f, 0.0f);
      GlVertex2f (- GLfloat) textureWidth / (GLfloat) windowWidth), - ((GLfloat) textureHeight / (GLfloat) windowHeight));
      GlTexCoord2f (1.0f, 0.0f);
      GlVertex2f (GLfloat) textureWidth / (GLfloat) windowWidth, - ((GLfloat) textureHeight / (GLfloat) windowHeight));
      GlTexCoord2f (1.0f, 1.0f);
      GlVertex2f ((GLfloat) textureWidth / (GLfloat) windowWidth, (GLfloat) textureHeight / (GLfloat) windowHeight);
      GlTexCoord2f (0.0f, 1.0f);
      GlVertex2f (- (GLfloat) textureWidth / (GLfloat) windowWidth, (GLfloat) textureHeight / (GLfloat) windowHeight);
    GlEnd ();
  }}

  GlEnable (GL_DEPTH_TEST);
  GlutSwapBuffers ();
}}

Sharpen

Sharpening is in contrast to blurring, which makes the edges of the object more visible and the text easier to read.

Sharpen Shader Code:

//sharpen.fs/
#version 120

Uniform sampler2D sampler0;
Uniform vec2 tc_offset [9];

Void main (void)
{
    Vec4 sampler [9];
    For (int i = 0; i & lt; 9; ++ i)
    {
        Sampler [i] = texture2D (sampler0, gl_TexCoord [0] .st + tc_offset [i]);
    }}

    // -1 -1 -1
    // - 1 9 -1 // The scaled convolution sum is 1
    // -1 -1 -1
    - sampler [2] - sampler [3] + 9 * sampler [4] - sampler [0]
                                    -sampler [5] - sampler [6] - sampler [7] - sampler [8]);
}}
Note that the result of the addition of the convolution kernel is 1, which is the same as the fuzzy filter. This operation ensures that the filter does not enhance or reduce the brightness.

Expansion and corrosion

Both expansion and corrosion are morphological filters, which means they change the shape of the object. Expansion expands the size of bright objects, while corrosion reduces the size of bright objects. (For the dark object is the opposite).

The expansion simply finds the adjacent maximum.

//dilation.fs
#version 120

Uniform sampler2D sampler0;
Uniform vec2 tc_offset [9];

Void main (void)
{
    Vec4 sampler [9];
    // find the max value
    Vec4 maxValue = vec4 (0.0);
    For (int i = 0; i & lt; 9; ++ i)
    {
        Sampler [i] = texture2D (sampler0, gl_TexCoord [0] .st + tc_offset [i]);
        MaxValue = max (sampler [i], maxValue);
    }}

    Gl_FragColor = maxValue;
}}

Corrosion takes the minimum value of the neighboring neighboring values.

//erosion.fs
#version 120

Uniform sampler2D sampler0;
Uniform vec2 tc_offset [9];

Void main (void)
{
    Vec4 sampler [9];
    Vec4 minValue = vec4 (1.0);
    For (int i = 0; i & lt; 9; ++ i)
    {
        Sampler [i] = texture2D (sampler0, gl_TexCoord [0] .st + tc_offset [i]);
        MinValue = min (minValue, sampler [i]);
    }}

    Gl_FragColor = minValue;
}}

Edge detection

A more valuable filter is edge detection. The edge of the image is where the color changes quickly, while the edge detection picks where the color changes dramatically and highlights them.

There are three edge detectors, Laplacian, Sobel and Prewitt. Sobel and Prewitt gradient filters, which detect changes in the first derivative of the intensity of each channel, but only in a single direction. Laplacian then detects the zero of the second derivative, ie where the intensity gradient of the color shifts from dark to dark (or vice versa). It can be used on all edges.

The following code uses the Laplacian filter.

//edgedetetion.fs
#version 120

Uniform sampler2D sampler0;
Uniform vec2 tc_offset [9];

Void main (void)
{
    Vec4 sampler [9];
    For (int i = 0; i & lt; 9; ++ i)
    {
        Sampler [i] = texture2D (sampler0, gl_TexCoord [0] .st + tc_offset [i]);
    }}

    // -1 -1 -1
    // - 1 8 -1
    // -1 -1 -1
    Sampler [0] + sampler [1] + sampler [2]
                        + Sampler [3] + sampler [5] + sampler [6] + sampler [7] + sampler [8]);

}}

The difference between this and the sharpening filter is that the value in the middle is 8, not 9, and the sum of the coefficients is zero. This also explains why the middle of the graphic is black. Because the color of the middle element close to the core through the convolution filter is close to 0 after the. Only the edge of the color of the dramatic changes in place, have a larger color value.

illumination

Prior to this, we discussed the illumination of the vertex-by-point. It is also discussed to enhance the lighting effect by separating the specular light and using texture search. Here we use the fragment shader approach to deal with light. The algorithm is the same.

Here we combine vertex shaders and fragment shaders to achieve. The vertex shader interpolates the normals along the lines and triangles of the light vector. The fragment shader then processes the values produced by the vertex shader to get the final result.

Scattered light

formula:

Cdiff = max {N • L, 0} * Cmat * Cli

// diffuse.vs
//

Uniform vec3 lightPos [1];
Varying vec3 N, L;

Void main (void)
{
    // vertex MVP transform
    Gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

    // eye-space normal
    N = gl_NormalMatrix * gl_Normal;

    // eye-space light vector
    Vec4 V = gl_ModelViewMatrix * gl_Vertex;
    L = lightPos [0] - V.xyz;

    // Copy the primary color
    Gl_FrontColor = gl_Color;
}}

This is different from previous vertex shaders only, where the identifiers N and L decorated with varyings are used as output, and N and L can be accessed with the same name in the fragment shader. This method is easier to understand and less error-prone than using texture coordinates as an output (it is assumed that L is output to textureCoord [1], but textureCoord [0] is actually used and no compile error occurs, But not the desired result).

Here is the fragment shader code:

//diffuse.fs
#version 120
Varying vec3 N, L;

Void main (void)
{
    Float intensity = max (0.0, dot (normalize (N), normalize (L)));
    Gl_FragColor = gl_Color;
    Gl_FragColor.rgb * = intensity;
}}

Multiple mirror lights

Mirror light formula:

Cspec = max {N • H, 0} Sexp * Cmat * Cli

VS has multiple L outputs

//3light.vs
#version 120

Uniform vec3 lightPos [3];
Varying vec3 N, L [3];
Void main (void)
{
      // MVP transform
    Gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    // eye-space
    Vec4 V = gl_ModelViewMatrix * gl_Vertex;
    // eye-space noraml vector
    N = gl_NormalMatrix * gl_Normal;

    For (int i = 0; i & lt; 3; ++ i)
        {
      L [i] = lightPos [i] - V.xyz;
        }}
    // primary vector
        Gl_FrontColor = gl_Color;
}}
//3light.fs
#version 120

Varying vec3 N, L1 [3];

Void main (void)
{
    Vec3 NN = normalize (N);
    Gl_FragColor = vec4 (0.0);
    // 3 colors of light
    Vec3 lightCol [3];
    LightCol [0] = vec3 (0.5, 0.5, 1.0);
    LightCol [1] = vec3 (0.2, 0.3, 0.5);
    LightCol [2] = vec3 (0.8, 0.4, 0.8);
    Const float expose = 128.0f;
    For (int i = 0; i & lt; 3; ++ i)
    {
        Vec3 NL = normalize (L1 [i]);
        Vec3 H = normalize (NL + vec3 (0.0, 0.0, 1.0));
        Float NdotL = max (0.0, dot (NN, NL));
        // diffuse
        Gl_FragColor.rgb + = gl_Color.rgb * lightCol [i] * NdotL;

        // specular
        If (NdotL> 0.0)
        {
            Gl_FragColor.rgb + = lightCol [i] * pow (max (0.0, dot (NN, H)), expose);
        }}
    }}
    Gl_FragColor.a = gl_Color.a;
}}
     
         
         
         
  More:      
 
- Android Qemu GPS module (Programming)
- Four levels of intrusion on Linux server and counter-measures (Linux)
- Samba public folder permissions (Server)
- Linux Security trust with users (Linux)
- Network security system (Network)
- The next key to install Linux bash script PowerShell (Linux)
- CentOS7 installed MySQL (Database)
- Packages with Snort intrusion monitoring light (Linux)
- Recycle Bin function realization in Linux (Linux)
- Hibernate4 The Hello World (basic environmental structures) (Programming)
- System with Windows Remote Desktop to connect Ubuntu 15.04 (Linux)
- Git Advanced Tutorial (Linux)
- YUM install desktop environment in CentOS (Linux)
- Deb package installation method under ubuntu (Linux)
- CentOS permanently banned from running in the background PackageKit (Linux)
- Linux environment to configure Apache + Django + wsgi (Server)
- IOS distributed management using GitHub project development (Linux)
- Linux Creating a new user error Creating mailbox file: File exists (Linux)
- Use the DBMS_SCHEDULER package to manage scheduled tasks (Database)
- Github Getting Started Basic Course (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.