我想使用 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,我不明白为什么。