jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1 | |
| 2 | |
| 3 | /* |
| 4 | * A demonstration of using the GLXPixmap functions. This program is in |
| 5 | * the public domain. |
| 6 | * |
| 7 | * Brian Paul |
| 8 | */ |
| 9 | |
| 10 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 11 | #include <GL/gl.h> |
Brian Paul | f02a5f6 | 2002-07-12 15:54:01 +0000 | [diff] [blame] | 12 | #define GLX_GLXEXT_PROTOTYPES |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 13 | #include <GL/glx.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 16 | #include <string.h> |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | static GLXContext ctx; |
| 20 | static XVisualInfo *visinfo; |
| 21 | static GC gc; |
| 22 | |
| 23 | |
| 24 | |
| 25 | static Window make_rgb_window( Display *dpy, |
| 26 | unsigned int width, unsigned int height ) |
| 27 | { |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 28 | const int sbAttrib[] = { GLX_RGBA, |
| 29 | GLX_RED_SIZE, 1, |
| 30 | GLX_GREEN_SIZE, 1, |
| 31 | GLX_BLUE_SIZE, 1, |
| 32 | None }; |
| 33 | const int dbAttrib[] = { GLX_RGBA, |
| 34 | GLX_RED_SIZE, 1, |
| 35 | GLX_GREEN_SIZE, 1, |
| 36 | GLX_BLUE_SIZE, 1, |
| 37 | GLX_DOUBLEBUFFER, |
| 38 | None }; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 39 | int scrnum; |
| 40 | XSetWindowAttributes attr; |
| 41 | unsigned long mask; |
| 42 | Window root; |
| 43 | Window win; |
| 44 | |
| 45 | scrnum = DefaultScreen( dpy ); |
| 46 | root = RootWindow( dpy, scrnum ); |
| 47 | |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 48 | visinfo = glXChooseVisual( dpy, scrnum, (int *) sbAttrib ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 49 | if (!visinfo) { |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 50 | visinfo = glXChooseVisual( dpy, scrnum, (int *) dbAttrib ); |
| 51 | if (!visinfo) { |
| 52 | printf("Error: couldn't get an RGB visual\n"); |
| 53 | exit(1); |
| 54 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 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; |
| 64 | |
| 65 | win = XCreateWindow( dpy, root, 0, 0, width, height, |
| 66 | 0, visinfo->depth, InputOutput, |
| 67 | visinfo->visual, mask, &attr ); |
| 68 | |
| 69 | /* make an X GC so we can do XCopyArea later */ |
| 70 | gc = XCreateGC( dpy, win, 0, NULL ); |
| 71 | |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 72 | /* need indirect context */ |
| 73 | ctx = glXCreateContext( dpy, visinfo, NULL, False ); |
| 74 | if (!ctx) { |
| 75 | printf("Error: glXCreateContext failed\n"); |
| 76 | exit(-1); |
| 77 | } |
| 78 | |
| 79 | printf("Direct rendering: %s\n", glXIsDirect(dpy, ctx) ? "Yes" : "No"); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 80 | |
| 81 | return win; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static GLXPixmap make_pixmap( Display *dpy, Window win, |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 86 | unsigned int width, unsigned int height, |
| 87 | Pixmap *pixmap) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 88 | { |
| 89 | Pixmap pm; |
| 90 | GLXPixmap glxpm; |
| 91 | XWindowAttributes attr; |
| 92 | |
| 93 | pm = XCreatePixmap( dpy, win, width, height, visinfo->depth ); |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 94 | if (!pm) { |
| 95 | printf("Error: XCreatePixmap failed\n"); |
| 96 | exit(-1); |
| 97 | } |
| 98 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 99 | XGetWindowAttributes( dpy, win, &attr ); |
| 100 | |
| 101 | /* |
| 102 | * IMPORTANT: |
| 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. |
| 107 | */ |
| 108 | #ifdef GLX_MESA_pixmap_colormap |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 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 ); |
| 112 | } |
| 113 | else { |
| 114 | glxpm = glXCreateGLXPixmap( dpy, visinfo, pm ); |
| 115 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 116 | #else |
| 117 | /* This will work with Mesa too if the visual is TrueColor or DirectColor */ |
| 118 | glxpm = glXCreateGLXPixmap( dpy, visinfo, pm ); |
| 119 | #endif |
| 120 | |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 121 | if (!glxpm) { |
| 122 | printf("Error: GLXCreateGLXPixmap failed\n"); |
| 123 | exit(-1); |
| 124 | } |
| 125 | |
| 126 | *pixmap = pm; |
| 127 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 128 | return glxpm; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | static void event_loop( Display *dpy, GLXPixmap pm ) |
| 134 | { |
| 135 | XEvent event; |
| 136 | |
| 137 | while (1) { |
| 138 | XNextEvent( dpy, &event ); |
| 139 | |
| 140 | switch (event.type) { |
| 141 | case Expose: |
| 142 | printf("Redraw\n"); |
| 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 */ |
| 147 | break; |
| 148 | case ConfigureNotify: |
| 149 | /* nothing */ |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | |
| 157 | int main( int argc, char *argv[] ) |
| 158 | { |
| 159 | Display *dpy; |
| 160 | Window win; |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 161 | Pixmap pm; |
| 162 | GLXPixmap glxpm; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 163 | |
| 164 | dpy = XOpenDisplay(NULL); |
| 165 | |
| 166 | win = make_rgb_window( dpy, 300, 300 ); |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 167 | glxpm = make_pixmap( dpy, win, 300, 300, &pm ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 168 | |
Brian Paul | fb88eee | 2000-07-11 16:05:29 +0000 | [diff] [blame] | 169 | glXMakeCurrent( dpy, glxpm, ctx ); |
| 170 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 171 | |
| 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 ); |
| 180 | glFlush(); |
| 181 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 182 | XMapWindow( dpy, win ); |
| 183 | |
| 184 | event_loop( dpy, pm ); |
| 185 | return 0; |
| 186 | } |