使用iOS渲染到OpenGL ES 2.0中的纹理

时间:2013-10-30 10:50:50

标签: ios opengl-es-2.0

我很难在OpenGL ES 2.0中设置渲染到纹理环境。

我需要的是一个提供给屏幕的主FrameBuffer和一个连接到Texture的辅助FrameBuffer。然后在主FrameBuffer中使用此纹理。

我首先使用以下代码初始化主FrameBuffer:

-(void)initializeBuffers{

    //Build the main FrameBuffer
    glGenFramebuffers(1, &frameBuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);

    //Build the color Buffer
    glGenRenderbuffers(1, &colorBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, colorBuffer);

    //setup the color buffer with the EAGLLayer (it automatically defines width and height of the buffer)
    [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:EAGLLayer];
    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &bufferWidth);
    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &bufferHeight);

    //Attach the colorbuffer to the framebuffer
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBuffer);

    //Check the Framebuffer status
    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

    NSAssert(status == GL_FRAMEBUFFER_COMPLETE, ERROR_FRAMEBUFFER_FAIL);

}

然后我调用一个函数来初始化辅助缓冲区

-(void)setupRenderToTexture{

    glGenTextures(1, &gridTextureID);
    glBindTexture(GL_TEXTURE_2D, gridTextureID);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    glBindTexture(GL_TEXTURE_2D, 0);

    glGenRenderbuffers(1, &textureColorBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, textureColorBuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, bufferWidth, bufferHeight);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);

    glGenFramebuffers(1, &textureFrameBuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, textureFrameBuffer);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gridTextureID, 0);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, textureColorBuffer);

    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER) ;
    if(status != GL_FRAMEBUFFER_COMPLETE) {
        NSLog(@"failed to make complete framebuffer object %x", status);
    }else{
        NSLog(@"Buffer ok");
    }

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

}

此时,当需要绘制时,我调用一个名为render的函数来调用函数renderToTexture

- (void)render:(CADisplayLink*)displaylink{

    [self renderToTexture];

    glBindBuffer(GL_FRAMEBUFFER, frameBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, colorBuffer);

    glClearColor(0.0, 1.0, 0.0, 1.0);
    glViewport(0, 0, bufferWidth, bufferHeight);
    glClear(GL_COLOR_BUFFER_BIT);


    for(GLNode *node in sceneNodes){
        [node draw]; //Draw some triangles...
    }

    [context presentRenderbuffer:GL_RENDERBUFFER];
}

此处renderToTexture功能: - (无效)renderToTexture {

glBindFramebuffer(GL_FRAMEBUFFER, textureFrameBuffer);

glClearColor(1.0, 0.0, 0.0, 1.0);
glViewport(0, 0, bufferWidth, bufferHeight);
glClear(GL_COLOR_BUFFER_BIT);

//Render something to texture

}

如果我只使用与主缓冲区相关的代码(所以函数initializeBuffersrender),主FrameBuffer的内容会在屏幕上正确显示。如果我尝试使用与渲染相关的函数(setupRenderToTexturerenderToTexture),则不再在屏幕上绘制主Framebuffer。

我想知道这个配置是否有问题,我该怎么做才能纠正这种行为。

0 个答案:

没有答案