3D computer Grapihcs Using OpenGL-07 passing Data from Vertex to Fragment Shader

Source: Internet
Author: User

At the end of the last section we implemented two green triangles, and the green was specified directly in the fragment shader.

In this section we will be coloring the two triangles more freely-five vertices each using different colors.

To achieve this, we take two steps, first of all

Add data to the vertex array to represent the color

Modify the Verts array in the SENDDATATOOPENGL () function:

1Glfloat verts[] =2     {3+0.0f, +0.0f,//Vertex 04+1.0,  +0.0, +0.0f,//Color 05+1.0f, +1.0f,//Vertex 16+0.0,  +1.0, +0.0f,//Color 17-1.0f, +1.0f,//Vertex 28+0.0,  +0.0, +1.0f,//Color 29-1.0f, -1.0f,//Vertex 3Ten+0.5f, +0.3f, +0.1f,//Color 3 One+1.0f, -1.0f,//Vertex 4 A+0.1f, +0.4f, +0.2f,//Color 4 -};

Added 5*3=15 elements, interspersed at each vertex position, representing the r,g,b value of the color. Now each of the 5 data describes a vertex, the first two representing the vertex position, and the last three representing the color.

In order to enter this data into the GPU, we also need to enable the second channel. (We have only enabled one channel before 0)

Modify the Senddatatoopengl () function:

1 voidMyglwindow::senddatatoopengl ()2 {3Glfloat verts[] =4     {5+0.0f, +0.0f,//Vertex 06+1.0,  +0.0, +0.0f,//Color 07+1.0f, +1.0f,//Vertex 18+0.0,  +1.0, +0.0f,//Color 19-1.0f, +1.0f,//Vertex 2Ten+0.0,  +0.0, +1.0f,//Color 2 One-1.0f, -1.0f,//Vertex 3 A+1.0f, +1. 0f, +0.0f,//Color 3 -+1.0f, -1.0f,//Vertex 4 -+0.0f, +1. 0f, +1. 0f,//Color 4 the     }; -  - Gluint Vertexbufferid; -Glgenbuffers (1, &Vertexbufferid); + Glbindbuffer (Gl_array_buffer, Vertexbufferid); -Glbufferdata (Gl_array_buffer,sizeof(Verts), Verts, Gl_static_draw); +  AGlushort indices[] = at     { -         0,1,2, -         0,3,4, -     }; - Gluint Indexbufferid; -Glgenbuffers (1, &Indexbufferid); in Glbindbuffer (Gl_element_array_buffer, Indexbufferid); -Glbufferdata (Gl_element_array_buffer,sizeof(indices), indices, gl_static_draw); to  +Glenablevertexattribarray (0); -     Glvertexattribpointer (0,2, Gl_float, Gl_false,sizeof(glfloat) *5,0); the  *Glenablevertexattribarray (1); $Glvertexattribpointer (1,3, Gl_float, Gl_false,sizeof(glfloat) *5, (Char*)(sizeof(glfloat) *2));Panax Notoginseng}

Note the 33rd line, the function Glvertexattribpointer function The third parameter becomes sizeof (glfloat), because Stride becomes 5 float, as mentioned earlier, 5 elements describe a vertex.

In addition, the 35th and 36 lines, 35 lines open the Channel 1, this 1 and the preceding 0 is the layout (location = x) in the shader corresponding to the explanation later.

Line 36th The first parameter of the Glvertexattribpoint function corresponds to 35 rows of 1, the second parameter represents a group of three elements.

Special attention is given to the last parameter, which represents the starting offset value of this group of data, the first position of this group of data in the code of the sixth line, it is preceded by 2 elements, so this is sizeof (glfloat) * *, but because the function of the fourth parameter type is char* type, We can only cast to char *.

Then we need

Modify Shader
1 Const Char* Vertexshadercode =2 "#version 430 \ r \ n"3 "\ r \ n"4 "In layout (location=0) vec2 position; \ r \ n"5 "In layout (location=1) vec3 vertexcolor; \ r \ n"6 "\ r \ n"7 "Out vec3 Passingcolor; \ r \ n"8 "\ r \ n"9 "void Main () \ r \ n"Ten "{\ r \ n" One "gl_position= vec4 (position,0.0,1.0); \ r \ n" A "passingcolor= Vertexcolor; \ r \ n" - "} \ r \ n" - "\ r \ n" the "\ r \ n"; -  - Const Char* Fragmentshadercode = - "#version 430 \ r \ n" + "\ r \ n" - "In vec3 Passingcolor; \ r \ n" + "Out VEC4 Finalcolor; \ r \ n" A "\ r \ n" at "\ r \ n" - "void Main () \ r \ n" - "{\ r \ n" - "Finalcolor = VEC4 (passingcolor,1.0); \ r \ n" - "} \ r \ n" - "\ r \ n" in "\ r \ n";

See Vertex Shader first.

Added the 5th line.

Take a closer look at lines 4th and 5th, comparing lines 33 and 36 of the Senddatatoopengl () function, location=0 specifying channel 0, VEC2 illustrates the need for a two-dimensional vector (2 float), 33 rows of the first two parameters of the function are 0 (for channel 0), 2 (representing two elements), and the two correspond to each other.

The 36 lines of Shadercode's line 5th and Senddatatoopengl () are also corresponding.

Then the corresponding verts[] array is compared to see how it works. The Verts array is a group of five elements, the first two elements of each group will be sent to channel 0, and the next three elements will be sent to Channel 1.

In addition, the vertex shader adds the 7th line, where the Out keyword defines a three-dimensional vector passingcolor, indicating that the parameter will be sent out and passed to the next segment of the rendering pipeline. In main, we pass the data that we just received from Channel 2 (in the presence of Vertexcolor) to Passingcolor. So the equivalent of we did not pass the data in Channel 2 to do any processing, directly sent to the next link.

Observing fragment Shader, we added 20 lines, noting that this line is very similar to the 7th line of the vertex Shader except that the first keyword is changed to in.

This match is a fixed pattern of GLSL, and the upstream out variable is passed to the downstream in variable as long as the two are consistent.

In the main of fragment shader, we changed the final color output to VEC4 (passingcolor,1.0), which is what we showed in this color.

Across the globe, the changes we've made this time will pass some of the new information in the Verts array first to vertex shader, then to Fragmentshader, and finally to the color of the vertex.

Compile and run we see two colored triangles:

3D computer Grapihcs Using OpenGL-07 passing Data from Vertex to Fragment 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.