Q1. You would call glGenTexture() like soglGenTextures(1, &handle)
. handle
would now be an object containing an empty GL_TEXTURE_1D, GL_TEXTURE_2D, etc..
that you can fill with a texture by using e.g. glBindTexture(GL_TEXTURE_2D, handle)
followed by glTexImage2D(GL_TEXTURE_2D, ...)
. I don't think there is a real limit to have many texture objects you could have.
There is a limit to how many you can use at the time however. You might have multiple textures per model in your scene, diffuse-map, normal-map, etc... so you would link each of your texture handles to their own texture slot so you can sample all of them at the same time.
Imagine each time your render something you can only use 8 colors at the time. You may create as many colors as you like and store that in GPU memory. But when it comes to rendering you can only select 8 color slots to render with and no more.
Example:
// Generate the textures
glGenTexture(1, &handle1)
glGenTexture(1, &handle2)
// Set the active texture slot to 0, bind handle1 to it and fill it with a texture.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, handle1);
glTexImage2D(GL_TEXTURE_2D, ...)
// Set the active texture slot to 0, bind handle1 to it and fill it with a texture.
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, handle2);
glTexImage2D(GL_TEXTURE_2D, ...)
// Get the uniform samplers from the shader program.
GLint sampler1 = glGetUniformLocation(program_handle, "sampler1");
GLint sampler2 = glGetUniformLocation(program_handle, "sampler2");
// Set sampler1 to sampler from texture slot 0 and sampler2 will sample from texture
// slot 1.
glUniform1i(sampler1, 0);
glUniform1i(sampler2, 1);
Q2. Let me quote this page: https://www.opengl.org/sdk/docs/man/html/glTexParameter.xhtml
glTexParameter specifies the texture parameters for the active texture unit, specified by calling glActiveTexture. glTextureParameter specifies the texture parameters for the texture object with ID texture.
To clarify, glTexParameteri(GL_TEXTURE_2D, ...)
should affect the texture associated with the handle abound to GL_TEXTURE_2D
. So if you called glBindTexture(GL_TEXTURE_2D, handle1)
then all glTexParameteri(GL_TEXTURE_2D, ...)
should be associated with handle1
. This assumes you didn't change active texture unit as that would have a different GL_TEXTURE_2D
bound to it. glTextureParameter()
is new in OpenGL 4.5 and can be used to avoid all that binding by passing the handle directly.