Color coloring Tool

Source: Internet
Author: User

Color shader

Glsl has access to part of the OpenGL State. In this tutorial we'll see how to access the color as set in an OpenGL applicationGlcolor.

Glsl has an attribute variable where it keeps track of the current color. it also provides varying variables to get the color from the vertex shader to the fragment shader // attribute variables of the vertex shader are used to access the status of a fixed pipeline, such as the current color, the current vertex coordinates, and the varying (Transfer variable) variable is used to pass the variable value from the vertex coloring tool to the chip coloring tool.

 
Attribute vec4 gl_color; // attribute variable to obtain the currently colored varying vec4 gl_frontcolor set by glcolro (); // writable on the vertex shader // writable foreground color, you can use varying vec4 gl_backcolor; // writable on the vertex shadervarying vec4 gl_color; // readable on the fragment shader //

The idea is as follows:

    1. the OpenGL applications sends a color using the glcolor function // OpenGL uses glcolor () to set colors

    2. the vertex shader coloring es the color value in the attribute gl_color // use gl_color to obtain the color set by glcolor ().

    3. the vertex shader computes the front face and back face colors, and stores them in gl_frontcolor, and gl_backcolor respectively // The front and back colors of the vertex shader are calculated, and store gl_frontcolor and gl_backcolor.

    4. the fragment shader extends es an interpolated color in the varying variable gl_color, depending on the orientation of the current primitive, I. e. the interpolation is done using either the gl_frontcolor or the gl_backcolor values. // The color calculated by gl_color interpolation is received by the cell native colorant. The gl_frontcolor or gl_backcolor is used based on the sequence of vertex settings of the element. Which variable is used according to glfrontmode () OK

    5. the fragment shader sets gl_fragcolor based on the value of gl_color

This is an exception to the "rule" where a varying variable shocould be declared with the same name both in the vertex shader and the fragment shader. the concept in here is that we have two variables in the vertex shader, namelyGl_frontcolorAndGl_backcolor, And these are used to derive automatically the value of gl_color depending in the orientation of the current face. note that there is no conflict between the attribute gl_color and the varying variable gl_color, since the former is visible only in the vertex shader, and the latter in the fragment shader.

Enough talk, the code for the vertex shader, where only the front face color is computed is:

 
Void main () {gl_frontcolor = gl_color; // gl_color is the current vertex. gl_position = ftransform ();}

The fragment shader is also a very simple shader:

Void main () {gl_fragcolor = gl_color; // gl_color is the color of each element after interpolation} // ------------ supplement
 
Here there are two gl_color, which have different meanings. They are used in vertex pasters and fragment pasters respectively. In vertex pasters, The gl_color color comes from the glcolor () function ()
 
The current set color, while gl_frontcolor or gl_backcolor receives gl_color is determined by the glfrontmode () parameter.
 
The gl_color value is obtained from the fixed pipeline in the Cell Coloring tool, which is obtained after the vertex color interpolation.

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.