http://blog.csdn.net/zhuyingqingfen/article/details/19331721
Category: GLSL
Projected texture mapping (projective texture mapping): It is the projection of a texture onto the object of the scene, like a projector projecting a slide onto another object.
such as: projecting onto a teapot with the texture image on the left
How to implement a projection texture:
In fact, the most important point is to determine the texture coordinates, texture coordinate determination depends on the object surface point relative position and the position of the projector. In OpenGL we can define a camera, we define a coordinate space at the center of the projector, Viewmatrix (V) Converts the point of the world coordinate system into the projector's coordinate system, and then defines a projection matrix (P). This p*v the point into the projection space, but the normalized projection space is [ -1,1], and the texture coordinate is [0,1], so we need to convert the scene to [0,1], we can first reduce it by 1/2, and then translate 1/2, so that the conversion to [0,1].
Note: Since the coordinates are homogeneous coordinate systems (homogeneous), we need to divide by W before we can access the texture coordinates.
Application Settings Code Snippet
[CPP]View Plaincopyprint?
- VEC3 Projpos = VEC3 (5.0f,5.0f,5.0f);
- VEC3 Projat = VEC3 ( -2.0f,-4.0f,0.0f);
- VEC3 Projup = VEC3 (0.0f,1.0f,0.0f);
- Mat4 Projview = Glm::lookat (Projpos, Projat, Projup);p rint (Projview);
- Mat4 projproj = glm::p erspective (30.0f, 1.0f, 0.2f, 1000.0f);p rint (projproj);
- Mat4 Projscaletrans = glm::translate (VEC3 (0.5f)) * Glm::scale (VEC3 (0.5f));
- Prog.setuniform ("Projectormatrix", Projscaletrans * projproj * projview);
- //Load texture file
- Glactivetexture (GL_TEXTURE0);
- Tgaio::loadtex (".. /media/texture/flower.tga ");
- Gltexparameteri (gl_texture_2d, gl_texture_wrap_s, Gl_clamp_to_border);
- Gltexparameteri (gl_texture_2d, gl_texture_wrap_t, Gl_clamp_to_border);
Vertex shader
[CPP]View Plaincopyprint?
- #version 430
- Layout (location = 0) in VEC3 vertexposition;
- Layout (location = 1) in VEC3 vertexnormal;
- Out VEC3 Eyenormal; //Normal in eye coordinates
- Out VEC4 eyeposition; //Position in eye coordinates
- Out VEC4 Projtexcoord;
- Uniform mat4 Projectormatrix;
- Uniform VEC3 worldcameraposition;
- Uniform mat4 Modelviewmatrix;
- Uniform mat4 Modelmatrix;
- Uniform MAT3 Normalmatrix;
- Uniform mat4 ProjectionMatrix;
- Uniform mat4 MVP;
- void Main ()
- {
- VEC4 Pos4 = VEC4 (vertexposition,1.0);
- Eyenormal = Normalize (Normalmatrix * vertexnormal);
- eyeposition = Modelviewmatrix * POS4;
- Projtexcoord = Projectormatrix * (Modelmatrix * pos4);
- gl_position = MVP * POS4;
- }
piece element Shader
[CPP]View Plaincopyprint?
- #version 430
- In VEC3 Eyenormal; //Normal in eye coordinates
- In Vec4 eyeposition; //Position in eye coordinates
- In Vec4 Projtexcoord;
- Layout (binding=0) uniform sampler2d Projectortex;
- struct Materialinfo {
- VEC3 Kd;
- VEC3 Ks;
- VEC3 Ka;
- float shininess;
- };
- Uniform Materialinfo Material;
- struct Lightinfo {
- VEC3 Intensity;
- VEC4 Position;
- };
- Uniform Lightinfo Light;
- Layout (location = 0) out VEC4 fragcolor;
- VEC3 Phongmodel (vec3 pos, vec3 norm) {
- VEC3 s = Normalize (VEC3 (light.position)-POS);
- VEC3 v = normalize (-pos.xyz);
- VEC3 r = Reflect (-s, Norm);
- VEC3 ambient = light.intensity * MATERIAL.KA;
- float Sdotn = max (dot (s,norm), 0.0);
- VEC3 diffuse = light.intensity * MATERIAL.KD * SDOTN;
- VEC3 spec = vec3 (0.0);
- if (Sdotn > 0.0)
- Spec = light.intensity * MATERIAL.KS *
- POW (max (dot (r,v), 0.0), material.shininess);
- return ambient + diffuse + spec;
- }
- void Main () {
- VEC3 color = Phongmodel (VEC3 (eyeposition), eyenormal);
- VEC4 Projtexcolor = vec4 (0.0);
- if (projtexcoord.z > 0.0)
- Projtexcolor = Textureproj (Projectortex, Projtexcoord);
- Fragcolor = VEC4 (color,1.0) + projtexcolor * 0.5;
- }
In the element shader, if (projtexcoord.z > 0.0) means that if the projtexcoord.z is negative, then behind the projector, we do not need to find the corresponding texture.
The Textureproj function is used to access the texture (the incoming texture coordinates are the coordinates of the projected coordinate system), and as we mentioned before, after panning the scale projection matrix, we need to divide the W term in its coordinates, and textureproj will do it for us.
In this case there is a big drawback that the projection texture (projected texture) will be projected onto any object in the scene, even if there are objects obscured. For example, we can let the floor move down some. Results
Summarize:
Projection texture mapping The real process is "based on the projector (viewpoint camera) position, projection angle, object coordinates, the corresponding texture coordinates of each vertex, and then according to texture coordinates to query the texture value", that is, not the texture projection to the wall, but the wall projection to the texture. The coordinate of the projection texture is not related to the texture itself, but is determined by the position and angle of the projector and the vertex coordinates of the 3D model.
projtexcoord= Offset Matrix * light source projection matrix * Light source observation matrix * Modeling Matrix
OpenGL 4.0 GLSL implement projected texture mapping (projective Texture Mapping) (RPM)