drawing problem

I have written a sample code with Opengl and X11 on Linux. But I am confused with multi-windows behavior because I don’t know opengl well. The code is as follow:


  • Example of an X Window System OpenGL program.
    #include <GL/glx.h>
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <X11/keysym.h>
    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
    #include <stdio.h>

/* X globals, defines, and prototypes */
Display *dpy;
Window glwin, glwinc;
static int attributes[] = {GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, None};

#define SWAPBUFFERS glXSwapBuffers(dpy, glwin)
#define WIDTH 600
#define HEIGHT 400

GLvoid resize(GLsizei, GLsizei);
GLvoid initializeGL(GLsizei, GLsizei);
GLvoid drawScene(GLvoid);

static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
return GL_TRUE;
return GL_FALSE;

main(int argc, char **argv)
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
GLXContext cx;
XEvent event;
GLboolean needRedraw = GL_FALSE, recalcModelView = GL_TRUE;
int dummy;

dpy = XOpenDisplay(NULL); 
if (dpy == NULL){ 
    fprintf(stderr, "could not open display

printf("dpy = %p
", dpy);

if(!glXQueryExtension(dpy, &dummy, &dummy)){ 
    fprintf(stderr, "could not open display"); 

/* find an OpenGL-capable Color Index visual with depth buffer */ 
vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes); 
if (vi == NULL) { 
    fprintf(stderr, "could not get visual


/* create an OpenGL rendering context */ 
cx = glXCreateContext(dpy, vi,  None, GL_TRUE); 
if (cx == NULL) { 
    fprintf(stderr, "could not create rendering context


/* create an X colormap since probably not using default visual */ 
cmap = XCreateColormap(dpy, RootWindow(dpy, vi-&gt;screen),  
                            vi-&gt;visual, AllocNone); 
swa.colormap = cmap; 
swa.border_pixel = 0; 
swa.event_mask = ExposureMask | KeyPressMask | StructureNotifyMask; 
glwin = XCreateWindow(dpy, RootWindow(dpy, vi-&gt;screen), 0, 0, WIDTH, 
                    HEIGHT, 0, vi-&gt;depth, InputOutput, vi-&gt;visual, 
                    CWBorderPixel | CWColormap | CWEventMask, &swa); 

// glwin = XCreateSimpleWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, WIDTH,
// HEIGHT, CopyFromParent, CopyFromParent, CopyFromParent);

XSetStandardProperties(dpy, glwin, "xogl", "xogl", None, argv,  
                            argc, NULL); 

glXMakeCurrent(dpy, glwin, cx); 

XMapWindow(dpy, glwin); 
XIfEvent(dpy,  &event,  WaitForMapNotify,  (char *)glwin); 
initializeGL(WIDTH, HEIGHT); 
resize(WIDTH, HEIGHT); 

glwinc = XCreateWindow(dpy, glwin, 0, 0, WIDTH/4, 
                    HEIGHT/4, 0, vi-&gt;depth, InputOutput, vi-&gt;visual, 
                    CWBorderPixel | CWColormap | CWEventMask, &swa);

glXMakeCurrent(dpy, glwinc, cx); 

XMapWindow(dpy, glwinc); 
XIfEvent(dpy,  &event,  WaitForMapNotify,  (char *)glwinc); 
initializeGL(WIDTH, HEIGHT); 
resize(WIDTH, HEIGHT); 

/* Animation loop */
while (1) {
KeySym key;

while (XPending(dpy)) { 
    XNextEvent(dpy, &event); 
    switch (event.type) { 
    case KeyPress: 
            XLookupString((XKeyEvent *)&event, NULL, 0, &key, NULL); 
    switch (key) { 
    case XK_Left: 
        longinc += 0.5; 
    case XK_Right: 
        longinc -= 0.5; 
    case XK_Up: 
        latinc += 0.5; 
    case XK_Down: 
        latinc -= 0.5; 
    case ConfigureNotify:
    resize(event.xconfigure.width, event.xconfigure.height); 


/* OpenGL code */

GLvoid resize( GLsizei width, GLsizei height )
// Set viewport to window dimmension
glViewport(0, 0, width, height); // parameters: lower-left and w and h

// Reset coord system

// Establish clipping volume (left, right, bottom, top, near, far);
glOrtho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);



GLvoid initializeGL(GLsizei width, GLsizei height)

GLvoid drawScene(GLvoid)
// Clear the buffer

glColor3f(0.0f, 0.0f, 1.0f);	// blue

    glVertex3f(0.25f, 0.25f, 0.0f);
glVertex3f(0.75f, 0.25f, 0.0f);
glVertex3f(0.75f, 0.75f, 0.0f);
    glVertex3f(0.25f, 0.75f, 0.0f);



I want to Draw a Parent window and Child window on it and a button on the child window. But the behavior is not like that. If I use GL_TRUE in glXCreateContext() function call, I can see the black Parent window and a blue button on it. If I use GL_FALSE, I can see the black Parent window and a grey child window on it, but not button. I know something is wrong in my code, but I don’t know where it is. Can anybody knows that and tell me the solution? Thx a lot.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.