How to do it...

To apply a projected texture to a scene, use the following steps:

  1. In the OpenGL application, load the texture into texture unit zero. While the texture object is bound to the GL_TEXTURE_2D target, use the following code to set the texture's settings:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  1. Also within the OpenGL application, set up your transformation matrix for the slide projector and assign it to the uniform ProjectorMatrix. Use the following code to do this. Note that this code makes use of the GLM libraries discussed in Chapter 1, Getting Started with GLSL:
vec3 projPos, projAt, projUp;
// Set the above 3 to appropriate values...
mat4 projView = glm::lookAt(projPos, projAt, projUp);
mat4 projProj = glm::perspective(glm::radians(30.0f), 1.0f, 0.2f, 1000.0f);
mat4 bias = glm::translate(mat4(1.0f), vec3(0.5f));
bias = glm::scale(bias, vec3(0.5f));
prog.setUniform("ProjectorMatrix", bias * projProj * projView);
  1. Use the following code for the vertex shader:
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 mat4 ModelViewMatrix;
uniform mat4 ModelMatrix;
uniform mat3 NormalMatrix;
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;
}
  1. Use the following code for the fragment shader:
in vec3 EyeNormal;       // Normal in eye coordinates 
in vec4 EyePosition;     // Position in eye coordinates 
in vec4 ProjTexCoord; 
 
layout(binding=0) uniform sampler2D ProjectorTex; 

// Light and material uniforms... layout( location = 0 ) out vec4 FragColor; vec3 blinnPhong( vec3 pos, vec3 norm ) { // Blinn-Phong model... } void main() { vec3 color = blinnPhong(EyePosition.xyz, normalize(EyeNormal));

vec3 projTexColor = vec3(0.0);
if( ProjTexCoord.z > 0.0 )
projTexColor = textureProj( ProjectorTex, ProjTexCoord ).rgb;

FragColor = vec4(color + projTexColor * 0.5, 1); }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.253.152