Android OpenGL ES 2.0 Dynamic Lighting (and OBJ Parser/Loader

I’ve started porting my little 3D real-time engine to Android.
I like to keep the code as portable as possible so I tend to reduce at minum the need of libraries.
I only had to modify how I upload vertex data to the GPU because OpenGL ES 2.0 doesn’t support Vertex Array Object (VAO).
And of course I had to rewrite the shaders in GLSL 1.20 (OpenGL ES 2.0 compatible).
Here’s a simple dynamic lighting test using diffuse lighting:

Build Assimp for Android NDK

1. Download Assimp-2.0 SDK complete source and extract it wherever you want (mine is ~/Desktop/assimp–2.0.863-sdk)
2. Enter into the extracted directory and type: mkdir BUILD and then cd BUILD
3. Run this command: cmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN -DENABLE_BOOST_WORKAROUND=ON -DCMAKE_INSTALL_PREFIX:PATH=/assimp-2.0 ..
4. Now run make
5. It will return a lot of errors, so here are the needed corrections:

  • “aiDefines.h” starting from line 237:
    #    if defined(__x86_32__) || defined(__i386__)
    #        define ASSIMP_BUILD_X86_32BIT_ARCHITECTURE
    #    elif defined(__x86_64__)
    #        define ASSIMP_BUILD_X86_64BIT_ARCHITECTURE
    #    elif defined(__ppc__)
    #        define ASSIMP_BUILD_PPC_32BIT_ARCHITECTURE
    #   elif defined(__arm__)
    #       define ASSIMP_ANDROID_ARM_ARCHITECTURE
    #    else
    #        error unknown architecture
    #    endif
    #else
    #    error unknown compiler
    #endif
  • “Importer.cpp” –> starting from line 901
    #if defined(ASSIMP_BUILD_X86_32BIT_ARCHITECTURE)
            << ” x86″
    #elif defined(ASSIMP_BUILD_X86_64BIT_ARCHITECTURE)
            << ” amd64″
    #elif defined(ASSIMP_BUILD_IA_64BIT_ARCHITECTURE)
            << ” itanium”
    #elif defined(ASSIMP_BUILD_PPC_32BIT_ARCHITECTURE)
            << ” ppc32″
    #elif defined(ASSIMP_ANDROID_ARM_ARCHITECTURE)
            << ” arm”
    #else
    #    error unknown architecture
    #endif 

    Once we have put this corrections run make again and it should work!
    Finally run make install!

Setting up Android standalone toolchain for CMake

It took me a while to figure out how to make the Android NDK working with CMake generated makefiles.
Recently I’m trying to port my simple test of character animation with Assimp Library on Android NDK so I needed to build an Assimp version for Android ARM CPU.
I could not find any infos on this topic on the net and officially, it seems that Assimp Library only supports iOS devices.
So I’ve experimented quite a bit and finally I come up with a solution!
Before we start, I have to say that I’ve tested my solution on OSX and probably it will work on Linux too but I seriously doubt that it will work on Windows + cygwin. You have been warned! 😀
Here we go!

1. Set up an Android Standalone Toolchain

Extract the downloaded Android NDK archive wherever you want (mine is ~/android-ndk-r8b)
Enter into this directory:

cd ~/android-ndk-r8b/build/tools

Fromt there run this command:

sh make-standalone-toolchain.sh –platform=android-8 –ndk-dir=/Users/prometheus/android-ndk-r8b –install-dir=/Users/prometheus/android-toolchain –toolchain=arm-linux-androideabi-4.4.3

Now you should have a new directory called android-toolchain.

2. Download the Anroid CMake configuration file
Inside the directory where you have android-ndk-r8b and android-toolchain directories, run this command:

hg clone https://android-cmake.googlecode.com/hg/ android-cmake

Now you should have these three directories: android-ndk-r8b, android-toolchain and android-cmake

3. Setup the needed Environment Variables
Edit your .profile file (if you don’t have it, just create it) as follows:

export PATH=$PATH:$HOME/android-sdk-macosx/tools
export PATH=$PATH:$HOME/android-sdk-macosx/platform-tools
export PATH=$PATH:$HOME/android-toolchain/bin

export ANDROID_NDK_TOOLCHAIN_ROOT=$HOME/android-toolchain
export ANDTOOLCHAIN=$HOME/android-cmake/toolchain/android.toolchain.cmake

4. Test CMake
Go inside this directory:

cd ~/android-cmake/samples/hello-cmake

There run this commands:

mkdir BUILD
cd BUILD
cmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN ..
make

It should build libhello-cmake.so inside ~/android-cmake/samples/hello-cmake-test/libs/armeabi-v7a

And that’s it!

P.S.
As you’ve probably noticed, we have specified which GCC version we need to use by adding the argument –toolchain=arm-linux-androideabi-4.4.3
At the time of this writing, this is mandatory because if you try to use CMake with GCC-4.6 it won’t work!