Skip to content

Instantly share code, notes, and snippets.

@kovrov
Created October 21, 2011 14:48
Show Gist options
  • Save kovrov/1304027 to your computer and use it in GitHub Desktop.
Save kovrov/1304027 to your computer and use it in GitHub Desktop.
// g++ gl3.cpp -o gl3 -lX11 -lGL
#include <GL/glx.h>
#include <GL/gl.h>
#include <iostream>
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
typedef GLXContext (glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
int main(int argc, char **argv)
{
glXCreateContextAttribsARBProc *glx_createContextAttribs = (glXCreateContextAttribsARBProc*)glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
if (glx_createContextAttribs == NULL) {
std::cout << "glXCreateContextAttribsARB entry point not found. Aborting." << std::endl;
return false;
}
Display *display = XOpenDisplay(0);
const char *extensions = glXQueryExtensionsString(display, DefaultScreen(display));
std::cout << extensions << std::endl;
std::cout << "Getting framebuffer config" << std::endl;
static int visual_attribs[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_DOUBLEBUFFER, true,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
None};
int fbcount;
GLXFBConfig *fbc = glXChooseFBConfig(display, DefaultScreen(display), visual_attribs, &fbcount);
if (!fbc) {
std::cout << "Failed to retrieve a framebuffer config" << std::endl;
return 1;
}
std::cout << "Getting XVisualInfo" << std::endl;
XVisualInfo *vi = glXGetVisualFromFBConfig(display, fbc[0]);
std::cout << "Creating colormap" << std::endl;
XSetWindowAttributes swa;
swa.colormap = XCreateColormap(display, RootWindow(display, vi->screen), vi->visual, AllocNone);
swa.border_pixel = 0;
swa.event_mask = StructureNotifyMask;
std::cout << "Creating window" << std::endl;
Window win = XCreateWindow(display, RootWindow(display, vi->screen), 0, 0, 100, 100, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel|CWColormap|CWEventMask, &swa);
if (!win) {
std::cout << "Failed to create window." << std::endl;
return 1;
}
std::cout << "Mapping window" << std::endl;
XMapWindow(display, win);
std::cout << "Creating context" << std::endl;
static int context_attribs[] = {
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 2,
None};
GLXContext ctx = glx_createContextAttribs(display, fbc[0], NULL, true, context_attribs);
if (!ctx) {
std::cout << "Failed to create GL3 context." << std::endl;
return 1;
}
std::cout << "Making context current" << std::endl;
glXMakeCurrent(display, win, ctx);
// testing
{
glClearColor (0, 0.5, 1, 1);
glClear (GL_COLOR_BUFFER_BIT);
glXSwapBuffers(display, win);
sleep(1);
glClearColor(1, 0.5, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glXSwapBuffers(display, win);
sleep(1);
}
std::cout << "Destroying context" << std::endl;
ctx = glXGetCurrentContext();
glXMakeCurrent(display, 0, 0);
glXDestroyContext(display, ctx);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment