Merge branch 'gallium-0.1' into gallium-tex-surfaces
[mesa.git] / progs / xdemos / glxpbdemo.c
1
2 /*
3 * This program demonstrates how to do "off-screen" rendering using
4 * the GLX pixel buffer extension.
5 *
6 * Written by Brian Paul for the "OpenGL and Window System Integration"
7 * course presented at SIGGRAPH '97. Updated on 5 October 2002.
8 *
9 * Updated on 31 January 2004 to use native GLX by
10 * Andrew P. Lentvorski, Jr. <bsder@allcaps.org>
11 *
12 * Usage:
13 * glxpbdemo width height imgfile
14 * Where:
15 * width is the width, in pixels, of the image to generate.
16 * height is the height, in pixels, of the image to generate.
17 * imgfile is the name of the PPM image file to write.
18 *
19 *
20 * This demo draws 3-D boxes with random orientation.
21 *
22 * On machines such as the SGI Indigo you may have to reconfigure your
23 * display/X server to enable pbuffers. Look in the /usr/gfx/ucode/MGRAS/vof/
24 * directory for display configurations with the _pbuf suffix. Use
25 * setmon -x <vof> to configure your X server and display for pbuffers.
26 *
27 * O2 systems seem to support pbuffers well.
28 *
29 */
30
31 #include <string.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <X11/Xlib.h>
35 #include <GL/glx.h>
36
37 /* Some ugly global vars */
38 static GLXFBConfig gFBconfig = 0;
39 static Display *gDpy = NULL;
40 static int gScreen = 0;
41 static GLXPbuffer gPBuffer = 0;
42 static int gWidth, gHeight;
43
44
45 /*
46 * Test for appropriate version of GLX to run this program
47 * Input: dpy - the X display
48 * screen - screen number
49 * Return: 0 = GLX not available.
50 * 1 = GLX available.
51 */
52 static int
53 RuntimeQueryGLXVersion(Display *dpy, int screen)
54 {
55 #if defined(GLX_VERSION_1_3) || defined(GLX_VERSION_1_4)
56 char *glxversion;
57
58 glxversion = (char *) glXGetClientString(dpy, GLX_VERSION);
59 if (!(strstr(glxversion, "1.3") || strstr(glxversion, "1.4")))
60 return 0;
61
62 glxversion = (char *) glXQueryServerString(dpy, screen, GLX_VERSION);
63 if (!(strstr(glxversion, "1.3") || strstr(glxversion, "1.4")))
64 return 0;
65
66 return 1;
67 #else
68 return 0;
69 #endif
70 }
71
72
73
74 /*
75 * Create the pbuffer and return a GLXPbuffer handle.
76 */
77 static GLXPbuffer
78 MakePbuffer( Display *dpy, int screen, int width, int height )
79 {
80 GLXFBConfig *fbConfigs;
81 GLXFBConfig chosenFBConfig;
82 GLXPbuffer pBuffer = None;
83
84 int nConfigs;
85 int fbconfigid;
86
87 int fbAttribs[] = {
88 GLX_RENDER_TYPE, GLX_RGBA_BIT,
89 GLX_DEPTH_SIZE, 1,
90 GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT | GLX_PBUFFER_BIT,
91 None
92 };
93
94 int pbAttribs[] = {
95 GLX_PBUFFER_WIDTH, 0,
96 GLX_PBUFFER_HEIGHT, 0,
97 GLX_LARGEST_PBUFFER, False,
98 GLX_PRESERVED_CONTENTS, False,
99 None
100 };
101
102 pbAttribs[1] = width;
103 pbAttribs[3] = height;
104
105 fbConfigs = glXChooseFBConfig(dpy, screen, fbAttribs, &nConfigs);
106
107 if (0 == nConfigs || !fbConfigs) {
108 printf("Error: glxChooseFBConfig failed\n");
109 XCloseDisplay(dpy);
110 return 0;
111 }
112
113 chosenFBConfig = fbConfigs[0];
114
115 glXGetFBConfigAttrib(dpy, chosenFBConfig, GLX_FBCONFIG_ID, &fbconfigid);
116 printf("Chose 0x%x as fbconfigid\n", fbconfigid);
117
118 /* Create the pbuffer using first fbConfig in the list that works. */
119 pBuffer = glXCreatePbuffer(dpy, chosenFBConfig, pbAttribs);
120
121 if (pBuffer) {
122 gFBconfig = chosenFBConfig;
123 gWidth = width;
124 gHeight = height;
125 }
126
127 XFree(fbConfigs);
128
129 return pBuffer;
130 }
131
132
133
134 /*
135 * Do all the X / GLX setup stuff.
136 */
137 static int
138 Setup(int width, int height)
139 {
140 #if defined(GLX_VERSION_1_3) || defined(GLX_VERSION_1_4)
141 GLXContext glCtx;
142
143 /* Open the X display */
144 gDpy = XOpenDisplay(NULL);
145 if (!gDpy) {
146 printf("Error: couldn't open default X display.\n");
147 return 0;
148 }
149
150 /* Get default screen */
151 gScreen = DefaultScreen(gDpy);
152
153 /* Test that GLX is available */
154 if (!RuntimeQueryGLXVersion(gDpy, gScreen)) {
155 printf("Error: GLX 1.3 or 1.4 not available\n");
156 XCloseDisplay(gDpy);
157 return 0;
158 }
159
160 /* Create Pbuffer */
161 gPBuffer = MakePbuffer( gDpy, gScreen, width, height );
162 if (gPBuffer==None) {
163 printf("Error: couldn't create pbuffer\n");
164 XCloseDisplay(gDpy);
165 return 0;
166 }
167
168 /* Create GLX context */
169 glCtx = glXCreateNewContext(gDpy, gFBconfig, GLX_RGBA_TYPE, NULL, True);
170 if (glCtx) {
171 if (!glXIsDirect(gDpy, glCtx)) {
172 printf("Warning: using indirect GLXContext\n");
173 }
174 }
175 else {
176 printf("Error: Couldn't create GLXContext\n");
177 XCloseDisplay(gDpy);
178 return 0;
179 }
180
181 /* Bind context to pbuffer */
182 if (!glXMakeCurrent(gDpy, gPBuffer, glCtx)) {
183 printf("Error: glXMakeCurrent failed\n");
184 XCloseDisplay(gDpy);
185 return 0;
186 }
187
188 return 1; /* Success!! */
189 #else
190 printf("Error: GLX version 1.3 or 1.4 not available at compile time\n");
191 return 0;
192 #endif
193 }
194
195
196
197 /* One-time GL setup */
198 static void
199 InitGL(void)
200 {
201 static GLfloat pos[4] = {0.0, 0.0, 10.0, 0.0};
202 glEnable(GL_LIGHTING);
203 glEnable(GL_LIGHT0);
204 glLightfv(GL_LIGHT0, GL_POSITION, pos);
205 glEnable(GL_NORMALIZE);
206 glEnable(GL_DEPTH_TEST);
207 glEnable(GL_CULL_FACE);
208
209 glViewport(0, 0, gWidth, gHeight);
210 glMatrixMode( GL_PROJECTION );
211 glLoadIdentity();
212 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
213 glMatrixMode( GL_MODELVIEW );
214 glLoadIdentity();
215 glTranslatef( 0.0, 0.0, -15.0 );
216
217 }
218
219
220 /* Return random float in [0,1] */
221 static float
222 Random(void)
223 {
224 int i = rand();
225 return (float) (i % 1000) / 1000.0;
226 }
227
228
229 static void
230 RandomColor(void)
231 {
232 GLfloat c[4];
233 c[0] = Random();
234 c[1] = Random();
235 c[2] = Random();
236 c[3] = 1.0;
237 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c);
238 }
239
240
241 /* This function borrowed from Mark Kilgard's GLUT */
242 static void
243 drawBox(GLfloat x0, GLfloat x1, GLfloat y0, GLfloat y1,
244 GLfloat z0, GLfloat z1, GLenum type)
245 {
246 static GLfloat n[6][3] =
247 {
248 {-1.0, 0.0, 0.0},
249 {0.0, 1.0, 0.0},
250 {1.0, 0.0, 0.0},
251 {0.0, -1.0, 0.0},
252 {0.0, 0.0, 1.0},
253 {0.0, 0.0, -1.0}
254 };
255 static GLint faces[6][4] =
256 {
257 {0, 1, 2, 3},
258 {3, 2, 6, 7},
259 {7, 6, 5, 4},
260 {4, 5, 1, 0},
261 {5, 6, 2, 1},
262 {7, 4, 0, 3}
263 };
264 GLfloat v[8][3], tmp;
265 GLint i;
266
267 if (x0 > x1) {
268 tmp = x0;
269 x0 = x1;
270 x1 = tmp;
271 }
272 if (y0 > y1) {
273 tmp = y0;
274 y0 = y1;
275 y1 = tmp;
276 }
277 if (z0 > z1) {
278 tmp = z0;
279 z0 = z1;
280 z1 = tmp;
281 }
282 v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
283 v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
284 v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
285 v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
286 v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
287 v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
288
289 for (i = 0; i < 6; i++) {
290 glBegin(type);
291 glNormal3fv(&n[i][0]);
292 glVertex3fv(&v[faces[i][0]][0]);
293 glVertex3fv(&v[faces[i][1]][0]);
294 glVertex3fv(&v[faces[i][2]][0]);
295 glVertex3fv(&v[faces[i][3]][0]);
296 glEnd();
297 }
298 }
299
300
301
302 /* Render a scene */
303 static void
304 Render(void)
305 {
306 int NumBoxes = 100;
307 int i;
308
309 InitGL();
310 glClearColor(0.2, 0.2, 0.9, 0.0);
311 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
312
313 for (i=0;i<NumBoxes;i++) {
314 float tx = -2.0 + 4.0 * Random();
315 float ty = -2.0 + 4.0 * Random();
316 float tz = 4.0 - 16.0 * Random();
317 float sx = 0.1 + Random() * 0.4;
318 float sy = 0.1 + Random() * 0.4;
319 float sz = 0.1 + Random() * 0.4;
320 float rx = Random();
321 float ry = Random();
322 float rz = Random();
323 float ra = Random() * 360.0;
324 glPushMatrix();
325 glTranslatef(tx, ty, tz);
326 glRotatef(ra, rx, ry, rz);
327 glScalef(sx, sy, sz);
328 RandomColor();
329 drawBox(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0, GL_POLYGON);
330 glPopMatrix();
331 }
332
333 glFinish();
334 }
335
336
337
338 static void
339 WriteFile(const char *filename)
340 {
341 FILE *f;
342 GLubyte *image;
343 int i;
344
345 image = malloc(gWidth * gHeight * 3 * sizeof(GLubyte));
346 if (!image) {
347 printf("Error: couldn't allocate image buffer\n");
348 return;
349 }
350
351 glPixelStorei(GL_PACK_ALIGNMENT, 1);
352 glReadPixels(0, 0, gWidth, gHeight, GL_RGB, GL_UNSIGNED_BYTE, image);
353
354 f = fopen(filename, "w");
355 if (!f) {
356 printf("Couldn't open image file: %s\n", filename);
357 return;
358 }
359 fprintf(f,"P6\n");
360 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
361 fprintf(f,"%i %i\n", gWidth, gHeight);
362 fprintf(f,"255\n");
363 fclose(f);
364 f = fopen(filename, "ab"); /* now append binary data */
365 if (!f) {
366 printf("Couldn't append to image file: %s\n", filename);
367 return;
368 }
369
370 for (i=0;i<gHeight;i++) {
371 GLubyte *rowPtr;
372 /* Remember, OpenGL images are bottom to top. Have to reverse. */
373 rowPtr = image + (gHeight-1-i) * gWidth*3;
374 fwrite(rowPtr, 1, gWidth*3, f);
375 }
376
377 fclose(f);
378 free(image);
379
380 printf("Wrote %d by %d image file: %s\n", gWidth, gHeight, filename);
381 }
382
383
384
385 /*
386 * Print message describing command line parameters.
387 */
388 static void
389 Usage(const char *appName)
390 {
391 printf("Usage:\n");
392 printf(" %s width height imgfile\n", appName);
393 printf("Where imgfile is a ppm file\n");
394 }
395
396
397
398 int
399 main(int argc, char *argv[])
400 {
401 if (argc!=4) {
402 Usage(argv[0]);
403 }
404 else {
405 int width = atoi(argv[1]);
406 int height = atoi(argv[2]);
407 char *fileName = argv[3];
408 if (width<=0) {
409 printf("Error: width parameter must be at least 1.\n");
410 return 1;
411 }
412 if (height<=0) {
413 printf("Error: height parameter must be at least 1.\n");
414 return 1;
415 }
416 if (!Setup(width, height)) {
417 return 1;
418 }
419
420 printf("Setup completed\n");
421 Render();
422 printf("Render completed.\n");
423 WriteFile(fileName);
424 printf("File write completed.\n");
425
426 glXDestroyPbuffer( gDpy, gPBuffer );
427 }
428 return 0;
429 }
430