Glsl projective texture

Source: Internet
Author: User

Projective texture is an important basic technology. Although it can be easily implemented in the era of fixed pipelines, projective texture can be flexibly used in the era of programmable pipelines, it is a basic operation to achieve a variety of advanced effects. Of course, its own effect is also very "cool". In addition, for "Shadow Mapping", which is not a new technology ", projective texture is a required operation. It is to project the depth texture (depth texture) obtained from the observation direction of the light source to the observation space of the light source;

It is very easy to implement projective texture in a fixed pipeline:

First, you must enable automatic texture generation and set relevant parameters:

Static glfloat splane [4] = {1, 0, 0, 0 };
Static glfloat tplane [4] = {0, 1, 0, 0 };
Static glfloat rplane [4] = {0, 0, 1, 0 };
Static glfloat qplane [4] = {0, 0, 0, 1 };

Gltexgenfv (gl_s, gl_eye_plane, splane );
Gltexgenfv (gl_t, gl_eye_plane, tplane );
Gltexgenfv (gl_r, gl_eye_plane, rplane );
Gltexgenfv (gl_q, gl_eye_plane, qplane );
Gltexgeni (gl_s, gl_texture_gen_mode, gl_eye_linear );
Gltexgeni (gl_t, gl_texture_gen_mode, gl_eye_linear );
Gltexgeni (gl_r, gl_texture_gen_mode, gl_eye_linear );
Gltexgeni (gl_q, gl_texture_gen_mode, gl_eye_linear );

Glable (gl_texture_gen_s );
Glable (gl_texture_gen_t );
Glable (gl_texture_gen_r );
Glable (gl_texture_gen_q );

In this way, the texture can be projected from the eye observation direction to the scene. If you want to project the texture from the light source direction, use glactivetexturearb to activate the Texture unit of the layer first, load the perspective observation matrix in the light source direction, and finally forget the gl_texturen_arb );

However, it is too difficult to implement various special effects without using the shader. When you enter the programmable pipeline, the automatic texture generation set above will not work; we need to implement projection sampling in the shader. However, it is very easy to implement in glsl:

 

BelowCodeTo put a texture in a specified observation direction, you need to load the projection observation matrix to be used into gl_texture0_arb in advance.

// Vertex shader

Uniform mat4 cammatinv;
Uniform mat4 cammat;
Varying vec4 projtexturecoord;
Void main (void)
{

Gl_position = gl_modelviewprojectionmatrix * gl_vertex;

Vec4 Pos = gl_modelviewmatrix * gl_vertex; // observe coordinates

Pos = cammatinv * Pos; // world coordinates
Projtexturecoord = gl_texturematrix [0] * Pos; // texture coordinate;

Projtexturecoord. Z ++ = 0.99;

}

// Fragment shader

Varying vec4 projtexturecoord;
Uniform sampler2d projectivetexture;

Void main (void)
{

Vec4 test = projtexturecoord;
Test. x = test. X/test. W;
Test. Y = test. Y/test. W;

Vec4 tex_color = texture2dproj (projtexturecoord, projectivetexture );
 

// Prevent the texture from extending the boundary and ing to the rear surface
If (test. x <0.0) tex_color.rgb = 1.0;
If (test. x> 1.0) tex_color.rgb = 1.0;
If (test. Y <0.0) tex_color.rgb = 1.0;
If (test. Y> 1.0) tex_color.rgb = 1.0;
If (test. z <0.0) tex_color.rgb = 1.0;

Gl_fragcolor = gl_color * tex_color;

}

 

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.