Reputation: 3349
i need to get access from native code (C++) to a java vertex array (float array) but i can not find any documentation how to do that.. I can access the object and its non array variables but don't know how to access an array.
The code to access a non array variable:
// get mesh object
jclass clazz = env->GetObjectClass(java_obj);
jfieldID mesh_fid = env->GetFieldID(clazz, field_name, "Lorg/siprop/bullet/util/Mesh;");
jobject mesh_obj = env->GetObjectField(java_obj, mesh_fid);
// get mesh vert count
jclass mesh_clazz = env->GetObjectClass(mesh_obj);
jfieldID mesh_vertCoun_fid = env->GetFieldID(mesh_clazz, "numVertex", "I");
int vertCount = env->GetIntField(java_obj, mesh_vertCoun_fid);
// java code
class Mesh
{
public float vertex[];
public int numVertex = 0;
...
}
How can i access vertex[] from mesh_obj?
Upvotes: 0
Views: 900
Reputation: 12927
You do it similary like you do it for numVertex field. Instead of int you will receive jarray:
jfieldID mesh_vertex_fid = env->GetFieldID(mesh_clazz, "vertex", "[F");
jfloatArray vertices = (jfloatArray)env->GetObjectField(java_obj, mesh_vertex_fid);
After that you can access elements of array either with GetFloatArrayElements or GetPrimitiveArrayCritical methods:
float* verticesPtr = env->GetFloatArrayElements(vertices, NULL);
if (verticesPtr)
{
// process vertices here
// ...
env->ReleaseFloatArrayElements(vertices, verticesPtr, JNI_ABORT); // discard changes, change to JNI_COMMIT to save changes
}
Upvotes: 1