Android OpenGL ES 纹理颜色在模拟器上正确显示,但在手机上不正确
我有一个问题,我使用的任何纹理的颜色都会被漂白(如果这是一个很好的词来描述它),在两部不同的手机上,但纹理在模拟器上显示得很好。
这是图像。 第一张图片是我正在使用的纹理。 第二张图片是纹理在模拟器上的显示方式以及它在手机上的显示方式。 第三张图片是纹理在手机上的实际显示效果。
我应该包含绘制矩形的代码吗?矩形只是一个顶点缓冲区和纹理坐标缓冲区。实际上对纹理所做的任何事情都是 gl.glBindTexture 和 gl.glBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA)。 gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f) 也被调用。
以下代码是我认为可能存在错误的地方。我希望有人能在这里帮助我。谢谢!
这是在我的 GLSurfaceView.Renderer 实现中
@Override
public void onSurfaceCreated(final GL10 gl, final EGLConfig config) {
DebugLog.d("onSurfaceCreated");
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glDisable(GL10.GL_DITHER);
gl.glDisable(GL10.GL_LIGHTING);
gl.glDisable(GL10.GL_MULTISAMPLE);
gl.glEnable(GL10.GL_BLEND);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glFrontFace(GL10.GL_CW);
gl.glCullFace(GL10.GL_BACK);
}
这是在我的 BitmapTexture 类中。
@Override
public void loadBitmapToHardware(final GL10 gl) throws IOException {
final Bitmap bitmap = loadBitmap();
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bitmap, GL10.GL_UNSIGNED_BYTE, 0);
bitmap.recycle();
}
@Override
public Bitmap loadBitmap() {
InputStream is = null;
try {
is = mContext.getAssets().open(mBitmapPath);
return BitmapFactory.decodeStream(is);
} catch (final IOException e) {
DebugLog.e("Failed to load Bitmap in " + this.getClass().getSimpleName() + " from path: " + mBitmapPath, e);
return null;
} finally {
try {
is.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
@Override
public void loadTexture() throws IOException {
gl.glGenTextures(1, TEXTURE_CONTAINER, 0);
mTextureId = TEXTURE_CONTAINER[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
loadBitmapToHardware();
}
I have a problem where the colors of any textures that I use are bleached, if that's a good word to use to describe it, on two different phones but the textures show up just fine on the emulator.
Here are the images.
The first image is the texture that I'm using.
The second image is how the texture shows up on the emulator and the way it is supposed to show up on the phones.
The third image is how the texture actually shows up on the phone.
Should I include the code where I draw the rectangle? The rectangle is just a vertex buffer and texcoord buffer. Anything that actually is done with the texture is gl.glBindTexture and gl.glBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA). gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f) is also called.
The following code is where I believe my error could exist. I hope someone can help me out here. Thanks!
This is in my GLSurfaceView.Renderer implementation
@Override
public void onSurfaceCreated(final GL10 gl, final EGLConfig config) {
DebugLog.d("onSurfaceCreated");
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glDisable(GL10.GL_DITHER);
gl.glDisable(GL10.GL_LIGHTING);
gl.glDisable(GL10.GL_MULTISAMPLE);
gl.glEnable(GL10.GL_BLEND);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glFrontFace(GL10.GL_CW);
gl.glCullFace(GL10.GL_BACK);
}
This is in my BitmapTexture class.
@Override
public void loadBitmapToHardware(final GL10 gl) throws IOException {
final Bitmap bitmap = loadBitmap();
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bitmap, GL10.GL_UNSIGNED_BYTE, 0);
bitmap.recycle();
}
@Override
public Bitmap loadBitmap() {
InputStream is = null;
try {
is = mContext.getAssets().open(mBitmapPath);
return BitmapFactory.decodeStream(is);
} catch (final IOException e) {
DebugLog.e("Failed to load Bitmap in " + this.getClass().getSimpleName() + " from path: " + mBitmapPath, e);
return null;
} finally {
try {
is.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
@Override
public void loadTexture() throws IOException {
gl.glGenTextures(1, TEXTURE_CONTAINER, 0);
mTextureId = TEXTURE_CONTAINER[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
loadBitmapToHardware();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说这看起来像是曝光过度的问题。模拟器和手机的屏幕设置可能会以不同的方式处理此问题,特别是因为您的代码中禁用了抖动。
您应该尝试:
1:删除 glColor4f() 调用,看看是否有效(根据我对 GL10 的了解,它在您的情况下将颜色设置为白色,这可能会导致混合问题)。
或
2:关闭混合并打开深度测试,看看你的混合是否给出了这个结果。
It looks like an overexposure issue to me. It is likely that the screen settings for the emulator and your phone handles this problem differently, especially since dither is disabled in your code.
You should try to:
1: Remove the glColor4f() call, and see if that works (from what I know of GL10, it sets the color to white in your case, which might cause problems with blending).
or
2: Turn of blending and turn on depth-test to see if your blending give this result.