0
$\begingroup$

I'm using OpenGL 4.30 with JOGL (in Java). I'm playing with basic texturing, trying to learn how it's done.

The textbook I'm using has the following code:

gl.glActiveTexture(GL_TEXTURE0); // is this line necessary?
gl.glBindTexture(GL_TEXTURE_2D, textureID);
gl.glDrawArrays(GL_TRIANGLES, 0, 3);

This works well enough. But I find that if I comment out the first line, that it appears to have absolutely no effect on my program. So what is its point? Can the GPU only hold a limited number of textures at once, and I'm using #0 to hold the texture referred to by textureID? If I keep reusing the same constant GL_TEXTURE0 for all of my textures, does that mean that I'm slowing everything down? Or is this just to support legacy code, and I don't need to call this function at all? I'm very confused as to why this line is needed, and what it's doing.

$\endgroup$

1 Answer 1

1
$\begingroup$

But I find that if I comment out the first line, that it appears to have absolutely no effect on my program.

That's because TEXTURE0 happens to be the initial value. It might have an effect if you had previously set a different value.

But more generally, the point of multiple texture units is to be able to use multiple textures simultaneously when drawing (the specific way they are used being determined by your shaders). In that case, you would need to bind each texture with a different active texture unit to be able to use them simultaneously.

$\endgroup$
3
  • $\begingroup$ Can you explain what you mean by "simultaneously"? I've found that so long as I call glBindTexture() before glDrawArrays(), and put those two things in a loop, that I draw a scene with many different textures just fine. But I'm new at this, and I'm likely making a stupid error or two. $\endgroup$
    – Adam Smith
    Oct 12, 2022 at 6:40
  • 2
    $\begingroup$ @AdamSmith "Simultaneously" like two different textures both contribute to one pixel's color. For an example of a rendering technique that does that in an obvious visual way, look up "triplanar mapping". Another example might be having multiple textures for multiple material properties, like diffuse versus specular reflectivity, or normal mapping. $\endgroup$
    – Kevin Reid
    Oct 12, 2022 at 14:47
  • 1
    $\begingroup$ @AdamSmith: Remember: textures are not pictures. They are multidimensional lookup tables used to represent values that contribute to the lighting equation. A texture can mean anything to a particular shader; it could represent a color, or the roughness of a surface at that position, or a normal, or any number of things. It all depends on how the texture is used. As such, a shader is not restricted to using only one lookup table at a time. $\endgroup$ Oct 12, 2022 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.