Android OpenGL ES 和2D

这是我的请求。我不知道 OpenGL 已经,我不愿意学习它,我想学习 OpenGL ES 直接,因为我的目标是我的开发 Android,但是。我想学习 OpenGL ES 为了发展我的 2D游戏。我选择它是出于性能目的(因为基本的 SurfaceView 绘图在 RT 游戏中效率不高)。 我的问题是: 从哪里开始? 我花了一个多月的时间浏览谷歌,阅读/尝试了一些我在任何地方都能找到的教程/示例,但是老实说,它没有多大帮助,原因有二:

  1. 几乎 所有的文章/教程,我遇到的是3D 相关的(我只想学习如何做我的2D 精灵绘制)
  2. 因为所有的文章都针对一些特定的事情,比如: “如何绘制一个三角形(带顶点)”,“如何创建一个网格”等等。

我也尝试过阅读一些源代码(例如: 复制岛) ,但是这些代码太复杂了,而且包含了很多不必要的东西; 结果: 我在100个代码中迷路了。带有奇怪类名的 java 文件。

我想没有一个课程像我所寻找的,但我会非常高兴,如果有人可以给我一些指导方针和一些 链接也许可以了解我正在做什么(只有 OpenGL ES 2D Sprites 渲染!没有3D 效果)。

48508 次浏览

2D programming is just 3D programming that's constrained to a plane. You'll have no choice but to learn 3D, but when you're using it just set z = 0.

There is an offical book on OpenGL ES. That might give you the intro that you're after: http://www.amazon.com/OpenGL-ES-2-0-Programming-Guide/dp/0321502795/

I would definately checkout Android - Chris Pruett Google IO lecture Writing real-time games for Android redux

grab the PDF also

it's really helpful on many levels, Chris has really great experience with creating games for mobile devices

but if you are really focused on 2D then start with Canvas http://developer.android.com/guide/topics/graphics/index.html#drawing-with-canvas

Another option depends on skill level is Flash+AdobeAIR to Android, I myself like and luv programming level and as you further start developing you will find out why.

OpenGL : Check for - Nehe Productions

a couple of apps you may want to put on your phone that is worth it and they are free is: OpenGL Demo, min3d Framework, RedBook Sample

I was in a similar situation.
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

See this question where I posted my own code: Using OpenGL to replace Canvas - Android

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

However, this is the tutorial that really helped me out with loading sprites: http://tkcodesharing.blogspot.com/2008/05/working-with-textures-in-androids.html

This is how I do it, I have a class named 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;
}
}

Then in my onDrawFrame() method I simply do:

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);

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/

You can see the project: https://github.com/ChillingVan/android-openGL-canvas/blob/master/README-en.md This implements canvas with OpenGL. It is pure Java. It implements parts of what normal canvas can do.

There are a lot of online tutorials that you can follow, but for a beginner nothing can replace this one: A real Open GL ES 2.0 2D tutorial