如何在单独的视口中使用framebuffer以及默认的framebuffer?

问题描述 投票:1回答:1

我试图了解Framebuffers如何工作。 AFAIK,帧缓冲区可以将渲染存储为纹理,其中渲染缓冲区存储深度信息,颜色缓冲区存储颜色信息。有一个默认的帧缓冲区,然后我可能创建任何其他帧缓冲区来存储单独的渲染。现在,我想在整个屏幕上渲染默认的帧缓冲区,而在小侧视口上,应该有我的新帧缓冲区存储的渲染。这不是在我的源代码中发生的。

代码:

#include <GL/glew.h>  
#include <GLFW/glfw3.h> 
#include <stdio.h>

#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480

int main() {
    GLFWwindow *window = NULL;
    GLuint vao;
    GLuint vbo;

    GLfloat points[] = { 0.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f };

    const char *vertex_shader = "#version 410\n"
                                "in vec3 vp;"
                                "void main () {"
                                "  gl_Position = vec4(vp, 1.0);"
                                "}";

    const char *fragment_shader = "#version 410\n"
                                "out vec4 frag_colour;"
                                "void main () {"
                                "  frag_colour = vec4(0.5, 0.0, 0.5, 1.0);"
                                "}";

    GLuint vert_shader, frag_shader;
    GLuint shader_programme;

    if ( !glfwInit() ) {
        fprintf( stderr, "ERROR: could not start GLFW3\n" );
        return 1;
    }

    glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 4 );
    glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 6 );
    glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
    glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );

    window = glfwCreateWindow( WINDOW_WIDTH, WINDOW_HEIGHT, "Hello Triangle", NULL, NULL );
    if ( !window ) {
        fprintf( stderr, "ERROR: could not open window with GLFW3\n" );
        glfwTerminate();
        return 1;
    }
    glfwMakeContextCurrent( window );

    glewExperimental = GL_TRUE;
    glewInit();

    glEnable( GL_DEPTH_TEST ); /* enable depth-testing */
    glDepthFunc( GL_LESS );

    glGenBuffers( 1, &vbo );
    glBindBuffer( GL_ARRAY_BUFFER, vbo );
    glBufferData( GL_ARRAY_BUFFER, 9 * sizeof( GLfloat ), points, GL_STATIC_DRAW );
    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );
    glEnableVertexAttribArray( 0 );
    glBindBuffer( GL_ARRAY_BUFFER, vbo );
    glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, NULL );

    vert_shader = glCreateShader( GL_VERTEX_SHADER );
    glShaderSource( vert_shader, 1, &vertex_shader, NULL );
    glCompileShader( vert_shader );
    frag_shader = glCreateShader( GL_FRAGMENT_SHADER );
    glShaderSource( frag_shader, 1, &fragment_shader, NULL );
    glCompileShader( frag_shader );
    shader_programme = glCreateProgram();
    glAttachShader( shader_programme, frag_shader );
    glAttachShader( shader_programme, vert_shader );
    glLinkProgram( shader_programme );


    GLuint g_fb;
    GLuint g_fb_tex;
    glGenFramebuffers( 1, &g_fb );
    glGenTextures( 1, &g_fb_tex );
    glBindTexture( GL_TEXTURE_2D, g_fb_tex );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, WINDOW_WIDTH/2, WINDOW_HEIGHT/2, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    glBindFramebuffer( GL_FRAMEBUFFER, g_fb );
    glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, g_fb_tex, 0 );
    GLuint rb = 0;
    glGenRenderbuffers( 1, &rb );
    glBindRenderbuffer( GL_RENDERBUFFER, rb );
    glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT, WINDOW_WIDTH/2, WINDOW_HEIGHT/2 );
    glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb );
    GLenum draw_bufs[] = { GL_COLOR_ATTACHMENT0 };
    glDrawBuffers( 1, draw_bufs );
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );



    while ( !glfwWindowShouldClose( window ) ) {
        glBindFramebuffer(GL_FRAMEBUFFER, g_fb);
        glViewport(0, 0, WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glUseProgram( shader_programme );
        glBindVertexArray( vao );
        glDrawArrays( GL_TRIANGLES, 0, 3 );

        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glUseProgram( shader_programme );
        glBindVertexArray( vao );
        glDrawArrays( GL_TRIANGLES, 0, 3 );

        glfwPollEvents();
        glfwSwapBuffers( window );
    }

    glfwTerminate();
    return 0;
}

在这里,只渲染最后提到的缓冲区。我怎么能纠正这个?另外,我正在我的项目中编写一个场景类和一个渲染器类。是否应该为每个渲染器对象存储帧缓冲区,还是应该在场景类中存储任何渲染器实例的所有帧缓冲区?主要是,存储多个fbos需要多少存储/计算?

编辑:我把答案中提到的新纹理,但我仍然只得到最后一次渲染的结果。请指出错误。我是否需要为新的帧缓冲区使用新的着色器?如果是这样,为什么早期的着色器不能重复使用?

码:

