* * *

Author Topic: New OpenGL Android Example  (Read 51268 times)

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3407
New OpenGL Android Example
« on: November 11, 2011, 05:52:50 am »
Hello,

There is a new Android NDK example in the Lazarus CCR. The most important thing that this example adds is that there are precise step-by-step instructions for the entire process and also a full project structure for building it. The instructions cover from configuring the cross-compiler up to installing the APK in the telephone:

http://wiki.lazarus.freepascal.org/Android_Interface/Android_Programming#Build_the_NDK_OpenGL_example

The example itself is very simple, and just processes some touch screen inputs and draws some colors in the screen.

It uses JNI and the NativeActivity so it runs on all phones with Android 2.3+ (because the cross-compiler uses soft-float). In this example there is zero Java source code, the NativeActivity does everything that we need. This method is fully sponsored by Google, so applications built like that are really on the safe side.

I am no OpenGL expert, so I don't know yet much about this part, so I just reused older examples for that, but what really amazed me is that I was able to create a project which correctly links to Android native libraries from the NDK, so I gained some knowledge here which I tought people might find useful:

1> Copying .so libraries from the device to link to them is not required, just link against the .so files which are in the NDK
2> Specifying the path to the .so files with -Fl works nicely as described in the example instructions
3> I previously heard from someone that FPC had some initialization issues in Android libs, but I haven't found any in this project (I still have to check if it doesn't appear if I added more units to the uses clause)
4> It is possible to write an application which uses OpenGL and touch screen input with zero Java code =D
5> You need at least ant 1.8 to use the latest Android SDK, so you might need to upgrade your Linux (I had to upgrade from Mandriva 2010 to Mageia 1 for this)

I hope you enjoy,

Stoney

  • Newbie
  • Posts: 4
    • Freeze Development
Re: New OpenGL Android Example
« Reply #1 on: November 12, 2011, 04:45:29 pm »
This is awesome. Thank you so much. Haven't had a chance to try this yet, but I will very soon.

Leledumbo

  • Hero Member
  • *****
  • Posts: 5612
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: New OpenGL Android Example
« Reply #2 on: November 12, 2011, 11:53:27 pm »
First problem, I got error regarding missing /lib/ld-linux.so.3, that's easy to fix looking at another thread. Second one, I got this:
Quote
C:\fpc\bin\i386-win32\arm-linux-ld.exe: warning: E:\Sources\lazarus-fpc\android-ndk\examples\opengltest\android\libs\armeabi\link.res contains output sections; did you forget -T?
E:\Sources\lazarus-fpc\android-ndk\examples\opengltest\lib\arm-linux\ndkopengltest.o: In function `FPC_SHARED_LIB_START':
ndkopengltest.pas:(.text.n_p$ndkopengltest_$$_fpc_shared_lib_start+0x0): multiple definition of `FPC_SHARED_LIB_START'
C:\fpc\units\arm-linux\rtl\dllprt0.o:dllprt0.as:(.text+0x0): first defined here
ndkopengltest.pas(255,37) Error: Error while linking
For this one, I have no idea...

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3407
Re: New OpenGL Android Example
« Reply #3 on: November 13, 2011, 02:41:48 pm »
For this one, I have no idea...

I had tested it with my FPC 2.5 snapshot and some things changed in the compiler in the mean time. Do svn update, it should be fixed now.

Laksen

  • Sr. Member
  • ****
  • Posts: 459
    • J-Software
Re: New OpenGL Android Example
« Reply #4 on: November 13, 2011, 03:14:08 pm »
felipemdc, the problem with FPC_SHARED_LIB_START has been fixed in svn trunk, so it should be removed from the example

Specifically this should be removed:
Code: [Select]
procedure PASCALMAIN; external name 'PASCALMAIN';

procedure FPC_SHARED_LIB_START; [public, alias: 'FPC_SHARED_LIB_START'];
begin
   PASCALMAIN;
end;

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3407
Re: New OpenGL Android Example
« Reply #5 on: November 13, 2011, 04:33:34 pm »
felipemdc, the problem with FPC_SHARED_LIB_START has been fixed in svn trunk, so it should be removed from the example

I already fixed that. That's what I ment on Reply #3 on: Today at 09:41:48 am »

Leledumbo

  • Hero Member
  • *****
  • Posts: 5612
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: New OpenGL Android Example
« Reply #6 on: November 14, 2011, 04:09:52 pm »
OK, compiled. But got exception when running:
Quote
E/AndroidRuntime(15688): FATAL EXCEPTION: main
E/AndroidRuntime(15688): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pascal.opengltest/android.app.NativeActivity}: java.lang.IllegalArgumentException: Unable to load native library: /data/data/com.pascal.opengltest/lib/libmain.so
Any reason why? I have android-10 for the SDK and android-9 for the NDK (I have no idea why the NDK skips version 10, it jumps over to 14).

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3407
Re: New OpenGL Android Example
« Reply #7 on: November 14, 2011, 04:25:11 pm »
OK, compiled. But got exception when running:
E/AndroidRuntime(15688): FATAL EXCEPTION: main
E/AndroidRuntime(15688): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pascal.opengltest/android.app.NativeActivity}: java.lang.IllegalArgumentException: Unable to load native library: /data/data/com.pascal.opengltest/lib/libmain.so

