Chapter 19: Textures
This is also a very good overview
How To Use Textures
Create a handle for the texture.
GLuint TextureID;
Create and bind the texture.
glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);
Set any parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Upload image data.
std::string const path = resourceDirectory + "/Image.jpg";
int width, height, channels;
unsigned char * data = stbi_load(path.c_str(), & width, & height, & channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
}
Now, in the render loop. Active a texture slot and bind your texture to it.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, TextureID);
Specify a uniform that contains the texture slot you just bound the texture to (NOT the texture handle itself).
glUniform1i(prog->getUniform("uTexture"), 0);
In the fragment shader (or vertex shader, if you want to), you can access the texture using the uniform we just bound.
It has type sampler2D
, even though we used the integer uniform method in our C++ code.
uniform sampler2D uTexture;
void main()
{
vec4 color = texture(uTexture, fTexCoords);
// ...
}