NDK, OpenGL - loading resources and assets from native code

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Re: NDK, OpenGL - loading resources and assets from native c

Postby andreasv » Fri Feb 04, 2011 11:02 am

julienr wrote:Hi,
With version 5 of the NDK, Google has finally included support to access APK from native code without using my "hack" anymore.
I haven't experimented with it yet, but I would recommend looking into it :
http://developer.android.com/sdk/ndk/index.html
http://developer.android.com/reference/ ... ivity.html

(Also see the docs/CHANGES.HTML included in the ndk).

Now, to answer your question, the point of using libzip is not to worry about compression. So you could read your XML file using the same code that I use to read my textures (libzip is used to decompress on-the-fly).

But really, you'd better do it using the official NDK way.

Hope this helps.


Thank you very much for your response!!

I am aware of the new release of the NDK..Unfortunately I am using a modified version of the ndk which currently does not support what I want.

Your example actually does work for me well! I can get the apk and read the included files and also can perform zip_fread() and store contents of the xml file in a buffer!. Well my problem is that I am using a function of OpenCV, cvLoad which takes as an argument the path of the file.

CvHaarClassifierCascade * cascade = (CvHaarClassifierCascade *)cvLoad(filename,0, 0, 0 );

I can not find a way to initialise the cascade from my buffer and therefore I put all my effort in order to find a way to read the file from cvLoad() and not a buffer. I tried to place the xml file also in res/raw and rename it to .mp3 in order to avoid being compressed and skip the zlib functionality.. unfortunately this doesn't seem to work at all..

Can you suggest something?

Anw, thank you very much for your response and from the initial code, it helped a lot!

Regards,

Andreas
andreasv
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 02, 2011 11:03 am

Top

Re: NDK, OpenGL - loading resources and assets from native c

Postby julienr » Fri Feb 04, 2011 11:57 am

Well, if OpenCV doesn't support loading from buffer instead of file, I think the solution would be to :
1. Read the XML from the APK using libzip (like for the textures)
2. Create a temporary file.
3. Write this XML to the temporary file
4. Load the XML in OpenCV using this temporary file

I am not quite sure how temporary files work on Android (never used them), but the Java API has a File.createTempFile function. Maybe the [url="http://linux.die.net/man/3/mktemp"]mktemp(3)[/url] function is available from the Android libC.
Also, you would probably need a permission to write to the filesystem to be able to create temporary files.
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby zing0000 » Sat Mar 26, 2011 7:53 pm

Thanks for the great post!

I also am wrestling with a design decision to read the APK or create files for access from native code. But I don't understand your comment about 'the official NDK way' in r5b.

NativeActivity does not seem to offer anything that helps with the resource access issue.

docs/CHANGES.HTML claims, 'easily reading assets out of an APK from native code', but I cannot find any further details.

If you have any pointers, I would really appreciate it!
zing0000
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Mar 26, 2011 6:53 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby julienr » Sun Mar 27, 2011 6:18 pm

Looks like the SDK has no documentation in the docs/ directory about this load from APK thing.

But you should have a look at platforms/android-9/arch-arm/usr/include/android.
If you look in native_activity.h, a ANativeActivity struct is defined (you get a pointer to this struct when you create a native activity project, see the sample code).
It has a AAssetManager* assetManager member and if you look in asset_manager.h, there are a bunch of functions to open file/directory from the APK using this asset manager.

I have written some code that is using this native activity approach for one of my project : It's a freetype font display library and the interesting part lies in the _loadFont method. It loads a freetype font from a .ttf file stored in the APK using an AAssetManager. You can have a look at it here :
https://github.com/julienr/libfont/blob ... i/main.cpp

Now the problem with this native activity approach is that it requires a Android 2.3+ device... So well, only use it if you target last-generation devices.
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby AshMcConnell » Sun Apr 10, 2011 3:01 pm

Thanks a lot julienr, I am just starting to learn android coding and the NDK, your code and explanations have helped a lot. I haven't fully integrated the code yet, but I've learnt a lot by giving it a read.

Thanks again,
Ash
AshMcConnell
Freshman
Freshman
 
