0
\$\begingroup\$

In OpenGL, is there a format for a texture in glTexImage2D that prevents blending, or do you have to disable it using glDisable(GL_BLEND).

Reason for use is that I would like to store additional data in the alpha channel of a texture, but it blends with whatever's behind it.

\$\endgroup\$
  • \$\begingroup\$ Is this in the Mac or an iPhone? What format is the file that you're using for the texture? PNGs on the Mac and in Photoshop premultiply. What I ended up doing was storing them as separate files for RGB and for Alpha. Then I wrote a texture loader to combine them when creating the texture. If this sounds right I can write it up as an answer tomorrow. \$\endgroup\$ – badweasel Apr 26 '15 at 10:35
  • \$\begingroup\$ PC. When using glTexImage2D you set an internal format for the texture, usually GL_RGBA. My question is asking if there is a format similar to this, but prevents blending when alpha is less than 1. \$\endgroup\$ – Cimera42 Apr 27 '15 at 7:22
  • \$\begingroup\$ No that format supports a full 8,8,8,8 bit. What's happening is called premultiplication. Let's say the alpha is 50% and the RGB is white. The RGB is pre multiplied by 50% and the result is grey. If the alpha is 0 the RGB will be also. This isn't happening in the glteximage2d it's more likely happening in the source file. That's why I had to store the RGB and Alpha in different source files and then merge them to store in OpenGL. \$\endgroup\$ – badweasel Apr 27 '15 at 7:27
  • \$\begingroup\$ PNG files aren't the best for these kinds of textures because they always premultiply. At least with photoshop they do. \$\endgroup\$ – badweasel Apr 27 '15 at 7:29
  • \$\begingroup\$ There is no source file, as the texture is being written to from a framebuffer. \$\endgroup\$ – Cimera42 Apr 27 '15 at 8:14
0
\$\begingroup\$

Now that I know that the context is rendering to a texture frame buffer I can try to answer.

Yes, you do have to glDisable(GL_BLEND) otherwise it will blend according to whatever glBlendFunc is currently set, which will mess with the RGB channels based on what you're writing to the alpha channel. Then you need to do blending you write it in your shader.

I've done a lot of testing with writing to textures and trying to preserve the alpha channel. Check out this question on stack overflow: https://stackoverflow.com/questions/19674740/opengl-es2-premultiplied-vs-straight-alpha-blending It's not exactly the same thing but might be some help.

Of course you do have to make sure that your texture frame buffer is GL_RGBA in both places. Mine on iOS looks a little like this:

glGenFramebuffers(1, &textureFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, textureFramebuffer);
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glGenRenderbuffers(1, &textureDepthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, textureDepthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, textureDepthRenderbuffer);

Of course if you don't need the depth buffer you can skip that part.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thank you for clarifying that I would have to use glDisable to prevent blending. I'm accepting this as the answer. \$\endgroup\$ – Cimera42 Apr 27 '15 at 12:28

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.