OpenGL 4.0 GLSL implement projected texture mapping (projective Texture Mapping) (RPM)

Source: Internet
Author: User

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?
  1. VEC3 Projpos = VEC3 (5.0f,5.0f,5.0f);
  2. VEC3 Projat = VEC3 ( -2.0f,-4.0f,0.0f);
  3. VEC3 Projup = VEC3 (0.0f,1.0f,0.0f);
  4. Mat4 Projview = Glm::lookat (Projpos, Projat, Projup);p rint (Projview);
  5. Mat4 projproj = glm::p erspective (30.0f, 1.0f, 0.2f, 1000.0f);p rint (projproj);
  6. Mat4 Projscaletrans = glm::translate (VEC3 (0.5f)) * Glm::scale (VEC3 (0.5f));
  7. Prog.setuniform ("Projectormatrix", Projscaletrans * projproj * projview);
  8. //Load texture file
  9. Glactivetexture (GL_TEXTURE0);
  10. Tgaio::loadtex ("..  /media/texture/flower.tga ");
  11. Gltexparameteri (gl_texture_2d, gl_texture_wrap_s, Gl_clamp_to_border);
  12. Gltexparameteri (gl_texture_2d, gl_texture_wrap_t, Gl_clamp_to_border);


Vertex shader

[CPP]View Plaincopyprint?
  1. #version 430
  2. Layout (location = 0) in VEC3 vertexposition;
  3. Layout (location = 1) in VEC3 vertexnormal;
  4. Out VEC3 Eyenormal; //Normal in eye coordinates
  5. Out VEC4 eyeposition; //Position in eye coordinates
  6. Out VEC4 Projtexcoord;
  7. Uniform mat4 Projectormatrix;
  8. Uniform VEC3 worldcameraposition;
  9. Uniform mat4 Modelviewmatrix;
  10. Uniform mat4 Modelmatrix;
  11. Uniform MAT3 Normalmatrix;
  12. Uniform mat4 ProjectionMatrix;
  13. Uniform mat4 MVP;
  14. void Main ()
  15. {
  16. VEC4 Pos4 = VEC4 (vertexposition,1.0);
  17. Eyenormal = Normalize (Normalmatrix * vertexnormal);
  18. eyeposition = Modelviewmatrix * POS4;
  19. Projtexcoord = Projectormatrix * (Modelmatrix * pos4);
  20. gl_position = MVP * POS4;
  21. }


piece element Shader

[CPP]View Plaincopyprint?
  1. #version 430
  2. In VEC3 Eyenormal; //Normal in eye coordinates
  3. In Vec4 eyeposition; //Position in eye coordinates
  4. In Vec4 Projtexcoord;
  5. Layout (binding=0) uniform sampler2d Projectortex;
  6. struct Materialinfo {
  7. VEC3 Kd;
  8. VEC3 Ks;
  9. VEC3 Ka;
  10. float shininess;
  11. };
  12. Uniform Materialinfo Material;
  13. struct Lightinfo {
  14. VEC3 Intensity;
  15. VEC4 Position;
  16. };
  17. Uniform Lightinfo Light;
  18. Layout (location = 0) out VEC4 fragcolor;
  19. VEC3 Phongmodel (vec3 pos, vec3 norm) {
  20. VEC3 s = Normalize (VEC3 (light.position)-POS);
  21. VEC3 v = normalize (-pos.xyz);
  22. VEC3 r = Reflect (-s, Norm);
  23. VEC3 ambient = light.intensity * MATERIAL.KA;
  24. float Sdotn = max (dot (s,norm), 0.0);
  25. VEC3 diffuse = light.intensity * MATERIAL.KD * SDOTN;
  26. VEC3 spec = vec3 (0.0);
  27. if (Sdotn > 0.0)
  28. Spec = light.intensity * MATERIAL.KS *
  29. POW (max (dot (r,v), 0.0), material.shininess);
  30. return ambient + diffuse + spec;
  31. }
  32. void Main () {
  33. VEC3 color = Phongmodel (VEC3 (eyeposition), eyenormal);
  34. VEC4 Projtexcolor = vec4 (0.0);
  35. if (projtexcoord.z > 0.0)
  36. Projtexcolor = Textureproj (Projectortex, Projtexcoord);
  37. Fragcolor = VEC4 (color,1.0) + projtexcolor * 0.5;
  38. }



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)

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.