GLSL texture map "Go"

Source: Internet
Author: User

reprint:http://blog.csdn.net/hgl868/article/details/7872466 Easy Texture mapping (simple Texture)

To apply textures in GLSL, we need to access the texture coordinates of each vertex. Some attribute variables are provided in GLSL, one for each texture unit:

Attribute Vec4 gl_multitexcoord0;
Attribute Vec4 Gl_multitexcoord1;
Attribute Vec4 Gl_multitexcoord2;
Attribute Vec4 Gl_multitexcoord3;
Attribute Vec4 Gl_multitexcoord4;
Attribute Vec4 Gl_multitexcoord5;
Attribute Vec4 Gl_multitexcoord6;
Attribute Vec4 Gl_multitexcoord7;

GLSL also provides a consistent array of variables for accessing texture matrices for each texture:

Uniform mat4 Gl_texturematrix[gl_maxtexturecoords];

Vertex shader can access the texture coordinates specified in the OpenGL program through the content shown above. The texture coordinates must then be computed for each vertex and saved in a predefined volatile variable gl_texcoord[i], I represents the texture unit number.
The following statement copies the texture coordinates specified in the OpenGL program directly as the vertex texture coordinates of the texture unit 0.

Gl_texcoord[0] = gl_multitexcoord0;

The following is a simple example of setting the texture coordinate of texture unit 0 in vertex shader.

void Main ()
{
Gl_texcoord[0] = gl_multitexcoord0;
gl_position= Ftransform ();
}

If you want to use a texture matrix, you can do this:

void Main ()
{
Gl_texcoord[0] = gl_texturematrix[0] * GL_MULTITEXCOORD0;
gl_position= Ftransform ();
}

As I said earlier, Gl_texcoord is a volatile variable, so the interpolated texture coordinates can be accessed in the fragment Shder.
In order to access the values of the textures, it is necessary to declare a special variable in the fragment shader, which can be written for a 2D texture:

Uniform sampler2d Tex;

If the texture is 1D or 3D, it can be changed to sampler1d and Sampler3d.
This user-defined variable, tex, contains the texture unit we will use, and through the texture2d function we can get a texel (Texel), which is a pixel in a texture image. The function parameters are simpler2d and texture coordinates, respectively:

Vec4 texture2d (sampler2d, VEC2);

The return value of the function has taken into account all the texture settings defined in the OpenGL program, such as filtering, mipmap, clamp, and so on.
Our fragment shader can be written in the following form:

Uniform sampler2d Tex;

void Main ()
{
VEC4 color =texture2d (tex,gl_texcoord[0].st);
Gl_fragcolor= color;
}

Note When accessing Gl_texcoord, select the use of the child St. In a discussion of data types and variables earlier in this tutorial, you can access texture coordinates using the following selector: S, T, p, Q. (R is not used because it conflicts with RGB selectors)

The content of this section shader designer works:
Http://www.lighthouse3d.com/wp-content/uploads/2011/03/textureSimple.zip

Combining textures and fragments

opengl allows us to combine texture colors and fragment colors in a variety of ways. The following table shows the federated methods available when Rgba mode:

c =ct a =at
Span style= "font-family: ' Courier new '; font-size:13px; " >gl_modulate a =at*af
c =CF * (1–at) + Ct * at a =af
The CT and at expressions in the table represent the color and alpha values of the texture, CF and af represent the color and alpha values of the fragment (fragment), and C and a represent the final color and alpha values.
The example in the previous section is equivalent to using the gl_replace pattern. Below we are going to achieve the same effect as gl_modulate on a cube. Two shader only the scattering of a white directional light and the ambient light composition are counted, the complete definition of the material is described in the section on illumination.
Because lighting is used, normal information must be handled in vertex shader. The normals must be transformed into view space and normalized, and the light direction vectors must also be normalized (the light direction vectors have been transformed from OpenGL to view space). Now the new vertex shader is as follows:

Varying VEC3 lightdir,normal;

void Main ()
{
Normal =normalize (Gl_normalmatrix * gl_normal);

Lightdir =normalize (VEC3 (gl_lightsource[0].position));
Gl_texcoord[0] = gl_multitexcoord0;

gl_position= Ftransform ();
}

In fragment shader, the color and alpha values of the fragments obtained from the illumination are computed separately in CF and AF. The remaining code in shader is calculated according to the gl_modulate formula:

Varying VEC3 lightdir,normal;
Uniform sampler2d Tex;

void Main ()
{
VEC3CT,CF;
Vec4texel;
FLOATINTENSITY,AT,AF;

Intensity =max (dot (lightdir,normalize (normal)), 0.0);
CF =intensity * (gl_frontmaterial.diffuse). RGB +
Gl_FrontMaterial.ambient.rgb;
AF =gl_frontmaterial.diffuse.a;

Texel =texture2d (Tex,gl_texcoord[0].st);
CT =texel.rgb;
At =TEXEL.A;

gl_fragcolor= VEC4 (CT * CF, at * af);
}

Shader Designer's Project:
Http://www.lighthouse3d.com/wp-content/uploads/2011/03/textureComb.zip

Multiple textures
It is easy to implement multiple textures in GLSL, we only need to access all textures. Because we're going to use the same texture coordinates for each texture, the vertex shader doesn't need to be changed. Only a few changes are needed in the fragment shader, plus the color values of multiple textures.

The effect is as follows:

Add a different effect below: Glow in the dark. We want the second texture to glow in the dark, brightest when there is no light, and dark when there is light.


We calculate the final color in two steps: first the first texture and the fragment color are modulate computed, and then the second texture unit is added to the light intensity (indensity).
If Indensity is 0, the second texture cell takes the maximum value, and if Indensity is 1, only 10% of the second texture cell color is taken, and when Indensity is interpolated between 0 and 1 by the two size. You can use the Smoothstep function to implement this requirement:

Gentype Smoothstep (Gentype edge0, Gentype edge1, Gentype x);

If the x <= edge0 result is 0, if the x >= edge1 result is 1, the EDGE0 < x <edge1 result is Hermite interpolated between 0 and 1. In this case, we call it as follows:
Coef = Smoothstep (1.0, 0.2, intensity);
The following fragment shader implements the desired effect:

Varying VEC3 lightdir,normal;
Uniform sampler2d Tex,l3d;

void Main ()
{
Vec3ct,cf,c;
Vec4texel;
Floatintensity,at,af,a;

Intensity =max (dot (lightdir,normalize (normal)), 0.0);

CF =intensity * (gl_frontmaterial.diffuse). RGB +
Gl_FrontMaterial.ambient.rgb;
AF =gl_frontmaterial.diffuse.a;

Texel =texture2d (Tex,gl_texcoord[0].st);

CT =texel.rgb;
At =TEXEL.A;

c = CF *ct;
A = AF *at;

Float Coef =smoothstep (1.0,0.2,intensity);
c + = coef* vec3 (texture2d (l3d,gl_texcoord[0].st));

Gl_fragcolor= Vec4 (c, a);
}

Shader Designer's Project:
Http://www.lighthouse3d.com/wp-content/uploads/2011/03/textureGlow.zip

GLSL texture map "Go"

Related Article

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.