java - 如何在OpenGL中显示立方体的背景颜色以及两个纹理的颜色
问题描述
我正在尝试将两个纹理添加到 3d 立方体。我实现了我的目标,但在途中我失去了背景颜色。
我想显示图像的原始颜色以及背景颜色。我使用混合,但它显示背景完全黑暗。
这是我的 fragmentShaderCode 的样子:
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform sampler2D u_Texture0;" +
"uniform sampler2D u_Texture1;" +
"uniform vec4 aColor;" +
"varying vec2 v_TexCoordinate0;" +
"varying vec2 v_TexCoordinate1;" +
"void main() {" +
" vec4 base = texture2D(u_Texture0, v_TexCoordinate0);" +
" vec4 overlay = texture2D(u_Texture1, v_TexCoordinate1);" +
" mediump float ra = (overlay.a) * overlay.r + (1.0 - overlay.a) * base.r;" +
" mediump float ga = (overlay.a) * overlay.g + (1.0 - overlay.a) * base.g;" +
" mediump float ba = (overlay.a) * overlay.b + (1.0 - overlay.a) * base.b;" +
" gl_FragColor = vec4(mix(aColor.rgb, vec4(ra, ga, ba, 1.0).rgb , vec4(ra, ga, ba, 1.0).a), 1.0);" +
"}";
解决方案
The alpha channel of vec4(ra, ga, ba, 1.0)
is 1.0. Therefore the result of vec4(ra, ga, ba, 1.0).a
is always 1.0.
You need to use the texture's alpha channels. e.g.: max(base.a, overlay.a)
:
vec3 textureColor = vec3(ra, ga, ba);
float textureAlpha = max(base.a, overlay.a);
gl_FragColor = vec4(mix(aColor.rgb, textureColor, textureAlpha), 1.0);
Simplify the code by mixing the texture colors with the mix
function:
void main() {
vec4 base = texture2D(u_Texture0, v_TexCoordinate0);
vec4 overlay = texture2D(u_Texture1, v_TexCoordinate1);
vec3 textureColor = mix(base.rgb, overlay.rgb, overlay.a);
float textureAlpha = max(base.a, overlay.a);
gl_FragColor = vec4(mix(aColor.rgb, textureColor, textureAlpha), 1.0);
}
推荐阅读
- forms - 停止 LaravelCollective 表单模型绑定被 GET 变量覆盖?
- twitter-bootstrap - 具有不同高度移动与桌面模式的引导推/拉柱
- php - Codeigniter 数据库错误处理,试图涵盖我所有可能的场景
- python - 如何在 Inception v3 中解冻层以进行迁移学习?
- windows - 我应该使用哪种计时方法来测量时间变化
- python - 使用补充列从 numpy 数组初始化 DataFrame
- python - Kivy:分散内部分散
- json - 如何验证 JSON 模式中的属性数量
- javascript - parseInt 数组返回未定义 - javascript
- android - 如何在 strings.xml 中解决此错误“Unsupported Node 'item'”