PDA

View Full Version : X Window/GLX context



Lindley
07-15-2009, 01:08 PM
I need to create an X window and use GLX to attach an OpenGL context to it. The context creation is working fine; however, the window isn't showing up on the screen. What am I doing wrong?



Display *disp;
disp=XOpenDisplay(NULL);
XVisualInfo *vinfo;
int screen;
int glx_attr[32];
int glx_nattr;
screen = DefaultScreen(disp);
glx_nattr=0;
glx_attr[glx_nattr++]=GLX_RGBA;
glx_attr[glx_nattr++]=GLX_RED_SIZE;
glx_attr[glx_nattr++]=1;
glx_attr[glx_nattr++]=GLX_GREEN_SIZE;
glx_attr[glx_nattr++]=1;
glx_attr[glx_nattr++]=GLX_BLUE_SIZE;
glx_attr[glx_nattr++]=1;
glx_attr[glx_nattr++]=GLX_ALPHA_SIZE;
glx_attr[glx_nattr++]=1;
glx_attr[glx_nattr++]=GLX_DEPTH_SIZE;
glx_attr[glx_nattr++]=12;
glx_attr[glx_nattr++]=GLX_STENCIL_SIZE;
glx_attr[glx_nattr++]=1;
glx_attr[glx_nattr++]=GLX_DOUBLEBUFFER;
glx_attr[glx_nattr++]=None;
vinfo=glXChooseVisual(disp,screen,glx_attr);
Window win;
XSetWindowAttributes win_attr;
unsigned long win_attr_mask;
win_attr.event_mask=0;
win_attr.background_pixmap=None;
win_attr.background_pixel=0;
win_attr.border_pixel=0;
win_attr.colormap=XCreateColormap(disp,RootWindow( disp,screen),
vinfo->visual,AllocNone);
win_attr.override_redirect=True;
win_attr_mask=CWBackPixmap|CWBorderPixel|CWColorma p|CWEventMask|
CWOverrideRedirect;
win=XCreateWindow(disp,RootWindow(disp,screen),0,0 ,_w,_h,0,vinfo->depth,
InputOutput,vinfo->visual,win_attr_mask,&win_attr);
GLXContext glxctx;
glxctx=glXCreateContext(disp,vinfo,NULL,True);
XFree(vinfo);
glXMakeCurrent(disp,win,glxctx);
XMapWindow(disp,win);
XFlush(disp);
XEvent e;
while(XCheckMaskEvent(disp,~NoEventMask,&e));

while (1) {
// OpenGL drawing to GL_BACK here

glFlush();
XFlush(disp);
glXSwapBuffers(disp,win);
while(XCheckMaskEvent(disp,~NoEventMask,&e));
}

Lindley
07-15-2009, 02:05 PM
I've added the call XSelectInput(disp,win,StructureNotifyMask); prior to the XMapWindow() call. However, this seems to generate both a MapNotify and an UnmapNotify event. Why might this happen?

Also, what could cause glXChooseVisual() to fail given those parameters? The documentation doesn't say anything about causes other than bad parameters causing it to return null, but it certainly is doing so.....sometimes. Seems unpredictable really.

chris2718
07-15-2009, 11:30 PM
glx_attr[glx_nattr++]=GLX_RED_SIZE;
glx_attr[glx_nattr++]=1;
glx_attr[glx_nattr++]=GLX_GREEN_SIZE;
glx_attr[glx_nattr++]=1;
glx_attr[glx_nattr++]=GLX_BLUE_SIZE;
glx_attr[glx_nattr++]=1;

3 bit color. what is this? Try to make a simple window that is just RGBA 8 bit each component. No accum, double buffer, etc.

Dark Photon
07-20-2009, 05:57 PM
I need to create an X window and use GLX to attach an OpenGL context to it. The context creation is working fine; however, the window isn't showing up on the screen. What am I doing wrong?
I'm no override redirect guru (haven't done native Xlib prog in a decade), but best guess: you may not be waiting until you get a MapWindow event (not sure if that's required), try dropping CWOverrideRedirect, try adding StructureNotifyMask to your win_attr.event_mask (prob won't get a MapWindow even until you do), and make sure your _w and _h are what you think they are. Failing this, look in something like libtk or other to see how they handle override redirect windows. Maybe freeglut has it as well (?)

Also, if you can't get that to work, maybe you just want to turn off the window manager decorations and pop your window to the front of the stacking order? If so, the former can be had via setting _MOTIF_WM_HINTS. I can post or just google for the magic via: _MOTIF_WM_HINTS MWM_HINTS_DECORATIONS PROP_MOTIF_WM_HINTS_ELEMENT. glutFullScreen does this, so check out freeglut for one copy-paste source.