By shymaao
on Wed, 01/14/2015 - 11:29
Hello ..
I need to make like the Book example in which 2D object track target .. I face a black screen tracking target instead of my created bitmap ..
Code : ImageTargetRendrer.java
// The render function. private void renderFrame() { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); State state = mRenderer.begin(); mRenderer.drawVideoBackground(); GLES20.glEnable(GLES20.GL_DEPTH_TEST); // handle face culling, we need to detect if we are using reflection // to determine the direction of the culling GLES20.glEnable(GLES20.GL_CULL_FACE); GLES20.glCullFace(GLES20.GL_BACK); if (Renderer.getInstance().getVideoBackgroundConfig().getReflection() == VIDEO_BACKGROUND_REFLECTION.VIDEO_BACKGROUND_REFLECTION_ON) GLES20.glFrontFace(GLES20.GL_CW); // Front camera else GLES20.glFrontFace(GLES20.GL_CCW); // Back camera // did we find any trackables this frame? for (int tIdx = 0; tIdx < state.getNumTrackableResults(); tIdx++) { // Log.d("num trackables", state.getNumTrackableResults()+""); TrackableResult result = state.getTrackableResult(tIdx); Trackable trackable = result.getTrackable(); displayMessage(trackable.getName()); // printUserData(trackable); ImageTarget itarget = (ImageTarget) trackable; Matrix44F modelViewMatrix_Vuforia = Tool.convertPose2GLMatrix(result.getPose()); float[] modelViewMatrix = modelViewMatrix_Vuforia.getData(); //int textureIndex = 0; // trackable.getName().equalsIgnoreCase("stones") ? 0: 1; // textureIndex = trackable.getName().equalsIgnoreCase("tarmac") ? 2: textureIndex; // deal with the modelview and projection matrices float[] modelViewProjection = new float[16]; Matrix.translateM(modelViewMatrix, 0, 0.0f, 0.0f,OBJECT_SCALE_FLOAT); Matrix.scaleM(modelViewMatrix, 0, OBJECT_SCALE_FLOAT,OBJECT_SCALE_FLOAT, OBJECT_SCALE_FLOAT); Matrix.translateM(modelViewMatrix, 0, 0.0f, 0.0f, 0.0f); Vec2F targetSize = itarget.getSize(); Matrix.scaleM(modelViewMatrix, 0, targetSize.getData()[0], targetSize.getData()[1], 1.0f); Matrix.multiplyMM(modelViewProjection, 0, vuforiaAppSession .getProjectionMatrix().getData(), 0, modelViewMatrix, 0); // activate the shader program and bind the vertex/normal/tex coords GLES20.glUseProgram(shaderProgramID); GLES20.glDisable(GLES20.GL_CULL_FACE); GLES20.glVertexAttribPointer(vertexHandle, 3, GLES20.GL_FLOAT,false, 0, mMesh.vertices); GLES20.glVertexAttribPointer(normalHandle, 3, GLES20.GL_FLOAT,false, 0, mMesh.normals); GLES20.glVertexAttribPointer(textureCoordHandle, 2,GLES20.GL_FLOAT, false,0, mMesh.texCoords); GLES20.glEnableVertexAttribArray(vertexHandle); GLES20.glEnableVertexAttribArray(normalHandle); GLES20.glEnableVertexAttribArray(textureCoordHandle); if(ImageTargets.UIExists.equals("1")){ GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,mTextures.get(1).mTextureID[0]); GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false,modelViewProjection, 0); GLES20.glUniform1i(texSampler2DHandle, 0); GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT,mMesh.numIndices); SampleUtils.checkGLError("Render Frame"); } } GLES20.glDisable(GLES20.GL_DEPTH_TEST); mRenderer.end(); }
Code : ImageTarget.java .. code to get texture from bitmap
// Sets View measure - This size should be the same as the // texture generated to display the overlay in order for the // texture to be centered in screen Btns_layout.measure(MeasureSpec.makeMeasureSpec(mTextureSize, MeasureSpec.EXACTLY), MeasureSpec .makeMeasureSpec(mTextureSize, MeasureSpec.EXACTLY)); // updates layout size Btns_layout.layout(0, 0, Btns_layout.getMeasuredWidth(), Btns_layout.getMeasuredHeight()); // Draws the View into a Bitmap. Note we are allocating // several // large memory buffers thus attempt to clear them as soon // as // they are no longer required: Bitmap bitmap = Bitmap.createBitmap(mTextureSize, mTextureSize, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bitmap); Btns_layout.draw(c); // Clear the product view as it is no longer needed Btns_layout = null; System.gc(); // Allocate int buffer for pixel conversion and copy pixels int width = bitmap.getWidth(); int height = bitmap.getHeight(); int[] data = new int[bitmap.getWidth() * bitmap.getHeight()]; bitmap.getPixels(data, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight()); // Recycle the bitmap object as it is no longer needed bitmap.recycle(); bitmap = null; c = null; System.gc(); // Generates the Texture from the int buffer mBookDataTexture = Texture.loadTextureFromIntBuffer(data, width, height); mTextures.add(mBookDataTexture); // Clear the int buffer as it is no longer needed data = null; System.gc()
Code : QuadMesh.java
public class QuadMesh { public 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 }; public 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 }; public 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 }; public final short[] mIndices = new short[] { 0,1,2, //triangle 1 2,3,0 // triangle 2 }; public final Buffer vertices; public final Buffer normals; public final Buffer texCoords; public final Buffer indices; public final int numVertices = mVertices.length/3; //3 coords per vertex public final int numIndices = mIndices.length; public QuadMesh() { // init vertex buffers vertices = fillBuffer(mVertices); normals = fillBuffer(mNormals); texCoords = fillBuffer(mTexCoords); indices = fillBuffer(mIndices); } private Buffer fillBuffer(float[] array) { final int sizeOfFloat = 4;//1 float = 4 bytes ByteBuffer bb = ByteBuffer.allocateDirect(sizeOfFloat * array.length); bb.order(ByteOrder.LITTLE_ENDIAN); for (float f : array) { bb.putFloat(f); } bb.rewind(); return bb; } private Buffer fillBuffer(short[] array) { final int sizeOfShort = 2;//1 short = 2 bytes ByteBuffer bb = ByteBuffer.allocateDirect(sizeOfShort * array.length); bb.order(ByteOrder.LITTLE_ENDIAN); for (short s : array) { bb.putShort(s); } bb.rewind(); return bb; } }