You should only call QCAR::Renderer::getInstance().begin() from the renderFrame method. In general, you get the state object in the renderFrame method and check the visible trackable name there. Then you can save some information to a global variable like "check" for use in other methods.
Here are some code changes I made that worked:
1) Added a global variable to ImageTargets.cpp:
int check = 0;
2) Added the following method to ImageTargets.cpp:
JNIEXPORT jstring JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargetsRenderer_stringFromJNI2(JNIEnv *env,jobject obj,jstring javaString)
{
if (check==1)
{
return env->NewStringUTF("http://u5088014.polppol.com/file/entrance.mp3");
}
else
{
return env->NewStringUTF("Nothing visible");
}
}
3) Added some code to the renderFrame method in ImageTargets.cpp:
JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargetsRenderer_renderFrame(JNIEnv *, jobject)
{
check = 0;
...
// Did we find any trackables this frame?
for(int tIdx = 0; tIdx < state.getNumActiveTrackables(); tIdx++)
{
// Get the trackable:
const QCAR::Trackable* trackable = state.getActiveTrackable(tIdx);
QCAR::Matrix44F modelViewMatrix =
QCAR::Tool::convertPose2GLMatrix(trackable->getPose());
// Choose the texture based on the target name:
int textureIndex = (!strcmp(trackable->getName(), "stones")) ? 0 : 1;
const Texture* const thisTexture = textures[textureIndex];
if(strcmp(trackable->getName(), "stones") == 0)
{
check = 1;
}
...
}
...
QCAR::Renderer::getInstance().end();
}
4) Added the native method signature to ImageTargetsRenderer.java:
public native String stringFromJNI2(String s);
5) Added a call to stringFromJNI2 just after the renderFrame method is called in ImageTargetRenderer.java:
public void onDrawFrame(GL10 gl)
{
if (!mIsActive)
return;
// Call our native function to render content
renderFrame();
DebugLog.LOGI("string from native: " + stringFromJNI2("test"));
}
You can see the output in the log, switch to the DDMS perspective in Eclipse and look at the LogCat view.
You can call the stringFromJNI2 native method from another Java class, just be sure to update the native method signature with the new class name:
JNIEXPORT jstring JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_MyJavaClass_stringFromJNI2(JNIEnv *env,jobject obj,jstring javaString)
- Kim
Thank you Kim :)