Android OpenGL ES和2D

Coo*_*raw 95 android 2d opengl-es

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

  1. 我遇到的几乎所有文章/教程都是3D相关的(我只想学习如何进行2D Sprites绘图)
  2. 没有任何基础可以开始,因为所有文章都针对特定的事情,例如:"如何绘制三角形(带顶点)","如何创建网格"......等等.

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

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

Mig*_*les 85

我处于类似的情况.
通过查看非常基本的GLSurfaceView示例/演示,我开始使用openGL.

首先,设置您的应用活动,然后设置基本画布.

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

看到这个问题,我发布了自己的代码:使用OpenGL替换Canvas - Android

设置好画布后,首先调用类似:gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

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

但是,这个教程真正帮助我加载sprite: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;
 }
}
Run Code Online (Sandbox Code Playgroud)

然后在我的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);
Run Code Online (Sandbox Code Playgroud)

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

  • 老兄,非常感谢你!我从未得到过这样的指示,这正是我所寻找的!我会密切关注你的博客,希望你能在未来为我们提供一个很好的教程:),再次非常感谢你 (7认同)

No *_*lar 12

2D编程只是受限于平面的3D编程.你别无选择,只能学习3D,但是当你使用它时,只需设置z = 0.

有关OpenGL ES的官方书籍.这可能会给你介绍你想要的介绍:http: //www.amazon.com/OpenGL-ES-2-0-Programming-Guide/dp/0321502795/

  • 为了迂腐,您仍然使用Z,以便您可以让硬件为您排序.最大的变化是(可能)2D应用程序将使用正交视图而不是透视视图. (8认同)