Skip to content

Latest commit

 

History

History

1.8.SampleUnderAndroid

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Table of contents

This tutorial is part of OpenSceneGraph cross-platform guide.

In this tutorial we build and run sample OpenSceneGraph application under Android. The application displays provided model with simple GLSL shaders.

Note: this tutorial requires OpenSceneGraph model (see 1.1. Create a cube)

YouTube | Download

Video depicts running and building sample OpenSceneGraph application with Android Studio 2.3.1 under Xubuntu 16.04.

Note: steps below use frames from the video as screenshots. Watch the video to see all details.

Screenshot

Download the latest version of Android Studio. It contains all the tools you need to develop for Android.

Screenshot

According to Android Studio installation instructions 64-bit Ubuntu needs several 32-bit libraries to make Android Studio work.

Install necessary 32-bit libraries with the following command:

sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 libbz2-1.0:i386

Screenshot

Extract Android Studio archive into your home directory.

Screenshot

Run Android Studio with the following commands:

cd ~

./android-studio/bin/studio.sh

Screenshot

Select standard installation options.

Screenshot

Wait for standard components to install.

Screenshot

Select Configure -> SDK Manager to run SDK Manager, which we will use to install additional components.

Screenshot

We need CMake, LLDB, and NDK components to build C++ for Android.

Select them in SDK Tools tab of SDK Manager.

Screenshot

To install CMake, LLDB, and NDK, we need to accept the license.

Screenshot

Wait for additional components to install.

Screenshot

Select Import an Android code sample.

Screenshot

Locate 'Hello GL2' sample and import it.

Screenshot

When we try to run 'Hello GL2' sample, an empty list of devices is presented.

Screenshot

We need to create Android Virtual Device to run 'Hello GL2' sample.

Screenshot

Select device definition. This is effectively the size of AVD.

This tutorial uses Nexus One 3.7" with 480x800 resolution.

Screenshot

Download armeabi-v7a system image. It emulates ARMv7, which is the most widespread architecture for Android.

You may install other system images to test your application on other architectures.

Screenshot

Select downloaded system image.

Screenshot

This step is optional. You only need to select software graphics renderer if you run Android Studio in an environment that lacks 3D acceleration.

Screenshot

Select created AVD to run the sample.

Screenshot

You should see green triangle. This means AVD can display GLES2 graphics.

Screenshot

Start a new Android Studio project.

Screenshot

This option adds sample C++ native library to the project.

We will use its CMakeLists.txt file later.

Screenshot

Since we only need one screen, select empty activity.

Screenshot

Turn on C++11, exceptions, and RTTI, because they are necessary to build OpenSceneGraph.

Screenshot

Run the project. It should display a screen with Hello from C++ text.

Screenshot

Clone OpenSceneGraph alongside the project with the following commands:

cd ~

cd AndroidStudioProjects

git clone https://github.com/openscenegraph/OpenSceneGraph

Note: you must place OpenSceneGraph alongside the project, because sample OpenSceneGraph application's Android part requires it.

Screenshot

Clone sample OpenSceneGraph application alongside the project with the following commands:

cd ~

cd AndroidStudioProjects

git clone https://github.com/ogstudio/openscenegraph-cross-platform-guide-application

Note: you must place sample OpenSceneGraph application alongside the project, because sample OpenSceneGraph application's Android part requires it.

Screenshot

Copy Android part into the project with the following commands:

cd ~

cd AndroidStudioProjects

cp -R openscenegraph-cross-platform-guide-application/android/app your_project

Sample OpenSceneGraph application's android/app directory contains RenderActivity with backing native library, box model, and a few supporting files.

android/app is almost all you need to run OpenSceneGraph application under Android.

Screenshot

Import your package's R class inside RenderActivity, so that it can reference box model from you project's resources.

Screenshot

  • Request GLES2 support by adding

<uses-feature android:glEsVersion="0x00020000"/>

  • Make RenderActivity the main activity by referencing it with

<activity android:name="org.opengamestudio.osgapp.RenderActivity">

Screenshot

Reference RenderActivity's backing native library's CMakeLists file by adding

include(CMakeLists-osgNativeLib.txt)

to the project's CMakeLists file.

Screenshot

This step is optional.

By default, Android Studio builds the project for all supported architectures. Each architecture requires its own build of OpenSceneGraph. OpenSceneGraph build for single architecture takes about 1.5G.

We restrict the project to ARMv7 ABI to save disk space.

Screenshot

This builds OpenSceneGraph as static libraries and links them to RenderActivity's backing native library called osgNativeLib.

Note: make note of the native library's full name.

Screenshot

Try to run the project.

Screenshot

The logs say that osgNativeLib is missing.

As you noted above, the name should read like osgNativeLibd. d postfix is a side effect of building OpenSceneGraph in Debug mode.

Screenshot

Open osgNativeLib.java and reference osgNativeLibd instead of osgNativeLib.

Screenshot

You should now see red cube displayed.