我在使用 OpenGL ES 3.1 的 Android 上遇到问题。我编写了一个应用程序,显示液体从屏幕顶部落下。这种液体由许多有点透明的粒子组成,但使用 alpha 混合的颜色在另一部手机上显示不同。
每一滴的颜色定义如下:
private int particleColor = Color.argb(50, 0, 172, 231);
每个粒子颜色都存储在缓冲区中:
private ByteBuffer colorBuffer = ByteBuffer.allocateDirect(MAX_PARTICLES * PARTICLE_COLOR_COUNT).order(ByteOrder.nativeOrder());
并将这个缓冲区传递给要绘制的 OpenGL 实体:
/**
* Binds the data to OpenGL.
* @param program as the program used for the binding.
* @param positionBuffer as the position buffer.
* @param colorBuffer as the color buffer.
*/
public void bindData(LiquidShaderProgram program, ByteBuffer positionBuffer, ByteBuffer colorBuffer){
glVertexAttribPointer(program.getPositionAttributeLocation(), POSITION_COMPONENT_COUNT, GL_FLOAT, false, POSITION_COMPONENT_COUNT * OpenGLUtils.BYTE_PER_FLOAT, positionBuffer);
glEnableVertexAttribArray(program.getPositionAttributeLocation());
glVertexAttribPointer(program.getColorAttributeLocation(), COLOR_COMPONENT_COUNT, GL_UNSIGNED_BYTE, true, COLOR_COMPONENT_COUNT, colorBuffer);
glEnableVertexAttribArray(program.getColorAttributeLocation());
}
通过调用此函数来关闭渲染:
/**
* Render the liquid.
* @param particleCount as the particle count.
*/
public void draw(int particleCount) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays(GL_POINTS, 0, particleCount);
glDisable(GL_BLEND);
}
片段着色器只绘制它接收到的颜色:
precision mediump float;
varying vec4 v_Color;
void main() {
if (length(gl_PointCoord - vec2(0.5)) > 0.5) {
discard;
} else {
gl_FragColor = v_Color;
}
}
它在一部手机(Nexus 5X)上运行良好:
但是在另一部手机(Galaxy S10)上,使用完全相同的代码,颜色就不一样了:
有没有人知道解决这个问题的方法?我也想在第二部手机上显示正确的颜色。