How to do texturing with OpenGL direct state access? How do you use textures with direct state access in OpenGL?

I have the following in my code, which seems to work:

GLuint textureHandle;
glGenTextures(1, &amp;textureHandle);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureHandle);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, imageW, imageH, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


However, when I replace it with the following everything just becomes black:

GLuint textureHandle;
glActiveTexture(GL_TEXTURE0);
glCreateTextures(GL_TEXTURE_2D, 1, &amp;textureHandle);
glTextureImage2DEXT(textureHandle, GL_TEXTURE_2D, 0, GL_RGBA8, imageW, imageH, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
glTextureParameteri(textureHandle, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
You still have to bind the desired texture to the texture unit to use it for rendering. In your current code, you're not specifying which texture to use for rendering, so the GL driver doesn't know which one you want to use and is defaulting to "no texture".

There are a few ways to bind a texture. You can keep using glActiveTexture+glBindTexture as in your non-DSA code, or you can use either glBindTextures or glBindTextureUnit.

For example, with glBindTextureUnit, do this:

glBindTextureUnit(0, textureHandle);


The first parameter is the texture unit to bind to.

By the way, instead of using glTextureImage2DEXT (which is from the old and deprecated EXT_direct_state_access extension), it's probably better to stick with core OpenGL calls. You can initialize the texture by using glTextureStorage2D to allocate the memory and set the format, then glTextureSubImage2D to upload the data.

(Here's an article with more on the differences between EXT_direct_state_access and the newer core version of the functionality, if you're interested.)