From BlenderWiki

Jump to: navigation, search
Note: This is an archived version of the Blender Developer Wiki. The current and active wiki is available on wiki.blender.org.

In progress...

  • First you need to install Java JDK from app center
  • Then download Eclipse classic from sun.com (ubuntu's doesn't work)

Set up path

For convenience add following paths to PATH environmental variable

  •  ???/android-ndk-r7/
  •  ???/android-sdk-linux/tools/
  •  ???/android-sdk-linux/platform-tools/

Creating Almost Pure C Project for Blender

Because Android primarily uses java, java is unavoidable. But with Android 2.3 and higher we can minimized java code. The java jni binding will happen behind the scenes by using Android' Native Activity. It does all boring (and annoying) API binding for developer.


Create a folder "aghosty" in the home directory. Make it active in terminal. If you use a different folder name, make sure you also change name in AndroidManifest.xml!

cd ~/aghosty


Create an Android project there with command line.

android create project --target 10 --name Blender --path . --activity NativeActivity --package com.blender

This instruction for Android SDK 10. Make sure it is downloaded with eclipse sdk plugin, or use 'android list targets' to get the target ids.

Replace AndroidManifest.xml with

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.blender"
      android:versionCode="1"
      android:versionName="1.0">
 
      <uses-sdk android:minSdkVersion="10" />
 
      <application android:label="Blender"  android:hasCode="false">
 
        <activity android:name="android.app.NativeActivity"
                  android:label="Blender"
		  android:configChanges="orientation|keyboardHidden">
 
          <meta-data android:name="android.app.lib_name"
            android:value="aghosty" />		  
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


Now create 'jni' folder like ~/aghosty/jni Create in 'jni' folder 'Android.mk' file and insert there

LOCAL_PATH := $(call my-dir)
 
include $(CLEAR_VARS)
 
LOCAL_MODULE	:= aghosty
LOCAL_SRC_FILES	:= main.c
 
LOCAL_LDLIBS		:= -lEGL -lGLESv1_CM -llog -landroid
LOCAL_STATIC_LIBRARIES	:= android_native_app_glue
 
include $(BUILD_SHARED_LIBRARY)
 
$(call import-module,android/native_app_glue)


Create in 'jni' folder 'Application.mk' file and insert there

APP_PLATFORM := android-10

Now create actual 'main.c' inside jni and insert this.

      • Warning. The following code is partially taken from Android's samples. This to just get app running. The code will be later stripped down to bare essentials***
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
 
//BEGIN_INCLUDE(all)
#include <jni.h>
#include <errno.h>
 
#include <EGL/egl.h>
#include <GLES/gl.h>
 
#include <android/sensor.h>
#include <android/log.h>
#include <android_native_app_glue.h>
 
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__))
 
/**
 * Our saved state data.
 */
struct saved_state {
    float angle;
    int32_t x;
    int32_t y;
};
 
/**
 * Shared state for our app.
 */
struct engine {
    struct android_app* app;
 
    int animating;
    EGLDisplay display;
    EGLSurface surface;
    EGLContext context;
    int32_t width;
    int32_t height;
    struct saved_state state;
};
 
/**
 * Initialize an EGL context for the current display.
 */
