From ead3b26ea9df33c9ef57c5ffbd24a5a535effe3f Mon Sep 17 00:00:00 2001 From: arris69 Date: Thu, 12 Mar 2015 20:37:33 +0100 Subject: [PATCH 1/3] no one ever tried this java stuff??? (crashes if the model has a lot of meshes...) --- port/jassimp/jassimp-native/src/jassimp.cpp | 76 +++++++++++++++++---- 1 file changed, 62 insertions(+), 14 deletions(-) diff --git a/port/jassimp/jassimp-native/src/jassimp.cpp b/port/jassimp/jassimp-native/src/jassimp.cpp index db803f106..1eeafe304 100644 --- a/port/jassimp/jassimp-native/src/jassimp.cpp +++ b/port/jassimp/jassimp-native/src/jassimp.cpp @@ -5,12 +5,30 @@ #ifdef JNI_LOG +#ifdef ANDROID +#include +#define lprintf(...) __android_log_print(ANDROID_LOG_VERBOSE, __func__, __VA_ARGS__) +#else #define lprintf(...) printf (__VA_ARGS__) +#endif /* ANDROID */ #else #define lprintf #endif +static void dumpRefTable(JNIEnv *env){ + /* DEBUG STUFF */ + jclass clazz = env->FindClass("dalvik/system/VMDebug"); + jmethodID dump_mid = env->GetStaticMethodID( clazz, "dumpReferenceTables", "()V" ); + env->CallStaticVoidMethod( clazz, dump_mid ); + env->DeleteLocalRef(clazz); + + /*jint res = env->EnsureLocalCapacity(20); + if(res < 0){ + lprintf("too many meshes on the model!\n"); + }*/ +} + static bool createInstance(JNIEnv *env, const char* className, jobject& newInstance) { jclass clazz = env->FindClass(className); @@ -30,6 +48,7 @@ static bool createInstance(JNIEnv *env, const char* className, jobject& newInsta } newInstance = env->NewObject(clazz, ctr_id); + env->DeleteLocalRef(clazz); if (NULL == newInstance) { @@ -41,7 +60,7 @@ static bool createInstance(JNIEnv *env, const char* className, jobject& newInsta } -static bool createInstance(JNIEnv *env, const char* className, const char* signature, const jvalue* params, jobject& newInstance) +static bool createInstance(JNIEnv *env, const char* className, const char* signature,/* const*/ jvalue* params, jobject& newInstance) { jclass clazz = env->FindClass(className); @@ -60,6 +79,7 @@ static bool createInstance(JNIEnv *env, const char* className, const char* signa } newInstance = env->NewObjectA(clazz, ctr_id, params); + env->DeleteLocalRef(clazz); if (NULL == newInstance) { @@ -82,6 +102,7 @@ static bool getField(JNIEnv *env, jobject object, const char* fieldName, const c } jfieldID fieldId = env->GetFieldID(clazz, fieldName, signature); + env->DeleteLocalRef(clazz); if (NULL == fieldId) { @@ -106,6 +127,7 @@ static bool setIntField(JNIEnv *env, jobject object, const char* fieldName, jint } jfieldID fieldId = env->GetFieldID(clazz, fieldName, "I"); + env->DeleteLocalRef(clazz); if (NULL == fieldId) { @@ -130,6 +152,7 @@ static bool setFloatField(JNIEnv *env, jobject object, const char* fieldName, jf } jfieldID fieldId = env->GetFieldID(clazz, fieldName, "F"); + env->DeleteLocalRef(clazz); if (NULL == fieldId) { @@ -154,6 +177,7 @@ static bool setObjectField(JNIEnv *env, jobject object, const char* fieldName, c } jfieldID fieldId = env->GetFieldID(clazz, fieldName, signature); + env->DeleteLocalRef(clazz); if (NULL == fieldId) { @@ -192,7 +216,7 @@ static bool getStaticField(JNIEnv *env, const char* className, const char* field static bool call(JNIEnv *env, jobject object, const char* typeName, const char* methodName, - const char* signature, const jvalue* params) + const char* signature,/* const*/ jvalue* params) { jclass clazz = env->FindClass(typeName); @@ -203,6 +227,7 @@ static bool call(JNIEnv *env, jobject object, const char* typeName, const char* } jmethodID mid = env->GetMethodID(clazz, methodName, signature); + env->DeleteLocalRef(clazz); if (NULL == mid) { @@ -210,6 +235,27 @@ static bool call(JNIEnv *env, jobject object, const char* typeName, const char* return false; } + jboolean jReturnValue = env->CallBooleanMethod(object, mid, params[0].l); + + return (bool)jReturnValue; +} +static bool callv(JNIEnv *env, jobject object, const char* typeName, + const char* methodName, const char* signature,/* const*/ jvalue* params) { + jclass clazz = env->FindClass(typeName); + + if (NULL == clazz) { + lprintf("could not find class %s\n", typeName); + return false; + } + + jmethodID mid = env->GetMethodID(clazz, methodName, signature); + env->DeleteLocalRef(clazz); + + if (NULL == mid) { + lprintf("could not find method %s with signature %s in type %s\n", methodName, signature, typeName); + return false; + } + env->CallVoidMethodA(object, mid, params); return true; @@ -217,7 +263,7 @@ static bool call(JNIEnv *env, jobject object, const char* typeName, const char* static bool callStaticObject(JNIEnv *env, const char* typeName, const char* methodName, - const char* signature, const jvalue* params, jobject& returnValue) + const char* signature,/* const*/ jvalue* params, jobject& returnValue) { jclass clazz = env->FindClass(typeName); @@ -338,7 +384,7 @@ static bool loadMeshes(JNIEnv *env, const aiScene* cScene, jobject& jScene) /* set general mesh data in java */ jvalue setTypesParams[1]; setTypesParams[0].i = cMesh->mPrimitiveTypes; - if (!call(env, jMesh, "jassimp/AiMesh", "setPrimitiveTypes", "(I)V", setTypesParams)) + if (!callv(env, jMesh, "jassimp/AiMesh", "setPrimitiveTypes", "(I)V", setTypesParams)) { return false; } @@ -380,7 +426,7 @@ static bool loadMeshes(JNIEnv *env, const aiScene* cScene, jobject& jScene) allocateBuffersParams[1].i = cMesh->mNumFaces; allocateBuffersParams[2].z = isPureTriangle; allocateBuffersParams[3].i = (jint) faceBufferSize; - if (!call(env, jMesh, "jassimp/AiMesh", "allocateBuffers", "(IIZI)V", allocateBuffersParams)) + if (!callv(env, jMesh, "jassimp/AiMesh", "allocateBuffers", "(IIZI)V", allocateBuffersParams)) { return false; } @@ -470,7 +516,7 @@ static bool loadMeshes(JNIEnv *env, const aiScene* cScene, jobject& jScene) jvalue allocateDataChannelParams[2]; allocateDataChannelParams[0].i = 0; allocateDataChannelParams[1].i = 0; - if (!call(env, jMesh, "jassimp/AiMesh", "allocateDataChannel", "(II)V", allocateDataChannelParams)) + if (!callv(env, jMesh, "jassimp/AiMesh", "allocateDataChannel", "(II)V", allocateDataChannelParams)) { lprintf("could not allocate normal data channel\n"); return false; @@ -491,7 +537,7 @@ static bool loadMeshes(JNIEnv *env, const aiScene* cScene, jobject& jScene) jvalue allocateDataChannelParams[2]; allocateDataChannelParams[0].i = 1; allocateDataChannelParams[1].i = 0; - if (!call(env, jMesh, "jassimp/AiMesh", "allocateDataChannel", "(II)V", allocateDataChannelParams)) + if (!callv(env, jMesh, "jassimp/AiMesh", "allocateDataChannel", "(II)V", allocateDataChannelParams)) { lprintf("could not allocate tangents data channel\n"); return false; @@ -512,7 +558,7 @@ static bool loadMeshes(JNIEnv *env, const aiScene* cScene, jobject& jScene) jvalue allocateDataChannelParams[2]; allocateDataChannelParams[0].i = 2; allocateDataChannelParams[1].i = 0; - if (!call(env, jMesh, "jassimp/AiMesh", "allocateDataChannel", "(II)V", allocateDataChannelParams)) + if (!callv(env, jMesh, "jassimp/AiMesh", "allocateDataChannel", "(II)V", allocateDataChannelParams)) { lprintf("could not allocate bitangents data channel\n"); return false; @@ -535,7 +581,7 @@ static bool loadMeshes(JNIEnv *env, const aiScene* cScene, jobject& jScene) jvalue allocateDataChannelParams[2]; allocateDataChannelParams[0].i = 3; allocateDataChannelParams[1].i = c; - if (!call(env, jMesh, "jassimp/AiMesh", "allocateDataChannel", "(II)V", allocateDataChannelParams)) + if (!callv(env, jMesh, "jassimp/AiMesh", "allocateDataChannel", "(II)V", allocateDataChannelParams)) { lprintf("could not allocate colorset data channel\n"); return false; @@ -574,7 +620,7 @@ static bool loadMeshes(JNIEnv *env, const aiScene* cScene, jobject& jScene) } allocateDataChannelParams[1].i = c; - if (!call(env, jMesh, "jassimp/AiMesh", "allocateDataChannel", "(II)V", allocateDataChannelParams)) + if (!callv(env, jMesh, "jassimp/AiMesh", "allocateDataChannel", "(II)V", allocateDataChannelParams)) { lprintf("could not allocate texture coordinates data channel\n"); return false; @@ -701,6 +747,8 @@ static bool loadMeshes(JNIEnv *env, const aiScene* cScene, jobject& jScene) } } } + env->DeleteLocalRef(jMeshes); + env->DeleteLocalRef(jMesh); } return true; @@ -719,7 +767,7 @@ static bool loadSceneNode(JNIEnv *env, const aiNode *cNode, jobject parent, jobj wrapMatParams[0].l = jMatrixArr; jobject jMatrix; - if (!callStaticObject(env, "Ljassimp/Jassimp;", "wrapMatrix", "([F)Ljava/lang/Object;", wrapMatParams, jMatrix)) + if (!callStaticObject(env, "jassimp/Jassimp", "wrapMatrix", "([F)Ljava/lang/Object;", wrapMatParams, jMatrix)) { return false; } @@ -749,7 +797,7 @@ static bool loadSceneNode(JNIEnv *env, const aiNode *cNode, jobject parent, jobj wrapNodeParams[2].l = jMeshrefArr; wrapNodeParams[3].l = jNodeName; jobject jNode; - if (!callStaticObject(env, "Ljassimp/Jassimp;", "wrapSceneNode", + if (!callStaticObject(env, "jassimp/Jassimp", "wrapSceneNode", "(Ljava/lang/Object;Ljava/lang/Object;[ILjava/lang/String;)Ljava/lang/Object;", wrapNodeParams, jNode)) { return false; @@ -842,7 +890,7 @@ static bool loadMaterials(JNIEnv *env, const aiScene* cScene, jobject& jScene) setNumberParams[0].i = ttInd; setNumberParams[1].i = num; - if (!call(env, jMaterial, "jassimp/AiMaterial", "setTextureNumber", "(II)V", setNumberParams)) + if (!callv(env, jMaterial, "jassimp/AiMaterial", "setTextureNumber", "(II)V", setNumberParams)) { return false; } @@ -880,7 +928,7 @@ static bool loadMaterials(JNIEnv *env, const aiScene* cScene, jobject& jScene) wrapColorParams[0].f = ((float*) cProperty->mData)[0]; wrapColorParams[1].f = ((float*) cProperty->mData)[1]; wrapColorParams[2].f = ((float*) cProperty->mData)[2]; - if (!callStaticObject(env, "Ljassimp/Jassimp;", "wrapColor3", "(FFF)Ljava/lang/Object;", wrapColorParams, jData)) + if (!callStaticObject(env, "jassimp/Jassimp", "wrapColor3", "(FFF)Ljava/lang/Object;", wrapColorParams, jData)) { return false; } From 8b22ba25baa63e4ce30e5f3cd5ecb68f50993207 Mon Sep 17 00:00:00 2001 From: arris69 Date: Thu, 12 Mar 2015 21:24:49 +0100 Subject: [PATCH 2/3] removed debug function and add buildfile for android ndk-build command --- port/jassimp/jassimp-native/Android.mk | 13 +++++++++++++ port/jassimp/jassimp-native/src/jassimp.cpp | 14 -------------- 2 files changed, 13 insertions(+), 14 deletions(-) create mode 100644 port/jassimp/jassimp-native/Android.mk diff --git a/port/jassimp/jassimp-native/Android.mk b/port/jassimp/jassimp-native/Android.mk new file mode 100644 index 000000000..94b233b32 --- /dev/null +++ b/port/jassimp/jassimp-native/Android.mk @@ -0,0 +1,13 @@ +LOCAL_PATH:= $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_MODULE := jassimp +LOCAL_SRC_FILES := src/jassimp.cpp + +LOCAL_CFLAGS += -DJNI_LOG + +#LOCAL_STATIC_LIBRARIES := assimp_static +LOCAL_SHARED_LIBRARIES := assimp +LOCAL_LDLIBS := -llog + +include $(BUILD_SHARED_LIBRARY) diff --git a/port/jassimp/jassimp-native/src/jassimp.cpp b/port/jassimp/jassimp-native/src/jassimp.cpp index 1eeafe304..cc064bd3e 100644 --- a/port/jassimp/jassimp-native/src/jassimp.cpp +++ b/port/jassimp/jassimp-native/src/jassimp.cpp @@ -15,20 +15,6 @@ #define lprintf #endif - -static void dumpRefTable(JNIEnv *env){ - /* DEBUG STUFF */ - jclass clazz = env->FindClass("dalvik/system/VMDebug"); - jmethodID dump_mid = env->GetStaticMethodID( clazz, "dumpReferenceTables", "()V" ); - env->CallStaticVoidMethod( clazz, dump_mid ); - env->DeleteLocalRef(clazz); - - /*jint res = env->EnsureLocalCapacity(20); - if(res < 0){ - lprintf("too many meshes on the model!\n"); - }*/ -} - static bool createInstance(JNIEnv *env, const char* className, jobject& newInstance) { jclass clazz = env->FindClass(className); From 9094770d38fd278c09bcbced06d03e771f11bf11 Mon Sep 17 00:00:00 2001 From: arris69 Date: Fri, 13 Mar 2015 12:52:03 +0100 Subject: [PATCH 3/3] fixed light and camera access. --- port/jassimp/jassimp-native/src/jassimp.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/port/jassimp/jassimp-native/src/jassimp.cpp b/port/jassimp/jassimp-native/src/jassimp.cpp index cc064bd3e..14d9cb2d4 100644 --- a/port/jassimp/jassimp-native/src/jassimp.cpp +++ b/port/jassimp/jassimp-native/src/jassimp.cpp @@ -703,7 +703,7 @@ static bool loadMeshes(JNIEnv *env, const aiScene* cScene, jobject& jScene) wrapParams[0].l = jMatrixArr; jobject jMatrix; - if (!callStaticObject(env, "Ljassimp/Jassimp;", "wrapMatrix", "([F)Ljava/lang/Object;", wrapParams, jMatrix)) + if (!callStaticObject(env, "jassimp/Jassimp", "wrapMatrix", "([F)Ljava/lang/Object;", wrapParams, jMatrix)) { return false; } @@ -939,7 +939,7 @@ static bool loadMaterials(JNIEnv *env, const aiScene* cScene, jobject& jScene) wrapColorParams[1].f = ((float*) cProperty->mData)[1]; wrapColorParams[2].f = ((float*) cProperty->mData)[2]; wrapColorParams[3].f = ((float*) cProperty->mData)[3]; - if (!callStaticObject(env, "Ljassimp/Jassimp;", "wrapColor4", "(FFFF)Ljava/lang/Object;", wrapColorParams, jData)) + if (!callStaticObject(env, "jassimp/Jassimp", "wrapColor4", "(FFFF)Ljava/lang/Object;", wrapColorParams, jData)) { return false; } @@ -1171,7 +1171,7 @@ static bool loadLights(JNIEnv *env, const aiScene* cScene, jobject& jScene) wrapColorParams[1].f = cLight->mColorDiffuse.g; wrapColorParams[2].f = cLight->mColorDiffuse.b; jobject jDiffuse; - if (!callStaticObject(env, "Ljassimp/Jassimp;", "wrapColor3", "(FFF)Ljava/lang/Object;", wrapColorParams, jDiffuse)) + if (!callStaticObject(env, "jassimp/Jassimp", "wrapColor3", "(FFF)Ljava/lang/Object;", wrapColorParams, jDiffuse)) { return false; } @@ -1180,7 +1180,7 @@ static bool loadLights(JNIEnv *env, const aiScene* cScene, jobject& jScene) wrapColorParams[1].f = cLight->mColorSpecular.g; wrapColorParams[2].f = cLight->mColorSpecular.b; jobject jSpecular; - if (!callStaticObject(env, "Ljassimp/Jassimp;", "wrapColor3", "(FFF)Ljava/lang/Object;", wrapColorParams, jSpecular)) + if (!callStaticObject(env, "jassimp/Jassimp", "wrapColor3", "(FFF)Ljava/lang/Object;", wrapColorParams, jSpecular)) { return false; } @@ -1189,7 +1189,7 @@ static bool loadLights(JNIEnv *env, const aiScene* cScene, jobject& jScene) wrapColorParams[1].f = cLight->mColorAmbient.g; wrapColorParams[2].f = cLight->mColorAmbient.b; jobject jAmbient; - if (!callStaticObject(env, "Ljassimp/Jassimp;", "wrapColor3", "(FFF)Ljava/lang/Object;", wrapColorParams, jAmbient)) + if (!callStaticObject(env, "jassimp/Jassimp", "wrapColor3", "(FFF)Ljava/lang/Object;", wrapColorParams, jAmbient)) { return false; } @@ -1201,7 +1201,7 @@ static bool loadLights(JNIEnv *env, const aiScene* cScene, jobject& jScene) wrapVec3Params[1].f = cLight->mPosition.y; wrapVec3Params[2].f = cLight->mPosition.z; jobject jPosition; - if (!callStaticObject(env, "Ljassimp/Jassimp;", "wrapVec3", "(FFF)Ljava/lang/Object;", wrapVec3Params, jPosition)) + if (!callStaticObject(env, "jassimp/Jassimp", "wrapVec3", "(FFF)Ljava/lang/Object;", wrapVec3Params, jPosition)) { return false; } @@ -1210,7 +1210,7 @@ static bool loadLights(JNIEnv *env, const aiScene* cScene, jobject& jScene) wrapVec3Params[1].f = cLight->mPosition.y; wrapVec3Params[2].f = cLight->mPosition.z; jobject jDirection; - if (!callStaticObject(env, "Ljassimp/Jassimp;", "wrapVec3", "(FFF)Ljava/lang/Object;", wrapVec3Params, jDirection)) + if (!callStaticObject(env, "jassimp/Jassimp", "wrapVec3", "(FFF)Ljava/lang/Object;", wrapVec3Params, jDirection)) { return false; } @@ -1276,7 +1276,7 @@ static bool loadCameras(JNIEnv *env, const aiScene* cScene, jobject& jScene) wrapPositionParams[1].f = cCamera->mPosition.y; wrapPositionParams[2].f = cCamera->mPosition.z; jobject jPosition; - if (!callStaticObject(env, "Ljassimp/Jassimp;", "wrapVec3", "(FFF)Ljava/lang/Object;", wrapPositionParams, jPosition)) + if (!callStaticObject(env, "jassimp/Jassimp", "wrapVec3", "(FFF)Ljava/lang/Object;", wrapPositionParams, jPosition)) { return false; } @@ -1285,7 +1285,7 @@ static bool loadCameras(JNIEnv *env, const aiScene* cScene, jobject& jScene) wrapPositionParams[1].f = cCamera->mUp.y; wrapPositionParams[2].f = cCamera->mUp.z; jobject jUp; - if (!callStaticObject(env, "Ljassimp/Jassimp;", "wrapVec3", "(FFF)Ljava/lang/Object;", wrapPositionParams, jUp)) + if (!callStaticObject(env, "jassimp/Jassimp", "wrapVec3", "(FFF)Ljava/lang/Object;", wrapPositionParams, jUp)) { return false; } @@ -1294,7 +1294,7 @@ static bool loadCameras(JNIEnv *env, const aiScene* cScene, jobject& jScene) wrapPositionParams[1].f = cCamera->mLookAt.y; wrapPositionParams[2].f = cCamera->mLookAt.z; jobject jLookAt; - if (!callStaticObject(env, "Ljassimp/Jassimp;", "wrapVec3", "(FFF)Ljava/lang/Object;", wrapPositionParams, jLookAt)) + if (!callStaticObject(env, "jassimp/Jassimp", "wrapVec3", "(FFF)Ljava/lang/Object;", wrapPositionParams, jLookAt)) { return false; }