Use only alpha channel of texture in OpenGL?

筅森魡賤 提交于 2019-12-12 10:08:47

问题


Hey, I'm trying to draw a constant color to the framebuffer and blend it using the alpha channel from an RGBA texture. I've been looking at glBlendFunc and glBlendColor, but can't seem to figure out a way to ignore the RGB values from the texture. I'm thinking I'll have to pull out the alpha values myself and make a second texture with GL_ALPHA. Is there a better way to do this?

Thanks!


回答1:


I'm using colour coded picking. This is the code I'm using so that transparent areas of a textured quad will not be picked:

glEnable(GL_COLOR_MATERIAL);
glEnable(GL_TEXTURE_2D);
glEnable(GL_ALPHA_TEST);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);

glAlphaFunc(GL_GREATER, 0.5f);

glColor3ub(myColor[0], myColor[1], myColor[2]);

drawTexturedQuad();

Essentially I'm saying I want the RGB to be whatever it was before applying the texture (This would be myColor) and I want the ALPHA to be taken from the texture.




回答2:


When using glBlendFunc, GL_SRC_ALPHA and GL_DST_ALPHA can be used to select the alpha channel in your texture (which to choose depends on your blend operation and textures). For more detailed blending tutorials see:

  • nehe tutorial #32

The relevant OpenGL documentation:

  • http://www.opengl.org/sdk/docs/man/xhtml/glBlendFunc.xml


来源:https://stackoverflow.com/questions/2485370/use-only-alpha-channel-of-texture-in-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!