Saturday, August 20, 2011

Building OpenSceneGraph for iOS [In Progress]

After spending a few hours on trying to build OpenSceneGraph 3.0.1 on Xcode 4.2 for iOS 4.3/5.0, I finally gave up ...

The README.txt in the official OSG package describes how to generate an Xcode project using CMake. I tried to use CMake 2.8.5. Following the cmake command line described in README.txt, I got an error about not able to build a test project. Then I tried CMake GUI, but it still didn't work.

I searched the web and found a modified version at git://github.com/stmh/osg.git. This time, CMake GUI generated an Xcode project. However, when I tried to open and build this project in XCode 4.2, I got the following error:

OpenSceneGraph-2.9.10/src/OpenThreads/pthreads/PThread.c++:1: error: -mmacosx-version-min not allowed with -miphoneos-version-min

Reference: http://forum.openscenegraph.org/viewtopic.php?t=7433

It seems that CMake always made a mistake in setting the Architectures, Base SDK, Supported Platforms, and Valid Architectures consistently. Then I created a new OpenGL ES game project in XCode and followed the settings:

Architectures: Standard (armv7)
Base SDK: Latest iOS (iOS 5.0)
Supported Platforms: iphonesimulator iphoneos
Valid Architectures: armv6 armv7 armv7f armv7k

I also made sure that i386 did not appear in the CMAKE_CXX_FLAGS since I was building OSG for iOS Device.

The error disappeared, but a lot of new errors showed up:

GL

'*' was not declared in this scope
'glLoadMatrixf' was not declared in this scope
...

It seems that XCode was not able to find the OpenGL ES library. The XCode project does not contain the OpenGLES.framework. Therefore, I created a new OpenGL ES project and added the OSG project to it. I also set the Header Search Paths to point to the root of the OpenSceneGraph package.

However, I still got the following error at the end:

'vector' file not found in lwo2chunks.h

It seems XCode cannot locate the standard library. I have no clue how that can happen.

============

I just found a post about how to minimize the size of the osg library for iphone:
http://forum.openscenegraph.org/viewtopic.php?t=7224

Summary:
we should build the library in arm 7 only for the device, since arm 7 is for 3GS and above. I think there is no point to build the library in arm 6 now, since iPhone 3G or earlier are relatively old now.

We also only need OpenThreads, osg, osgDB, osgGA, osgUtil and osgViewer. We can probably throw away all of the plugins.

We should also build the osg libs with Optimization set to Fastest-Smallest.

BTW, the same post also mentions that display lists are not available in OpenGL ES. Therefore, we should turn off OSG_GL_DISPLAYLISTS_AVAILABLE.

==========

I found another post that mentions Apple loosened the terms on their license agreement to allow for dynamic linking and plug-ins now:

http://forum.openscenegraph.org/viewtopic.php?t=6582

BTW, Thomas Hogarth is a person who successfully built OSG for iOS and also published an iOS game called Alient Attack.