Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ OpenGL Superb Learning Notes - Fragment Shader     - Simple comparison of MySQL and Oracle in a sql analytical details (Database)

- Docker build their own private warehouses (Linux)

- Linux --- manual release system cache (Linux)

- Setting Wetty do not need an account login command line operations (Linux)

- Percona MySQL 5.6 semi-synchronous replication (Database)

- How to use the on-screen keyboard in Linux (Linux)

- Formatted output printf command (Programming)

- Linux operating system security settings initial understanding (Linux)

- Related to optimize the use of Btrfs file system on SSD (Linux)

- EChart simple and practical control on chart (Programming)

- Linux System Getting Started Tutorial: How do you know Shell which is currently being used (Linux)

- MultiWriter: while the ISO image concurrent writes 20 USB Startup Disk (Linux)

- Help you to see Linux system architecture type 5 Common Commands (Linux)

- The security administrator network analysis tools SATAN Introduction under Linux (Linux)

- ThinkPad X201i installation notes Debian 7.5 (Linux)

- Linux password file security issues detailed usage (Linux)

- Why everybody ought to know LLVM (Linux)

- RedHat Linux 6 desktop installation (Linux)

- Linux system security configuration (Linux)

- How to Check notebook CPU temperature in Ubuntu (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:      
 
- CentOS 7 - use cgroups limit process resource (Linux)
- GNU Linux use diff to generate a patch with the patch (Linux)
- Create RAID 1 (mirroring) with two disks (Linux)
- Minimum period string problem (Programming)
- Oracle RMAN-06023 and ORA-19693 errors (Database)
- Oracle 11g statistics collection - collection of multi-column statistics (Database)
- EXP-00091 Error resolved (Database)
- Linux User Rights Study Notes (Linux)
- Ubuntu 14.04, 13.10 install OpenCV 2.4.9 (Linux)
- Hibernate learning introductory tutorial (Programming)
- Ubuntu modify DNS restart loss problem (Linux)
- Smooth upgrade to OpenSSH 6.1 Procedure (Linux)
- Oracle 12C modify spfile path (Database)
- VMware virtual machines to install virt-manager unable to connect to libvirt's approach (Linux)
- Create the best introductory tutorial REST API (Linux)
- C # how to generate a folder or file automatically rename (Programming)
- DataGuard add temporary data files bug (Database)
- Virtualbox virtual machine can not copy CentOS Network (Linux)
- Oracle10g 64-bit installation solution in Windows 2008 Server R2 (Database)
- Shell Scripting Basics (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.