Sign in to follow this  
PrincessDaisy

xWindows and OpenGL

Recommended Posts

I am looking into using xWindows as the window manager for my linux based engine, but I am having a little trouble getting some detailed tutorials. Ideally, what I want is to create the window using xWindows in one class, then in my OpenGL render class, attach the windows handle to GL, to create the required context. This is how I do it when using win32 based windows, so I want to keep them the same. Unfortunatly, all the tutorials use glx (which I have no problem using) but the xWindow creation code and OpenGL creation code are very tightly intermingled, making it difficult to see how to seperate the two processes. Is this possible? Thanks Daisy

Share this post


Link to post
Share on other sites
There is a coupling between the OpenGL and the window creation: The XVisualInfo is to be created by already considering GLX flags. AFAIK only a window created that way is able to get a GL context attached. So IMHO you could not separate the two steps totally.

I personally am using an abstract GfxSupport object where requirements (depth buffer, bouble buffering, ...) are claimed. The GLX version of that class produces an appropriate XVisualInfo. Then the GLX version of the GfxSurface class is able to open either a new window or a full-screen surface, etc.

Some snippets:

::XVisualInfo*
GLXSupport::newXVisualInfo() const {
uint16_t colorBits = claimedColorBits();
int attributeList[] = {
GLX_RGBA,
GLX_RED_SIZE,colorBits,
GLX_GREEN_SIZE,colorBits,
GLX_BLUE_SIZE,colorBits,
GLX_DEPTH_SIZE,claimedDepthBits(),
claimedFrameBuffers()>=2 ? GLX_DOUBLEBUFFER : None,
None
};
return ::glXChooseVisual(video()->theXDisplay(),video()->theXScreen(),attributeList);
}

GLXSurface::GLXSurface(const X11Video* video,::XVisualInfo* visualInfo,::GLXContext sharedContext) { ... }

GLXSurface*
GLXSupport::newSurfaceInWindow(uint16_t width,uint16_t height,const char* title) const {
GLXSurface* surface = newOnscreenSurface();
if(surface) {
::Window window = video()->newWindowFor(surface->theXVisualInfo(),width,height,title);
if(window) {
surface->setup(window);
video()->mapAndRaise(window);
} else {
surface->destruct();
surface = static_cast<GLXSurface*>(0);
}
}
return surface;
}

::Window
X11Video::newWindowFor(const ::XVisualInfo* visualInfo,uint16_t width,uint16_t height,const char* title) const {
// the window attributes validation mask
static int attributesMask = CWBackPixel|CWBorderPixel|CWColormap|CWEventMask;
// temporary variables
::Window window = 0;
::XSetWindowAttributes attributes;
::Window rootWindow = RootWindow(theXDisplay(),_theXScreen);
// set up the window attributes
attributes.background_pixel = BlackPixel(theXDisplay(),_theXScreen);
attributes.border_pixel = 0;
///attributes.save_under = False;
///attributes.backing_store = NotUseful;
attributes.colormap = ::XCreateColormap(theXDisplay(),rootWindow,visualInfo->visual,AllocNone);
attributes.event_mask =
StructureNotifyMask |
FocusChangeMask |
KeyPressMask |
KeyReleaseMask |
ButtonPressMask |
ButtonReleaseMask |
PointerMotionMask |
ExposureMask |
VisibilityChangeMask;
// create the window
window = ::XCreateWindow(theXDisplay(),rootWindow,0,0,width,height,0,visualInfo->depth,InputOutput,
visualInfo->visual,attributesMask,&attributes);
if(window && title) {
::XStoreName(theXDisplay(),window,title);
}
// done
return window;
}


As you may see from the snippets, the XVisualInfo is the one and only direct coupling here.

[Edited by - haegarr on March 27, 2006 7:36:01 AM]

Share this post


Link to post
Share on other sites
Hi Daisy,

I've done some opengl programming in both win32 and xwindows, and I think I know what your talking about. I think you mean you have to make glx calls before you create your new xwindow, and when you work in win32 you can create a window and then make wgl calls to set up your opengl drawing. I think you're gonna want to combine your window creation and opengl setup into some sort of overall creation step in your program like create_win32_opengl_window or create_xwindow_opengl_window. You're working in 2 different OS's anyway that have different ways to make a window and different ways to set up opengl, so you may as well just lump them together and then you can let the rest of your code focus on making the opengl calls which can be the same on either platform.

Opengl Programming for the X Window System has a sample called glxsimple.c that you can search for on the web. It has a very straight-forward non-GLUT sample. The book is the green version of the red and blue opengl books, it's written by Mark J. Kilgard.

Linux 3D Graphics Programming by Norman Lin is another good book that covers both xwindows and opengl. These are the only 2 books I know that do Opengl on XWindows.

Hope this helps

Share this post


Link to post
Share on other sites
Sign in to follow this