Chapter 3. Following the Pipeline (imitating factory assembly line)

Monday to Friday, a day, 7 am Beijing time - time update, bilingual, while learning program while playing the guitar, do a wonderful yard farming!

Do not doubt whether there are translation problems, our engineers are Lanxiang graduation translate it!

What You'll Learn in This Chapter (You'll learn what)

What each of the stages in the OpenGL pipeline does ( to render each stage of the pipeline have done what)
How to Connect to your shaders at The Fixed-function Pipeline Stages (how to put your shader and those fixed function hardware splicing, make up a complete rendering pipeline)
how to program that uses the create a of every stage at the Graphics pipeline Simultaneously (GPU how to create a program that can render are parallel in each stage)
In this chapter, we will walk all the way along the OpenGL pipeline from start to finish (In this chapter, we will take the entire OpenGL rendering pipeline again), providing insight into each of the stages (in-depth understanding of each step) , which include fixed-function blocks and (part of which includes fixed-function and programmable shader part) programmable shader blocks. you have already read a whirlwind introduction to the vertex and fragment shader stages (you have quickly seen vertex and processing stages fragment shader is introduced). However, the application that you constructed simply drew a single triangle at a fixed position (however, that the program you built only in a fixed place drew a triangle only). If we want to render anything interesting with OpenGL (if we want to use OpenGL rendering spot something interesting, then), we're going to have to learn a lot more about the pipeline and all of the things you can do with it (we need to learn about knowledge rendering pipeline and you can use it to dry Sha). This chapter introduces every part of the pipeline (a portion of this section describes each of the rendering pipeline), hooks them up to one another,

Passing Data to the Vertex Shader (vertex shader to transfer data)

The vertex shader is the first programmable stage in the OpenGL pipeline and has the distinction of being the only mandatory stage in the graphics pipeline (vertex shader stage is the first stage of processing OpenGL rendering pipeline, and it must exist, or are behind did not have to play). However, before the vertex shader runs, a fixed-function stage known as vertex fetching, or sometimes vertex pulling, is run (before the vertex shader executed, performs a known vertex fetching or called vertex pulling operation ). this automatically provides inputs to the vertex shader. (this operation automatically input data supplied to the vertex shader)

Vertex Attributes (vertex attributes, in fact some terms you may not translate, tell you how to how foreigners have called, and translated also awkward, like the word, is purely literal translation)

In GLSL, the mechanism for getting data in and out of shaders is to declare global variables with the in and out storage qualifiers (in GLSL, and thereto shader afferent or a method of acquiring data from the shader is defined in a global variable, and use in go out or identifier modification). you were briefly introduced to the out qualifier in Chapter 2 (you've seen in the second chapter to the out modifier), "Our First OpenGL Program," when Listing 2.4 used it to output a color from the fragment shader (in Listing2.4, which is used to indicate a color data output from the fragment shader). at the start of the OpenGL pipeline, we use the in keyword to bring inputs into the vertex shader (in OpenGL rendering the beginning of the pipeline, we use in this keyword to get some input data to the vertex shader). Between stages, in and out can be used to form conduits from shader to shader and pass data between them (at various stages of rendering pipeline between, in and out may form a conduit for data transmission). We'll get to that shortly ( We will soon contact to that section). For now,

Vertex attributes are how vertex data is introduced into the OpenGL pipeline (vertex attribute defines how the data were transmitted to the OpenGL rendering pipeline). To declare a vertex attribute, you declare a variable in the vertex shader using the in storage qualifier (only you modified to require the use of a modifier in the vertex shader variables to declare a vertex attribute). an example of this is shown in Listing 3.1, where we declare thevariable offset as an input attribute (in Listing3.1, we affirm a variable offset as an attribute input)

#version 450 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;
}
Listing 3.1: Declaration of a vertex attribute

In Listing 3.1, we have added the variable offset as an input to the vertex shader ( in Listing3.1 we have to add a vertex shader input variable called offset). As it is an input to the first shader in the pipeline, it will be filled automatically by the vertex fetch stage ( as the first stage of the rendering pipeline, these data will be input vertex fetch the initialization process). We can tell this stage what to fill the variable with by using one of the many variants of at the Vertex attribute Functions, glVertexAttrib () (we can use glVertexAttrib series of API to tell at this stage, how to give these variable assignment). the prototype for glVertexAttrib4fv () , which we use in this example, is ( in our case , the API definition we use is :)

void glVertexAttrib4fv(GLuint index, const GLfloat * v);

Here, the parameter index is used to reference the attribute (here, index index is used to specify the attribute) and v is a pointer to the new data to put into the attribute (v is a pointer to the input data). You may have noticed the layout (location = 0) code in the declaration of the offset attribute (you may have noticed that this code the location = 0). This is a layout qualifier, which we have used to set the location of the vertex attribute to zero (this is a layout modifier, where we perform vertex attribute position 0). this location is the value we'll pass in index to refer to the attribute (here refers to the position we will pass that index parameters)

One of Call Time WE Each at The glVertexAttrib () Functions (of Which there are MANY), by Will Update at The value of IT at The Vertex attribute that passed to IS at The Vertex Shader (every time we call glVertexAttrib series function, it will refresh us pass those variables to the vertex shader). we can use this approach to animate our one triangle ( in this way we can make our move up the triangle). Listing 3.2 shows an updated version of our rendering function that updates the value of offset in each frame (Listing3.2 shows our new version of the code, each frame of the new version of the code will have to update the offset variable)

// Our rendering function
virtual void render(double currentTime)
{
const GLfloat color[] = {
(float)sin(currentTime) 0.5f + 0.5f,
(float)cos(currentTime)
0.5f + 0.5f,
0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, color);
// Use the program object we created earlier for rendering
glUseProgram(rendering_program);
GLfloat attrib[] = { (float)sin(currentTime) 0.5f,
(float)cos(currentTime)
0.6f,
0.0f, 0.0f };
// Update the value of input attribute 0
glVertexAttrib4fv(0, attrib);
// Draw one triangle
glDrawArrays(GL_TRIANGLES, 0, 3);
}
Listing 3.2: Updating a vertex attribute

When we run the program with the rendering function of Listing 3.2, the triangle will move in a smooth oval shape around the window (when we execute this program, we will make the triangle along the elliptical motion of a)

Translations of this day to get here, see you tomorrow, bye ~

Get the latest plot first time, please pay attention to the Eastern Han Dynasty academy and the heart of the public graphic No.

Han College, waiting for you to play Oh

Guess you like

Origin blog.51cto.com/battlefire/2420526