GLSL Core Tutorial–vertex Shader

Source: Internet
Author: User

Translated from: http://www.lighthouse3d.com/tutorials/glsl-core-tutorial/vertex-shader/

A vertex shader acts on a number of independent points, one at a time per vertex. The shader does not know the other vertices that make up the graphical entity, nor does it indicate which type of entity the vertex of the action belongs to. For each input vertex, shader outputs a single vertex (simply the vertex shader is a vertex-wise operation, the result is a vertex)

Each vertex has user-defined input attributes, such as: position, normal vector, and texture coordinates. Vertex shader can also be accessed by the uniform variable as a global read-only variable that draws all the vertices in the command.

In addition to user-defined variables, GLSL itself defines some vertex properties.

inch  int    Gl_vertexid; inch  int   Gl_instanceid;

Gl_vertexid is the index that points to the vertex in the property array. When using instance, shader operates n times for each vertex, and n is the instances number Gl_instanceid that is described in the gldraw* command to provide an index of instance. If you do not use instances,n, it is always 0.

Vertex shader Gets the input properties, outputs the vertices, and calculates their properties. The following is a fixed output property that can be written in vertex shader.

 out Gl_pervertex {    vec4  gl_position;     float gl_pointsize;     float gl_clipdistance[];};

It is optional to write to any of these items, however, some fixed function steps after vertex shader expect Gl_position to be written. This is because the purpose of this variable is to store the homogeneous coordinates of the output vertex position.

In addition, shader can output user-defined per-vertex variables.

Here is a simple example that shows the vertex shader feature:

#version410layout (std140) uniform matrices {mat4 Projmodelviewmatrix; //MVP MatrixMAT3 Normalmatrix;//Normal matrix}; inchVEC3 position;//VertexinchVEC3 Normal;//NormalinchVEC2 Texcoord;//Textures  outVertexData {//output vertex attribute blockVEC2 Texcoord; VEC3 Normal;} Vertexout; voidMain () {Vertexout.texcoord=Texcoord; Vertexout.normal= Normalize (Normalmatrix *normal); Gl_position= Projmodelviewmatrix * VEC4 (position,1.0);//coordinate transformation of vertices}

The top vertex shader receives three user-defined vertex attributes: position, Normal, Texcoord. It accepts a unifrom block called matrices, which contains two matrices that transform the vertices and normals. The output of vertex shader is also a user-defined property, Texcoorout and Normal, and a GLSL built-in property gl_position (a variable that starts with Gl_). Note that the output variable is also contained in a name block. The output is calculated from the main function. Each type of shader must have a main function, as well as a C program, and other additional functions can be defined.

PS: Here are some of the things that use the relationship between vertex caching and performance.

Notes regarding performance

Performance wise, there is a vertex cache this stores the outputs of the last N processed vertices. Before a new vertex is processed, it index is checked against the indices in the vertex cache. If the index is in the vertex cache, the respective previously processed data are sent to the remaining of the pipeline wit Hout further processing.

Taking advantage of the vertex cache, can therefore improve performance when using indexes, either explicitly, or impli citly, as in triangle strips and fans. For instance, with a triangle strip, at the most one vertex per new triangle would be processed. When using indexes with regular triangles, the gain are not as easy to compute, and the indices may require a reorganizatio N of the vertex data to improve vertex cache hits.

When the vertex data isn't a strip, it can be converted to a strip or a set of strips. Nvtristrip is a tool from NVIDIA that performes this task. It takes the array of indices and tries to create strips as large as possible. Other approaches, namely Tom Forsyth's algorithm (based on a cache of vertices using a least recently Used (LRU) replaceme NT policy), reorganize the index data to increase hits, keeping the GL_TRIANGLES mode. Tootle, Triangle Order optimization tool, is a AMD tool to optimize models, to reduce pixel overdraw and increase post-tr Ansform Cache and vertex prefecth cache hits. This latter improvment was achieved by the reorganization of the vertex data itself, so that vertices in a triangle are CLO Se to all other on memory. Adrian Stone has written an article where several algorithms is disscussed and tested. Ignacio Castaño have focused on grids in this article.

GLSL Core Tutorial–vertex Shader

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.