4 * A demonstration of using the GLXPixmap functions. This program is in
12 #define GLX_GLXEXT_PROTOTYPES
19 static GLXContext ctx
;
20 static XVisualInfo
*visinfo
;
25 static Window
make_rgb_window( Display
*dpy
,
26 unsigned int width
, unsigned int height
)
28 const int sbAttrib
[] = { GLX_RGBA
,
33 const int dbAttrib
[] = { GLX_RGBA
,
40 XSetWindowAttributes attr
;
45 scrnum
= DefaultScreen( dpy
);
46 root
= RootWindow( dpy
, scrnum
);
48 visinfo
= glXChooseVisual( dpy
, scrnum
, (int *) sbAttrib
);
50 visinfo
= glXChooseVisual( dpy
, scrnum
, (int *) dbAttrib
);
52 printf("Error: couldn't get an RGB visual\n");
57 /* window attributes */
58 attr
.background_pixel
= 0;
59 attr
.border_pixel
= 0;
60 /* TODO: share root colormap if possible */
61 attr
.colormap
= XCreateColormap( dpy
, root
, visinfo
->visual
, AllocNone
);
62 attr
.event_mask
= StructureNotifyMask
| ExposureMask
;
63 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
65 win
= XCreateWindow( dpy
, root
, 0, 0, width
, height
,
66 0, visinfo
->depth
, InputOutput
,
67 visinfo
->visual
, mask
, &attr
);
69 /* make an X GC so we can do XCopyArea later */
70 gc
= XCreateGC( dpy
, win
, 0, NULL
);
72 /* need indirect context */
73 ctx
= glXCreateContext( dpy
, visinfo
, NULL
, False
);
75 printf("Error: glXCreateContext failed\n");
79 printf("Direct rendering: %s\n", glXIsDirect(dpy
, ctx
) ? "Yes" : "No");
85 static GLXPixmap
make_pixmap( Display
*dpy
, Window win
,
86 unsigned int width
, unsigned int height
,
91 XWindowAttributes attr
;
93 pm
= XCreatePixmap( dpy
, win
, width
, height
, visinfo
->depth
);
95 printf("Error: XCreatePixmap failed\n");
99 XGetWindowAttributes( dpy
, win
, &attr
);
103 * Use the glXCreateGLXPixmapMESA funtion when using Mesa because
104 * Mesa needs to know the colormap associated with a pixmap in order
105 * to render correctly. This is because Mesa allows RGB rendering
106 * into any kind of visual, not just TrueColor or DirectColor.
108 #ifdef GLX_MESA_pixmap_colormap
109 if (strstr(glXQueryExtensionsString(dpy
, 0), "GLX_MESA_pixmap_colormap")) {
110 /* stand-alone Mesa, specify the colormap */
111 glxpm
= glXCreateGLXPixmapMESA( dpy
, visinfo
, pm
, attr
.colormap
);
114 glxpm
= glXCreateGLXPixmap( dpy
, visinfo
, pm
);
117 /* This will work with Mesa too if the visual is TrueColor or DirectColor */
118 glxpm
= glXCreateGLXPixmap( dpy
, visinfo
, pm
);
122 printf("Error: GLXCreateGLXPixmap failed\n");
133 static void event_loop( Display
*dpy
, GLXPixmap pm
)
138 XNextEvent( dpy
, &event
);
140 switch (event
.type
) {
143 /* copy the image from GLXPixmap to window */
144 XCopyArea( dpy
, pm
, event
.xany
.window
, /* src, dest */
145 gc
, 0, 0, 300, 300, /* gc, src pos, size */
146 0, 0 ); /* dest pos */
148 case ConfigureNotify
:
157 int main( int argc
, char *argv
[] )
164 dpy
= XOpenDisplay(NULL
);
166 win
= make_rgb_window( dpy
, 300, 300 );
167 glxpm
= make_pixmap( dpy
, win
, 300, 300, &pm
);
169 glXMakeCurrent( dpy
, glxpm
, ctx
);
170 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
172 /* Render an image into the pixmap */
173 glShadeModel( GL_FLAT
);
174 glClearColor( 0.5, 0.5, 0.5, 1.0 );
175 glClear( GL_COLOR_BUFFER_BIT
);
176 glViewport( 0, 0, 300, 300 );
177 glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
178 glColor3f( 0.0, 1.0, 1.0 );
179 glRectf( -0.75, -0.75, 0.75, 0.75 );
182 XMapWindow( dpy
, win
);
184 event_loop( dpy
, pm
);