prevent spaces in fp register names
[mesa.git] / progs / xdemos / glxpixmap.c
1 /* $Id: glxpixmap.c,v 1.1 1999/08/19 00:55:43 jtg Exp $ */
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
12 /*
13 * $Id: glxpixmap.c,v 1.1 1999/08/19 00:55:43 jtg Exp $
14 */
15
16
17 #include <GL/gl.h>
18 #include <GL/glx.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21
22
23
24 static GLXContext ctx;
25 static XVisualInfo *visinfo;
26 static GC gc;
27
28
29
30 static Window make_rgb_window( Display *dpy,
31 unsigned int width, unsigned int height )
32 {
33 int attrib[] = { GLX_RGBA,
34 GLX_RED_SIZE, 1,
35 GLX_GREEN_SIZE, 1,
36 GLX_BLUE_SIZE, 1,
37 None };
38 int scrnum;
39 XSetWindowAttributes attr;
40 unsigned long mask;
41 Window root;
42 Window win;
43
44 scrnum = DefaultScreen( dpy );
45 root = RootWindow( dpy, scrnum );
46
47 visinfo = glXChooseVisual( dpy, scrnum, attrib );
48 if (!visinfo) {
49 printf("Error: couldn't get an RGB, Double-buffered visual\n");
50 exit(1);
51 }
52
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;
60
61 win = XCreateWindow( dpy, root, 0, 0, width, height,
62 0, visinfo->depth, InputOutput,
63 visinfo->visual, mask, &attr );
64
65 /* make an X GC so we can do XCopyArea later */
66 gc = XCreateGC( dpy, win, 0, NULL );
67
68 ctx = glXCreateContext( dpy, visinfo, NULL, True );
69
70 return win;
71 }
72
73
74 static GLXPixmap make_pixmap( Display *dpy, Window win,
75 unsigned int width, unsigned int height )
76 {
77 Pixmap pm;
78 GLXPixmap glxpm;
79 XWindowAttributes attr;
80
81 pm = XCreatePixmap( dpy, win, width, height, visinfo->depth );
82 XGetWindowAttributes( dpy, win, &attr );
83
84 /*
85 * IMPORTANT:
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.
90 */
91 #ifdef GLX_MESA_pixmap_colormap
92 glxpm = glXCreateGLXPixmapMESA( dpy, visinfo, pm, attr.colormap );
93 #else
94 /* This will work with Mesa too if the visual is TrueColor or DirectColor */
95 glxpm = glXCreateGLXPixmap( dpy, visinfo, pm );
96 #endif
97
98 return glxpm;
99 }
100
101
102
103 static void event_loop( Display *dpy, GLXPixmap pm )
104 {
105 XEvent event;
106
107 while (1) {
108 XNextEvent( dpy, &event );
109
110 switch (event.type) {
111 case Expose:
112 printf("Redraw\n");
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 */
117 break;
118 case ConfigureNotify:
119 /* nothing */
120 break;
121 }
122 }
123 }
124
125
126
127 int main( int argc, char *argv[] )
128 {
129 Display *dpy;
130 Window win;
131 GLXPixmap pm;
132
133 dpy = XOpenDisplay(NULL);
134
135 win = make_rgb_window( dpy, 300, 300 );
136 pm = make_pixmap( dpy, win, 300, 300 );
137
138 #ifdef JUNK
139 glXMakeCurrent( dpy, win, ctx ); /*to make sure ctx is properly initialized*/
140 #endif
141
142 glXMakeCurrent( dpy, pm, ctx );
143
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 );
152 glFlush();
153
154 /* when a redraw is needed we'll just copy the pixmap image to the window */
155
156 XMapWindow( dpy, win );
157
158 event_loop( dpy, pm );
159 return 0;
160 }