Simple JNI Example with Eclipse CDT

1. Create a new Java Project and create a new class called HelloJNI.java and put this code inside:

public class HelloJNI {

    static {
        System.load(“C:/libHelloJNIC++.dll”);
    }
    
    public native void displayMessage();
    
    public static void main(String[] args) {
        HelloJNI app = new HelloJNI();
        app.displayMessage();
    }
}

2. Open a console and enter in your HelloJNI Project source directory and type:

javac HelloJNI.java
javah HelloJNI

This will return a new file called HelloJNI.h

3. Create a new C++ Shared Library Project in Eclipse and put the HelloJNI.h file inside of it.
Now create a new C++ source file called HelloJNI.cpp and put this code inside:

#include <stdio.h>
#include “HelloJNI.h”

JNIEXPORT void JNICALL Java_HelloJNI_displayMessage
  (JNIEnv *env, jobject obj)
{
    printf(“Hello JNI!\n”);
}

4. Now in the Project Properties go to MinGW C++ Linker –> Miscellaneous –> Linker flags and paste -Wl,–kill-at

5. Finally go to Run configurations –> Arguments –> VM arguments and paste -Djava.library.path= and then in Working directory select other and put ${workspace_loc:HelloJNIC++/Debug}

Advertisements

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!

A Simple Test of Character Animation done in OpenGL using Assimp Library

Asset loading is a pain in the arse let’s face it! I’ve made a Collada parser in Java and it’s not a fun thing to do, believe me.
So I’ve decided to test the OpenSource Assimp Library to make a simple animated-character loading test.
It took me a while because there aren’t many examples or tutorials out there but finally I’ve come up with a working example.
It still uses OpenGL fixed-function pipeline and Vertex Array to keep it simple in the rendering department.
Here is a short video of the example in action.

Collada Loader Part 1

Recently I’ve decided to use Collada as the main file format to import animated models in my simple 3D engine.
Why I decided to go with Collada? Two words: skeletal animations and tools!!!
Almost any 3D software has a Collada exporter!
The biggest problem when you have to parse Collada files, it’s how Collada organizes indices. Instead of returning a single index array, it gives you 3 different index arrays: one for vertices, one for normals and one for texture coordinates. And it’s a pain because OpenGL only accepts a single index array for all of the three!
Some people have overcome this issue by using an utility called Collada Refinery which has a “deindexer”: it takes a Collada files and it returns a new one with only one index array for vertex/normals/texture coordinates.
But because I’m a lucky guy, this utility doesn’t work on my PC 😀 So I’ve had to figure out how to reorganize indices.
Finally, last night I’ve found the solution and I was the happiest guy in the world 😀
Here’s a quick video of the loader in action, thanks for watching!

Blender + Freestyle = The perfect tool!

Tonight I’ve tried Blender build with Freestyle included (you can find it here) and I was amazed at the boost it brought to my toon shading technique.
Here’s an image of what I’ve got after messing up with Freestyle Parameter Editor:

The best thing about the Freestyle module, is that you can mark your “custom” edge to add to the final render so even the smallest detail can be achived! I love it!
Try it, you won’t be disappointed!