android opengl es 2d,Android OpenGL es 2D绘图代码(英文)

Android OpenGL es 2D绘图代码(英文)

The way I started with openGL with start by looking at the very

basic GLSurfaceView samples/demos.

Start, by setting up your app activity, and set up the basic

canvas.

Take a loot at the replica island source code file:

GameRenderer.java for how to setup your canvas with the proper GL

flags for 2D (sprite) rendering. You should really take a look at

SpriteMethodTest by the same author of replica island: http://code.google.com/p/apps-for-android/source/browse/trunk/SpriteMethodTest

After you have your canvas set up, you start by calling

something like: gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

After that you're ready to render a sprite. First, you'll need

to load the sprite into a texture: http://qdevarena.blogspot.com/2009/02/how-to-load-texture-in-android-opengl.html

This is how I do it, I have a class named Texture.java:

publicclassTexture{publicfloatx=0;publicfloaty=0;publicfloatz=0;publicfloatwidth=0;publicfloatheight=0;privateGL10 gl;publicint[]texture;//holds the texture in integer formprivateinttexture_name;privateint[]mCropWorkspace;privatefinalBitmapFactory.OptionssBitmapOptions;publicTexture(GL10 gl_obj){gl=gl_obj;texture=newint[1];mCropWorkspace=newint[4];sBitmapOptions=newBitmapFactory.Options();sBitmapOptions.inPreferredConfig=Bitmap.Config.RGB_565;//Log.d(TAG, "Initializing Texture Object");}publicintget_texture_name(){returntexture_name;}publicbooleanLoad(Bitmapbitmap)//rename this to glLoad and don't have it as an initializer parameter{if(gl==null){Log.e(TAG,"Failed to load resource. Context/GL is NULL");returnfalse;}interror;inttextureName=-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);returntrue;}

}

Then in my onDrawFrame() method I simply do:

Texturetexture=...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);

That should get you going with drawing 2D sprites on an openGL

canvas. I've noticed that there is really no straightforward

tutorial on this. Hopefully in the future I will post one in my dev

blog: http://developingthedream.blogspot.com/

Good luck.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值