在没有窗口的情况下创建 OpenGL 上下文

2021-12-19 00:00:00 opengl windows c++ off-screen

我正在尝试找出为离屏渲染创建无窗口 OpenGL 程序的最简单方法是什么.

I'm trying to figure out what is the simplest way to create a windowless OpenGL program for offscreen rendering.

目前我使用这个,到目前为止它工作正常:(为了清楚起见,这里删除了错误检查)

Currently I use this, and it works fine so far: (error checks removed here for clarity)

BOOL create_opengl_context(){
    GLuint PixelFormat;
    static PIXELFORMATDESCRIPTOR pfd;
    hDC = GetDC(NULL);
    PixelFormat = ChoosePixelFormat(hDC, &pfd);
    SetPixelFormat(hDC, PixelFormat, &pfd);
    hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC, hRC);
}

这样使用安全吗?创建无窗口 OpenGL 程序的标准"方法是什么?

Is this safe to use? What is the "standard" way to create a windowless OpenGL program?

我使用 FBO 进行离屏渲染.

I'm using FBO for the offscreen rendering.

推荐答案

纯无窗口 OpenGL 的旧方法是使用 PBuffer.在 Windows 上,这需要使用常规窗口创建中间 OpenGL 上下文以获得所需的扩展函数指针.在 X11/GLX 上,它可以毫不费力地工作.

The old method for purely windowless OpenGL is using a PBuffer. On Windows this requires the creation of a intermediate OpenGL context using a regular window to obtain the required extension function pointers. On X11/GLX it works without further ado.

实现离屏渲染的现代方法是使用常规但隐藏的窗口,其中包含常用的 OpenGL 上下文和 FBO 作为渲染目标.

The modern way to implement off-screen rendering is using a regular, but hidden window with the usual OpenGL context and a FBO as render target.

最前沿但支持度不高的方法(某些嵌入式设备除外)是使用 EGL 创建可绘制对象.

The bleeding edge, and yet not very well supported method (except on certain embedded devices) is using EGL for drawable creation.

相关文章