我有一个可以在显示器上渲染的 3D 场景。然而,除了渲染它以显示之外,我还希望能够将渲染的场景(例如每 100 帧一次)导出为图像(例如 JPG 或 PNG 图像),也许将其保存为文件在我的某个位置机器。
do{
glfwPollEvents();
glBindFramebuffer(GL_FRAMEBUFFER, framebuffername);
drawScene();
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.size());
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
image = (GLuint*)malloc(windowWidth * windowHeight);
glReadPixels(0, 0, windowWidth, windowHeight, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, &image);
free(image);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
drawScene();
glfwSwapBuffers(window);
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
Run Code Online (Sandbox Code Playgroud)
我想做的是将场景渲染到显示器以及我定义的帧缓冲区对象。上面的崩溃并给我一个奇怪的 Visual Studio 错误:“ig75icd64.pdb 未加载”。但如果我注释掉这一行:
glReadPixels(0, 0, windowWidth, windowHeight, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, &image);
Run Code Online (Sandbox Code Playgroud)
然后程序不会崩溃,并且它会正确渲染到显示器(但我希望能够使用 glReadPixels 写入 2d 纹理)。我的最终目标是能够将显示导出为格式化图像,以便稍后进行图像处理。
建议?
正如 Derhass 所说,您需要传递指针,而不是指针的地址。
该代码将(技术上)工作:
image = (GLuint*)malloc(windowWidth * windowHeight * sizeof(GLuint)); //Need to make it large enough!
glReadPixels(0, 0, windowWidth, windowHeight, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, image); //Note we're not taking the address of image
free(image);
Run Code Online (Sandbox Code Playgroud)
下面的代码会更安全,并且是全面更好的代码(假设您使用的是 C++,您的调用vertices.size()似乎暗示了这一点):
std::vector<GLuint> image(windowWidth * windowHeight);
glReadPixels(0, 0, windowWidth, windowHeight, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, image.data());
Run Code Online (Sandbox Code Playgroud)
还有一个考虑因素:根据提供的宽度和高度glfwCreateWindow(windowWidth, windowHeight, "Dragon!", nullptr, nullptr);创建一个大小合适的窗口,但是Framebuffer的实际大小(实际发生渲染的窗口部分)可能会有所不同(通常会更小),特别是在窗口可调整大小的情况下由用户。在尝试调整大小之前,您应该查询帧缓冲区的大小:
do {
glfwPollEvents();
int fbwidth, fbheight;
glfwGetFramebufferSize(window, &fbwidth, &fbheight);
//use fbwidth and fbheight in all code that needs the size of the rendered image
/*...*/
while(/*...*/);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1315 次 |
| 最近记录: |