[Glsl tutorial] (1) graphic Assembly Line

Source: Internet
Author: User
This is a glsl tutorial for some columns from lighthouse3d and is very suitable for getting started. I will learn and translate the content of this tutorial, and record it here for future query convenience.

Assembly line Overview

A simplified graphic processing pipeline is described. Although simple, some important concepts of shader programming can still be presented.

A fixed pipeline includes the following functions:

Vertex Transformation)

Here, a vertex is a set of information, including the position in the space, the color of the vertex, the normal, and the texture coordinate. The input at this stage is independent vertex information, and the fixed function pipeline usually performs the following work at this stage:

· Vertex position Transformation

· Calculate illumination for each vertex

· Texture coordinate generation and Transformation

Primitive assembly and Rasterization)

The input in this phase is the transformed vertex and connection information (connectivity information ). The connection information tells the assembly line how vertices form elements (triangles, quadrilateral, etc ). This phase is also responsible for view frustum cropping and back elimination.

Grating determines the fragment and the pixel position of the elements. The segment here refers to a piece of data, which is used to update a pixel at a specific position in the frame buffer. In addition to color, a piece also has attributes such as normal and texture coordinates. This information is used to calculate the new pixel color value.

The output in this phase includes:

· Position of the fragment in the frame Cache

· Interpolation of each piece of information calculated during vertex Transformation

This phase uses the data calculated in the vertex transformation phase and the connection information to calculate the piece of data. For example, each vertex contains a transformed position. When they form a graph element, they can be used to calculate the segment position of the element. Another example is color. If each vertex of a polygon has its own color value, the color value of the segment inside the polygon is obtained by interpolation of the color of each vertex.

Fragment texturing and coloring)

Input in this phase is the piece of information that is interpolated. In the previous phase, texture coordinates and a color value have been calculated through interpolation. This color can be used to combine texture elements in this phase. In addition, atomization can be performed at this stage. Generally, the final output is the color value and depth of the clip.

Raster operations)

Input for this phase:

· Pixel position

· Segment depth and color value

Perform a series of tests on the parts at this stage, including:

· Scissor Test)

· Alpha Testing

· Template Test

· Deep Test

If the test is successful, the pixel value is updated based on the current blend mode. Note that mixing can only be performed in this phase, because fragment textures and colors cannot access the frame cache. The frame cache can only be accessed at this stage.

A picture summarizes the fixed functional pipeline (visual Summary of the fixed functionality)

Intuitively summarizes the stages of the above pipeline:

Replacing Fixed functionality)

The current video card allows programmers to program two stages in the above pipeline:

· Vertex shader implements the function of vertex transformation phase

· The fragment shader replaces the fragment texture and color functions.

 

Vertex Processor

The vertex processor is used to run vertex shader (coloring program ). Vertex shader input is vertex data, that is, position, color, and normal.

The following OpenGL program sends data to the vertex processor. Each vertex contains a color information and a position information.

glBegin(...);    glColor3f(0.2,0.4,0.6);    glVertex3f(-1.0,1.0,2.0);    glColor3f(0.2,0.4,0.8);    glVertex3f(1.0,-1.0,2.0);glEnd();

A vertex shader can write code to implement the following functions:

· Use the model view matrix and projection matrix for vertex Transformation

· Normal transformation and Normalization

· Texture coordinate generation and Transformation

· Vertex-by-vertex or pixel-by-pixel illumination Calculation

· Color Calculation

You do not have to complete all the above operations. For example, your program may not use light. However, once you use the vertex shader, all fixed features of the vertex processor will be replaced. So you can't just write the shader of the normal transformation and expect the fixed function to help you complete the texture coordinate generation.

As we know from the previous section, the vertex processor does not know the connection information, so topology information-related operations cannot be performed here. For example, the vertex processor cannot remove the backend. It only operates the vertex rather than the surface.

The vertex shader must have at least one variable: gl_position, which is usually transformed using the model view matrix and projection matrix. Vertex processors can access the OpenGL state, so they can be used to process materials and lighting. The latest devices can also access textures.

 

Segment Processor

The segment processor can run the segment shader, which can perform the following operations:

· Calculate color and texture coordinates by Pixel

· Apply texture

· Atomization computing

· If pixel-by-pixel illumination is required, it can be used to calculate the normal

The input of the segment processor is the result of vertex coordinate, color, and normal calculation and interpolation. The attribute values of each vertex are calculated in the vertex shader. Now each segment in the element is processed, so interpolation is required.

Like a vertex processor, when you write a segment shader, all the fixed functions will be replaced. Therefore, you cannot use the segment shader to material the segment and use the fixed function for Atomization. Programmers must write programs to achieve all the results they need.

The segment processor only operates on each segment independently and does not know the content of adjacent segments. Similar to the vertex shader, We must access the OpenGL State to know the fog color and other content set in the application.

A piece of shader has two types of output:

· Discard the part content and output nothing

· Calculate the final color of the part gl_fragcolor. Calculate gl_fragdata when rendering to multiple targets.

Deep information can also be written, but it is not necessary because it has been computed in the previous stage.

It should be emphasized that the segment shader cannot access the frame cache, so the mixed (blend) operation can only happen after this.

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.