Thursday, July 26, 2012

GLES20Fix

This post is about how I added the OpenGL ES 2.0 methods glDrawElements and glVertexAttribPointer with VBOs on older android devices which (by a mistake?) did not get these methods. I am basically doing what the guys on this project have done and credit goes to them for their nice and clean solution. The fix will result in two static methods. I've also added a small part in the end about building the native code.

As the functions already exists in native code the only thing that is needed is to bind these functions to Java methods. To do this we create two native methods (one for glDrawElements and one for glVertexAttribPointer) and call the pre-existing OpenGL functions in native code. The source of the native code is named GLES20Fix.c and put inside the jni folder in the root of your project (create the folder if it doesn't exist). It should look like this:
#include <jni.h>
#include <GLES2/gl2.h>

void my_packagepath_GLES20Fix_glDrawElements(JNIEnv *env, 
    jclass c, jint mode, jint count, jint type, jint offset) {
        glDrawElements(mode, count, type, (void *) offset);
}

void my_packagepath_GLES20Fix_glVertexAttribPointer(
    JNIEnv *env, jclass clazz, jint index, jint size, 
    jint type, jboolean normalized, jint stride, 
    jint offset) {
        glVertexAttribPointer(index, size, type, normalized, 
                              stride, (void *) offset);
}
where my_package is changed to the package where the class containing the Java methods is - in this case the class is called GLES20Fix.

The Java class must declare the two native methods and should also load the jni library during initialization (unless you want to load it elsewhere).
package my.packagepath;

public class GLES20Fix {
 
    native public static void glVertexAttribPointer(int index,
        int size, int type, boolean normalized, int stride, 
        int offset);

    native public static void glDrawElements(int mode, 
        int count, int type, int offset);
 
    private GLES20Fix() {}
    
    static {
        System.loadLibrary("GLES20Fix");
    }
}

NDK Builder
Native code needs to be compiled and put inside the obj folder in the root of the project. This can be done by using a NDK builder for Eclipse which can be created by following this walkthrough. The builder needs a makefile for the GLES20Fix.c which must be named Android.mk and put inside the jni folder. The following is an example of a makefile for GLES20Fix.c
LOCAL_PATH := $(call my-dir)
 
include $(CLEAR_VARS)
 
LOCAL_MODULE := GLES20Fix
LOCAL_SRC_FILES := GLES20Fix.c
LOCAL_LDLIBS := -llog -lGLESv2 -ldl
 
include $(BUILD_SHARED_LIBRARY)

Sunday, July 22, 2012

Re-emerging with links

To get some consistent blogging going on I've decided that I will try to summarize links that I've found beeing useful for my project. In case I've forgotten to blog recently I will be using this type of post to get it going again - I will also try to include a shorter version in regular posts. The summary consists of a bunch of links which are divided into categories that addresses some part that I've been studying or found interesting.

Google I/O 
Google I/O is a great event for android developers as they provide a lot of information about how their OS works and how you can use it. I've found their youtube channel to be one of the best sources for android development and I want to emphasize three vids here.

Google I/O 2009: Writing Real-Time Games for Android - Chris Pruett talks about game development on android devices listing common pitfalls by using his game Replica Island as an example.

Google I/O 2010: Writing real-time games for Android redux - A follow-up on the Google I/O 2009. Chris Pruett goes more in-depth with his game.

Google I/O 2011: Memory management for Android Apps - A talk about efficient memory management on android devices.

The Game Loop and the Timestepping
Making sure that my game runs smooth even on older and slower devices is an important task and required some additional thought on my game loop. Even if the info from the following links is not directly applicable to my android game because I'm doing the threaded solution as suggested by Chris Pruett in the Google I/O talk, the fundamentals is still explained really well. This and this Q&A have also been useful.

Fix Your Timestep! - This is truly the main article on timestepping, even if you're not focusing on this right now you should still read it. It is written by Glenn Fiedler in his series of articles on game physics.

deWiTTERS Game Loop - Another great article on timestepping with a more implementation-specific approach for game loops.

Timestepping - A great follow-up to the Fix your Timestep! article. Written by Peter Sundqvist.

Lighthouse3D
Last, I want to give credits to Lighthouse3D with its many tutorials on game development and rendering using OpenGL. They are all very useful and interesting to read.