Merge branch '7.8'
[mesa.git] / progs / xdemos / glxpixmap.c
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
11 #include <GL/gl.h>
12 #define GLX_GLXEXT_PROTOTYPES
13 #include <GL/glx.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
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 {
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 };
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
48 visinfo = glXChooseVisual( dpy, scrnum, (int *) sbAttrib );
49 if (!visinfo) {
50 visinfo = glXChooseVisual( dpy, scrnum, (int *) dbAttrib );
51 if (!visinfo) {
52 printf("Error: couldn't get an RGB visual\n");
53 exit(1);
54 }
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
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");
80
81 return win;
82 }
83
84
85 static GLXPixmap make_pixmap( Display *dpy, Window win,
86 unsigned int width, unsigned int height,
87 Pixmap *pixmap)
88 {
89 Pixmap pm;
90 GLXPixmap glxpm;
91 XWindowAttributes attr;
92
93 pm = XCreatePixmap( dpy, win, width, height, visinfo->depth );
94 if (!pm) {
95 printf("Error: XCreatePixmap failed\n");
96 exit(-1);
97 }
98
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
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 }
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
121 if (!glxpm) {
122 printf("Error: GLXCreateGLXPixmap failed\n");
123 exit(-1);
124 }
125
126 *pixmap = pm;
127
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;
161 Pixmap pm;
162 GLXPixmap glxpm;
163
164 dpy = XOpenDisplay(NULL);
165
166 win = make_rgb_window( dpy, 300, 300 );
167 glxpm = make_pixmap( dpy, win, 300, 300, &pm );
168
169 glXMakeCurrent( dpy, glxpm, ctx );
170 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
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 glXWaitGL();
182
183 XMapWindow( dpy, win );
184
185 event_loop( dpy, pm );
186 return 0;
187 }