【发布时间】:2014-05-11 08:19:44
【问题描述】:
我正在使用 OpenGL 编写一个 2D 游戏,使用存储在我的资源中的 png 图像(64x64 像素,具有透明度)。
我的代码如下所示:
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
import javax.microedition.khronos.opengles.GL11Ext;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
public class TestGLRenderer implements Renderer {
private int mTexGLNames[];
private Context mContext;
public TestGLRenderer(Context ctx) {
mContext = ctx;
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// setup the gl renderer
gl.glClearColor(0.2f, 0.4f, 0.6f, 1.0f);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
// reserve GL texture names
mTexGLNames = new int[1];
gl.glGenTextures(1, mTexGLNames, 0);
// load image from resources
Bitmap b;
b = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.image);
// load image in opengl
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexGLNames[0]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, b, 0);
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexGLNames[0]);
int crop[] = new int[] { 0, 64, 64, -64 };
((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D,
GL11Ext.GL_TEXTURE_CROP_RECT_OES, crop, 0);
((GL11Ext) gl).glDrawTexfOES(160, 240, 0, 64, 64);
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
}
}
结果在模拟器(运行 Android 2.2)中按预期工作,但图像在我的手机(LG-P500,Android 2.2)上显示为黑色方块。附件是模拟器和我的手机的屏幕截图。
是我的代码有问题,还是我的手机有问题(我的手机可以运行其他3D游戏没有问题)?
【问题讨论】: