0

我与 Vuforia 合作开展一个项目。我使用Vuforia的VideoPlayback示例。

当 Vuforia 检测到标记时,会在该标记上播放视频。我尝试在视频叠加层上应用色键。我发现这篇文章http://pilcrowpipe.blogspot.fr/2013/03/chroma-keying-transparent-background.html

我尝试实现代码,但没有任何效果。我认为这个例子使用了 chromakey 的视频背景,但我想要覆盖视频上的 chromakey。我对吗 ?

在 Vuforia Sample 中,我想我必须在renderFrameWithState函数中进行一些更改。

if (NOT_READY != currentStatus) {
        // Convert trackable pose to matrix for use with OpenGL
        // ...

        /// TEST doesn't work
        //glDepthFunc(GL_LEQUAL);
        //glEnable(GL_BLEND);
        //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        ///

        // ...

        glUseProgram(shaderProgramID);

        glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, quadVertices);
        glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, quadNormals);
        glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, texCoords);

        glEnableVertexAttribArray(vertexHandle);
        glEnableVertexAttribArray(normalHandle);
        glEnableVertexAttribArray(textureCoordHandle);

        // I think I must change something in code above...
        // But what and where ????
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, frameTextureID);
        glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (GLfloat*)&modelViewProjectionVideo.data[0]);
        glUniform1i(texSampler2DHandle, 0 /*GL_TEXTURE0*/);
        glDrawElements(GL_TRIANGLES, kNumQuadIndices, GL_UNSIGNED_SHORT, quadIndices);

        glDisableVertexAttribArray(vertexHandle);
        glDisableVertexAttribArray(normalHandle);
        glDisableVertexAttribArray(textureCoordHandle);

        glUseProgram(0);
    }

我对 OpenGL ES 完全不熟悉。

任何人都可以帮助我吗?

谢谢

4

3 回答 3

0

最后

glEnable(GL_BLEND);

应该:

glDisable(GL_BLEND);

为了不冻结背景。

感谢您的代码!

于 2017-09-25T02:32:35.507 回答
0

好的,

我找到了 chromakey 的解决方案,但我遇到了一个新问题。

在片段着色器中,我使用上面的代码:

precision mediump float;
varying vec2 texCoord;

uniform sampler2D texSampler2D;

void main()
{
    // Keying for green color
    vec3 keying_color = vec3(0., 1., 0.);
    float thresh = 0.8;
    float slope = 0.2;
    vec3 input_color = texture2D(texSampler2D, texCoord).rgb;
    float d = abs(length(abs(keying_color.rgb - input_color.rgb)));
    float edge0 = thresh * (1.0 - slope);
    float alpha = smoothstep(edge0, thresh, d);
    gl_FragColor = vec4(input_color, alpha);
}

在 VideoPlaybackEAGL.mm 中,我将此代码添加到renderFrameWithState函数中。

if (NOT_READY != currentStatus) {
    // Convert trackable pose to matrix for use with OpenGL
    // ...

    /// It's work
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    ///

    // ...

    glUseProgram(shaderProgramID);

    glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, quadVertices);
    glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, quadNormals);
    glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, texCoords);

    glEnableVertexAttribArray(vertexHandle);
    glEnableVertexAttribArray(normalHandle);
    glEnableVertexAttribArray(textureCoordHandle);

    // I think I must change something in code above...
    // But what and where ????
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, frameTextureID);
    glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (GLfloat*)&modelViewProjectionVideo.data[0]);
    glUniform1i(texSampler2DHandle, 0 /*GL_TEXTURE0*/);
    glDrawElements(GL_TRIANGLES, kNumQuadIndices, GL_UNSIGNED_SHORT, quadIndices);

    glDisableVertexAttribArray(vertexHandle);
    glDisableVertexAttribArray(normalHandle);
    glDisableVertexAttribArray(textureCoordHandle);

    glUseProgram(0);
}

但是现在,我的 VideoBackground 停止渲染,完全冻结。而我的叠加视频反转位置。当我将手机定位到顶部时,视频叠加层定位到底部。左右都一样。。。

你有什么想法或解决方案。谢谢 !

于 2017-02-01T16:34:42.123 回答
0

经过搜索,我找到了解决方案!

VideoPlaybackEAGL.mm中,我将此代码添加到renderFrameWithState函数中。

if (NOT_READY != currentStatus) {
    // Convert trackable pose to matrix for use with OpenGL
    // ...

    /// It's work
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    ///

    // ...

    glUseProgram(shaderProgramID);

    glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, quadVertices);
    glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, quadNormals);
    glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, texCoords);

    glEnableVertexAttribArray(vertexHandle);
    glEnableVertexAttribArray(normalHandle);
    glEnableVertexAttribArray(textureCoordHandle);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, frameTextureID);
    glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (GLfloat*)&modelViewProjectionVideo.data[0]);
    glUniform1i(texSampler2DHandle, 0 /*GL_TEXTURE0*/);
    glDrawElements(GL_TRIANGLES, kNumQuadIndices, GL_UNSIGNED_SHORT, quadIndices);

    glDisableVertexAttribArray(vertexHandle);
    glDisableVertexAttribArray(normalHandle);
    glDisableVertexAttribArray(textureCoordHandle);

    glUseProgram(0);

    // Adding code above to resolve the problem...
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_BLEND);
}

这是工作!我稍微更改了顶点着色器以校正 z 轴。

于 2017-02-02T08:59:25.990 回答