blob: bde24ebcd044b1cf307673d943519a3a0f3e5cd2 [file] [log] [blame]
Brian Paulf02a5f62002-07-12 15:54:01 +00001/* $Id: glxpixmap.c,v 1.3 2002/07/12 15:54:02 brianp Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
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
jtgafb833d1999-08-19 00:55:39 +000012#include <GL/gl.h>
Brian Paulf02a5f62002-07-12 15:54:01 +000013#define GLX_GLXEXT_PROTOTYPES
jtgafb833d1999-08-19 00:55:39 +000014#include <GL/glx.h>
15#include <stdio.h>
16#include <stdlib.h>
Brian Paulfb88eee2000-07-11 16:05:29 +000017#include <string.h>
jtgafb833d1999-08-19 00:55:39 +000018
19
20static GLXContext ctx;
21static XVisualInfo *visinfo;
22static GC gc;
23
24
25
26static Window make_rgb_window( Display *dpy,
27 unsigned int width, unsigned int height )
28{
Brian Paulfb88eee2000-07-11 16:05:29 +000029 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 };
jtgafb833d1999-08-19 00:55:39 +000040 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 Paulfb88eee2000-07-11 16:05:29 +000049 visinfo = glXChooseVisual( dpy, scrnum, (int *) sbAttrib );
jtgafb833d1999-08-19 00:55:39 +000050 if (!visinfo) {
Brian Paulfb88eee2000-07-11 16:05:29 +000051 visinfo = glXChooseVisual( dpy, scrnum, (int *) dbAttrib );
52 if (!visinfo) {
53 printf("Error: couldn't get an RGB visual\n");
54 exit(1);
55 }
jtgafb833d1999-08-19 00:55:39 +000056 }
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 Paulfb88eee2000-07-11 16:05:29 +000073 /* 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");
jtgafb833d1999-08-19 00:55:39 +000081
82 return win;
83}
84
85
86static GLXPixmap make_pixmap( Display *dpy, Window win,
Brian Paulfb88eee2000-07-11 16:05:29 +000087 unsigned int width, unsigned int height,
88 Pixmap *pixmap)
jtgafb833d1999-08-19 00:55:39 +000089{
90 Pixmap pm;
91 GLXPixmap glxpm;
92 XWindowAttributes attr;
93
94 pm = XCreatePixmap( dpy, win, width, height, visinfo->depth );
Brian Paulfb88eee2000-07-11 16:05:29 +000095 if (!pm) {
96 printf("Error: XCreatePixmap failed\n");
97 exit(-1);
98 }
99
jtgafb833d1999-08-19 00:55:39 +0000100 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 Paulfb88eee2000-07-11 16:05:29 +0000110 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 }
jtgafb833d1999-08-19 00:55:39 +0000117#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 Paulfb88eee2000-07-11 16:05:29 +0000122 if (!glxpm) {
123 printf("Error: GLXCreateGLXPixmap failed\n");
124 exit(-1);
125 }
126
127 *pixmap = pm;
128
jtgafb833d1999-08-19 00:55:39 +0000129 return glxpm;
130}
131
132
133
134static 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
158int main( int argc, char *argv[] )
159{
160 Display *dpy;
161 Window win;
Brian Paulfb88eee2000-07-11 16:05:29 +0000162 Pixmap pm;
163 GLXPixmap glxpm;
jtgafb833d1999-08-19 00:55:39 +0000164
165 dpy = XOpenDisplay(NULL);
166
167 win = make_rgb_window( dpy, 300, 300 );
Brian Paulfb88eee2000-07-11 16:05:29 +0000168 glxpm = make_pixmap( dpy, win, 300, 300, &pm );
jtgafb833d1999-08-19 00:55:39 +0000169
Brian Paulfb88eee2000-07-11 16:05:29 +0000170 glXMakeCurrent( dpy, glxpm, ctx );
171 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
jtgafb833d1999-08-19 00:55:39 +0000172
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
jtgafb833d1999-08-19 00:55:39 +0000183 XMapWindow( dpy, win );
184
185 event_loop( dpy, pm );
186 return 0;
187}