无窗口OpenGL [英] Windowless OpenGL

查看:204
本文介绍了无窗口OpenGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有一个无窗口的OpenGL上下文(在具有Xorg和Windows的GNU / Linux上)。我不会渲染任何东西,而只会调用 glGetString glCompileShader 之类的函数。

I would like to have a windowless OpenGL context (on both GNU/linux with Xorg and Windows). I'm not going to render anything but only call functions like glGetString, glCompileShader and similar.

我做了些细想,但除了创建一个隐藏的窗口外,没有提出任何有用的建议;

I've done some goggling but not come up with anything useful, except creating a hidden window; which seems like a hack to me.

那么对于任何平台,有没有人有更好的主意?

So does anyone have a better idea (for any platform)?

编辑:使用Xorg,我能够创建OpenGL上下文并将其附加到根窗口:

With Xorg I was able to create and attach an OpenGL context to the root-window:

#include<stdio.h>
#include<stdlib.h>
#include<X11/X.h>
#include<X11/Xlib.h>
#include<GL/gl.h>
#include<GL/glx.h>

int main(int argc, const char* argv[]){
  Display *dpy;
  Window root;
  GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
  XVisualInfo *vi;
  GLXContext glc;

  dpy = XOpenDisplay(NULL);

  if ( !dpy ) {
    printf("\n\tcannot connect to X server\n\n");
    exit(0);
  }

  root = DefaultRootWindow(dpy);
  vi = glXChooseVisual(dpy, 0, att);

  if (!vi) {
    printf("\n\tno appropriate visual found\n\n");
    exit(0);
  }

  glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
  glXMakeCurrent(dpy, root, glc);

  printf("vendor: %s\n", (const char*)glGetString(GL_VENDOR));

  return 0;
}

EDIT2:我写了一篇有关无窗口opengl (带有示例代码)基于已接受的答案。

I've written a short article about windowless opengl (with sample code) based on the accepted answer.

推荐答案

实际上,必须要有一个窗口句柄才能创建传统的渲染上下文(X11上的根窗口或Windows上的桌面窗口对此非常有用)。

Actually, it is necessary to have a window handle to create a "traditional" rendering context (the root window on X11 or the desktop window on Windows are good for this). It is used to fetch OpenGL information and extentions availability.

一旦获得了这些信息,就可以破坏渲染上下文并释放虚拟窗口!

Once you got that information, you can destroy the render context and release the "dummy" window!

您应该测试扩展名 ARB_extensions_string ARB_create_context_profile ,(在以下页面中进行介绍: ARB_create_context )。然后,您可以通过以独立于平台的方式调用 CreateContextAttribs 来创建渲染上下文,而无需关联系统窗口并且仅需要系统设备上下文:

You should test for the extensions ARB_extensions_string and ARB_create_context_profile, (described in these page: ARB_create_context). Then, you can create a render context by calling CreateContextAttribs, in a platform independent way, without having a system window associated and requiring only the system device context:

        int[] mContextAttrib = new int[] {
            Wgl.CONTEXT_MAJOR_VERSION, REQUIRED_OGL_VERSION_MAJOR,
            Wgl.CONTEXT_MINOR_VERSION, REQUIRED_OGL_VERSION_MINOR,
            Wgl.CONTEXT_PROFILE_MASK, (int)(Wgl.CONTEXT_CORE_PROFILE_BIT),
            Wgl.CONTEXT_FLAGS, (int)(Wgl.CONTEXT_FORWARD_COMPATIBLE_BIT),
            0
        };


        if ((mRenderContext = Wgl.CreateContextAttribs(mDeviceContext, pSharedContext, mContextAttrib)) == IntPtr.Zero)
            throw new Exception("unable to create context");

然后,如果您将帧缓冲区对象或系统窗口与创建的渲染上下文关联,则可以希望渲染(但据我了解,您只想编译着色器)。

Then, you could associate a frame buffer object or a system window to the created render context, if you wish to render (but as I understand, you want to compile only shaders).

使用 CreateContextAttribs 有很多优点:


  • 它是与平台无关的

  • 可以请求特定的OpenGL实现

  • 可以请求> 3.2 OpenGL实现

  • 可以强制前进兼容性选项(仅着色器,这是未来的方式)

  • 可以选择(仅在向前兼容的环境中) a特定的OpenGL实现配置文件(实际上只有CORE配置文件,但将来可能还会更多。

  • 可以启用调试选项 >,即使未定义h因为此选项可以由实际的驱动程序实现使用

  • It is platform independent
  • It's possible to request specific OpenGL implementation
  • It's possible to request a > 3.2 OpenGL implementation
  • It's possible to force the forward compatibility option (shader only rendering, that's the future way)
  • It's possible to select (in a forward compatible context only) a specific OpenGL implementation profile (actually there is only the CORE profile, but there could be more in the future.
  • It's possible to enable a debugging option, even if it isn't defined how this option could be used by the actual driver implementation

较早的硬件/驱动程序无法实现此扩展,实际上我建议编写一个后备代码以创建向后兼容的上下文。

这篇关于无窗口OpenGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