Log in or register to post comments

Rendering 2D object tracking Target in Java

January 1, 2015 - 5:34am #1

Hello ,

I'm using Vuforia samples 3.0

And I need to render 2D object tracking  target positions instead of TeaPot

I read this intersting guide :

Textured Plane on Target

https://developer.vuforia.com/resources/dev-guide/textured-plane-target

But I found difficulties in translating code from c++ to java because there are many changes in functions names and code architecture generally .

Please help changing this code to java compatible with vuforia samples 3.0

// assuming this is an image target
 
QCAR::Matrix44F modelViewProjection;
QCAR::Vec2F targetSize = ((QCAR::ImageTarget *) &trackable)->getSize();
 
SampleUtils::translatePoseMatrix(0.0f, 0.0f, kObjectScale, &modelViewMatrix.data[0]);
//SampleUtils::scalePoseMatrix(kObjectScale, kObjectScale, kObjectScale, &modelViewMatrix.data[0]);
SampleUtils::scalePoseMatrix(targetSize.data[0], targetSize.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*) &teapotVertices[0]);
//glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &teapotNormals[0]);
//glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &teapotTexCoords[0]);
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);
glUniform1i(texSampler2DHandle, 0 /*GL_TEXTURE0*/);
 
glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (GLfloat*) &modelViewProjection.data[0]);
 
//glDrawElements(GL_TRIANGLES, NUM_TEAPOT_OBJECT_INDEX, GL_UNSIGNED_SHORT, (const GLvoid*) &teapotIndices[0]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (const GLvoid*) &planeIndices[0]);

 

Rendering 2D object tracking Target in Java

January 6, 2015 - 7:18am #6
but I need to make 2D object track marker not 3D !
 
From an OpenGL programming standpoint, there is no difference: in both cases you have a mesh represented by a vertex array.
 
 
How can I get bufffer arrays for 2D rectangular ?!!
See my previous reply (the code in Teapot.java sample code shows how to create a Buffer from an array, see the mVertBuffer variable and how it is used for instance)
 
The article you referred to just use a float arrays while function applicable for ONLY int !
as I said, the glVertexAttribPointer takes a Buffer as the last argument (see the sample code in my previous post); 
 
 
 
 

Rendering 2D object tracking Target in Java

January 6, 2015 - 6:26am #5

Thanks for response .. but I need to make 2D object track marker not 3D !

How can I get bufffer arrays for 2D rectangular ?!!

The article you referred to just use a float arrays while function applicable for ONLY int !

Please Help !

private final float[] mVertices = new float[] {
        -0.5f, -0.5f, 0.0f, //bottom-left corner
         0.5f, -0.5f, 0.0f, //bottom-right corner
         0.5f,  0.5f, 0.0f, //top-right corner
        -0.5f,  0.5f, 0.0f  //top-left corner
    };
     
    private final float[] mNormals = new float[] {
         0.0f, 0.0f, 1.0f, //normal at bottom-left corner
         0.0f, 0.0f, 1.0f, //normal at bottom-right corner
         0.0f, 0.0f, 1.0f, //normal at top-right corner
         0.0f, 0.0f, 1.0f  //normal at top-left corner
    };
     
    private final float[] mTexCoords = new float[] {
        0.0f, 0.0f, //tex-coords at bottom-left corner
        1.0f, 0.0f, //tex-coords at bottom-right corner
        1.0f, 1.0f, //tex-coords at top-right corner
        0.0f, 1.0f  //tex-coords at top-left corner
    };
     
    private final short[] mIndices = new short[] {
     0,1,2, //triangle 1
     2,3,0 // triangle 2
    };

 

Rendering 2D object tracking Target in Java

January 6, 2015 - 1:56am #4

That's because in Java with OpenGL you need to pass a Buffer (such as ByteBuffer for instance) ad argument to the GLES20.glVertexAttribPointer

Have a look at the Teapot.java source code in the Vuforia Samples to see how a Buffer can be built from an array in Java, then look at the code in ImageTargetsRenderer.java (renderFrame method) to see how it is used in the GLES20.glVertexAttribPointer function.

See this sample code:

GLES20.glVertexAttribPointer(vertexHandle, 3, GLES20.GL_FLOAT,

                    false, 0, mTeapot.getVertices());
                GLES20.glVertexAttribPointer(normalHandle, 3, GLES20.GL_FLOAT,
                    false, 0, mTeapot.getNormals());
                GLES20.glVertexAttribPointer(textureCoordHandle, 2,
                    GLES20.GL_FLOAT, false, 0, mTeapot.getTexCoords());

Rendering 2D object tracking Target in Java

January 6, 2015 - 1:47am #3

Thanks It helps in a good way but

There is a problem in getting the corresponding java for plane block of code ..as the java functions require int type arrays not double

static const float planeVertices[] =
{
    -0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.5, 0.5, 0.0, -0.5, 0.5, 0.0,
};
static const float planeTexcoords[] =
{
    0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0
};
static const float planeNormals[] =
{
    0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0
};
static const unsigned short planeIndices[] =
{
    0, 1, 2, 0, 2, 3
};

When I replace it with 

	static final double planeVertices[] = { -0.5, -0.5, 0.0, 0.5, -0.5, 0.0,
			0.5, 0.5, 0.0, -0.5, 0.5, 0.0, };
	static final double planeTexcoords[] = { 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0,
			1.0 };
	static final double planeNormals[] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
			0.0, 1.0, 0.0, 0.0, 1.0 };
	static final int planeIndices[] = { 0, 1, 2, 0, 2, 3 };

syntax errors appears here .. glVertexAttribPointer not applicable for double value

				GLES20.glVertexAttribPointer(vertexHandle, 3, GLES20.GL_FLOAT,false, 0,  planeVertices[0]);
				GLES20.glVertexAttribPointer(normalHandle, 3, GLES20.GL_FLOAT,false, 0,  planeNormals[0]);
				GLES20.glVertexAttribPointer(textureCoordHandle, 2,GLES20.GL_FLOAT, false,0, planeTexcoords[0]);

How can we get the plane arrays for 2D object in Java ?!

 

Rendering 2D object tracking Target in Java

January 4, 2015 - 6:25am #2

You can probably do the Java conversion by looking at this article, which provides a more general guide to replacing the teapot model with a custom geometry using Java and C++:

https://developer.vuforia.com/resources/dev-guide/replacing-teapot

 

Log in or register to post comments