【发布时间】:2026-02-24 18:30:01
【问题描述】:
找到example of offscreen rendering in QT(更新 1 中有一个工作代码)。
但无法使其支持 alpha,请参见下面的代码:
QSurfaceFormat surfaceFormat;
surfaceFormat.setColorSpace(QSurfaceFormat::ColorSpace::sRGBColorSpace);
surfaceFormat.setRenderableType(QSurfaceFormat::RenderableType::OpenGL);
surfaceFormat.setMajorVersion(4);
surfaceFormat.setMinorVersion(3);
surfaceFormat.setAlphaBufferSize(8);
if (surfaceFormat.hasAlpha())
{
qInfo() << "The surface has alpha.";
}
else
{
qInfo() << "The surface does not have alpha.";
}
它总是打印“表面有 alpha”。但是我的屏幕外渲染没有 alpha 或者更确切地说我得到了一个奇怪的效果,透明像素变成白色,而背景是黑色:
将其与不透明的原始图像进行比较:
区别在于片段着色器中的vec4(fragColor, 0.5)和vec4(fragColor, 1.0):
program.addShaderFromSourceCode(QOpenGLShader::Fragment,
"#version 330\r\n"
"in vec3 fragColor;\n"
"out vec4 color;\n"
"void main() {\n"
" color = vec4(fragColor, 1.0);\n"
"}\n"
);
我可以设置其他选项吗?
我的环境:Windows 10、MSVC 2019、QT 6.2。
EDIT1:
在渲染三角形之前做了一些进一步的实验并添加了以下内容:
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquation(GL_FUNC_ADD);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
得到白色背景:
与glClearColor(0, 0, 0, 0.5) 我变灰了:
【问题讨论】:
-
可能有类似的问题*.com/questions/49688111/…
-
他们都是黑色的...
-
@Swift-FridayPie 它也可以是白色的,参见 EDIT1