TP2 : Textures
The deadline for this project is: Saturday, December 7th, 2013 (Midnight European Time)
Texture mapping
In order to create and map a texture, you need to take the following steps:
- get a texture ID: (glGenTextures)
- link with this texture: (glBindTexture)
- set-up the texture parameters: (glTexParameter*)
- load the actual picture: (glTexImage2D)
To make your life easier, all these steps are encapsulated inside the load and create methods of the GQTexture class.
At display, you specify the active texture unit (glActiveTexture), then connect the texture to it using the right texture ID (glBindTexture). Inside the shader, the texture units are named, and receive the correct value through a uniform (uniform sampler2D Tex2D receives the value 0 if you have to access the texture unit 0, for example). The bindNameTexture method in the classe GQShaderRef class encapsulates all these operations for you.
Of course, you also need to specify the texture coordinates on the object when you access the sampler using the GLSL function texture.
A textured sphere: the earth
For this practical, we will use the sphere model, for which it is easy to compute texture coordinates.
Day and Night
First, we map the earth-by-day texture ((textureDay) onto the earth. Note that the texture is actually reversed compared to the actual earth. Feel free to reverse the texture coordinates to display a more realistic planet if you wish.
Then, we add the sun illuminating the planet. As in the first practical, you need to compute a diffuse coefficient, and use it to adapt the color of the texture between dark (night) and the texture (day). Remember that at least half the sphere should appear dark.
Try changing the position of the light source, and animating the sphere by rotating it.
Next, try loading the second texture ((textureNight), which represents the earth at night. Change the code in the fragment shader to replace dark with this texture (I suggest using the GLSL function mix).
Normal / Bump mapping
Next, we are going to add some relief to this sphere, using normal mapping, then bump mapping.
Normal Mapping
For normal mapping, we start with a texture map of the normals (stored in "earth3.png", and available in textureNormals). Start by visualizing this texture on the earth surface.
This texture contains modified normals, stored in local coordinates, tangent to the object surface. In order to use them, you need to convert the view vector and the light vector to the same coordinates.
- Add the variable vec3 VertexTangent to the vertex shader (that's the x-axis for these coordinates).
- The normal is the z-axis for these coordinates.
- First, convert both tangent and normal from the model coordinates to the viewpoint coordinates, giving the tv and nv vectors.
- Next, get the third axis of the coordinates, bv, using a cross product of tv and nv,
- With these three vectors, create the transform matrix 3x3 tbnvMatrix, whose rows are the vectors tv, bv and nv (remember there is a transpose function in GLSL).
- Once you have this matrix, use it to convert the view vector and the light vector into the tangent-space coordinates.
- Congratulations, you now have all three vectors (view, light and normal) in the same coordinates, and you can use them to compute diffuse and specular shading.
- One small warning: inside a texture, the normals are stored in [0;1]³. You need to convert them into [-1; 1]³.