Log in or register to post comments

Draw a 2D Image on a marker using OpenGL ES

November 5, 2017 - 11:16pm #1

I'm trying to display a 2D Image/Photo on top of a marker using OpenGL ES by following the tutorial in the following link:

https://library.vuforia.com/content/vuforia-library/en/articles/Solution/Working-with-Vuforia-and-OpenGL-ES.html#How-To-Draw-a-2D-Image-on-top-of-a-Target-using-OpenGL-ES

However, I'm facing the following compilation error caused by the line

Vuforia::Vec2F targetSize = ((Vuforia::ImageTarget *) trackable)->getSize();

 

Error:(329, 53) error: expected expression

Error:(329, 40) error: no member named 'ImageTarget' in namespace 'Vuforia'

Error:Execution failed for task ':app:buildNative'.

> Process 'command 'C:\Users\monkeyhanny\Development\ndk-bundle/ndk-build.cmd'' finished with non-zero exit value 2

 

I'm testing on the ImageTargetsNative-6-5-19 at the moment.

 

Does anyone have any idea how to get the target size? 

Draw a 2D Image on a marker using OpenGL ES

November 7, 2017 - 6:57am #3

Hi Strasza, 

 

Thank you for the reply. I have managed to compile and run the app without any error. However, my 2D image was not displaying. I've followed the steps mentioned here by adding my own image to the asset folder, as well as replacing the file name in loadTextures() method. Any idea why this happened? 

Below is my code for renderFrame() method:

 

JNIEXPORT void JNICALL

Java_com_test_monkeyhanny_demoapp_MainRenderer_renderFrame(JNIEnv *, jobject){

         // Clear color and depth buffer

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Render video background:

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

        //glDisable(GL_TEXTURE_2D);

        glEnable(GL_TEXTURE_2D);

        glEnable(GL_DEPTH_TEST);

        glEnable(GL_CULL_FACE);

        glEnable(GL_BLEND);

        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        Vuforia::Matrix44F mainModelViewMatrix;

        Vuforia::Vec3F targetCenters[3]; // make this big enough to hold all your targets



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

                // assuming this is an image target

                Vuforia::Matrix44F modelViewMatrix =

                Vuforia::Tool::convertPose2GLMatrix(result->getPose());

                if (tIdx == 0)

                {

                    // Make the first visible target our world center (0, 0, 0)

                    // Store its modelViewMatrix and continue looking for other targets

                    mainModelViewMatrix = modelViewMatrix;

                    targetCenters[0].data[0] = 0.0f;

                    targetCenters[0].data[1] = 0.0f;

                    targetCenters[0].data[2] = 0.0f;

                }else{



                    // This is another visible target

                    // Find its center point in relation to the first target

                    // To do this we use the matrix inverse function (VuforiaMath.h from the

                    //  Dominoes project)

                    Vuforia::Matrix44F mainModelViewInverse = VuforiaMath::Matrix44FInverse

                    (mainModelViewMatrix);

                    Vuforia::Matrix44F modelViewTranspose = VuforiaMath::Matrix44FTranspose

                    (modelViewMatrix); // let's work with row-major matrices

                    Vuforia::Matrix44F offsetMatrix = Vuforia::Tool::multiply(mainModelViewInverse,

                    modelViewTranspose);

                    // Transform a point on the second target by this offset matrix

                    // (0, 0, 0) is the local center of the target

                    Vuforia::Vec4F position(0.0f, 0.0f, 0.0f, 1.0f);

                    position = VuforiaMath::Vec4FTransform(position, offsetMatrix);

                    // Add this position to our array

                    targetCenters[1].data[0] = position.data[0];

                    targetCenters[1].data[1] = position.data[1];

                    targetCenters[1].data[2] = position.data[2];

                }

                Vuforia::Vec3F targetSize = ((Vuforia::ImageTarget *) &trackable)->getSize();

                Vuforia::Matrix44F modelViewProjection;

                Utils::translatePoseMatrix(0.0f, 0.0f, kObjectScale, &mainModelViewMatrix.data[0]);

                Utils::scalePoseMatrix(targetSize.data[0], targetSize.data[1], 1.0f, &mainModelViewMatrix.data[0]);

                Utils::multiplyMatrix(&projectionMatrix.data[0], &mainModelViewMatrix.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);

                const Texture* const thisTexture = textures[0];

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



        }

        glDisable(GL_DEPTH_TEST);

        glDisable(GL_BLEND);

        glDisable(GL_TEXTURE_2D);

        glDisableVertexAttribArray(vertexHandle);

        //glDisableVertexAttribArray(normalHandle);

        glDisableVertexAttribArray(textureCoordHandle);

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

    // views and then it will call renderFrameForView per each of the views available,

    // in this case there is only one view since it is not rendering in stereo mode

       appRenderer->renderFrame();

}

 

Draw a 2D Image on a marker using OpenGL ES

November 6, 2017 - 11:43am #2

Hello monkeyhanny,

You should be able to use ObjectTarget instead for getSize:

https://library.vuforia.com/content/vuforia-library/en/reference/cpp/classVuforia_1_1ObjectTarget.html#ac1af34225cd33fb87e08d8d07688b002

Thanks,

-Vuforia Support

Log in or register to post comments