Log in or register to post comments

Replacing teapot with image

August 13, 2016 - 1:35pm #1

Hi, there are a lot of topics about this but they have almost all broken links.



I was following this:

https://library.vuforia.com/articles/Solution/How-To-Draw-a-2D-image-on-top-of-a-target-using-OpenGL-ES



And  had a LOT of errors. 

Now after struggling for a couple of hours i finally managed to get it working.

This is the updated code from ImageTargets.cpp which you need now if you want to display a flat 2D image instead of the teapot. I'm referring to the native sample of course. So take the old one from the above link and replace the renderFrame method. 

Please note that it worked only once i removed the glVertexAttribPointer(normalHandle ... statement, as the teapot model no longer used it too.

To replace the image with your own image, just work on the java loadTextures() method as usual.

---

 

 

JNIEXPORT void JNICALL

Java_com_vuforia_samples_ImageTargets_ImageTargetsRenderer_renderFrame(JNIEnv *, jobject)

{

    //LOG("Java_com_vuforia_samples_ImageTargets_GLRenderer_renderFrame");

    // Clear color and depth buffer

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Get the state from Vuforia and mark the beginning of a rendering section

    Vuforia::State state = Vuforia::Renderer::getInstance().begin();

    // Explicitly render the Video Background

    Vuforia::Renderer::getInstance().drawVideoBackground();

    glEnable(GL_DEPTH_TEST);

    // We must detect if background reflection is active and adjust the culling direction.

    // If the reflection is active, this means the post matrix has been reflected as well,

    // therefore standard counter clockwise face culling will result in "inside out" models.

    glEnable(GL_CULL_FACE);

    glCullFace(GL_BACK);

    if(Vuforia::Renderer::getInstance().getVideoBackgroundConfig().mReflection == Vuforia::VIDEO_BACKGROUND_REFLECTION_ON)

        glFrontFace(GL_CW);  //Front camera

    else

        glFrontFace(GL_CCW);   //Back camera

    // Set the viewport

    glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);

    // Did we find any trackables this frame?

    for(int tIdx = 0; tIdx < state.getNumTrackableResults(); tIdx++)

    {

        // Get the trackable:

        const Vuforia::TrackableResult* result = state.getTrackableResult(tIdx);

        const Vuforia::Trackable& trackable = result->getTrackable();

        Vuforia::Matrix44F modelViewMatrix = Vuforia::Tool::convertPose2GLMatrix(result->getPose());

        if(!isExtendedTrackingActivated)

        {

            // Choose the texture based on the target name:

            int textureIndex;

            if (strcmp(trackable.getName(), "chips") == 0)

            {

                textureIndex = 0;

            }

            else if (strcmp(trackable.getName(), "stones") == 0)

            {

                textureIndex = 1;

            }

            else

            {

                textureIndex = 2;

            }

            const Texture* const thisTexture = textures[textureIndex];

            Vuforia::Matrix44F modelViewProjection;

            // assuming this is an image target

            Vuforia::Vec3F target3Size = ((Vuforia::ImageTarget &) trackable).getSize();

            glUseProgram(shaderProgramID);

            SampleUtils::translatePoseMatrix(0.0f, 0.0f, kObjectScale, &modelViewMatrix.data[0]);

            SampleUtils::scalePoseMatrix(target3Size.data[0], target3Size.data[1], 1.0f, &modelViewMatrix.data[0]);

            SampleUtils::multiplyMatrix(&projectionMatrix.data[0],

                                       &modelViewMatrix.data[0] ,

                                       &modelViewProjection.data[0]);

            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, thisTexture->mTextureID);

            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);

            SampleUtils::checkGlError("ImageTargets renderFrame");

        }

    }

    glDisable(GL_DEPTH_TEST);

    Vuforia::Renderer::getInstance().end();



    //

}

 

...

Log in or register to post comments