7
$\begingroup$

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, &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, &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);
$\endgroup$
9
$\begingroup$

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.)

$\endgroup$
  • 1
    $\begingroup$ "You might want to convert your code to use that if possible" His code is using that. glCreateTextures is specific to the ARB_DSA function. He's using an odd mixture of the two, likely because he couldn't find a glTextureImage2D in the ARB version (since the ARB wants to discourage non-immutable textures). $\endgroup$ – Nicol Bolas Jan 6 '17 at 1:11
  • 1
    $\begingroup$ @NicolBolas Aha, thanks, I hadn't caught that. I've updated the answer again. $\endgroup$ – Nathan Reed Jan 6 '17 at 3:12
5
$\begingroup$

I have now replaced it with the following, which works:

GLuint textureHandle;
glCreateTextures(GL_TEXTURE_2D, 1, &textureHandle);
glTextureStorage2D(textureHandle, 1, GL_RGBA8, imageW, imageH);
glTextureSubImage2D(textureHandle, 0, 0, 0, imageW, imageH, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
glTextureParameteri(textureHandle, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTextureUnit(0, textureHandle);
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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