我也遇到过类似情况。
我从查看非常基本的 GLSurfaceView 示例/演示开始使用 openGL 的方式。
首先,设置您的应用活动,然后设置基本画布。
查看副本岛源代码文件:GameRenderer.java,了解如何使用适当的 GL 标志设置您的画布以进行 2D(精灵)渲染。
你真的应该看看副本岛的同一作者的SpriteMethodTest:http://code.google.com/p/apps-for-android/source/browse/trunk/SpriteMethodTest
在我发布自己的代码的地方看到这个问题:Using OpenGL to replace Canvas - Android
设置好画布后,您首先调用以下代码:
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
之后你就可以渲染一个精灵了。
首先,您需要将精灵加载到纹理中:http://qdevarena.blogspot.com/2009/02/how-to-load-texture-in-android-opengl.html
然而,这是真正帮助我加载精灵的教程:
http://tkcodesharing.blogspot.com/2008/05/working-with-textures-in-androids.html
我就是这样做的,我有一个名为 Texture.java 的类:
public class Texture
{
/*Begin public declarations*/
public float x = 0;
public float y = 0;
public float z = 0;
public float width = 0;
public float height = 0;
/*Begin Private Declarations*/
private GL10 gl;
public int[] texture; //holds the texture in integer form
private int texture_name;
private int[] mCropWorkspace;
private final BitmapFactory.Options sBitmapOptions;
/*Begin Methods*/
public Texture( GL10 gl_obj )
{
gl = gl_obj;
texture = new int[1];
mCropWorkspace = new int[4];
sBitmapOptions = new BitmapFactory.Options();
sBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
//Log.d(TAG, "Initializing Texture Object");
}
public int get_texture_name( )
{
return texture_name;
}
/*Loads the resource to memory*/
public boolean Load( Bitmap bitmap ) //rename this to glLoad and don't have it as an initializer parameter
{
/*many thanks to sprite method test if this works*/
if ( gl == null )
{
Log.e(TAG, "Failed to load resource. Context/GL is NULL");
return false;
}
int error;
int textureName = -1;
gl.glGenTextures(1, texture, 0);
textureName = texture[0];
//Log.d(TAG, "Generated texture: " + textureName);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureName);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
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_REPLACE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
mCropWorkspace[0] = 0;
mCropWorkspace[1] = bitmap.getHeight();
mCropWorkspace[2] = bitmap.getWidth();
mCropWorkspace[3] = -bitmap.getHeight();
((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D,
GL11Ext.GL_TEXTURE_CROP_RECT_OES, mCropWorkspace, 0);
error = gl.glGetError();
if (error != GL10.GL_NO_ERROR)
{
Log.e(TAG, "GL Texture Load Error: " + error);
}
//Log.d(TAG, "Loaded texture: " + textureName);
return true;
}
}
然后在我的 onDrawFrame() 方法中我只是这样做:
Texture texture = ...
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture.texture[0]);
((GL11Ext) gl).glDrawTexfOES((float)(draw_x + 0.5), (float)(draw_y + 0.5), 0, tile_width, tile_height);
这应该可以帮助您在 openGL 画布上绘制 2D 精灵。
我注意到这方面真的没有简单的教程。希望将来我会在我的开发博客中发布一个:http://developingthedream.blogspot.com/