OpenGL second HELLO OPENGL (cont.)

Source: Internet
Author: User

The last time we analyzed the OpenGL example here, we ended up with less analysis of the most important part: shader-related code. So this time as a sequel to the previous article.

Address of previous post http://www.cnblogs.com/MyGameAndYOU/p/4609203.html

First we look at the previous code

Shaderinfo shaders[] = {        {gl_vertex_shader, "Triangles.vert"},        {gl_fragment_shader, "Triangles.frag"},        {gl_none, NULL}    };

The Triangles.vert here corresponds to the vertex shader file

#version 430 corelayout (location = 0) in Vec4 vposition;void main () {gl_position = vposition;}

The first line of #version 430 core represents the GLSL language that we use for the 4.3 version of OpenGL, and the core represents the kernel mode of using OpenGL.

If #version is not set, the 110 version is used by default.

The second line of layout (location=0) in VEC4 vposition assigns a shader variable (the shader variable is the connection between the shader and the outside world), and this line contains a lot of content, and we look from right to left:

1, vposition refers to the variable name, it holds the vertex position information

2, VEC4 is a data type in GLSL, here represents the four-dimensional floating-point vector of GLSL, the default value is (0,0,0,1), the expression (x,y,z,w). When a field is missing, the corresponding default value is populated.

In the third chapter, we will further study the GLSL, which is a first step here.

3, in field, indicates that the variable is set, that is, vposition is the input variable for the shader stage, specifying the flow of data into the shader, where the data flows from the outside.

4, Layout (location=0), this field is very important, it sets the location of Vposition to 0, provides metadata for it.

Looking back at the previous code, this field has been linked to Glvertexattribpointer and Glenablevertexattribarray. So that our shader variables can get the associated data.

/**        function prototype: void Glvertexattribpointer (Gluint index, glint size, glenum type,        glboolean normalized, Glsizei stride , const glvoid* pointer);        Before we called the data that Glbinddata passed to the cache, and then we used it, we had to specify the data type.        so the main task of this function is:        1, tell OpenGL, the format of the stored data        2, because we use the shader to program, so in the vertex shader phase, we will use this function to the shader in the type of property variables to pass data.           So how do they relate to each other?           It is the index of the first parameter that indicates the role of the subscript of the variable in the shader program.           For example: Layout (location=index) in vec4 position;           If the index is the same as the first parameter of the Glvertexattribpointer, then the data of the relevant buffer will be passed to the position variable.        3, after the function is executed, it will affect the state of changing Vao, VBO will be copied to Vao. Then, if you change the cached object that is currently bound, it will not change to the object in the Vao.    *    /Glvertexattribpointer (vposition, 2, Gl_float, Gl_false, 0, Buffer_offset (0));    /**        because the array of vertex attributes is inaccessible by default, we need to invoke the following function to activate it.        Range is 0 to gl_max_vertex_attribs-1    *    /Glenablevertexattribarray (vposition);

5, the last main function, no matter in which coloring phase, there will be a main function. What it does is copy the input vertex position into the specified output position of the vertex shader gl_position.

Gl_positon is a built-in variable that does not need to be declared before use. In OpenGL, it represents the output attribute-the position of the transformed vertex, for subsequent fixed cropping operations. All vertex shaders must write this value.

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------

Let's go back to the code for the fragment shader.

The Triangles.frag here corresponds to the vertex shader file

#version 430 coreout vec4 fcolor;void Main () {Fcolor = VEC4 (0.0, 0.0, 1.0, 1.0);}

It can be seen that almost all shaders have the same basic structure.

We'll focus on the different places.

1. The Fcolor of the out type used by the variable, as opposed to the previous in, indicates that the shader will output the value corresponding to the Fcolor, which is also the color value of the fragment.

2, Fcolor = VEC4 (0.0, 0.0, 1.0, 1.0) sets the color value of the fragment with a four-dimensional vector. In fact, OpenGL uses the color space of RGBA,

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------

Since then, we have completed the first OpenGL example. Again, it's time to explain

Glvertexattribpointer and Glenablevertexattribarray Two functions specify the relationship between the variables of the vertex shader and the data stored in the cached object.

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------

In the learning process, found that Vao and Vbo is not very clear, you can continue to see this article "Vao and Vbo" http://www.cnblogs.com/MyGameAndYOU/p/4678657.html

In addition, if there are errors found in my content, please tell me, I will correct, thank you!

In a few days, I will continue to write the third article about GLSL coloring language .

2015.7.29

Guangzhou

OpenGL second HELLO OPENGL (cont.)

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.