97

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. 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.

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.

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

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
CoolStraw
  • 5,282
  • 8
  • 42
  • 64
  • 34
    `and I'm not willing to learn it` That's not a very good way to start a question here – Falmarri Aug 24 '10 at 03:11
  • 64
    Please read my whole question, you took one sensitive part of it and left the most important completion:"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" What's the point of learning OpenGL if I'm going to work only with OpenGL ES on embedded devices ? Cheers – CoolStraw Aug 24 '10 at 04:00
  • 5
    Given that OpenGL ES is mostly a subset of OpenGL, I don't see the point of saying you're not going to learn OpenGL, since by definition you're going to have to. – dash-tom-bang Aug 25 '10 at 01:12
  • 2
    BTW- Replica Island is pretty much the place to look. You might start with that and start by making changes to it. Strip things out that you don't want or need, and then 100 classes becomes 99 then 98 then 97... – dash-tom-bang Aug 25 '10 at 01:14

5 Answers5

85

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/

Community
  • 1
  • 1
Miguel Morales
  • 1,707
  • 10
  • 10
  • 7
    Dude thank you very much ! I've never gotten directions like these, that's exactly what I was looking for ! And I'll keep an eye on your blog, hopefully you'll provide us with a great tutorials in the future :), Again thank you very much – CoolStraw Sep 10 '10 at 05:08
  • 1
    In the same situation as OP and as thankful as him for you answer. Good luck with your Game/Blog. thx again – unR Oct 09 '11 at 13:52
  • 1
    Thank you @Miguel Morales I was tired of look for 2D drawing with GL. As CoolStraw said there are too much tutorials with lots of unnecessary things in it. That answer was clear like a crystal. – Evren Ozturk Mar 08 '12 at 13:51
  • @Miguel Morales After lots of try I can't draw anything it my textures shows up black. I asked a question http://stackoverflow.com/questions/9624260/gl10-2d-texture-shows-up-fully-black/9624770#9624770 can you take a look? – Evren Ozturk Mar 08 '12 at 21:24
12

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/

No one in particular
  • 2,682
  • 2
  • 17
  • 20
  • 8
    To be pedantic, you still use Z so that you can let the hardware do sorting for you. The big change is that (likely) the 2D application will use an orthographic view instead of a perspective one. – dash-tom-bang Aug 25 '10 at 01:14
  • Thank you very much for your answer, your statement shows me that I was wrong about the Opengl and Opengl ES definition. – CoolStraw Aug 25 '10 at 17:19
3

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

1

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.

ChillingVan
  • 131
  • 6
0

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

ucMedia
  • 4,105
  • 4
  • 38
  • 46