• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
  • entries
    5
  • comments
    9
  • views
    23389

Dev Journal: 2D sprite animation using OpenGL ES 2.0 for Android

Sign in to follow this  
Followers 0
tom_mai78101

15657 views

Chapter 7 - Making heavy use of Android


You probably might see that the way I write this journal is a bit different than my other entries. The fact that I'm using Chrome for Android to write this, means that I'm lacking the WYSIWYG editor most people are using. So, there won't be anything to show, even images and screenshots.

I'm currently using AIDE, also called Android IDE. It's an integrated Android development environment suited for Android programming on the go. Been using it since the Christmas holidays (when it was 50% off), so I have a pretty solid knowledge of using it, and have become a power user of Android. What you probably won't believe is that I'm using HTC Desire S to program Android apps, especially at times when I have to lie down on the bed and program away at night.

I'm going to hasten this up and move on to the most important part of this entry. There's not a lot of stuff that I wanted to share for the moment.

----------------------



Chapter 8 - 2D sprite animation using OpenGL ES 2.0 on Android


In this entry, I will be showing you how to do 2D sprite animation using OpenGL ES 2.0 for Android. I use a bit of GLSL in order to manipulate the textures around and display the effects of sprites animating on the screen.

Note that in this entry, I jump around often. It's done so I can explain the steps when it is needed (a "demand-first" approach). If you get confused, please leave a comment explaining where you can't understand, and I'll try to modify the post for you at a later time.

The first thing you want to do is to create a spritesheet ("Sprite sheet" or "spritesheet"? Spelling is correct?). How you lay out your sprites is up to you, but you need to be consistent throughout the creation of many sprites you'll probably do in the later part of your project.

For me, I will be using a layout of 4x4 spritesheet, where a sprite character has 4 directions (NORTH, SOUTH, EAST, and WEST), with 4 frames of animation per direction. Each sprite character is 16x16 large, so the entire spritesheet is 64x64.

player.png

For demo purposes, I will use a simple sprite character named "Joe", from the Pokemon series (2nd generation, to be exact. Joe actually exists!) You can use any sprite characters you can use, regardless of size and shape.

This spritesheet should be placed in your assets folder of your Android project. You can place it in your res folder, but for the most part, I'm going to stick to the convention. You can choose to separate your sprite characters via folders within your assets folder, or in your res folder (You have to place it in /res/drawable-nodpi if you really are going for the res method.)

