OpenGL tutorial (5) shader (2) uniform variable

Source: Internet
Author: User

Original post address: http://ogldev.atspace.co.uk/www/tutorial05/tutorial05.html

 

In this tutorial, we will be exposed to a new shader variable uniform variables. The difference between this variable and the attribute variable is that when each vertex shader is called, the corresponding attribute values of the vertex are loaded from the vertex buffer according to the position of the attribute, while the uniform variable remains unchanged for each draw call, which means you load the variable before the draw call, the variable can be accessed when each vertex shader in the draw is executed, and the variable value remains unchanged. The uniform variable is often used to store some constant data during draw execution, such as illumination parameters, change matrices, texture object handles, and so on. Note: The uniform variable is similar to the variable in the const buffer in d3d11.

In this tutorial, we will make the rendered object motion on the screen to achieve the animation effect. Bind an uniform variable and an idle callback function. The value of the uniform variable changes in each frame.

GLUT does not repeatedly call our rendering functions. Rendering operations are performed only when special events occur, such as window maximization and minimization, and the current window is blocked by other windows, if we do not make any changes after the program is executed, the rendering function will only execute once. We can add a print function to the rendering function to verify the function. When the window is maximized and minimized, this function prints related information in the console window. If you want to implement the rendering of static objects, this method is acceptable. But in this tutorial, we need to call the rendering function repeatedly to implement the animation. How can we achieve this? We can register an idle callback function, place the rendering function in the function, or directly register the rendering function as an idle function. When glut does not accept Windows system events, the idle function will be executed repeatedly, so that we can achieve the animation effect by combining the idle function with the changed uniform variable.

Main Code:

glutIdleFunc(RenderSceneCB);

Through the above Code, we register the rendering function as an idle function. It should be noted that the function call is added at the end of the rendering function. Otherwise, the idle function will be executed repeatedly, however, the rendering function does not exist. The current display window is re-painted by the glut message () function, and the rendering function is called in the next glut message loop.

gScaleLocation = glGetUniformLocation(ShaderProgram, "gScale");
assert(gScaleLocation != 0xFFFFFFFF);

By querying the shader program object, we can get the location of the uniform variable, which is consistent with the location of the uniform variable in the shader code. Normally, we cannot directly access and update the uniform variable. When glsl compiler compiles the shader code, it will assign an index to each uniform variable, which is used to access the uniform variable within the shader, the application accesses the corresponding uniform variable through the glgetuniformlocation function. The parameter of the function is the shader program handle and the uniform variable name. If the function is successfully called, the index value is returned. If the function fails,-1 is returned. It is also important to check the returned value for errors. This ensures that the shader value is correctly updated. In both cases, the function fails to be called. The first one is the misspelling of the variable name or the optimization operation performed by compiler. In this case, the correct index value cannot be obtained.

static float Scale = 0.0f;
Scale += 0.001f;
glUniform1f(gScaleLocation, sinf(Scale));

We define a scaling factor variable, which increases by 0.001 every time a rendering function is called. The sin value of this variable is transmitted to the shader through the gluniform1f function. The sin function can ensure that the scale range is between [-]. Note that the sinf parameter is in radians. OpenGL provides multiple versions of the gluniform {1234} {if} function, which are used to load 1-, 2-, 3-, and 4-dimensional vectors respectively. I indicates that the variable is an integer, F indicates that the variable is a floating point. This function also has a version of the vector and matrix. It can be used to set the uniform variable of the vector or matrix type. The first parameter of this function is the index value of the uniform variable obtained by using the glgetuniformlocation function.

Next, let's take a look at the code changes in vs (Note: FS is the same as that in the previous tutorial, but it hasn't changed ).

uniform float gScale;

Declare the uniform type variable in the shader.

gl_Position = vec4(gScale * Position.x, gScale * Position.y, Position.z, 1.0);

We use the uniform variable gscale to change the x and y values of the vertex, so that the X and Y coordinate values in each frame are different to achieve the animation effect.

After running the program, the effect is as follows:

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.