简体   繁体   中英

Is it possible to make OpenGL ES calls from your C++ and Java code?

I have an application that will be taking advantage of the NDK do to high graphics requirements and a terrain generation library that I wrote in c++. My question is if I already made my GUI with the SDK and I make a random opengl call in java such as GLES20.glEnable(GL_CULL_FACE); and then proceed to call glEnable(GL_DEPTH_TEST); in C++ via JNI would there be any known errors/build issues? In the case that someone wonders why I am asking this and/or thinks it is a stupid question it is because in desktop OpenGL there is an existing OpenGL context (though GLFW took care of most of this). I am concerned if OpenGL ES also has an existing context . If so, would making OpenGL ES calls from both java and C++ write to previously stated context ?

In OpenGL you're always dealing with context, yes. The critical parts for you are

  • when and how is your OpenGL context bound in the Java parts?
  • is the OpenGL context kept current when calling into native code.

Practically all Java calls to OpenGL go into native code any way. So if you write parts of your program with the NDK and call into these parts in the same way as you'd call directly into OpenGL, then a OpenGL context will be current and be usable.

The direct answer is Yes , but you must be careful how you write your C++ and Java code

  • NDK offer some NativeActivity and native_app_glue codes, which helps you write pure C++ codes for game logic, rendering, etc., and minimize the requirement for writing Java code. You will find some entry point function like android_main() if you are using this way. In this case, you can't mix call OpenGL in Java and C++ codes in the same context, since your native code are run in different thread, and communicate through pipe with Java thread [Dalvik VM thread]
  • Call native function in Java through JNI, this will in the same thread, same context, call OpenGL API in Java or C++ should be no difference, just as the answer of @datenwolf

Hope this helps~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM