本文介绍了Android OpenGL ES 和 2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是我的要求.我已经不知道 OpenGL,我也不愿意学习它,但是我想直接学习 OpenGL ES,因为我的目标是开发 android.我想学习 OpenGL ES 以开发我的 2D 游戏.我选择它是为了表演(因为在 RT 游戏中,基本的 SurfaceView 绘图效率不高).我的问题是:从哪里开始?我花了一个多月的时间浏览 Google 并阅读/尝试我在任何地方找到的一些教程/示例,但说实话,它没有多大帮助,这是有两个原因:

Well, here's my request. I don't know OpenGL already, and I'm not willing to learn it, I want to learn OpenGL ES directly since I'm targeting my development to android, however. I want to learn OpenGL ES in order to develop my 2D games. I chose it for performances purpose (since basic SurfaceView drawing isn't that efficient when it comes to RT games).My question is: where to start?I've spent over a month browsing Google and reading/trying some tutorials/examples I've found anywhere but to be honest, it didn't help much and this is for two reasons:

  1. 几乎所有我遇到的文章/教程都是 3D 相关的(我只想学习如何绘制我的 2D Sprites)
  2. 没有基础,因为所有文章都针对特定的事情,例如:如何绘制三角形(带顶点)"、如何创建网格"...等.
  1. Almost all the articles/tutorials I've came across are 3D related (I only want to learn how to do my 2D Sprites drawing)
  2. There's no base to start from since all the articles targets a specific things like: "How to draw a triangle (with vertices)", "How to create a Mesh"... etc.

我也尝试阅读一些源代码(例如:replica island)但代码太复杂,包含很多不必要的东西;结果:我迷失在 100 个带有奇怪类名和内容的 .java 文件中.

I've tried to read some source code too (ex.: replica island) but the codes are too complicated and contains a lot of things that aren't necessary; result: I get lost among 100 .java files with weird class names and stuff.

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

I guess there's no course like the one I'm looking for, but I'll be very glad if somebody could give me some guidelines and some links maybe to learn what I'm up to (only OpenGL ES 2D Sprites rendering! nothing 3D).

推荐答案

我也遇到过类似的情况.
我开始使用 openGL 的方式是从查看非常基本的 GLSurfaceView 示例/演示开始.

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.

查看副本岛源代码文件:GameRenderer.java,了解如何使用适当的 GL 标志设置画布以进行 2D(精灵)渲染.您真的应该看看副本岛的同一作者的 SpriteMethodTest:http://code.google.com/p/apps-for-android/source/browse/trunk/SpriteMethodTest

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

在我发布自己的代码的地方看到这个问题:Using OpenGL to替换画布 - Android

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

设置好画布后,首先调用如下代码:gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

After you have your canvas set up, you start by calling something like:gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

之后,您就可以渲染精灵了.首先,您需要将精灵加载到纹理中:http://qdevarena.blogspot.com/2009/02/how-to-load-texture-in-android-opengl.html

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

然而,这是真正帮助我加载精灵的教程:http://tkcodesharing.blogspot.com/2008/05/working-with-textures-in-androids.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

我就是这样做的,我有一个名为 Texture.java 的类:

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

然后在我的 onDrawFrame() 方法中,我只是这样做:

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

这应该可以帮助您在 openGL 画布上绘制 2D 精灵.我注意到这真的没有简单的教程.希望将来我会在我的开发博客中发布一个:http://developingthedream.blogspot.com/

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/

这篇关于Android OpenGL ES 和 2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:16
查看更多