Is it possible to load a texture using the NDK, but pass the result back to use in java.

I have used the code in the link above and it appears to load the png correctly and generates a texture ID without an error (the id is 0 before the loading function is called and non-zero afterwards).

Is it not allowed to mix opengl operations between C++ and java?

Thanks for your help All the best, Ash

  • \$\begingroup\$ The question is not clear. Yes, it is allowed to mix OpenGL operations between C++ and Java. But I don’t think this is really what you are asking. Are you having a problem doing so? If so, please explain what you are doing and what happens. \$\endgroup\$ Commented Apr 10, 2011 at 18:55

1 Answer 1


I do quite a bit of the opposite -- I leverage the Android facilities for loading images from disk and into OpenGL, and then return the texture ID back over JNI.

So basically, I have native code, a JNI bridge, and then a series of utility functions in Java that do the actual work, and my call sequence looks something like:

[native code] -> [JNI code] -> [Java Code]

and of course the texture ID is passed back to native code. There's no reason this can't work the other way around, though I have to wonder what compels you to do so, since it's so easy to do in Java/Android as it is. What is the problem you're encountering?

Make sure you aren't attempting to make OpenGL calls simultaneously from multiple threads, and check to see if the OpenGL error flag is being set anywhere.

  • \$\begingroup\$ A reason for doing so would be if the image format could not be read from Java, or if the texture required important CPU resources (procedural textures, or various effects). I do the exact opposite, too, but I don’t see either why the texture ID couldn’t be passed back to Java. \$\endgroup\$ Commented Apr 10, 2011 at 18:53
  • \$\begingroup\$ Hi @stephelton, thanks for your reply. I have some C++ code from a iPhone project i'd like to use (for loading a font texture and texture co-ords), but I'd like to do the majority of the opengl stuff in java (as it's easier to access resources and assets). I'm not doing any threading stuff yet, unless it's done automatically? Thanks! Ash \$\endgroup\$ Commented Apr 10, 2011 at 18:59
  • 1
    \$\begingroup\$ GLSurfaceView will run your renderer in its own thread. So if you're using this class, you should not do any OpenGL work anywhere else (generally only in its onDrawFrame(), onSurfaceCreated(), and onSurfaceChanged() methods) \$\endgroup\$
    – notlesh
    Commented Apr 11, 2011 at 14:56
  • \$\begingroup\$ / Sam. Sorry for not getting back earlier, my son is in hospital and my laptop is in here to stop my wife going crazy. Aha, I use GLSurfaceView. Is there another way of using openGL? A normal game loop perhaps? I'll do some googling on the subject. Thanks for the help! \$\endgroup\$ Commented Apr 11, 2011 at 21:03
  • \$\begingroup\$ Ah, I finally understand what you were saying in the comment above. I moved the call to my native texture loading code to onSurfaceCreated and all works fine now! Thanks for your help! \$\endgroup\$ Commented Apr 25, 2011 at 13:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .