繁体   English   中英

OpenGL:渲染到帧缓冲区以及显示

[英]OpenGL: Render to framebuffer as well as to display

我有一个3D场景,可以在显示器上进行渲染。 但是,除了将其渲染显示以外,我还希望能够将渲染的场景(例如每100帧一次)导出为图像(例如JPG或PNG图像),也许将其保存为文件在我的某个位置机。

我尝试了以下内容:

do{
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffername);
    drawScene();
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    drawScene();
}while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS

我不确定我是否正确使用了FrameBuffer对象,但是在寻求达到目标的情况下正在寻求建议。 从上面的代码中,我将帧缓冲区绑定到我定义的FBO,然后绘制场景(如果我正确的话,应该绘制到FBO吗?)。 然后,我再次回到常规显示。 但是,此代码使显示不断在我的场景和不需要的空白(黑色)场景之间切换。 我希望我的场景作为一个干净的3d场景显示在显示设备上,而不会一直变黑。

我的绘图函数如下所示:

void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glBindTexture(GL_TEXTURE_2D, 0);
//glBindFramebuffer(GL_FRAMEBUFFER, framebuffername);

// Use our shader
glUseProgram(programID);

// Compute the MVP matrix from keyboard and mouse input
computeMatricesFromInputs();
glm::mat4 ProjectionMatrix = getProjectionMatrix();
glm::mat4 ViewMatrix = getViewMatrix();
glm::mat4 ModelMatrix = glm::mat4(1.0); //DEFINE MODEL TO WORLD TRANSFORMATION
                                        //glm::mat4 ModelMatrix = glm::scale(2.0,2.0,2.0);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;

// Send our transformation to the currently bound shader, 
// in the "MVP" uniform
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
glUniform1f(FARPLANE, farplane_control);
glUniform1f(NEARPLANE, nearplane_control);
glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &ModelMatrix[0][0]);
glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, &ViewMatrix[0][0]);
glm::vec3 lightPos = glm::vec3(0, 0, 4);
glUniform3f(LightID, lightPos.x, lightPos.y, lightPos.z);

// 1st attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
    0,                  // attribute
    3,                  // size
    GL_FLOAT,           // type
    GL_FALSE,           // normalized?
    0,                  // stride
    (void*)0            // array buffer offset
);

// 2nd attribute buffer : normals
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glVertexAttribPointer(
    1,                                // attribute
    3,                                // size
    GL_FLOAT,                         // type
    GL_FALSE,                         // normalized?
    0,                                // stride
    (void*)0                          // array buffer offset
);

// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.size());

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

glReadBuffer(GL_FRONT);

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

您的drawScene()代码最后包括以下drawScene()行:

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

如果您实际上没有在屏幕上渲染时调用此方法,您会怎么想?

显而易见的解决方案是在函数中包含一个标志,该标志指示是否执行缓冲区交换:

void drawScene(bool swap = true) {
    /*...*/
    if(swap) {
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
}

do{
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffername);
    drawScene(false);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    drawScene(true);
}while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS

或者,您可以将这些调用移到函数之外,这可能会更好,因为首先将它们本地化到draw函数实际上并没有任何意义。

do{
    glfwPollEvents(); //Prefer having this be the first call, in case you need the window
    //responding to user input immediately
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffername);
    drawScene();
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    drawScene();
    glfwSwapBuffers(window); //No longer in drawScene() function
}while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM