Brian Paul | f02a5f6 | 2002-07-12 15:54:01 +0000 | [diff] [blame^] | 1 | /* $Id: glxpixmap.c,v 1.3 2002/07/12 15:54:02 brianp Exp $ */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | /* |
| 5 | * A demonstration of using the GLXPixmap functions. This program is in |
| 6 | * the public domain. |
| 7 | * |
| 8 | * Brian Paul |
| 9 | */ |
| 10 | |
| 11 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 12 | #include <GL/gl.h> |
Brian Paul | f02a5f6 | 2002-07-12 15:54:01 +0000 | [diff] [blame^] | 13 | #define GLX_GLXEXT_PROTOTYPES |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 14 | #include <GL/glx.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 17 | #include <string.h> |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | static GLXContext ctx; |
| 21 | static XVisualInfo *visinfo; |
| 22 | static GC gc; |
| 23 | |
| 24 | |
| 25 | |
| 26 | static Window make_rgb_window( Display *dpy, |
| 27 | unsigned int width, unsigned int height ) |
| 28 | { |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 29 | const int sbAttrib[] = { GLX_RGBA, |
| 30 | GLX_RED_SIZE, 1, |
| 31 | GLX_GREEN_SIZE, 1, |
| 32 | GLX_BLUE_SIZE, 1, |
| 33 | None }; |
| 34 | const int dbAttrib[] = { GLX_RGBA, |
| 35 | GLX_RED_SIZE, 1, |
| 36 | GLX_GREEN_SIZE, 1, |
| 37 | GLX_BLUE_SIZE, 1, |
| 38 | GLX_DOUBLEBUFFER, |
| 39 | None }; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 40 | int scrnum; |
| 41 | XSetWindowAttributes attr; |
| 42 | unsigned long mask; |
| 43 | Window root; |
| 44 | Window win; |
| 45 | |
| 46 | scrnum = DefaultScreen( dpy ); |
| 47 | root = RootWindow( dpy, scrnum ); |
| 48 | |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 49 | visinfo = glXChooseVisual( dpy, scrnum, (int *) sbAttrib ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 50 | if (!visinfo) { |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 51 | visinfo = glXChooseVisual( dpy, scrnum, (int *) dbAttrib ); |
| 52 | if (!visinfo) { |
| 53 | printf("Error: couldn't get an RGB visual\n"); |
| 54 | exit(1); |
| 55 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* window attributes */ |
| 59 | attr.background_pixel = 0; |
| 60 | attr.border_pixel = 0; |
| 61 | /* TODO: share root colormap if possible */ |
| 62 | attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone); |
| 63 | attr.event_mask = StructureNotifyMask | ExposureMask; |
| 64 | mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; |
| 65 | |
| 66 | win = XCreateWindow( dpy, root, 0, 0, width, height, |
| 67 | 0, visinfo->depth, InputOutput, |
| 68 | visinfo->visual, mask, &attr ); |
| 69 | |
| 70 | /* make an X GC so we can do XCopyArea later */ |
| 71 | gc = XCreateGC( dpy, win, 0, NULL ); |
| 72 | |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 73 | /* need indirect context */ |
| 74 | ctx = glXCreateContext( dpy, visinfo, NULL, False ); |
| 75 | if (!ctx) { |
| 76 | printf("Error: glXCreateContext failed\n"); |
| 77 | exit(-1); |
| 78 | } |
| 79 | |
| 80 | printf("Direct rendering: %s\n", glXIsDirect(dpy, ctx) ? "Yes" : "No"); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 81 | |
| 82 | return win; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | static GLXPixmap make_pixmap( Display *dpy, Window win, |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 87 | unsigned int width, unsigned int height, |
| 88 | Pixmap *pixmap) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 89 | { |
| 90 | Pixmap pm; |
| 91 | GLXPixmap glxpm; |
| 92 | XWindowAttributes attr; |
| 93 | |
| 94 | pm = XCreatePixmap( dpy, win, width, height, visinfo->depth ); |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 95 | if (!pm) { |
| 96 | printf("Error: XCreatePixmap failed\n"); |
| 97 | exit(-1); |
| 98 | } |
| 99 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 100 | XGetWindowAttributes( dpy, win, &attr ); |
| 101 | |
| 102 | /* |
| 103 | * IMPORTANT: |
| 104 | * Use the glXCreateGLXPixmapMESA funtion when using Mesa because |
| 105 | * Mesa needs to know the colormap associated with a pixmap in order |
| 106 | * to render correctly. This is because Mesa allows RGB rendering |
| 107 | * into any kind of visual, not just TrueColor or DirectColor. |
| 108 | */ |
| 109 | #ifdef GLX_MESA_pixmap_colormap |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 110 | if (strstr(glXQueryExtensionsString(dpy, 0), "GLX_MESA_pixmap_colormap")) { |
| 111 | /* stand-alone Mesa, specify the colormap */ |
| 112 | glxpm = glXCreateGLXPixmapMESA( dpy, visinfo, pm, attr.colormap ); |
| 113 | } |
| 114 | else { |
| 115 | glxpm = glXCreateGLXPixmap( dpy, visinfo, pm ); |
| 116 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 117 | #else |
| 118 | /* This will work with Mesa too if the visual is TrueColor or DirectColor */ |
| 119 | glxpm = glXCreateGLXPixmap( dpy, visinfo, pm ); |
| 120 | #endif |
| 121 | |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 122 | if (!glxpm) { |
| 123 | printf("Error: GLXCreateGLXPixmap failed\n"); |
| 124 | exit(-1); |
| 125 | } |
| 126 | |
| 127 | *pixmap = pm; |
| 128 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 129 | return glxpm; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | static void event_loop( Display *dpy, GLXPixmap pm ) |
| 135 | { |
| 136 | XEvent event; |
| 137 | |
| 138 | while (1) { |
| 139 | XNextEvent( dpy, &event ); |
| 140 | |
| 141 | switch (event.type) { |
| 142 | case Expose: |
| 143 | printf("Redraw\n"); |
| 144 | /* copy the image from GLXPixmap to window */ |
| 145 | XCopyArea( dpy, pm, event.xany.window, /* src, dest */ |
| 146 | gc, 0, 0, 300, 300, /* gc, src pos, size */ |
| 147 | 0, 0 ); /* dest pos */ |
| 148 | break; |
| 149 | case ConfigureNotify: |
| 150 | /* nothing */ |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | |
| 158 | int main( int argc, char *argv[] ) |
| 159 | { |
| 160 | Display *dpy; |
| 161 | Window win; |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 162 | Pixmap pm; |
| 163 | GLXPixmap glxpm; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 164 | |
| 165 | dpy = XOpenDisplay(NULL); |
| 166 | |
| 167 | win = make_rgb_window( dpy, 300, 300 ); |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 168 | glxpm = make_pixmap( dpy, win, 300, 300, &pm ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 169 | |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 170 | glXMakeCurrent( dpy, glxpm, ctx ); |
| 171 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 172 | |
| 173 | /* Render an image into the pixmap */ |
| 174 | glShadeModel( GL_FLAT ); |
| 175 | glClearColor( 0.5, 0.5, 0.5, 1.0 ); |
| 176 | glClear( GL_COLOR_BUFFER_BIT ); |
| 177 | glViewport( 0, 0, 300, 300 ); |
| 178 | glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 ); |
| 179 | glColor3f( 0.0, 1.0, 1.0 ); |
| 180 | glRectf( -0.75, -0.75, 0.75, 0.75 ); |
| 181 | glFlush(); |
| 182 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 183 | XMapWindow( dpy, win ); |
| 184 | |
| 185 | event_loop( dpy, pm ); |
| 186 | return 0; |
| 187 | } |