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;
}
void
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
");
exit(1);
}
printf("dpy = %p
", dpy);
if(!glXQueryExtension(dpy, &dummy, &dummy)){
fprintf(stderr, "could not open display");
exit(1);
}
/* 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
");
exit(1);
}
/* create an OpenGL rendering context */
cx = glXCreateContext(dpy, vi, None, GL_TRUE);
if (cx == NULL) {
fprintf(stderr, "could not create rendering context
");
exit(1);
}
/* create an X colormap since probably not using default visual */
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
vi->visual, AllocNone);
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = ExposureMask | KeyPressMask | StructureNotifyMask;
glwin = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, WIDTH,
HEIGHT, 0, vi->depth, InputOutput, vi->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->depth, InputOutput, vi->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;
break;
case XK_Right:
longinc -= 0.5;
break;
case XK_Up:
latinc += 0.5;
break;
case XK_Down:
latinc -= 0.5;
break;
}
break;
case ConfigureNotify:
resize(event.xconfigure.width, event.xconfigure.height);
break;
}
}
drawScene();
}
}
/* 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
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far);
glOrtho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
GLvoid initializeGL(GLsizei width, GLsizei height)
{
}
GLvoid drawScene(GLvoid)
{
// Clear the buffer
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 1.0f); // blue
glBegin(GL_POLYGON);
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);
glEnd();
SWAPBUFFERS;
}
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.