static int engine_init_display(struct engine* engine) {
    // initialize OpenGL ES and EGL
 
    /*
     * Here specify the attributes of the desired configuration.
     * Below, we select an EGLConfig with at least 8 bits per color
     * component compatible with on-screen windows
     */
    const EGLint attribs[] = {
            EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
            EGL_BLUE_SIZE, 8,
            EGL_GREEN_SIZE, 8,
            EGL_RED_SIZE, 8,
            EGL_NONE
    };
    EGLint w, h, dummy, format;
    EGLint numConfigs;
    EGLConfig config;
    EGLSurface surface;
    EGLContext context;
 
    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
 
    eglInitialize(display, 0, 0);
 
    /* Here, the application chooses the configuration it desires. In this
     * sample, we have a very simplified selection process, where we pick
     * the first EGLConfig that matches our criteria */
    eglChooseConfig(display, attribs, &config, 1, &numConfigs);
 
    /* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
     * guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
     * As soon as we picked a EGLConfig, we can safely reconfigure the
     * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
    eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
 
    ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);
 
    surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);
    context = eglCreateContext(display, config, NULL, NULL);
 
    if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
        LOGW("Unable to eglMakeCurrent");
        return -1;
    }
 
    eglQuerySurface(display, surface, EGL_WIDTH, &w);
    eglQuerySurface(display, surface, EGL_HEIGHT, &h);
 
    engine->display = display;
    engine->context = context;
    engine->surface = surface;
    engine->width = w;
    engine->height = h;
    engine->state.angle = 0;
 
    // Initialize GL state.
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
    glEnable(GL_CULL_FACE);
    glShadeModel(GL_SMOOTH);
    glDisable(GL_DEPTH_TEST);
 
    return 0;
}
 
/**
 * Just the current frame in the display.
 */
static void engine_draw_frame(struct engine* engine) {
    if (engine->display == NULL) {
        // No display.
        return;
    }
 
    // Just fill the screen with a color.
    glClearColor(1.0f, 0.5f,0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
 
    eglSwapBuffers(engine->display, engine->surface);
}
 
/**
 * Tear down the EGL context currently associated with the display.
 */
static void engine_term_display(struct engine* engine) {
    if (engine->display != EGL_NO_DISPLAY) {
        eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
        if (engine->context != EGL_NO_CONTEXT) {
            eglDestroyContext(engine->display, engine->context);
        }
        if (engine->surface != EGL_NO_SURFACE) {
            eglDestroySurface(engine->display, engine->surface);
        }
        eglTerminate(engine->display);
    }
    engine->animating = 0;
    engine->display = EGL_NO_DISPLAY;
    engine->context = EGL_NO_CONTEXT;
    engine->surface = EGL_NO_SURFACE;
}
 
/**
 * Process the next input event.
 */
static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) {
    struct engine* engine = (struct engine*)app->userData;
 
    return 0;
}
 
/**
 * Process the next main command.
 */
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
    struct engine* engine = (struct engine*)app->userData;
    switch (cmd) {
        case APP_CMD_SAVE_STATE:
            // The system has asked us to save our current state.  Do so.
            engine->app->savedState = malloc(sizeof(struct saved_state));
            *((struct saved_state*)engine->app->savedState) = engine->state;
            engine->app->savedStateSize = sizeof(struct saved_state);
            break;
        case APP_CMD_INIT_WINDOW:
            // The window is being shown, get it ready.
            if (engine->app->window != NULL) {
                engine_init_display(engine);
                engine_draw_frame(engine);
            }
            break;
        case APP_CMD_TERM_WINDOW:
            // The window is being hidden or closed, clean it up.
            engine_term_display(engine);
            break;
        case APP_CMD_GAINED_FOCUS:
 
            break;
        case APP_CMD_LOST_FOCUS:
            // Also stop animating.
            engine->animating = 0;
            engine_draw_frame(engine);
            break;
    }
}
 
/**
 * This is the main entry point of a native application that is using
 * android_native_app_glue.  It runs in its own thread, with its own
 * event loop for receiving input events and doing other things.
 */
void android_main(struct android_app* state) {
    struct engine engine;
 
     LOGW("Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee started");
    // Make sure glue isn't stripped.
    app_dummy();
 
    memset(&engine, 0, sizeof(engine));
    state->userData = &engine;
    state->onAppCmd = engine_handle_cmd;
    state->onInputEvent = engine_handle_input;
    engine.app = state;
 
 
 
    if (state->savedState != NULL) {
        // We are starting with a previous saved state; restore from it.
        engine.state = *(struct saved_state*)state->savedState;
    }
 
    // loop waiting for stuff to do.
 
    while (1) {
        // Read all pending events.
        int ident;
        int events;
        struct android_poll_source* source;
 
        // If not animating, we will block forever waiting for events.
        // If animating, we loop until all events are read, then continue
        // to draw the next frame of animation.
        while ((ident=ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events,
                (void**)&source)) >= 0) {
 
            // Process this event.
            if (source != NULL) {
                source->process(state, source);
            }
 
            // Check if we are exiting.
            if (state->destroyRequested != 0) {
                engine_term_display(&engine);
                return;
            }
        }
 
        if (engine.animating) {
            // Done with events; draw next animation frame.
            engine.state.angle += .01f;
            if (engine.state.angle > 1) {
                engine.state.angle = 0;
            }
 
            // Drawing is throttled to the screen update rate, so there
            // is no need to do timing here.
            engine_draw_frame(&engine);
        }
    }
}
//END_INCLUDE(all)

Now you have to build the code: To do that call this from ~/aghosty

ndk-build

Now lets assemble debug app package

ant debug

Warning. "Ant" function doesn't see changes in C code/library. So, to rebuild we need to delete 'bin' directory (for now :) ).

Now lets deploy to real device. (The Blender on Android would probably use openGles 2.0 for the shader, but it is not supported in emulator)

On android

  • Settings->Applications->Development->[x] USB Debugging
  • Connect the device to the computer

Run

adb install -r bin/Blender-debug.apk

Yay! (or may be not) If not, check if there error messages in any of that steps.


notes by ideasman42

... challange would be to get blender running on linux with mesa's gles2 and SDL, once this is possible it *should* be possible to build on android.