Log in or register to post comments

Create and render dynamic texture text reco sample

February 22, 2014 - 12:30am #1

I am working with vuforia sdk (text reco demo). I would like to create dynamic textures for each text that is recognised and display the texture over this text. i create the texture in java. I tried using the image recognition function to create dynamic textures but couldnt do it. Any idea what i am doing wrong?

 

JNIEXPORT void JNICALL
Java_com_busolo_augmented_tarnslatar_AppRenderer_renderFrame(JNIEnv * env, jobject obj)
{
    jclass rendererJavaClass = env->GetObjectClass(obj);
 
    // Clear color and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
    // Get the state from QCAR and mark the beginning of a rendering section
    QCAR::State state = QCAR::Renderer::getInstance().begin();
 
    // Explicitly render the Video Background
    QCAR::Renderer::getInstance().drawVideoBackground();
 
    glEnable(GL_DEPTH_TEST);
 
    glEnable(GL_CULL_FACE);
 
    glCullFace(GL_BACK);
 
    if(QCAR::Renderer::getInstance().getVideoBackgroundConfig().mReflection == QCAR::VIDEO_BACKGROUND_REFLECTION_ON)
        glFrontFace(GL_CW);  //Front camera
    else
        glFrontFace(GL_CCW);   //Back camera
 
   env->CallVoidMethod(obj, env->GetMethodID(rendererJavaClass, "wordsStartLoop", "()V"));
 
        NbWordsFound = 0;
 
        textureCount = state.getNumTrackableResults();
        textures = new Texture*[textureCount];
 
        jmethodID getTextureMethodID = env->GetMethodID(rendererJavaClass,"getTexture", "(II)L");
 
   // Did we find any trackables this frame?
   for(int tIdx = 0; tIdx < textureCount; tIdx++)
   {
    int wordWidth = 0;
    int wordHeight = 0;
    int wordX = 0;
    int wordY = 0;
 
       // Get the trackable:
       const QCAR::TrackableResult* result = state.getTrackableResult(tIdx);
       const QCAR::Trackable& trackable = result->getTrackable();
 
       QCAR::Matrix44F modelViewMatrix = QCAR::Tool::convertPose2GLMatrix(result->getPose());
 
       QCAR::Vec2F wordBoxSize(0, 0);
 
       if (result->getType() == QCAR::TrackableResult::WORD_RESULT)
       {
           const QCAR::WordResult* wordResult = (const QCAR::WordResult*) result;
           // Get the word
           const QCAR::Word& word = wordResult->getTrackable();
           const QCAR::Obb2D& obb = wordResult->getObb();
           wordBoxSize = word.getSize();
 
           wordWidth = wordBoxSize.data[0];
           wordHeight = wordBoxSize.data[1];
 
           if (word.getStringU())
           {
               float wordx = - obb.getCenter().data[1];
               float wordy = obb.getCenter().data[0];
 
               // For debugging purposes convert the string to 7bit ASCII
               // (if possible) and log it.
               char* stringA = 0;
               if (unicodeToAscii(word, stringA))
               {
                   // we store the word
                   if (NbWordsFound < MAX_NB_WORDS)
                   {
                       struct WordDesc * word =  & WordsFound[NbWordsFound];
                       NbWordsFound++;
                       strncpy(word->text, stringA, MAX_WORD_LENGTH - 1);
                       word->text[MAX_WORD_LENGTH - 1] = '\0';
                       word->Ax = wordx - (int)(wordBoxSize.data[0] / 2);
                       word->Ay = wordy - (int)(wordBoxSize.data[1] / 2);
                       word->Bx = wordx + (int)(wordBoxSize.data[0] / 2);
                       word->By = wordy + (int)(wordBoxSize.data[1] / 2);
 
                   wordX = word->Ax;
                   wordY = word->Ay;
                   }
 
                   delete[] stringA;
               }
 
           }
       }
       else
       {
           LOG("Unexpected detection:%d", result->getType());
           continue;
       }
       QCAR::Matrix44F modelViewProjection;
 
       AppUtilities::translatePoseMatrix(0.0f, 0.0f, 0.0f, &modelViewMatrix.data[0]);//4 by 4 matrix
       AppUtilities::scalePoseMatrix(wordBoxSize.data[0] + TEXTBOX_PADDING, wordBoxSize.data[1] + TEXTBOX_PADDING, 1.0f, &modelViewMatrix.data[0]);
       AppUtilities::multiplyMatrix(&projectionMatrix.data[0], &modelViewMatrix.data[0] ,&modelViewProjection.data[0]);
 
       jobject textureObject = env->CallObjectMethod(obj, getTextureMethodID, wordWidth, wordHeight);
       textures[tIdx] = Texture::create(env, textureObject);
 
       if (textureObject == NULL)
       {
        LOG("GetTexture() returned zero pointer");
           return;
       }
 
       glGenTextures(1, &(textures[tIdx]->mTextureID));
       glBindTexture(GL_TEXTURE_2D, textures[tIdx]->mTextureID);
       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textures[tIdx]->mWidth,
                textures[tIdx]->mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
                (GLvoid*)  textures[tIdx]->mData);
 
       glUseProgram(shaderProgramID);
 
       glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
                             (const GLvoid*) &planeVertices[0]);
       glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
                             (const GLvoid*) &planeNormals[0]);
       glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
                             (const GLvoid*) &planeTexcoords[0]);
 
       glEnableVertexAttribArray(vertexHandle);
       glEnableVertexAttribArray(normalHandle);
       glEnableVertexAttribArray(textureCoordHandle);
 
       glActiveTexture(GL_TEXTURE0);
       glBindTexture(GL_TEXTURE_2D, textures[tIdx]->mTextureID);
       glUniform1i(texSampler2DHandle, 0 /*GL_TEXTURE0*/);
       glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
                          (GLfloat*)&modelViewProjection.data[0] );
       glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT,
                      (const GLvoid*) &planeIndices[0]);
 
       glDisableVertexAttribArray(vertexHandle);
       glDisableVertexAttribArray(normalHandle);
       glDisableVertexAttribArray(textureCoordHandle);
   }
 
   if (NbWordsFound > 0)
   {
       jmethodID method = env->GetMethodID(rendererJavaClass, "addWord", "(Ljava/lang/String;)V");
 
       // we order the words per line and left to right
       qsort(& WordsFound[0], NbWordsFound, sizeof(struct WordDesc), wordDescCompare);
       for(int i = 0 ; i < NbWordsFound ; i++)
       {
           struct WordDesc * word =  & WordsFound[i];
           jstring js = env->NewStringUTF(word->text);
           env->CallVoidMethod(obj, method, js);
       }
   }
 
   env->CallVoidMethod(obj, env->GetMethodID(rendererJavaClass, "wordsEndLoop", "()V"));
 
   glDisable(GL_DEPTH_TEST);
 
   drawRegionOfInterest(ROICenterX, ROICenterY, ROIWidth, ROIHeight);
 
   // Disable blending (restore default state)
   glDisable(GL_BLEND);
 
   QCAR::Renderer::getInstance().end();
}

Create and render dynamic texture text reco sample

February 23, 2014 - 1:06am #2

on the Vuforia side, once you get the Pose from the trackable result, you can then render anything you like on top of it (i.e. with the modelview matrix). The rest is  just simply and purely about OpenGL programming, so the best is to look at OpenGL dedicated forums.

 

Log in or register to post comments