OpenGL Implementing MultiPass

故事扮演 提交于 2019-12-01 22:52:12

But I am not able to render the buffer to itself as a texture, I understand that it outputs undefined behaviour and that I should be using a technique as Ping-Pong.

Can somebody point me in the right direction?

You have to create 2 framebuffer objects, as you did it with the one in your question: frameBufferOne and frameBufferTwo.
Each fo this 2 framebuffer objects has to have a texture object attached: textureColourBufferOne and textureColourBufferTwo.

In the main loop of your program you have to know if the number of the frame is even or odd.
If the number is even then you have to render to textureColourBufferOne and the input to the render pass is textureColourBufferTwo.
It the number is odd then you have to render to textureColourBufferTwo and the input to the render pass is textureColourBufferOne.
The result of the rendering is always stored int the texture which belongs to the framebuffer of the current frame:

bool even = true;
while( !glfwWindowShouldClose( window ) )
{
    .....

    glBindFramebuffer( GL_FRAMEBUFFER, even ? frameBufferOne : frameBufferTwo );

    glClearColor( 0.2f, 0.3f, 0.1f, 1.0f );
    glClear( GL_COLOR_BUFFER_BIT );

    .....

    glBindVertexArray( VAO );
    glBindTexture( GL_TEXTURE_2D, even ? textureColourBufferTwo : textureColourBufferOne );
    glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0 );
    glBindVertexArray( 0 );

    .....

    glBindFramebuffer( GL_FRAMEBUFFER, 0 );
    glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
    glClear( GL_COLOR_BUFFER_BIT );

    .....

    glBindVertexArray(VAO);
    glBindTexture( GL_TEXTURE_2D, even ? textureColourBufferOne : textureColourBufferTwo );
    glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0 );

    glfwSwapBuffers( window );
    glfwPollEvents();

    even = !even;

    .....
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!