Geometry Shader

 

  Usually we only use vertex and fragment shaders, which is necessary for the basic and two shaders, and an optional geometry shader shader is located between the vertex and fragment shaders.

  Geometry shader receives a set of vertices from the vertex shader is a fragment, which can then be converted, outputting the new different types of tile, it can also increase the number of vertices, which is very powerful.

  General worded as follows:

#version 330 core
layout (points) in;
layout (line_strip, max_vertices = 2) out;

void main() {    
    gl_Position = gl_in[0].gl_Position + vec4(-0.1, 0.0, 0.0, 0.0); 
    EmitVertex();

    gl_Position = gl_in[0].gl_Position + vec4( 0.1, 0.0, 0.0, 0.0);
    EmitVertex();

    EndPrimitive();
}

  You can see first define the input and output layout modifier.

  Input modifier:

layout (points) in;

  Consistent vertex data input of parentheses tile type to be received generally and vertex shader, there are several types of tile (behind the numbers in brackets indicate minimum number of vertices contained in this fragment):

  • points: Draw GL_POINTS Figure element (1).
  • lines: Draw GL_LINES or GL_LINE_STRIP (2)
  • lines_adjacencyGL_LINES_ADJACENCYGL_LINE_STRIP_ADJACENCY(4)
  • trianglesGL_TRIANGLESGL_TRIANGLE_STRIPGL_TRIANGLE_FAN(3)
  • triangles_adjacencyGL_TRIANGLES_ADJACENCYGL_TRIANGLE_STRIP_ADJACENCY(6)

  Output modifiers:

layout (line_strip, max_vertices = 2) out;

  max_vertices = 2 represents: each fragment corresponding to the vertex shader output, the maximum number of vertex shader geometry of each treatment, beyond the vertex is no longer processed.

  Choice of output modifiers are:

  • points
  • line_strip
  • triangle_strip

  

  Show results:

  1, the input fragment as points, rendering "house"

#version 430 core
layout (points) in;
layout (triangle_strip, max_vertices=5) out;

in VS_OUT{
    vec3 color;
}gs_in[];

out vec3 fColor;

void build_Quad(vec4 p){
    fColor=gs_in[0].color;
    gl_Position=p+vec4(-0.2,-0.2, 0, 0);
    EmitVertex();
    gl_Position=p+vec4( 0.2,-0.2, 0, 0);
    EmitVertex();
    gl_Position=p+vec4(-0.2, 0.2, 0, 0);
    EmitVertex();
    gl_Position=p+vec4( 0.2, 0.2, 0, 0);
    EmitVertex();
    gl_Position=p+vec4( 0.0, 0.4, 0, 0);
    fColor=vec3(1.0,1.0,1.0);
    EmitVertex();
    EndPrimitive();
}

void main(){
    build_Quad(gl_in[0].gl_Position);
}

 

  2, nanosuit model explosions

#version 430 core
layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;

in VS_OUT{
    vec3 FragPos;
    vec2 TexCoords;
    vec3 Normal;
}gs_in[];

out vec2 TexCoords;

uniform float time;

vec3 GetNormal(){
    vec3 a=vec3(gl_in[0].gl_Position) - vec3(gl_in[1].gl_Position);
    vec3 b=vec3(gl_in[2].gl_Position) - vec3(gl_in[1].gl_Position);
    return normalize(cross(a,b));
}

vec4 explode(vec4 position, vec3 normal){
    float magnitude=2.0;
    vec3 direction=normal*((sin(time)+1.0)/2.0)*magnitude;
    return position+vec4(direction,0.0);
}

void main(){
    vec3 normal = GetNormal();

    gl_Position=explode(gl_in[0].gl_Position,normal);
    TexCoords=gs_in[0].TexCoords;
    EmitVertex();
    gl_Position=explode(gl_in[1].gl_Position,normal);
    TexCoords=gs_in[1].TexCoords;
    EmitVertex();
    gl_Position=explode(gl_in[2].gl_Position,normal);
    TexCoords=gs_in[2].TexCoords;
    EmitVertex();
    EndPrimitive();
}

 

  3, the model visualization normal nanosuit

#version 430 core
layout (triangles) in;
layout (line_strip, max_vertices = 6) out;

in VS_OUT{
    vec3 FragPos;
    vec2 TexCoords;
    vec3 Normal;
}gs_in[];

void GenerateLine(int index){
    gl_Position=gl_in[index].gl_Position;
    EmitVertex();
    gl_Position=gl_in[index].gl_Position + vec4(gs_in[index].Normal,0.0) * 0.4;
    EmitVertex();
    EndPrimitive();
}

void main(){
    GenerateLine(0);
    GenerateLine(1);
    GenerateLine(2);
}

 

Guess you like

Origin www.cnblogs.com/chen9510/p/11459431.html