使用openGL帧缓冲区时如何获得相机运动

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

我一直在编写程序来可视化分形的点云,到目前为止,一切都已经进行了,相机移动使用的是以原点为中心的弧光球移动,并且正在渲染点。但是,我需要将场景输出到UI内的集成窗口中,因此我一直在尝试使帧缓冲区起作用。

到目前为止,我已经将纹理成功渲染到四边形上,然后将其输出到屏幕上,用于测试目的与我不使用fbo时基本相同。我的问题是试图使摄像机移动以使用渲染纹理进行显示时出现。我知道这绝对是可能的,因为有很多这样的例子,但是我还无法使用我的代码。我相当确定我的着色器存在问题,但是我已经搜索了许多网站,YouTube教程,openGL示例,但没有发现任何可行的方法。据我所知,我必须正常渲染场景,因此我一直使用与以前为初始渲染步骤工作的着色器相同的着色器,但我使用的是fbo而不是默认的fbo

为了简单起见,我刚渲染了一个点立方体,因为它比每次生成分形都要快。

这里是fbo,texture和rbo的主要设置:

GLuint fbo;
GLuint texturebuffer;
GLuint rbo;
void initFBO(){
    glGenFramebuffers(1, &fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);

    glGenTextures(1, &texturebuffer);
    glBindTexture(GL_TEXTURE_2D, texturebuffer);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WINDOW_WIDTH, WINDOW_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texturebuffer, 0);

    glGenRenderbuffers(1, &rbo);
    glBindRenderbuffer(GL_RENDERBUFFER, rbo);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, WINDOW_WIDTH, WINDOW_HEIGHT);
    glBindRenderbuffer(GL_RENDERBUFFER, 0); // once rbo memory allocated unbind
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
}

这里是主循环和绘制循环:

int main(){
    if(!setup())
        return -1;

    // // white background
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);

    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);

    // GLuint programID = LoadShaders( "new_vertex_shader", "new_fragment_shader" ); // custom shader
    GLuint programID = LoadShaders("new_vertex_shader", "new_fragment_shader");
    GLuint modelMatrixID = glGetUniformLocation(programID, "model");
    GLuint viewMatrixID = glGetUniformLocation(programID, "view");    
    GLuint matrixID = glGetUniformLocation(programID, "MVP");

    GLuint quad_programID = LoadShaders( "screen_vertex_shader", "screen_fragment_shader" );
    GLuint textureID = glGetUniformLocation(quad_programID, "screenTexture");

    // initialise mvp matrices
    glm::mat4 ProjectionMatrix = perspective(radians(45.0f), 4.0f / 3.0f, 0.1f, 100.0f);
    glm::mat4 ViewMatrix = translate(mat4(1.0f), vec3(0,0,-RADIUS));
    glm::mat4 ModelMatrix = mat4(1.0f);
    glm::mat4 MVP;

    GLuint vertexbuffer;
    glGenBuffers(1, &vertexbuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

    GLfloat cube[24] = {
        0.5,0.5,0.5,
        0.5,-0.5,0.5,
        -0.5,0.5,0.5,
        -0.5,-0.5,0.5,
        0.5,0.5,-0.5,
        0.5,-0.5,-0.5,
        -0.5,-0.5,-0.5,
        -0.5,0.5,-0.5
    };
    glBufferData(GL_ARRAY_BUFFER, sizeof(cube),cube, GL_STATIC_DRAW);

    glfwSetMouseButtonCallback(window, mouseCallback);


    // ################# main draw loop #######################
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 ){

        // render to fbo first
        glBindFramebuffer(GL_FRAMEBUFFER, fbo);
        glEnable(GL_DEPTH_TEST);
        glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        computeMatricesFromInputs();
        ProjectionMatrix = getProjectionMatrix();
        ViewMatrix = getViewMatrix();
        ModelMatrix = getModelMatrix();
        MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;

        // Use our shader
        glUseProgram(programID);

        // Send our transformation to the currently bound shader in the "MVP" uniform
        glUniformMatrix4fv(matrixID, 1, GL_FALSE, &MVP[0][0]);
        glUniformMatrix4fv(modelMatrixID, 1, GL_FALSE, &ModelMatrix[0][0]);
        glUniformMatrix4fv(viewMatrixID, 1, GL_FALSE, &ViewMatrix[0][0]);

        // render scene as normal
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); // index buffer
        glVertexAttribPointer(
            0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
            3,                  // size
            GL_FLOAT,           // type
            GL_FALSE,           // normalized?
            0,                  // stride
            (void*)0            // array buffer offset
        );

        glPointSize(POINT_SIZE);
        glDrawArrays(GL_POINTS, 0, sizeof(cube));
        glDisableVertexAttribArray(0);


        // bind back to default frame buffer to show on screen
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glDisable(GL_DEPTH_TEST);
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glUseProgram(quad_programID);

        glEnableVertexAttribArray(0);
        glBindVertexArray(quad_vertex_buffer);

        glVertexAttribPointer(
            0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
            3,                  // size
            GL_FLOAT,           // type
            GL_FALSE,           // normalized?
            0,                  // stride
            (void*)0            // array buffer offset
        );
        glDrawArrays(GL_POINTS, 0, sizeof(cube));
        glDisableVertexAttribArray(0);

        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

顶点着色器:

#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;

// Values that stay constant for the whole mesh.
uniform mat4 MVP;
uniform mat4 view;
uniform mat4 model;

void main(){

    // Output position of the vertex, in clip space : MVP * position
    gl_Position =  MVP * vec4(vertexPosition_modelspace,1.0);

}

片段着色器:

#version 330 core

// Output data
out vec3 color;

void main(){

    // Output color = black 
    color = vec3(0,0,0);

}

屏幕四边形顶点着色器:

#version 330 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec2 aTexCoords;

out vec2 TexCoords;

void main()
{
    TexCoords = aTexCoords;
    gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
}

屏幕四片段着色器:

#version 330 core
out vec4 FragColour;

in vec2 TexCoords;

uniform sampler2D screenTexture;

void main()
{
    FragColour = texture(screenTexture, TexCoords);
}

很抱歉,如果这是一个很大的代码块,我不能完全确定错误将在哪里,并且对于使用openGL来说有点新。非常感谢您的任何帮助。

c++ opengl-3
1个回答
0
投票
glBindVertexArray

当绘制屏幕空间四边形(第二遍)时,必须使用quad_vertex_buffer类型生成(填充的)多边形(可能是glBindVertexArray(quad_vertex_buffer); glEnableVertexAttribArray(0); primitive),而不是点图元类型[C0 ]:

GL_TRIANGLES

GL_TRIANGLE_STRIP

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