Nexus设备中OpenGL2.0渲染的问题(Android中的Vuforia)

时间:2015-04-16 07:34:21

标签: android opengl-es rendering vuforia

当程序找到目标时,我试图用png图像替换茶壶。我正在使用vuforia 4。

这是我使用的代码:

private void renderFrame() {

    State state = mRenderer.begin();
    mRenderer.drawVideoBackground();
    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

    if (state.getNumTrackableResults() == 0)
        displayMessage("nothing");
    // did we find any trackables this frame?
    for (int tIdx = 0; tIdx < state.getNumTrackableResults(); tIdx++) {

        TrackableResult result = state.getTrackableResult(tIdx);
        Trackable trackable = result.getTrackable();
        ImageTarget itarget = (ImageTarget) trackable;

        Log.d("USER", itarget.getName());
        Matrix44F modelViewMatrix_Vuforia = Tool
                .convertPose2GLMatrix(result.getPose());
        float[] modelViewMatrix = modelViewMatrix_Vuforia.getData();
        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);
        Vec3F 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);

        GLES20.glUseProgram(shaderProgramID);

        if (!mActivity.isExtendedTrackingActive()) {

            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());
            GLES20.glEnableVertexAttribArray(vertexHandle);
            GLES20.glEnableVertexAttribArray(normalHandle);
            GLES20.glEnableVertexAttribArray(textureCoordHandle);

            int index = 0;
            if (itarget.getName().equals("ERMITA-2-1")) {
                index = 1;
            }
            GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,
                    mTextures.get(index).mTextureID[0]);
            GLES20.glUniform1i(texSampler2DHandle, 0);
            // pass the model view matrix to the shader
            GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false,
                    modelViewProjection, 0);
            GLES20.glEnable(GLES20.GL_BLEND);
            GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA,
                    GLES20.GL_ONE_MINUS_SRC_ALPHA);
            GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6,
                    GLES20.GL_UNSIGNED_SHORT, mTeapot.getIndices());
            GLES20.glDisable(GLES20.GL_BLEND);

            lastTrackableId = -1;

            GLES20.glDisableVertexAttribArray(vertexHandle);
            GLES20.glDisableVertexAttribArray(normalHandle);
            GLES20.glDisableVertexAttribArray(textureCoordHandle);
            //
        } else {
            GLES20.glDisable(GLES20.GL_CULL_FACE);
            GLES20.glVertexAttribPointer(vertexHandle, 3, GLES20.GL_FLOAT,
                    false, 0, mBuildingsModel.getVertices());
            GLES20.glVertexAttribPointer(normalHandle, 3, GLES20.GL_FLOAT,
                    false, 0, mBuildingsModel.getNormals());
            GLES20.glVertexAttribPointer(textureCoordHandle, 2,
                    GLES20.GL_FLOAT, false, 0,
                    mBuildingsModel.getTexCoords());

            GLES20.glEnableVertexAttribArray(vertexHandle);
            GLES20.glEnableVertexAttribArray(normalHandle);
            GLES20.glEnableVertexAttribArray(textureCoordHandle);

            GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,
                    mTextures.get(0).mTextureID[0]);
            GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false,
                    modelViewProjection, 0);
            GLES20.glUniform1i(texSampler2DHandle, 0);
            GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0,
                    mBuildingsModel.getNumObjectVertex());

            SampleUtils.checkGLError("Renderer DrawBuildings");
        }
        SampleUtils.checkGLError("Render Frame");
    }

    GLES20.glDisable(GLES20.GL_DEPTH_TEST);
    // GLES20.glDisable(GLES20.GL_BLEND);
    mRenderer.end();
}

适用于某些设备,如Galaxy S2和通用平板电脑。我遇到的问题与以下设备有关:

Nexus 4:Android 5.1:识别目标(打印日志)并在屏幕上显示一些奇怪的内容。它似乎设法渲染纹理,但它会立即消失。

Nexus 10:Android 5.1:识别目标(打印日志)但什么都不做,根本没有渲染。没有错误消息。

One Plus One:Android 4.4:与Nexus 10相同。

有什么想法吗?

我也在Vuforia开发论坛上发布了这个问题,但它似乎是渲染问题,而不是Vuforia问题。

1 个答案:

答案 0 :(得分:0)

通过添加:

解决
GLES20.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

就在renderFrame方法的最开头