The next thing you want to do is to load the bitmap.
public class Art { public static Bitmap joe; public static void loadAllBitmaps(Activity activity){ final AssetManager mgr = activity.getAssets(); joe = load(mgr, "player/player.png"); } private static Bitmap load(final AssetManager manager, String filename){ Bitmap result = null; try { result = BitmapFactory.decodeStream(manager.open(filename)); } catch (IOException e) { Log.e("Art", "Error loading art resource.", e); } return result; }}
To initiate the loading, call the loadAllBitmaps() method in the constructor method of a GLSurfaceView subclass. (My preferred suggestion.) You load most of your assets this way by passing the main activity to your subclass. It not only controls the OpenGL context creation, but also allows you to control how your assets are to be loaded inside a method.
//In the main activity's onCreate(Bundle b) method.ActivityManager mgr = (ActivityManager) this.getSystemService(Activity.ACTIVITY_SERVICE);ConfigurationInfo info = mgr.getDeviceConfigurationInfo();if (info.reqGlEsVersion >= 0x20000){ renderView = new RenderView(this);//In the RenderView class. It's a GLSurfaceView subclass.public RenderView(MainActivity activity) { super(activity); this.activity = activity; Art.loadAllBitmaps(activity); //<----- This is where you load your sprites. this.setOnTouchListener(input); this.setEGLContextClientVersion(2); this.setRenderer(this); this.requestFocus(); //... //... Of course there are more codes than this.}
Once the sprite has been loaded, the next step is to load it in as a texture from a Bitmap.
public static int loadTexture(Bitmap bitmap, int copy) { //This is to check for possible reinitialization of this object. //We want to prevent this from happening, otherwise there will be //unwanted black textures in your app. if (bitmap.isRecycled()) { Log.e("Entity", "Bitmap is already recycled. Detected too soon."); bitmap = Art.copy(Art.joe); } //Texture loading. //This is the part where most of the time, new OpenGL programmers would want //to search for it on Google. I'm putting it here for future references. //** Of course this is for Android. ** final int[] textureID = new int[1]; GLES20.glGenTextures(1, textureID, 0); BitmapFactory.Options options = new BitmapFactory.Options(); options.inScaled = false; GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID[0]); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); //This is to make sure the bitmap is recycled, and then pass the texture ID handle //to somewhere else where it is safe from being Garbage Collected. //For the time being, I placed it in to let readers know of this. //The exact code itself is useless at the moment. bitmap.recycle(); copy = textureID[0]; return textureID[0]; }
After converting the bitmap to a texture object the OpenGL ES can understand, we need to setup the shader codes as well as shader initialization codes.
public void loadShaderLocations() { this.uMatrixLocation = GLES20.glGetUniformLocation(Shader.BaseProgram, Shader.U_MATRIX); this.uTextureUnitLocation = GLES20.glGetUniformLocation(Shader.BaseProgram, Shader.U_TEXTURE_UNIT); this.aPositionLocation = GLES20.glGetAttribLocation(Shader.BaseProgram, Shader.A_POSITION); this.aTexturePositionLocation = GLES20.glGetAttribLocation(Shader.BaseProgram, Shader.A_TEXTURE_POSITION); this.uTextureMatrixLocation = GLES20.glGetUniformLocation(Shader.BaseProgram, Shader.U_TEXTURE_MATRIX);} public void setVertexAttributePointers() { this.vertexFloatBuffer.position(0); GLES20.glVertexAttribPointer(aPositionLocation, 3, GLES20.GL_FLOAT, false, 3 * 4, vertexFloatBuffer); GLES20.glEnableVertexAttribArray(aPositionLocation); this.vertexFloatBuffer.position(0); this.textureCoordinatesFloatBuffer.position(0); GLES20.glVertexAttribPointer(aTexturePositionLocation, 2, GLES20.GL_FLOAT, false, 2 * 4, textureCoordinatesFloatBuffer); GLES20.glEnableVertexAttribArray(aTexturePositionLocation); this.textureCoordinatesFloatBuffer.position(0);} public void setUniforms(float[] modelMatrix, float[] textureMatrix) { GLES20.glUniformMatrix4fv(uMatrixLocation, 1, false, modelMatrix, 0); GLES20.glUniformMatrix4fv(uTextureMatrixLocation, 1, false, textureMatrix, 0); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID); GLES20.glUniform1i(uTextureUnitLocation, 0);} public void loadFloatBuffers() { this.vertexFloatBuffer = ByteBuffer.allocateDirect(4 * 3 * 6).order(ByteOrder.nativeOrder()).asFloatBuffer(); this.vertexFloatBuffer.put(new float[]{ 0f, 0f, 1f, 0f, 0.5f, 1f, 0.25f, 0f, 1f, 0.25f, 0f, 1f, 0f, 0.5f, 1f, 0.25f, 0.5f, 1f }); //Graphics Ratio: //Width:Height = 1:2 this.textureCoordinatesFloatBuffer = ByteBuffer.allocateDirect(4 * 2 * 6).order(ByteOrder.nativeOrder()).asFloatBuffer(); this.textureCoordinatesFloatBuffer.put(new float[]{ 0f, 0.25f, 0f, 0f, 0.25f, 0.25f, 0.25f, 0.25f, 0f, 0f, 0.25f, 0f });}
For shaders, we want to pass in a model matrix, and a texture matrix. The model matrix determines where the entity is to be drawn. The texture matrix determines what portion of the spritesheet texture should be drawn to the mesh that is defined in the vertex buffer object. We pass in these matrices as uniforms, since they won't be modified while we're doing vertex and texture calculations. (Not sure the terminology for calculating vertex and fragment shaders be called.)
//Vertex shader code:attribute vec4 a_position;attribute vec2 a_texture_position;varying vec2 v_texture_position;uniform mat4 u_matrix;uniform mat4 u_texture_matrix;void main(){ v_texture_position = (u_texture_matrix * vec4(a_texture_position, 0.0, 1.0)).xy; gl_Position = u_matrix * a_position;}//-------------------------------------//Fragment shader code:precision mediump float;varying vec2 v_texture_position;uniform sampler2D u_texture_unit;void main(){ gl_FragColor= texture2D(u_texture_unit, v_texture_position);}
To be honest, the texture matrix calculations can be placed in any shaders. I placed it in the vertex shader, as I believed the fragment shader is executed more than the vertex shader. We need to update the texture's UV coordinates for the fragment shader to draw. I obtain the translated U and V coordinates by matrix multiplication using the above code.

To determine texture UV coordinates of a spritesheet, we use the origin UV of the texture, which is located at the top left corner of the texture. Each side is worth 1.0, or 100% of its width and height. Half width is 50%, or 0.5. A quarter of height is worth 25%, or 0.25 of its original height.

To keep what our texture UVs are, I use 2 float variables (or 1 float array of size "2") to mark its position.
//In the sprite class object, I used a float array to keep its texture position.protected float[] texture_uv_coordinates;//I also used a float array to keep its vertex position.protected float[] xy_coordinates;
I pass the texture matrix and the model matrix through to the sprite class object, and then pass both of the matrices to the shader, as mentioned earlier.
public void setUniforms(float[] modelMatrix, float[] textureMatrix) { GLES20.glUniformMatrix4fv(uMatrixLocation, 1, false, modelMatrix, 0); GLES20.glUniformMatrix4fv(uTextureMatrixLocation, 1, false, textureMatrix, 0); //...}
Then I update both matrices.
public void tick(float[] modelMatrix, float[] textureMatrix) { //Input update code Matrix.setIdentityM(modelMatrix, 0); Matrix.translateM(modelMatrix, 0, this.xy_coordinates[0], this.xy_coordinates[1], 0f); if (RenderView.input.pressed){ this.xy_coordinates[1] -= 0.001f; } //Frame animation update code tickCounter--; if (tickCounter < 0) { texture_uv_coordinates[1] += 0.25f; if (texture_uv_coordinates[1] >= 1f) texture_uv_coordinates[1] = 0f; tickCounter = 60; } Matrix.setIdentityM(textureMatrix, 0); Matrix.translateM(textureMatrix, 0, 0f, texture_uv_coordinates[1], 0f);}
Ignoring some of the codes I didn't mention, there's a "tickCounter" that is used to slow down the rapidness of the sprite animation. Once the tickCounter reaches 0, it updates the texture UV coordinates by what we determined when we're creating our sprites. Then we reset the tickCounter, and repeat.

After updating the texture UV coordinates, we pass it to our shader to let it manipulate the texture drawing itself. This is the core of the sprite animation.

The input code mentioned above is for handling touch events. Once the user executes a touch event, in this case, a single tap on the screen, the sprite character moves down by 0.001 or 0.1% of the entire height of the viewport of the screen.

Once you cleaned up your project and used the above vertex and fragment shader codes, you can now start your own sprite animations, or you can go up a step and improve how animations work.

Again, if you have any questions, please leave them in the comments below, and I'll check back to try to help you out.

2
Sign in to follow this  
Followers 0


2 Comments


Cool. This is kinda like what I'm doing with 2D spline shapes in OpenGL (the ES subset). I've gotta play around with textures and shader effects at some point; should be interesting. Thanks for sharing.

0

Share this comment


Link to comment

Cool. This is kinda like what I'm doing with 2D spline shapes in OpenGL (the ES subset). I've gotta play around with textures and shader effects at some point; should be interesting. Thanks for sharing.

 

Wonderful. I hoped I explained it in an digestible format.

0

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now