如何使用 OpenGL 渲染到帧缓冲区而不在屏幕上显示?

发布于 2024-12-11 07:39:45 字数 96 浏览 0 评论 0原文

我知道可以离线渲染而不将其显示在屏幕上。

如何做到这一点,也许

创建一个不可见的窗口然后绘制。

渲染时可以使用特定的 fbo 吗?

I knew it is possible to render offline without displaying it on screen.

How to do it, maybe

create an invisible window then draw.

Can I use specific fbo when render?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痕至 2024-12-18 07:39:45

您可以使用以下方法创建帧缓冲区对象:

GLint fbo, tex; // "handles" for framebuffer and it's texture
glGenFramebuffersEXT(1, &fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);

// setup texture for colour attachment
glGenTextures(1, &tex);
glEnable(GL_TEXTURE_2D);
glDisable(GL_COLOR_MATERIAL);
glBindTexture(GL_TEXTURE_2D, tex);
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);
// set a size fotr the texture, but not any initial data
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, fbo_resX, fbo_resY, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
// You might want a depth attachment here too perhaps?
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex, 0);

const GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
assert(status == GL_FRAMEBUFFER_COMPLETE_EXT);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

然后使用它:

// specify which FBO to use
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
// glDraw...

// return to the default
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

您可以根据需要创建多个帧缓冲区并在需要时绑定到它们。 (给予或接受)。您需要一个有效的 OpenGL 上下文才能使用它,这通常类似于在大多数平台上创建一个窗口,但您不必在该窗口中绘制任何内容。

You can create a framebuffer object using something like:

GLint fbo, tex; // "handles" for framebuffer and it's texture
glGenFramebuffersEXT(1, &fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);

// setup texture for colour attachment
glGenTextures(1, &tex);
glEnable(GL_TEXTURE_2D);
glDisable(GL_COLOR_MATERIAL);
glBindTexture(GL_TEXTURE_2D, tex);
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);
// set a size fotr the texture, but not any initial data
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, fbo_resX, fbo_resY, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
// You might want a depth attachment here too perhaps?
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex, 0);

const GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
assert(status == GL_FRAMEBUFFER_COMPLETE_EXT);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

You then use it like:

// specify which FBO to use
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
// glDraw...

// return to the default
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

You can create multiple framebuffers as you like and bind to them when you please. (Give or take). You'll need a valid OpenGL context to use this, which usually approximates to creating a window on most platforms, but you don't ever have to draw anything into that window.

两人的回忆 2024-12-18 07:39:45

为此,您可以使用简单 DirectMedia 层 (SDL)。

You can use Simple DirectMedia Layer (SDL) for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文