【问题标题】:Bind Opengl texture from the first fbo to a second fbo, use shader and render the second result (ping pong)将 Opengl 纹理从第一个 fbo 绑定到第二个 fbo,使用着色器并渲染第二个结果(乒乓球)
【发布时间】:2011-04-22 04:23:37
【问题描述】:

我一直在研究乒乓着色,并认为在我上一个问题之后我已经破解了它。但是,随着对着色器的进一步了解,看起来虽然我能够在 FBO A 和 FBO B 上运行着色器,但 A 的输出并未用作 B 的源。换句话说,我没有正确绑定它。

我正在使用的代码如下。第二个着色器的输出显示基于颜色的输出,但第一个着色器将数据设置为灰度。因此,我知道这不能按要求工作。

我将不胜感激任何(进一步的!!)帮助。

代码如下,

干杯,

西蒙

- (void) PingPong:(CVImageBufferRef)cameraframe; 
{
// Standard texture coords for the rendering pipeline
static const GLfloat squareVertices[] = {
    -1.0f, -1.0f,
    1.0f, -1.0f,
    -1.0f,  1.0f,
    1.0f,  1.0f,
};

static const GLfloat textureVertices[] = {
    1.0f, 1.0f,
    1.0f, 0.0f,
    0.0f,  1.0f,
    0.0f,  0.0f,
};

if (context)
{
    [EAGLContext setCurrentContext:context];
}

// Create two textures with the same configuration
int bufferHeight = CVPixelBufferGetHeight(cameraframe);
int bufferWidth = CVPixelBufferGetWidth(cameraframe);

// texture 1
glGenTextures(1, &tex_A);
glBindTexture(GL_TEXTURE_2D, tex_A);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

// Using BGRA extension to pull in video frame data directly
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(cameraframe));

// Texture 2
glGenTextures(1, &tex_B);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

// Bind framebuffer A
glBindFramebuffer(GL_FRAMEBUFFER, fbo_A);
glViewport(0, 0, backingWidth, backingHeight);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex_A);

// Update uniform values
glUniform1i(uniforms[UNIFORM_VIDEOFRAME], 0);   

// Update attribute values.
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_TEXTUREPOSITON, 2, GL_FLOAT, 0, 0, textureVertices);
glEnableVertexAttribArray(ATTRIB_TEXTUREPOSITON);

// Use the first shader
glUseProgram(greyscaleProgram);

// Render a quad
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

// Use the second shader
glUseProgram(program);

// Bind framebuffer B
glBindFramebuffer(GL_FRAMEBUFFER, fbo_B);

// Bind texture A and setup texture units for the shader
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex_A);

// Render output of FBO b is texture B
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex_B, 0);

// Update uniform values
glUniform1i(uniforms[UNIFORM_VIDEOFRAME], 0);   

// Update attribute values.
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_TEXTUREPOSITON, 2, GL_FLOAT, 0, 0, textureVertices);
glEnableVertexAttribArray(ATTRIB_TEXTUREPOSITON);

// Render a quad
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

// Render the whole thing
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];

glDeleteTextures(1, &tex_A);
glDeleteTextures(1, &tex_B);
}

【问题讨论】:

    标签: ios4 opengl-es-2.0


    【解决方案1】:

    认为可能发生的情况是您仍在渲染到帧缓冲内存而不是纹理内存。 iirc glFramebufferTexture2D 不充当解析/复制,而是将帧缓冲区绑定到纹理,以便将来的渲染操作写入纹理。您可能会遇到更多问题,但是我很确定您对 glFramebufferTexture2D 的调用应该在您第一次调用 glBindFramebuffer 之后直接发生。这可能不是您唯一的问题,但它似乎是一个重要的问题。

    【讨论】:

    • 是的 - 我已经尝试在创建这些纹理后立即将 tex_a 和 tex_B 绑定到各自的帧缓冲区,但是然后我得到一个锁定的屏幕,它永远不会更新。我目前正在检查我是否正在正确清理所有内容并清理着色器(等),以便我的程序中没有额外的人工制品
    • 看起来 tex_A 不应该绑定到渲染缓冲区,它看起来像你的纹理输入。另外,我不明白为什么你有两个渲染缓冲区。为了完成这一切,一旦你绑定了最终的渲染缓冲区,它看起来就像你正在显示的那个,你永远不会渲染任何东西。从您的代码的外观来看,从不更新的屏幕对我来说似乎暗示渲染缓冲区实际上正在工作。使用当前代码,如果它有效,我不希望显示任何内容。
    • 也就是说,我想你误解了 glFramebufferTexture2D 的作用。您可以使用它来设置要渲染到的纹理,不是要渲染的纹理,以及不是要将当前渲染缓冲区复制到的纹理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多