| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720 |
- /*
- ---------------------------------------------------------------------------
- Open Asset Import Library (ASSIMP)
- ---------------------------------------------------------------------------
- Copyright (c) 2006-2008, ASSIMP Development Team
- All rights reserved.
- Redistribution and use of this software in source and binary forms,
- with or without modification, are permitted provided that the following
- conditions are met:
- * Redistributions of source code must retain the above
- copyright notice, this list of conditions and the
- following disclaimer.
- * Redistributions in binary form must reproduce the above
- copyright notice, this list of conditions and the
- following disclaimer in the documentation and/or other
- materials provided with the distribution.
- * Neither the name of the ASSIMP team, nor the names of its
- contributors may be used to endorse or promote products
- derived from this software without specific prior
- written permission of the ASSIMP Development Team.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ---------------------------------------------------------------------------
- */
- /** @file Implementation of the JNI API for jAssimp */
- #if (defined ASSIMP_JNI_EXPORT)
- // include the header files generated by javah
- #include "assimp_Importer.h"
- #include "assimp_Animation.h"
- #include "assimp_Node.h"
- #include "assimp_Texture.h"
- #include "assimp_Mesh.h"
- #include "assimp_Material.h"
- // include assimp
- #include "../../include/aiTypes.h"
- #include "../../include/aiMesh.h"
- #include "../../include/aiAnim.h"
- #include "../../include/aiScene.h"
- #include "../../include/aiAssert.h"
- #include "../../include/aiPostProcess.h"
- #include "../../include/assimp.hpp"
- #include "../../include/DefaultLogger.h"
- #include "JNIEnvironment.h"
- #include "JNILogger.h"
- using namespace Assimp;
- #include <list>
- namespace Assimp {
- namespace JNIBridge {
- // used as error return code
- #define AI_JNI_ERROR_RETURN 0xffffffff
- // typedef for a jassimp context, used to uniquely identify
- // the Importer object which belongs to a java Importer
- typedef uint64_t JASSIMP_CONTEXT;
- #if (defined _DEBUG)
- typedef std::list< JASSIMP_CONTEXT > ImporterContextList;
- static ImporterContextList g_listActiveContexts;
- // ------------------------------------------------------------------------------------------------
- /* Used in debug builds to validate a context
- */
- bool jValidateContext (JASSIMP_CONTEXT context)
- {
- for (ImporterContextList::const_iterator
- i = g_listActiveContexts.begin();
- i != g_listActiveContexts.end();++i)
- {
- if (context == *i)return true;
- }
- DefaultLogger::get()->error("[jnibridge] Invalid context");
- return false;
- }
- // ------------------------------------------------------------------------------------------------
- /* Used in debug builds to validate a given scene
- */
- bool jValidateScene (const aiScene* scene)
- {
- if (!scene)
- {
- DefaultLogger::get()->error("[jnibridge] No asset loaded at the moment");
- return false;
- }
- return true;
- }
- #endif // ! ASSIMP_DEBUG
- // ------------------------------------------------------------------------------------------------
- /* Used in debug builds to validate a given scene
- */
- Assimp::Importer* jGetValidImporterScenePair (JASSIMP_CONTEXT jvmcontext)
- {
- #if (defined _DEBUG)
- if (!jValidateContext((JASSIMP_CONTEXT)jvmcontext))return NULL;
- #endif // ! ASSIMP_DEBUG
- // get the importer instance from the context
- Assimp::Importer* pcImp = (Assimp::Importer*)jvmcontext;
- #if (defined _DEBUG)
- if (!jValidateScene(pcImp->GetScene()))return NULL;
- #endif // ! ASSIMP_DEBUG
- return pcImp;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Importer
- * Method: _NativeInitContext
- * Signature: ()I
- */
- JNIEXPORT jlong JNICALL Java_assimp_Importer__1NativeInitContext
- (JNIEnv * jvmenv, jobject jvmthis)
- {
- // 2^64-1 indicates error
- JASSIMP_CONTEXT context = 0xffffffffffffffffL;
- // create a new Importer instance
- Assimp::Importer* pcImp = new Assimp::Importer();
- context = (JASSIMP_CONTEXT)(uintptr_t)pcImp;
- #if (defined _DEBUG)
- g_listActiveContexts.push_back(context);
- #endif // ! ASSIMP_DEBUG
- // need to setup the logger
- JNILogDispatcher* pcLogger;
- if (DefaultLogger::isNullLogger())
- {
- pcLogger = new JNILogDispatcher();
- DefaultLogger::set (pcLogger);
- }
- else
- {
- JNILogDispatcher* pcLogger = ( JNILogDispatcher* )DefaultLogger::get();
- pcLogger->AddRef();
- }
- // setup the JNI environment ...
- // simply setup the newest JNIEnv*
- JNIEnvironment::Get()->AttachToCurrentThread(jvmenv);
- return context;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Importer
- * Method: _NativeFreeContext
- * Signature: (I)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Importer__1NativeFreeContext
- (JNIEnv * jvmenv, jobject jvmthis, jlong jvmcontext)
- {
- #if (defined _DEBUG)
- if (!jValidateContext((JASSIMP_CONTEXT)jvmcontext))return AI_JNI_ERROR_RETURN;
- #endif // ! ASSIMP_DEBUG
- // delete the Importer instance
- Assimp::Importer* pcImp = (Assimp::Importer*)jvmcontext;
- delete pcImp;
- #if (defined _DEBUG)
- g_listActiveContexts.remove(jvmcontext);
- #endif // ! ASSIMP_DEBUG
- JNIEnvironment::Get()->DetachFromCurrentThread();
- return 0;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Importer
- * Method: _NativeLoad
- * Signature: (Ljava/lang/String;II)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Importer__1NativeLoad
- (JNIEnv *jvmenv, jobject jvmthis, jstring jvmpath, jint jvmflags, jlong jvmcontext)
- {
- jint iRet = 0;
- #if (defined _DEBUG)
- if (!jValidateContext((JASSIMP_CONTEXT)jvmcontext))return AI_JNI_ERROR_RETURN;
- #endif // ! ASSIMP_DEBUG
- // get the path from the jstring
- const char* szPath = JNU_GetStringNativeChars(jvmenv,jvmpath);
- if (!szPath)
- {
- DefaultLogger::get()->error("[jnibridge] Unable to get path string from the java vm");
- return AI_JNI_ERROR_RETURN;
- }
- // get the importer instance from the context
- Assimp::Importer* pcImp = (Assimp::Importer*)jvmcontext;
- // and load the file. The aiScene object itself remains accessible
- // via Importer.GetScene().
- if(NULL == pcImp->ReadFile(std::string(szPath),(unsigned int)jvmflags))
- {
- DefaultLogger::get()->error("[jnibridge] Unable to load asset");
- // release the path again
- free((void*)szPath);
- return AI_JNI_ERROR_RETURN;
- }
- // release the path again
- free((void*)szPath);
- return iRet;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_PostProcessStep
- * Method: _NativeSetVertexSplitLimit
- * Signature: (I)V
- */
- JNIEXPORT void JNICALL Java_assimp_PostProcessStep__1NativeSetVertexSplitLimit
- (JNIEnv *jvmenv, jclass jvmthis, jint jvmlimit)
- {
- aiSetVertexSplitLimit(jvmlimit);
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_PostProcessStep
- * Method: _NativeSetTriangleSplitLimit
- * Signature: (I)V
- */
- JNIEXPORT void JNICALL Java_assimp_PostProcessStep__1NativeSetTriangleSplitLimit
- (JNIEnv *jvmenv, jclass jvmthis, jint jvmlimit)
- {
- aiSetTriangleSplitLimit(jvmlimit);
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Scene
- * Method: _NativeGetNumMeshes
- * Signature: (I)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Scene__1NativeGetNumMeshes
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- return (jint)pcImp->GetScene()->mNumMeshes;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Scene
- * Method: _NativeGetNumAnimations
- * Signature: (I)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Scene__1NativeGetNumAnimations
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- return (jint)pcImp->GetScene()->mNumAnimations;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Scene
- * Method: _NativeGetNumTextures
- * Signature: (I)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Scene__1NativeGetNumTextures
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- return (jint)pcImp->GetScene()->mNumTextures;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Scene
- * Method: _NativeGetNumMaterials
- * Signature: (I)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Scene__1NativeGetNumMaterials
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- return (jint)pcImp->GetScene()->mNumMaterials;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Mesh
- * Method: _NativeGetPresenceFlags
- * Signature: (JJ)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Mesh__1NativeGetPresenceFlags
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext, jlong jvmindex)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- // get the corresponding mesh
- ai_assert(jvmindex < pcImp->GetScene()->mNumMeshes);
- aiMesh* pcMesh = pcImp->GetScene()->mMeshes[jvmindex];
- // now build the flag list
- jint iRet = 0;
- if (pcMesh->HasPositions())
- iRet |= assimp_Mesh_PF_POSITION;
- if (pcMesh->HasBones())
- iRet |= assimp_Mesh_PF_BONES;
- if (pcMesh->HasNormals())
- iRet |= assimp_Mesh_PF_NORMAL;
- if (pcMesh->HasTangentsAndBitangents())
- iRet |= assimp_Mesh_PF_TANGENTBITANGENT;
- unsigned int i = 0;
- while (pcMesh->HasTextureCoords(i))
- iRet |= assimp_Mesh_PF_UVCOORD << i++;
- i = 0;
- while (pcMesh->HasVertexColors(i))
- iRet |= assimp_Mesh_PF_VERTEXCOLOR << i++;
- return iRet;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Mesh
- * Method: _NativeGetNumVertices
- * Signature: (JJ)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Mesh__1NativeGetNumVertices
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext, jlong jvmindex)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- // get the corresponding mesh
- ai_assert(jvmindex < pcImp->GetScene()->mNumMeshes);
- return pcImp->GetScene()->mMeshes[jvmindex]->mNumVertices;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Mesh
- * Method: _NativeGetNumFaces
- * Signature: (JJ)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Mesh__1NativeGetNumFaces
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext, jlong jvmindex)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- // get the corresponding mesh
- ai_assert(jvmindex < pcImp->GetScene()->mNumMeshes);
- return pcImp->GetScene()->mMeshes[jvmindex]->mNumFaces;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Mesh
- * Method: _NativeGetNumBones
- * Signature: (JJ)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Mesh__1NativeGetNumBones
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext, jlong jvmindex)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- // get the corresponding mesh
- ai_assert(jvmindex < pcImp->GetScene()->mNumMeshes);
- return pcImp->GetScene()->mMeshes[jvmindex]->mNumBones;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Mesh
- * Method: _NativeGetMaterialIndex
- * Signature: (JJ)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Mesh__1NativeGetMaterialIndex
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext, jlong jvmindex)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- // get the corresponding mesh
- ai_assert(jvmindex < pcImp->GetScene()->mNumMeshes);
- return pcImp->GetScene()->mMeshes[jvmindex]->mMaterialIndex;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Mesh
- * Method: _NativeGetNumUVComponents
- * Signature: (JJ[I)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Mesh__1NativeGetNumUVComponents
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext, jlong jvmindex, jintArray jvmout)
- {
- ai_assert(AI_MAX_NUMBER_OF_TEXTURECOORDS == jvmenv->GetArrayLength(jvmout) );
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- // get the corresponding mesh
- ai_assert(jvmindex < pcImp->GetScene()->mNumMeshes);
- const unsigned int* piArray = pcImp->GetScene()->mMeshes[jvmindex]->mNumUVComponents;
- jint* pArray = jvmenv->GetIntArrayElements(jvmout,NULL);
- if (NULL == pArray)return AI_JNI_ERROR_RETURN;
- for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
- pArray[i] = piArray[i];
- jvmenv->ReleaseIntArrayElements(jvmout,pArray,0);
- return 0;
- }
- // ------------------------------------------------------------------------------------------------
- void CpyVectorToFloatArray(jfloat* pDest, const aiVector3D* pSource, unsigned int iNum)
- {
- jfloat* pCursor = pDest;
- const aiVector3D* const pvEnd = pSource + iNum;
- while (pvEnd != pSource)
- {
- *pCursor++ = pSource->x;
- *pCursor++ = pSource->y;
- *pCursor++ = pSource->z;
- ++pSource;
- }
- return;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Mesh
- * Method: _NativeMapVertices
- * Signature: (JJ[F)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Mesh__1NativeMapVertices
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext, jlong jvmindex, jfloatArray jvmout)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- // get the corresponding mesh
- ai_assert(jvmindex < pcImp->GetScene()->mNumMeshes);
- aiMesh* pcMesh = (aiMesh*)pcImp->GetScene()->mMeshes[jvmindex];
- ai_assert(jvmenv->GetArrayLength(jvmout) == pcMesh->mNumVertices * 3);
- const aiVector3D* pcData = pcMesh->mVertices;
- // now copy the data to the java array
- jfloat* pArray = jvmenv->GetFloatArrayElements(jvmout,NULL);
- CpyVectorToFloatArray(pArray,pcData,pcMesh->mNumVertices);
- jvmenv->ReleaseFloatArrayElements(jvmout,pArray,0);
- // delete the original data
- delete[] pcMesh->mVertices;
- pcMesh->mVertices = NULL;
- return 0;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Mesh
- * Method: _NativeMapNormals
- * Signature: (JJ[F)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Mesh__1NativeMapNormals
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext, jlong jvmindex, jfloatArray jvmout)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- // get the corresponding mesh
- ai_assert(jvmindex < pcImp->GetScene()->mNumMeshes);
- aiMesh* pcMesh = (aiMesh*)pcImp->GetScene()->mMeshes[jvmindex];
- ai_assert(jvmenv->GetArrayLength(jvmout) == pcMesh->mNumVertices * 3);
- const aiVector3D* pcData = pcMesh->mNormals;
- // now copy the data to the java array
- jfloat* pArray = jvmenv->GetFloatArrayElements(jvmout,NULL);
- CpyVectorToFloatArray(pArray,pcData,pcMesh->mNumVertices);
- jvmenv->ReleaseFloatArrayElements(jvmout,pArray,0);
- // delete the original data
- delete[] pcMesh->mNormals;
- pcMesh->mNormals = NULL;
- return 0;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Mesh
- * Method: _NativeMapTangents
- * Signature: (JJ[F)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Mesh__1NativeMapTangents
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext, jlong jvmindex, jfloatArray jvmout)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- // get the corresponding mesh
- ai_assert(jvmindex < pcImp->GetScene()->mNumMeshes);
- aiMesh* pcMesh = (aiMesh*)pcImp->GetScene()->mMeshes[jvmindex];
- ai_assert(jvmenv->GetArrayLength(jvmout) == pcMesh->mNumVertices * 3);
- const aiVector3D* pcData = pcMesh->mTangents;
- // now copy the data to the java array
- jfloat* pArray = jvmenv->GetFloatArrayElements(jvmout,NULL);
- CpyVectorToFloatArray(pArray,pcData,pcMesh->mNumVertices);
- jvmenv->ReleaseFloatArrayElements(jvmout,pArray,0);
- // delete the original data
- delete[] pcMesh->mNormals;
- pcMesh->mNormals = NULL;
- return 0;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Mesh
- * Method: _NativeMapBitangents
- * Signature: (JJ[F)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Mesh__1NativeMapBitangents
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext, jlong jvmindex, jfloatArray jvmout)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- // get the corresponding mesh
- ai_assert(jvmindex < pcImp->GetScene()->mNumMeshes);
- aiMesh* pcMesh = (aiMesh*)pcImp->GetScene()->mMeshes[jvmindex];
- ai_assert(jvmenv->GetArrayLength(jvmout) == pcMesh->mNumVertices * 3);
- const aiVector3D* pcData = pcMesh->mBitangents;
- // now copy the data to the java array
- jfloat* pArray = jvmenv->GetFloatArrayElements(jvmout,NULL);
- CpyVectorToFloatArray(pArray,pcData,pcMesh->mNumVertices);
- jvmenv->ReleaseFloatArrayElements(jvmout,pArray,0);
- // delete the original data
- delete[] pcMesh->mBitangents;
- pcMesh->mBitangents = NULL;
- return 0;
- }
- // ------------------------------------------------------------------------------------------------
- void CpyColorToFloatArray(jfloat* pDest, const aiColor4D* pSource, unsigned int iNum)
- {
- jfloat* pCursor = pDest;
- const aiColor4D* const pvEnd = pSource + iNum;
- while (pvEnd != pSource)
- {
- *pCursor++ = pSource->r;
- *pCursor++ = pSource->g;
- *pCursor++ = pSource->b;
- *pCursor++ = pSource->a;
- ++pSource;
- }
- return;
- }
- // ------------------------------------------------------------------------------------------------
- void CpyVectorToFloatArray(jfloat* pDest, const aiVector3D* pSource,
- unsigned int iNum, unsigned int iNumComponents)
- {
- jfloat* pCursor = pDest;
- const aiVector3D* const pvEnd = pSource + iNum;
- while (pvEnd != pSource)
- {
- if (iNumComponents >= 1)*pCursor++ = pSource->x;
- if (iNumComponents >= 2)*pCursor++ = pSource->y;
- if (iNumComponents >= 3)*pCursor++ = pSource->z;
- ++pSource;
- }
- return;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Mesh
- * Method: _NativeMapUVs
- * Signature: (JJI[F)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Mesh__1NativeMapUVs
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext, jlong jvmindex,
- jint jvmchannel, jfloatArray jvmout)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- // get the corresponding mesh
- ai_assert(jvmindex < pcImp->GetScene()->mNumMeshes);
- aiMesh* pcMesh = (aiMesh*)pcImp->GetScene()->mMeshes[jvmindex];
- ai_assert(jvmenv->GetArrayLength(jvmout) == pcMesh->mNumVertices*pcMesh->mNumUVComponents[jvmchannel]);
- const aiVector3D* pcData = pcMesh->mTextureCoords[jvmchannel];
- // now copy the data to the java array
- jfloat* pArray = jvmenv->GetFloatArrayElements(jvmout,NULL);
- CpyVectorToFloatArray(pArray,pcData,pcMesh->mNumVertices,pcMesh->mNumUVComponents[jvmchannel]);
- jvmenv->ReleaseFloatArrayElements(jvmout,pArray,0);
- // delete the original data
- delete[] pcMesh->mTextureCoords[jvmchannel];
- pcMesh->mTextureCoords[jvmchannel] = NULL;
- return 0;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Mesh
- * Method: _NativeMapColors
- * Signature: (JJI[F)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Mesh__1NativeMapColors
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext, jlong jvmindex,
- jint jvmchannel, jfloatArray jvmout)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- // get the corresponding mesh
- ai_assert(jvmindex < pcImp->GetScene()->mNumMeshes);
- aiMesh* pcMesh = (aiMesh*)pcImp->GetScene()->mMeshes[jvmindex];
- ai_assert(jvmenv->GetArrayLength(jvmout) == pcMesh->mNumVertices*4);
- const aiColor4D* pcData = pcMesh->mColors[jvmchannel];
- // now copy the data to the java array
- jfloat* pArray = jvmenv->GetFloatArrayElements(jvmout,NULL);
- CpyColorToFloatArray(pArray,pcData,pcMesh->mNumVertices);
- jvmenv->ReleaseFloatArrayElements(jvmout,pArray,0);
- // delete the original data
- delete[] pcMesh->mColors[jvmchannel];
- pcMesh->mColors[jvmchannel] = NULL;
- return 0;
- }
- // ------------------------------------------------------------------------------------------------
- void CpyFacesToIntArray(jint* pDest, const aiFace* pSource, unsigned int iNum)
- {
- // assume that all faces are triangles
- jint* pCursor = pDest;
- const aiFace* const pvEnd = pSource + iNum;
- while (pvEnd != pSource)
- {
- *pCursor++ = pSource->mIndices[0];
- *pCursor++ = pSource->mIndices[1];
- *pCursor++ = pSource->mIndices[2];
- ++pSource;
- }
- return;
- }
- // ------------------------------------------------------------------------------------------------
- /*
- * Class: assimp_Mesh
- * Method: _NativeMapFaces
- * Signature: (JJ[I)I
- */
- JNIEXPORT jint JNICALL Java_assimp_Mesh__1NativeMapFaces
- (JNIEnv *jvmenv, jobject jvmthis, jlong jvmcontext, jlong jvmindex, jintArray jvmout)
- {
- // we need a valid scene for this
- Assimp::Importer* pcImp = jGetValidImporterScenePair(jvmcontext);
- if (!pcImp)return AI_JNI_ERROR_RETURN;
- // get the corresponding mesh
- ai_assert(jvmindex < pcImp->GetScene()->mNumMeshes);
- aiMesh* pcMesh = (aiMesh*)pcImp->GetScene()->mMeshes[jvmindex];
- ai_assert(jvmenv->GetArrayLength(jvmout) == pcMesh->mNumFaces*3);
- const aiFace* pcData = pcMesh->mFaces;
- // now copy the data to the java array
- jint* pArray = jvmenv->GetIntArrayElements(jvmout,NULL);
- CpyFacesToIntArray(pArray,pcData,pcMesh->mNumFaces);
- jvmenv->ReleaseIntArrayElements(jvmout,pArray,0);
- // delete the original data
- for (unsigned int i = 0; i < pcMesh->mNumFaces;++i)
- delete[] pcMesh->mFaces[i].mIndices;
- delete[] pcMesh->mFaces;
- pcMesh->mFaces = NULL;
- return 0;
- }
- }; //! namespace JNIBridge
- }; //! namespace Assimp
- #endif // !ASSIMP_JNI_EXPORT
|