【发布时间】:2014-05-10 16:50:45
【问题描述】:
在某些设备上渲染纹理时(仅确认 Galaxy s3 mini),我在纹理上出现了暗区闪烁,如该线程中所述:
Black Artifacts on Android in OpenGL ES 2
我不能评论这个帖子(没有足够的信用),但我想从解决这个问题的作者那里得到澄清:
您能否再解释一下您如何使用glTexImage2D() 和glTexSubImage2D() 来解决这个问题?
在代码中,我得到了这些行来加载位图:
(如您所见,我使用texImage2D 加载位图,关于gltexImage2D 的android 文档仅提供属性类型但没有说明)
...
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
final Bitmap bitmap = BitmapFactory.decodeResource(
context.getResources(), resourceId, options);
if (bitmap == null) {
if (LoggerConfig.ON) {
Log.w(TAG, "Resource ID " + resourceId + " could not be decoded.");
}
glDeleteTextures(1, textureObjectIds, 0);
return 0;
}
glBindTexture(GL_TEXTURE_2D, textureObjectIds[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);
glGenerateMipmap(GL_TEXTURE_2D);
...
编辑:
尝试根据顶部的链接实现解决方案但没有运气,同样的闪烁效果, 加载位图的新代码:
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bitmap.getWidth() * bitmap.getHeight() * 4);
byteBuffer.order(ByteOrder.BIG_ENDIAN);
IntBuffer ib = byteBuffer.asIntBuffer();
int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for(int i=0; i<pixels.length; i++){
ib.put(pixels[i] << 8 | pixels[i] >>> 24);
}
bitmap.recycle();
byteBuffer.position(0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap.getWidth(), bitmap.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, null);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap.getWidth(), bitmap.getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, byteBuffer);
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 );
奇怪行为的说明,请看图片中右侧的黑色区域:
(我需要 10 个声望才能发布图片?!?!?)
https://dl.dropboxusercontent.com/u/61092317/blackflickering.jpg
【问题讨论】:
-
我想如果您将 NULL 传递给
glTexImage2D (...)然后尝试从中生成 mipmap,您将得到的不仅仅是简单的黑线。整个基础图像将是不确定的,因此从基础图像生成 mipmap 也是没有意义的。
标签: android opengl-es bitmap texture-mapping