OpenGL:为什么在复制纹理时alpha通道设置为255?

时间:2012-02-13 22:47:06

标签: opengl alpha-transparency

我想使用OpenGL移动和旋转透明的BGRA图像(文本叠加)。这是我使用的代码片段:

glViewport(0, 0, iWidth, iHeight); // Reset The Current Viewport
glEnable(GL_TEXTURE_2D);

glGenTextures(1, &arTex[0].iName);
glBindTexture(GL_TEXTURE_2D, arTex[0].iName);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D(GL_TEXTURE_2D, 0, 4, imf.iWidth, imf.iHeight, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, ib.GetData());

glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Note: Transparent alpha value
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(-dAspectCanvas / 2.0, dAspectCanvas / 2.0, -0.5, 0.5, -1.0, 1.0); // Note: Using (-1.0; 1.0) for Z-planes
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScaled(Motion.Scale.GetValue(dPosition), Motion.Scale.GetValue(dPosition), 1.0);
glTranslated(Motion.OffsetX.GetValue(dPosition) * dAspectCanvas, Motion.OffsetY.GetValue(dPosition), 0.0);
glRotated(Motion.Rotation.GetValue(dPosition), 0.0, 0.0, 1.0);
glTranslated(Motion.PositionX.GetValue(dPosition) * dAspectCanvas, Motion.PositionY.GetValue(dPosition), 1.0);

// Rotating and moving

glBindTexture(GL_TEXTURE_2D, arTex[iTexIndex].iName); // has to be called outside glBegin/glEnd scope

glBegin(GL_TRIANGLE_STRIP); // "Many OpenGL applications avoid quads altogether because of their inherent rasterization problems"

glTexCoord2d(rcTextureIntersection.left, rcTextureIntersection.top);
glVertex2d(rcVertexIntersection.left, rcVertexIntersection.top);
glTexCoord2d(rcTextureIntersection.right, rcTextureIntersection.top);
glVertex2d(rcVertexIntersection.right, rcVertexIntersection.top);
glTexCoord2d(rcTextureIntersection.left, rcTextureIntersection.bottom);
glVertex2d(rcVertexIntersection.left, rcVertexIntersection.bottom);
glTexCoord2d(rcTextureIntersection.right, rcTextureIntersection.bottom);
glVertex2d(rcVertexIntersection.right, rcVertexIntersection.bottom);
glEnd();

glDisable(GL_TEXTURE_2D);
glFlush();   
glReadBuffer(GL_BACK);
glReadPixels(0, 0, imf.iWidth, imf.iHeight, GL_BGRA_EXT, GL_UNSIGNED_BYTE, ibOut.GetData());

但无论我在glTexImage2D中尝试什么,我的透明黑色图像都会变成完全不透明的黑色图像。输入BGRA图像包含字节:0,0,0,0,0 ....旋转后,输出图像包含0,0,0,255,0,0,0,255,0 .... A总是设置为255,我无法理解为什么。

4 个答案:

答案 0 :(得分:0)

Alpha并不意味着“透明”。 Alpha本身并不意味着任何。当你使用那个值来做某事时,它才有意义。

如果要渲染对象并使用alpha来控制对象的透明度,则需要激活混合和set up a proper blend mode

答案 1 :(得分:0)

你在这里遇到两个问题。一个是,你没有启用混合,这有效,纹理的alpha值对透明度没有影响。

另一个问题 - 对你来说更重要的是 - 你的帧缓冲区(你在哪里绘制纹理)可能没有alpha通道,因此默认为最大值。您不需要启用混合以使纹理影响帧缓冲区alpha值(它只会替换它)。但是你需要一个带有alpha通道的帧缓冲来完成这项工作。请注意,在屏幕上的帧缓冲区上获取alpha缓冲区是可能的(我甚至推荐它)。但是对于图像处理,帧缓冲区只是错误的地方。您需要一个安全的环境: Framebuffer对象

答案 2 :(得分:0)

无需混合任何内容,但需要使用alpha位设置帧缓冲区。这是一个问题,我完全忘记了wglCreateContext。

感谢您的帮助!

答案 3 :(得分:0)

可能使用正确的alpha颜色设置glClearColor。

相关问题