Another person had exactly the same error, and he was also trying to do it from Windows.

I had no problem in Linux.

some ideas:
1> Did you configure the binutils for eabi=5?
2> Did you compile your cross-compiler with software floating point?

Quote
I have android-10 for the SDK and android-9 for the NDK (I have no idea why the NDK skips version 10, it jumps over to 14).

I think they jumped because they didn't change anything in the NDK between these versions, it should be no problem. For me it worked fine

Gintas

  • Jr. Member
  • **
  • Posts: 71
    • Developer's Diary
Re: New OpenGL Android Example
« Reply #8 on: November 14, 2011, 09:26:43 pm »
Gee,too bad it only runs on Android 2.3+ .  :(
I thought about testing it on a real hardware.

Laksen

  • Sr. Member
  • ****
  • Posts: 459
    • J-Software
Re: New OpenGL Android Example
« Reply #9 on: November 14, 2011, 10:51:17 pm »
A related entry has surfaced over at pascalgamedevelopment here: http://www.pascalgamedevelopment.com/showthread.php?11624-Android-game-FoembJump-now-opensourced

BeRo has opensourced an Android game which apparently works in more version

Leledumbo

  • Hero Member
  • *****
  • Posts: 5612
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: New OpenGL Android Example
« Reply #10 on: November 15, 2011, 12:35:47 am »
Quote
Another person had exactly the same error, and he was also trying to do it from Windows.

I had no problem in Linux.
Hmm... that seems weird, I'll try on Linux tonight.
Quote
Did you configure the binutils for eabi=5?
Uhuh, I did it by renaming the original assembler to arm-linux-as.orig.exe and create a wrapper program named arm-linux-as.exe with this contents:
Code: [Select]
program arm_linux_as;

uses
  SysUtils;
var
  Params: array of AnsiString;
  i: Word;
begin
  SetLength(Params,ParamCount + 1);
  Params[0] := '-meabi=5';
  for i := 1 to ParamCount do
    Params[i] := ParamStr(i);
  ExecuteProcess(IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) +
    'arm-linux-as.orig.exe',Params);
end.
Quote
Did you compile your cross-compiler with software floating point?
I'm not sure about this one, I only pass -dFPC_ARMEL when building.

Rustam Asmandiarov

  • New member
  • *
  • Posts: 46
Re: New OpenGL Android Example
« Reply #11 on: November 16, 2011, 05:42:35 am »
Thank you so much! This is very cool!
But I noticed that you download the OpenGL statically. How do I compile android app loading OpenGL dynamically via dynlibs, that is, through LoadLibrary and GetProcedureAddress?
I use FPC 2.7.1 and your demo has not tried to run me just interested in the source code

Огромнейшее спасибо!!! Это очень клево!
Но я заметил что вы загружаете OpenGL статически. Как мне скомпилировать Андроид приложение загружая OpenGL динамически через dynlibs, то есть через LoadLibrary и GetProcedureAddress?
Я использую FPC 2.7.1 и ваше демо еще не пробовал запускать мне просто интересен исходный код.

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3407
Re: New OpenGL Android Example
« Reply #12 on: November 16, 2011, 07:09:14 am »
Thank you so much! This is very cool!
But I noticed that you download the OpenGL statically. How do I compile android app loading OpenGL dynamically via dynlibs, that is, through LoadLibrary and GetProcedureAddress?

What for? The demo requires Android 2.3+ for multiple reasons, not just OpenGL. It also uses NativeActivity extensively. So if you can accept that it runs on 2.3+ only then you don't need to dynamically load anything because the existence of the required libraries is 100% guaranteed in Android.

Quote
I use FPC 2.7.1 and your demo has not tried to run me just interested in the source code

That's probably a bad idea to use 2.7 for Android, there were multiple breaks in 2.7 in the Android cross-compiler, you can even work around that by changing some parts of the compiler source code, but I still think that people would save themselves a lot of work by just using my Linux snapshot pre-compiled compiler, as described in the step-by-step instructions.

herux

  • Jr. Member
  • **
  • Posts: 66
Re: New OpenGL Android Example
« Reply #13 on: November 16, 2011, 07:34:27 am »
Quote
How do I compile android app loading OpenGL dynamically via dynlibs, that is, through LoadLibrary and GetProcedureAddress?

I've tried to compile it using dynlib, but an error like the following link http://www.lazarus.freepascal.org/index.php?topic=12585.0

I have not tried it again, there's new?

Rustam Asmandiarov

  • New member
  • *
  • Posts: 46
Re: New OpenGL Android Example
« Reply #14 on: November 16, 2011, 07:55:05 am »
I ported GLScene 1.2 for Android. GLScene completely based on the dynamic link library and uses dynlibs which produces an error when connecting.
Just GLScene 1.2 uses the C # naming style modules, such as GLScene.Base.OpenGL.Tokens.pas it's only available in 2.7.1, if you add in 2.5.1 the same opportunity that would be great!

Я портирую GLScene 1.2 на Android. GLScene Полностью основан на динамически загружаемой библиотеке и использует dynlibs которая выдает ошибки при подключении.
Так же GLScene 1.2 использует С# стиль именования модулей, например GLScene.Base.OpenGL.Tokens.pas а он доступен только в 2.7.1, если вы добавите в 2.5.1 такую же возможность это будет замечательно!

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads