我想使用OpenGL移动并旋转透明的BGRA图像(文字覆盖图)。这里是代码片段我用:OpenGL:为什么复制纹理时alpha通道设置为255?
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,我不明白为什么。
“旋转后,输出图像包含0,0,0,0,0,0,0,0,0,05,0 ....”当您创建OpenGL上下文时,您是否要求帧缓存具有alpha在里面? – 2012-02-13 22:53:17