OpenGL ES运行多个着色器

时间:2011-03-28 23:35:13

标签: iphone opengl-es shader

根据之前的一个问题,我一直在研究如何在一次传递中运行多个着色器(即有一个FBO并渲染到纹理)。这是Iphone上的OpenGLES 2.

我已经创建了一些运行但仅适用于一帧的代码。所以很明显我做错了但是我无法发现它。

inout数据是来自摄像头的核心视频缓冲区。

非常欢迎任何指针或修正:)。

谢谢,

西蒙

PS:我现在把所有东西都放在一个方法中 - 这样我就可以专注于让它发挥作用了!

- (void) PingPong:(CVImageBufferRef)cameraframe;
{
int bufferHeight = CVPixelBufferGetHeight(cameraframe);

int bufferWidth = CVPixelBufferGetWidth(cameraframe);

// Build the first FBO - this is wasteful - don't do this everytime
GLuint firstFBO;
glGenFramebuffers(1, &firstFBO);
glBindFramebuffer(GL_FRAMEBUFFER, firstFBO);

// Build the first texture and copy the video stuff into it
GLuint sourcetexture;
glGenTextures(1, &sourcetexture);
glBindTexture(GL_TEXTURE_2D, sourcetexture);
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);


// Build the second texture
GLuint nextTexture;
glGenTextures(1, &nextTexture);
glBindTexture(GL_TEXTURE_2D, nextTexture);
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);

// Attach the texture to our first FBO
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sourcetexture, 0);


// 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));

//glDrawBuffer(sourcetexture);
glEnable(GL_TEXTURE_2D);
glViewport(0, 0, backingWidth, backingHeight);
glBindTexture(GL_TEXTURE_2D, sourcetexture);
glUseProgram(greyscaleProgram);

// Now do the 2nd pass using the sourcetexture as input
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, nextTexture, 0);
glBindTexture(GL_TEXTURE_2D, nextTexture);
glUseProgram(program);

// Present the framebuffer to the render buffer
[EAGLContext setCurrentContext:context];

glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);

[context presentRenderbuffer:GL_RENDERBUFFER];

// Clean up stuff
glDeleteTextures(1, &sourcetexture);
glDeleteTextures(1, &nextTexture);

}

1 个答案:

答案 0 :(得分:3)

您的抽奖代码在哪里?绘图是触发着色器执行的原因,我现在看到的代码没有绘制调用(glDrawArrays,glDrawElements等),所以它不起作用,因为你没有绘图。

绘制一个全屏四边形以触发每次传递的着色器执行,它将起作用。

此外,建议使用glCheckFramebufferStatus检查您的FBO配置是否受支持,并且可以在GL实施中使用。

相关问题