Posts: 4
Joined: Sat Apr 09, 2011 5:48 pm
Location: Northern Ireland

Re: NDK, OpenGL - loading resources and assets from native c

Postby AshMcConnell » Sun Apr 10, 2011 7:19 pm

One question, is it possible to mix native and java calls to the OpenGL API? I am trying to load a resource in NDK and pass the id back to java.

Thanks!
Ash
AshMcConnell
Freshman
Freshman
 
Posts: 4
Joined: Sat Apr 09, 2011 5:48 pm
Location: Northern Ireland

Top

Re: NDK, OpenGL - loading resources and assets from native c

Postby julienr » Sun Apr 10, 2011 7:32 pm

Happy this helps :-)

Regarding the native/java Opengl calls mixing, I have really no idea and I haven't seen anything in the documentation about that. But I would really recommend doing all the rendering either in Java, either using the NDK.

Do you really need to load your resource with the NDK and display it using Java ? Couldn't you just load it in Java or display it using the NDK ?
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby jettimadhu » Tue May 17, 2011 7:29 am

Hi,

I am working on OpenGL2.0 in android.But I didn't find the way how to enable and disable OpenGL2.0 in andorid and Android Browser. If anyone knows please help me on this..

Thanx in advance..
jettimadhu
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Sep 09, 2009 12:50 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby julienr » Thu May 19, 2011 2:14 am

You should probably start a new topic with that question.
I don't really understand what you are trying to achieve, but I think you should look at the NDK OpenGL examples.
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby jettimadhu » Thu May 19, 2011 5:11 am

Hi Julienr,
Thanx for immediate reply.I already worked on basic examples.But I dont know to enable or disable OpenGL 2.0. And I want to know the device supports it or it doesn't. I want to find out how to check what it(the device) supports.
jettimadhu
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Sep 09, 2009 12:50 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby julienr » Thu Jan 19, 2012 5:21 pm

jettimadhu wrote:Hi Julienr,
Thanx for immediate reply.I already worked on basic examples.But I dont know to enable or disable OpenGL 2.0. And I want to know the device supports it or it doesn't. I want to find out how to check what it(the device) supports.

You don't need to enable or disable OpenGL ES 2.0 explicitely. If you use 2.0 functions, your code will not work on a device that doesn't support OpenGL 2.0.
Now, to check if the device supports ES 2.0, you should have a look at glGetString(GL_VERSION). It allows you to query the latest version supported by the device (a device that support 2.0 automatically supports 1.0).
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby daavidfischer » Fri Nov 30, 2012 1:01 pm

I did not even know that android games could be developed using c and c++. I am new to android development and learning to develop from this forum. I try out some codes here, it is very helpful.



__________
David Fischer
view site
daavidfischer
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Oct 12, 2012 8:00 am

Re: NDK, OpenGL - loading resources and assets from native c

Postby Sagar jacky » Wed Dec 05, 2012 12:41 pm

Thanks for the guidelines to load the resources and assets from native code, NDK, OpenGL applications and I hope many users like us could able to get the maximum ideas out of it. Thanks for the efforts you have put on this. http://www.gripgomount.com"
Sagar jacky
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Nov 21, 2012 6:03 am

Re: NDK, OpenGL - loading resources and assets from native c

Postby tomjackson » Tue Jan 01, 2013 1:57 pm

I want to read an xml file from native code and package it in the apk as your texture image in the assets folder. My worries is that as far as I understand, if I place my xml file in assets folder it will be compressed except if I change it's extension and put something like .mp3. How does it affect me if it is compressed? will I need extra libraries like the libzip?

___________
read more
tomjackson
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jan 01, 2013 1:55 pm

Re: NDK, OpenGL - loading resources and assets from native c

Postby ayrton » Wed Jan 16, 2013 7:45 am

I appreciate the post because there are many people like me who finds it flushed while performing on the android actually made the very best of it. In addition, the most prominent of android's feature, its open-source nature was a new experience to me.
ayrton
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jan 15, 2013 10:15 am

Top
PreviousNext

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests