Android OpenGL ES 和 2D

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3553244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:44:40  来源:igfitidea点击:

Android OpenGL ES and 2D

androidopengl-es2d

提问by CoolStraw

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 2Dgames. 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:

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

  1. Almost allthe 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.
  1. 我遇到的几乎所有文章/教程都与 3D 相关(我只想学习如何绘制 2D Sprites)
  2. 没有基础,因为所有文章都针对特定的事情,例如:“如何绘制三角形(带顶点)”,“如何创建网格”......等。

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.

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

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 linksmaybe to learn what I'm up to (only OpenGL ES 2D Sprites rendering! nothing 3D).

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

采纳答案by Miguel Morales

I was in a similar situation.
The way I started with openGL with start by looking at the very basic GLSurfaceView samples/demos.

我处于类似的情况。
我开始使用 openGL 的方式是从查看非常基本的 GLSurfaceView 示例/演示开始。

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

获取副本岛源代码文件:GameRenderer.java,了解如何使用适当的 GL 标志设置画布以进行 2D(精灵)渲染。您真的应该看看副本岛的同一作者的 SpriteMethodTest: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

请参阅我发布自己的代码的问题: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);

设置好画布后,首先调用如下代码: 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

之后,您就可以渲染精灵了。首先,您需要将精灵加载到纹理中: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

然而,这是真正帮助我加载精灵的教程: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:

我就是这样做的,我有一个名为 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:

然后在我的 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);

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/

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

回答by No one in particular

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.

2D 编程只是受限于平面的 3D 编程。你别无选择,只能学习 3D,但是当你使用它时,只需设置 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/

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

回答by Atom Arcade

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

我肯定会检查 Android - Chris Pruett Google IO 讲座为 Android redux 编写实时游戏

grab the PDF also

也抓取PDF

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

它在很多层面上都非常有帮助,Chris 在为移动设备创建游戏方面拥有非常丰富的经验

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

但如果你真的专注于 2D,那么从 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.

另一个取决于技能水平的选择是 Flash+AdobeAIR 到 Android,我自己喜欢和 luv 编程水平,当您进一步开始开发时,您会发现原因。

OpenGL : Check for - Nehe Productions

OpenGL:检查 - 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

您可能想放在手机上的几个值得并且免费的应用程序是:OpenGL Demo、min3d Framework、RedBook Sample

回答by ChillingVan

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

可以看到项目: https://github.com/ChillingVan/android-openGL-canvas/blob/master/README-en.md这个是用OpenGL实现canvas的。它是纯Java。它实现了普通画布可以做的部分。

回答by ucMedia

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

有很多在线教程可以学习,但对于初学者来说,没有什么可以替代这个教程A real Open GL ES 2.0 2D tutorial

回答by MikeyE

I see a lot of good info has already been provided. I wanted to share a site that helped get up to speed on OpenGLE quick! It only took a few months and had a custom coordinate system based on the Cartesian coordinate system. I was able to render 3D object no camera using Augmented Reality techniques.

我看到已经提供了很多好的信息。我想分享一个可以帮助您快速了解 OpenGLE 的网站!只花了几个月的时间,就有了一个基于笛卡尔坐标系的自定义坐标系。我能够使用增强现实技术在没有相机的情况下渲染 3D 对象。

I started with only programming experience, with no OpenGL experience. I used Ray Wenderlich's tutorials site. The information provided there is top notch and easy to comprehend. He cuts through most of the superfluous information and provides what you need to know to be productive quickly. I highly recommend this tutorial as the starting point: http://www.raywenderlich.com/5223/beginning-opengl-es-2-0-with-glkit-part-1

我开始时只有编程经验,没有 OpenGL 经验。我使用了 Ray Wenderlich 的教程网站。那里提供的信息一流且易于理解。他删除了大部分多余的信息,并提供了您快速提高工作效率所需的知识。我强烈推荐本教程作为起点:http: //www.raywenderlich.com/5223/beginning-opengl-es-2-0-with-glkit-part-1

The other resource I'd recommend is a book by Erik M Buck, titled Learning OpenGL ES for iOS.

我推荐的另一个资源是 Erik M Buck 的一本书,名为 Learning OpenGL ES for iOS。

Learning OpenGL ES for iOS book cover art

为 iOS 书籍封面艺术学习 OpenGL ES

Some criticized it saying it was too simplistic. But that's exactly what I was looking for. It helped me understand all of the basics and gave me an idea on where i should go next to learn more advanced stuff. But not surprisingly, I was able to build my augmented reality app using the simple techniques i'd learned from Ray's site and Erik's book. Thanks to them both for sharing!!!

有人批评它说它过于简单化。但这正是我正在寻找的。它帮助我理解了所有基础知识,并让我知道下一步应该去哪里学习更高级的东西。但毫不奇怪,我能够使用从 Ray 的网站和 Erik 的书中学到的简单技术来构建我的增强现实应用程序。感谢两位的分享!!!