1.Passing Data to the Vertex Shader
The vertex shader is the first programmable stage in the OpenGL pipeline and has the distinction of being the only mandatory stage in the pipeline.
Vertex Attributes
In GLSL,the mechanism for getting data in and out of shaders is to declare global variables with the in and out storage qualifiers.
Between stages,in and out can be used to form conduits from shader to shader and pass data between them.
Vertex attributes are how vertex data is introduced into the OpenGL pipeline.
To declare a vertex attribute,declare a variable in the vertex shader using the in storage qualifier.
vertex shader
#version 430 core
//offset is an input vertex attribute
layout(location = 0) in vec4 offset;
void main(void)
{
const vec4 vertices[3] = vec4[3](
vec4(0.25, -0.25, 0.5, 1.0),
vec4(-0.25, -0.25, 0.5, 1.0),
vec4(0.25, 0.25, 0.5, 1.0)
);
//Add offset to our hard-coded vertex position
GL_Position = vertices[gl_VertexID] + offset;
}
The vertex attributes is automatically filled in by the fixed-function vertex fetch stage.
void glVertexAttrib*(GLuint index, const GLfloat* v);this function can tell the vertex stage what to fill the variable with.
The parameter index is used to reference the attribute and v is a pointer to the new data to put into the attribute.
the layout qualifier sets the location of the vertex attribute,this location is the value we’ll pass in index to refer to the attribute.
Each time we call glVertexAttrib*(),it will update the value of the vertex atrribute that is passed to the vertex shader.
2.Passing Data from Stage to Stage
Anything you write to output variables in one shader get sent to similarly named variables declared with the in keyword in the subsequent stage.
vertex shader
#version 430 core
//offset and color are input vertex attributes
layout(location = 0) in vec4 offset;
layout(location = 1) in vec4 color;
//vs_color is an output that will be sent to the next shader stage
out vec4 vs_color;
void main(void)
{
const vec4 vertices[3] = vec4[3](
vec4(0.25, -0.25, 0.5, 1.0),
vec4(-0.25, -0.25, 0.5, 1.0),
vec4(0.25, 0.25, 0.5, 1.0)
);
//Add offset to our hard-coded vertex position
GL_Position = vertices[gl_VertexID] + offset;
//Output a fixed value for vs_color
vs_color = color;
}
frame shader
#version 430 core
//input from the vertex shader
in vec4 vs_color;
//output to the framebuffer
out vec4 color;
void main(void)
{
//simply assign the color we were given by the vertex shader to our output
color = vs_color;
}
This allow us to pass a color all the way from a vertex attribute that we can set with glVertexAttrib*() through the vertex shader, into the fragment shader and out to the framebuffer.
Interface Blocks
In most non-trival applications,you may wish to communicate a number of different pieces of data between stages,and these may include arrays,structures and other complex arrangement of variables.To achieve this,we can group together a number of variables into an interface block.
#version 430 core
//offset and color are input vertex attributes
layout(location = 0) in vec4 offset;
layout(location = 1) in vec4 color;
//declare VS_OUT as an output interface block
out VS_OUT
{
vec4 color;//send color to next stage
}vs_out;
void main(void)
{
const vec4 vertices[3] = vec4[3](
vec4(0.25, -0.25, 0.5, 1.0),
vec4(-0.25, -0.25, 0.5, 1.0),
vec4(0.25, 0.25, 0.5, 1.0)
);
//Add offset to our hard-coded vertex position
GL_Position = vertices[gl_VertexID] + offset;
//Output a fixed value for vs_color
vs_out.color = color;
}
#version 430 core
//declare VS_OUT as an input interface block
in VS_OUT
{
vec4 color;
}fs_in;
//output to the framebuffer
out vec4 color;
void main(void)
{
//simply assign the color we were given by the vertex shader to our output
color = fs_in.color;
}
Interface blocks are matched between stages using the block name(VS_OUT in this case),but are referenced in shaders using the instance name.
3.Tesselation 曲面细分着色器
Tesselation is the process of breaking a high-order primitive(which is known as a patch in Opengl)into many smaller,simpler primitives such as triangles for rendering.
Logically,the tesselation phase sits directly after the vertex shading stage in the OpenGL pipeline and is made up of three parts:the tessellation control shader,the fixed-function tessellation engine and the tessellation evaluation shader.
Tesselation Control Shader