// In our fragment shader, is where all of our texturing is going // to take place. First off we need to set a uniform variable. // A uniform variable is a variable set from inside our OpenGL // program and then read in our shader program. These can // take any form, such as sampler2D (2D texture), sampler 3D // (3D texture), float, int, vec, etc. // In this case we are going to call in a 2D texture so we need // a sampler2D uniform variable. This variable I am going to // call base_texture and will hold our texture loaded and bound // inside our OpenGL program. // After that, we need to add the varying vec2 texCoord so that // we can read in our texture coordinates set from inside our // vertex shader. // Now that we have our texture ready to be read, and our texture // coordinates. It is now time to call the texture and use it. // I am going to store the texture in a variable called: // vec4 base_color; // Even though our variable is only really a vec3 (r,g,b) // gl_FragColor requires a vec4 color to output, so we // are just going to assing base_color as a vec4 straight out. // Now to read in a texture we need to call: // texture2D(texture, texture coordinates); // Put together, this will look like: // vec4 base_color = texture2D(base_texture, texCoord); // This has just loaded in our texture and is now ready // to use. So in our gl_FragColor line, I am going to replace // vec4(1,0,0,1) with base_color. // Then our final gl_FragColor line will look like: // gl_FragColor = Ambient + (Diffuse * base_color) + Specular; // And when you run the program, you should see a textured // teapot. Now don't worry about any redish orange you see in // the darker parts of the texture, this is just because of // the objects red material and the red light we set up. uniform sampler2D base_texture; varying vec2 texCoord; varying vec3 Normal; varying vec3 Light; varying vec3 HalfVector; void main(void) { vec4 base_color = texture2D(base_texture, texCoord); Normal = normalize(Normal); float Diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * max(dot(Normal, Light),0.0); float Ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient; Ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient; float Specular = gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(max(dot(Normal,HalfVector),0.0), gl_FrontMaterial.shininess); gl_FragColor = Ambient + (Diffuse * base_color) + Specular; }