将Opengl纹理从第一个fbo绑定到第二个fbo,使用着色器并渲染第二个结果(乒乓)

时间:2011-04-12 23:51:31

标签: ios4 opengl-es-2.0

我一直在研究乒乓阴影,并且认为我在上一个问题之后已经破解了它。但是,有了更多着色器知识,看起来我能够在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);
}

1 个答案:

答案 0 :(得分:0)

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