Add ProgressHandler support.

pull/2295/head
Marc Kurtz 2019-01-08 11:17:11 -05:00
parent e06b1d0de2
commit d2f6c6d65e
4 changed files with 95 additions and 9 deletions

View File

@ -1,6 +1,7 @@
#include "jassimp.h" #include "jassimp.h"
#include <assimp/Importer.hpp> #include <assimp/Importer.hpp>
#include <assimp/ProgressHandler.hpp>
#include <assimp/scene.h> #include <assimp/scene.h>
#include <assimp/IOStream.hpp> #include <assimp/IOStream.hpp>
#include <assimp/IOSystem.hpp> #include <assimp/IOSystem.hpp>
@ -248,7 +249,7 @@ static bool call(JNIEnv *env, jobject object, const char* typeName, const char*
return false; return false;
} }
jboolean jReturnValue = env->CallBooleanMethod(object, mid, params[0].l); jboolean jReturnValue = env->CallBooleanMethodA(object, mid, params);
return (bool)jReturnValue; return (bool)jReturnValue;
} }
@ -591,6 +592,24 @@ class JavaIOSystem : public Assimp::IOSystem {
}; };
class JavaProgressHandler : public Assimp::ProgressHandler {
private:
JNIEnv* mJniEnv;
jobject& mJavaProgressHandler;
public:
JavaProgressHandler(JNIEnv* env, jobject& javaProgressHandler) :
mJniEnv(env),
mJavaProgressHandler(javaProgressHandler)
{};
bool Update(float percentage)
{
jvalue params[1];
params[0].f = percentage;
return call(mJniEnv, mJavaProgressHandler, "jassimp/AiProgressHandler", "update", "(F)Z", params);
}
};
static bool loadMeshes(JNIEnv *env, const aiScene* cScene, jobject& jScene) static bool loadMeshes(JNIEnv *env, const aiScene* cScene, jobject& jScene)
{ {
@ -1880,7 +1899,7 @@ JNIEXPORT jstring JNICALL Java_jassimp_Jassimp_getErrorString
JNIEXPORT jobject JNICALL Java_jassimp_Jassimp_aiImportFile JNIEXPORT jobject JNICALL Java_jassimp_Jassimp_aiImportFile
(JNIEnv *env, jclass jClazz, jstring jFilename, jlong postProcess, jobject ioSystem) (JNIEnv *env, jclass jClazz, jstring jFilename, jlong postProcess, jobject ioSystem, jobject progressHandler)
{ {
jobject jScene = NULL; jobject jScene = NULL;
@ -1896,6 +1915,11 @@ JNIEXPORT jobject JNICALL Java_jassimp_Jassimp_aiImportFile
lprintf("Created aiFileIO\n"); lprintf("Created aiFileIO\n");
} }
if(progressHandler != NULL)
{
imp.SetProgressHandler(new JavaProgressHandler(env, progressHandler));
}
lprintf("opening file: %s\n", cFilename); lprintf("opening file: %s\n", cFilename);
/* do import */ /* do import */

View File

@ -39,7 +39,7 @@ JNIEXPORT jstring JNICALL Java_jassimp_Jassimp_getErrorString
* Signature: (Ljava/lang/String;J)Ljassimp/AiScene; * Signature: (Ljava/lang/String;J)Ljassimp/AiScene;
*/ */
JNIEXPORT jobject JNICALL Java_jassimp_Jassimp_aiImportFile JNIEXPORT jobject JNICALL Java_jassimp_Jassimp_aiImportFile
(JNIEnv *, jclass, jstring, jlong, jobject); (JNIEnv *, jclass, jstring, jlong, jobject, jobject);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -0,0 +1,46 @@
/*
---------------------------------------------------------------------------
Open Asset Import Library - Java Binding (jassimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2012, assimp 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 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.
---------------------------------------------------------------------------
*/
package jassimp;
public interface AiProgressHandler
{
boolean update(float percentage);
}

View File

@ -69,7 +69,8 @@ public final class Jassimp {
* @throws IOException if an error occurs * @throws IOException if an error occurs
*/ */
private static native AiScene aiImportFile(String filename, private static native AiScene aiImportFile(String filename,
long postProcessing, AiIOSystem<?> ioSystem) throws IOException; long postProcessing, AiIOSystem<?> ioSystem,
AiProgressHandler progressHandler) throws IOException;
/** /**
@ -158,11 +159,26 @@ public final class Jassimp {
public static AiScene importFile(String filename, public static AiScene importFile(String filename,
Set<AiPostProcessSteps> postProcessing, AiIOSystem<?> ioSystem) Set<AiPostProcessSteps> postProcessing, AiIOSystem<?> ioSystem)
throws IOException { throws IOException {
return importFile(filename, postProcessing, ioSystem, null);
}
/**
* Imports a file via assimp.
*
* @param filename the file to import
* @param postProcessing post processing flags
* @param ioSystem ioSystem to load files, or null for default
* @return the loaded scene, or null if an error occurred
* @throws IOException if an error occurs
*/
public static AiScene importFile(String filename,
Set<AiPostProcessSteps> postProcessing, AiIOSystem<?> ioSystem,
AiProgressHandler progressHandler) throws IOException {
loadLibrary(); loadLibrary();
return aiImportFile(filename, AiPostProcessSteps.toRawValue( return aiImportFile(filename, AiPostProcessSteps.toRawValue(
postProcessing), ioSystem); postProcessing), ioSystem, progressHandler);
} }