#include <GL/glew.h>  
#include <GLFW/glfw3.h> 
#include <stdio.h>

#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480

int main() {
    GLFWwindow *window = NULL;
    GLuint vao;
    GLuint vbo;

    GLfloat points[] = { 0.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f };

    const char *vertex_shader = "#version 410\n"
                                "in vec3 vp;"
                                "void main () {"
                                "  gl_Position = vec4(vp, 1.0);"
                                "}";

    const char *fragment_shader = "#version 410\n"
                                "out vec4 frag_colour;"
                                "void main () {"
                                "  frag_colour = vec4(0.5, 0.0, 0.5, 1.0);"
                                "}";

    GLuint vert_shader, frag_shader;
    GLuint shader_programme;

    if ( !glfwInit() ) {
        fprintf( stderr, "ERROR: could not start GLFW3\n" );
        return 1;
    }

    glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 4 );
    glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 6 );
    glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
    glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );

    window = glfwCreateWindow( WINDOW_WIDTH, WINDOW_HEIGHT, "Hello Triangle", NULL, NULL );
    if ( !window ) {
        fprintf( stderr, "ERROR: could not open window with GLFW3\n" );
        glfwTerminate();
        return 1;
    }
    glfwMakeContextCurrent( window );

    glewExperimental = GL_TRUE;
    glewInit();

    glEnable( GL_DEPTH_TEST ); /* enable depth-testing */
    glDepthFunc( GL_LESS );

    glGenBuffers( 1, &vbo );
    glBindBuffer( GL_ARRAY_BUFFER, vbo );
    glBufferData( GL_ARRAY_BUFFER, 9 * sizeof( GLfloat ), points, GL_STATIC_DRAW );
    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );
    glEnableVertexAttribArray( 0 );
    glBindBuffer( GL_ARRAY_BUFFER, vbo );
    glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, NULL );

    vert_shader = glCreateShader( GL_VERTEX_SHADER );
    glShaderSource( vert_shader, 1, &vertex_shader, NULL );
    glCompileShader( vert_shader );
    frag_shader = glCreateShader( GL_FRAGMENT_SHADER );
    glShaderSource( frag_shader, 1, &fragment_shader, NULL );
    glCompileShader( frag_shader );
    shader_programme = glCreateProgram();
    glAttachShader( shader_programme, frag_shader );
    glAttachShader( shader_programme, vert_shader );
    glLinkProgram( shader_programme );


    GLuint g_fb;
    GLuint g_fb_tex;
    glGenFramebuffers( 1, &g_fb );
    glGenTextures( 1, &g_fb_tex );
    glBindTexture( GL_TEXTURE_2D, g_fb_tex );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, WINDOW_WIDTH/2, WINDOW_HEIGHT/2, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    glBindFramebuffer( GL_FRAMEBUFFER, g_fb );
    glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, g_fb_tex, 0 );
    GLuint rb = 0;
    glGenRenderbuffers( 1, &rb );
    glBindRenderbuffer( GL_RENDERBUFFER, rb );
    glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT, WINDOW_WIDTH/2, WINDOW_HEIGHT/2 );
    glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb );
    GLenum draw_bufs[] = { GL_COLOR_ATTACHMENT0 };
    glDrawBuffers( 1, draw_bufs );
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );



    while ( !glfwWindowShouldClose( window ) ) {
        glBindFramebuffer(GL_FRAMEBUFFER, g_fb);
        glViewport(0, 0, WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glUseProgram( shader_programme );
        glBindVertexArray( vao );
        glDrawArrays( GL_TRIANGLES, 0, 3 );

        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glUseProgram( shader_programme );
        glBindVertexArray( vao );
        glDrawArrays( GL_TRIANGLES, 0, 3 );


        GLuint m_vbo, m_vao;
        GLfloat points[] = { 
            -0.5f,  0.5f, 0.0f, 
             0.5f,  0.5f, 0.0f, 
            -0.5f, -0.5f, 0.0f,
            -0.5f, -0.5f, 0.0f,
             0.5f,  0.5f, 0.0f,
             0.5f, -0.5f, 0.0f
        };

        glGenBuffers(1, &m_vbo);
        glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
        glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), points, GL_STATIC_DRAW);
        glGenVertexArrays(1, &m_vao);
        glBindVertexArray(m_vao);
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
        glBindTexture(GL_TEXTURE_2D, g_fb_tex);

        glViewport(0, 0, WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glUseProgram( shader_programme );
        glBindVertexArray( m_vao );
        glDrawArrays( GL_TRIANGLES, 0, 6 );

        glfwPollEvents();
        glfwSwapBuffers( window );
    }

    glfwTerminate();
    return 0;
}
c++ opengl 2d
1个回答
2
投票

渲染循环的第一个块渲染到g_fb。结果是g_fb_tex。如果要在窗口(= framebuffer 0)中的“小视口”上显示此纹理,则应渲染适当大小的矩形并使用从g_fb_tex采样的着色器。

© www.soinside.com 2019 - 2024. All rights reserved.