1 /* $Id: glxpixmap.c,v 1.1 1999/08/19 00:55:43 jtg Exp $ */
5 * A demonstration of using the GLXPixmap functions. This program is in
13 * $Id: glxpixmap.c,v 1.1 1999/08/19 00:55:43 jtg Exp $
24 static GLXContext ctx
;
25 static XVisualInfo
*visinfo
;
30 static Window
make_rgb_window( Display
*dpy
,
31 unsigned int width
, unsigned int height
)
33 int attrib
[] = { GLX_RGBA
,
39 XSetWindowAttributes attr
;
44 scrnum
= DefaultScreen( dpy
);
45 root
= RootWindow( dpy
, scrnum
);
47 visinfo
= glXChooseVisual( dpy
, scrnum
, attrib
);
49 printf("Error: couldn't get an RGB, Double-buffered visual\n");
53 /* window attributes */
54 attr
.background_pixel
= 0;
55 attr
.border_pixel
= 0;
56 /* TODO: share root colormap if possible */
57 attr
.colormap
= XCreateColormap( dpy
, root
, visinfo
->visual
, AllocNone
);
58 attr
.event_mask
= StructureNotifyMask
| ExposureMask
;
59 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
61 win
= XCreateWindow( dpy
, root
, 0, 0, width
, height
,
62 0, visinfo
->depth
, InputOutput
,
63 visinfo
->visual
, mask
, &attr
);
65 /* make an X GC so we can do XCopyArea later */
66 gc
= XCreateGC( dpy
, win
, 0, NULL
);
68 ctx
= glXCreateContext( dpy
, visinfo
, NULL
, True
);
74 static GLXPixmap
make_pixmap( Display
*dpy
, Window win
,
75 unsigned int width
, unsigned int height
)
79 XWindowAttributes attr
;
81 pm
= XCreatePixmap( dpy
, win
, width
, height
, visinfo
->depth
);
82 XGetWindowAttributes( dpy
, win
, &attr
);
86 * Use the glXCreateGLXPixmapMESA funtion when using Mesa because
87 * Mesa needs to know the colormap associated with a pixmap in order
88 * to render correctly. This is because Mesa allows RGB rendering
89 * into any kind of visual, not just TrueColor or DirectColor.
91 #ifdef GLX_MESA_pixmap_colormap
92 glxpm
= glXCreateGLXPixmapMESA( dpy
, visinfo
, pm
, attr
.colormap
);
94 /* This will work with Mesa too if the visual is TrueColor or DirectColor */
95 glxpm
= glXCreateGLXPixmap( dpy
, visinfo
, pm
);
103 static void event_loop( Display
*dpy
, GLXPixmap pm
)
108 XNextEvent( dpy
, &event
);
110 switch (event
.type
) {
113 /* copy the image from GLXPixmap to window */
114 XCopyArea( dpy
, pm
, event
.xany
.window
, /* src, dest */
115 gc
, 0, 0, 300, 300, /* gc, src pos, size */
116 0, 0 ); /* dest pos */
118 case ConfigureNotify
:
127 int main( int argc
, char *argv
[] )
133 dpy
= XOpenDisplay(NULL
);
135 win
= make_rgb_window( dpy
, 300, 300 );
136 pm
= make_pixmap( dpy
, win
, 300, 300 );
139 glXMakeCurrent( dpy
, win
, ctx
); /*to make sure ctx is properly initialized*/
142 glXMakeCurrent( dpy
, pm
, ctx
);
144 /* Render an image into the pixmap */
145 glShadeModel( GL_FLAT
);
146 glClearColor( 0.5, 0.5, 0.5, 1.0 );
147 glClear( GL_COLOR_BUFFER_BIT
);
148 glViewport( 0, 0, 300, 300 );
149 glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
150 glColor3f( 0.0, 1.0, 1.0 );
151 glRectf( -0.75, -0.75, 0.75, 0.75 );
154 /* when a redraw is needed we'll just copy the pixmap image to the window */
156 XMapWindow( dpy
, win
);
158 event_loop( dpy
, pm
);