Opengl/windowless

In general it isn't possible to create an OpenGL context without having a window; Which can be quite annoying depending on what you want to archive. More than once have I wished for a way to either call non-rendering functions (like glGetString) or be able to render to a framebuffer and read the result back into a buffer.

= Non-rendering context =

A simple way to get an OpenGL context is to use the root window in GLX (DefaultRootWindow) or the desktop window (GetDesktopWindow) in Windows. While you're not allowed to do any rendering using this context it is enough to call functions like glGetString.


 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 
 * 5) include 
 * 6) include 

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

/* open display */ if ( ! (dpy = XOpenDisplay(NULL)) ) { fprintf(stderr, "cannot connect to X server\n\n"); exit(1); }

/* get root window */ root = DefaultRootWindow(dpy);

/* get visual matching attr */ if( ! (vi = glXChooseVisual(dpy, 0, attr)) ) { fprintf(stderr, "no appropriate visual found\n\n"); exit(1); }

/* create a context using the root window */ if ( ! (glc = glXCreateContext(dpy, vi, NULL, GL_TRUE)) ){ fprintf(stderr, "failed to create context\n\n"); exit(1); }       glXMakeCurrent(dpy, root, glc);

/* try it out, remember to *NOT* render to the default framebuffer! */       printf("vendor: %s\n", (const char*)glGetString(GL_VENDOR));

return 0; }

Compile with: gcc -Wall -lGL windowless1.c

= Offscreen rendering only =

Since OpenGL3 a new extension called ARB_create_context gives a new API to create an OpenGL context; This is the API needed to actually create an OpenGL3 context and it also allows us not to pass a window to MakeCurrent. The description to MakeCurrent states that if you pass 0 as both read- and write-drawable "the context is made current without a default framebuffer".


 * 1) include <stdio.h>
 * 2) include <stdlib.h>
 * 3) include <X11/X.h>
 * 4) include <X11/Xlib.h>
 * 5) include <GL/gl.h>
 * 6) include <GL/glx.h>

typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*); typedef Bool (*glXMakeContextCurrentARBProc)(Display*, GLXDrawable, GLXDrawable, GLXContext); static glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0; static glXMakeContextCurrentARBProc glXMakeContextCurrentARB = 0;

int main(int argc, const char* argv[]){ static int visual_attribs[] = { None };       int context_attribs[] = { GLX_CONTEXT_MAJOR_VERSION_ARB, 3, GLX_CONTEXT_MINOR_VERSION_ARB, 0, None };

Display* dpy = XOpenDisplay(0); int fbcount = 0; GLXFBConfig* fbc = NULL; GLXContext ctx; GLXPbuffer pbuf;

/* open display */ if ( ! (dpy = XOpenDisplay(0)) ){ fprintf(stderr, "Failed to open display\n"); exit(1); }

/* get framebuffer configs, any is usable (might want to add proper attribs) */ if ( !(fbc = glXChooseFBConfig(dpy, DefaultScreen(dpy), visual_attribs, &fbcount) ) ){ fprintf(stderr, "Failed to get FBConfig\n"); exit(1); }

/* get the required extensions */ glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB"); glXMakeContextCurrentARB = (glXMakeContextCurrentARBProc)glXGetProcAddressARB( (const GLubyte *) "glXMakeContextCurrent"); if ( !(glXCreateContextAttribsARB && glXMakeContextCurrentARB) ){ fprintf(stderr, "missing support for GLX_ARB_create_context\n"); XFree(fbc); exit(1); }

/* create a context using glXCreateContextAttribsARB */ if ( !( ctx = glXCreateContextAttribsARB(dpy, fbc[0], 0, True, context_attribs)) ){ fprintf(stderr, "Failed to create opengl context\n"); XFree(fbc); exit(1); }

/* create temporary pbuffer */ int pbuffer_attribs[] = { GLX_PBUFFER_WIDTH, 800, GLX_PBUFFER_HEIGHT, 600, None };       pbuf = glXCreatePbuffer(dpy, fbc[0], pbuffer_attribs);

XFree(fbc); XSync(dpy, False);

/* try to make it the current context */ if ( !glXMakeContextCurrent(dpy, pbuf, pbuf, ctx) ){ /* some drivers does not support context without default framebuffer, so fallback on                * using the default window. */               if ( !glXMakeContextCurrent(dpy, DefaultRootWindow(dpy), DefaultRootWindow(dpy), ctx) ){ fprintf(stderr, "failed to make current\n"); exit(1); }       }

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

return 0; }

Compile with: gcc -Wall -lGL windowless2.c