mesa: added new linux-gallium and linux-gallium-debug configs
[mesa.git] / progs / xdemos / pbdemo.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 * Usage:
10 * pbuffers width height imgfile
11 * Where:
12 * width is the width, in pixels, of the image to generate.
13 * height is the height, in pixels, of the image to generate.
14 * imgfile is the name of the PPM image file to write.
15 *
16 *
17 * This demo draws 3-D boxes with random orientation. A pbuffer with
18 * a depth (Z) buffer is prefered but if such a pbuffer can't be created
19 * we use a non-depth-buffered config.
20 *
21 * On machines such as the SGI Indigo you may have to reconfigure your
22 * display/X server to enable pbuffers. Look in the /usr/gfx/ucode/MGRAS/vof/
23 * directory for display configurationswith the _pbuf suffix. Use
24 * setmon -x <vof> to configure your X server and display for pbuffers.
25 *
26 * O2 systems seem to support pbuffers well.
27 *
28 * IR systems (at least 1RM systems) don't have single-buffered, RGBA,
29 * Z-buffered pbuffer configs. BUT, they DO have DOUBLE-buffered, RGBA,
30 * Z-buffered pbuffers. Note how we try four different fbconfig attribute
31 * lists below!
32 */
33
34
35 #include <assert.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <X11/Xlib.h>
40 #include "pbutil.h"
41
42
43 /* Some ugly global vars */
44 static Display *gDpy = NULL;
45 static int gScreen = 0;
46 static FBCONFIG gFBconfig = 0;
47 static PBUFFER gPBuffer = 0;
48 static int gWidth, gHeight;
49 static GLXContext glCtx;
50
51
52
53 /*
54 * Create the pbuffer and return a GLXPbuffer handle.
55 *
56 * We loop over a list of fbconfigs trying to create
57 * a pixel buffer. We return the first pixel buffer which we successfully
58 * create.
59 */
60 static PBUFFER
61 MakePbuffer( Display *dpy, int screen, int width, int height )
62 {
63 #define NUM_FB_CONFIGS 4
64 const char fbString[NUM_FB_CONFIGS][100] = {
65 "Single Buffered, depth buffer",
66 "Double Buffered, depth buffer",
67 "Single Buffered, no depth buffer",
68 "Double Buffered, no depth buffer"
69 };
70 int fbAttribs[NUM_FB_CONFIGS][100] = {
71 {
72 /* Single buffered, with depth buffer */
73 GLX_RENDER_TYPE, GLX_RGBA_BIT,
74 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
75 GLX_RED_SIZE, 1,
76 GLX_GREEN_SIZE, 1,
77 GLX_BLUE_SIZE, 1,
78 GLX_DEPTH_SIZE, 1,
79 GLX_DOUBLEBUFFER, 0,
80 GLX_STENCIL_SIZE, 0,
81 None
82 },
83 {
84 /* Double buffered, with depth buffer */
85 GLX_RENDER_TYPE, GLX_RGBA_BIT,
86 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
87 GLX_RED_SIZE, 1,
88 GLX_GREEN_SIZE, 1,
89 GLX_BLUE_SIZE, 1,
90 GLX_DEPTH_SIZE, 1,
91 GLX_DOUBLEBUFFER, 1,
92 GLX_STENCIL_SIZE, 0,
93 None
94 },
95 {
96 /* Single buffered, without depth buffer */
97 GLX_RENDER_TYPE, GLX_RGBA_BIT,
98 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
99 GLX_RED_SIZE, 1,
100 GLX_GREEN_SIZE, 1,
101 GLX_BLUE_SIZE, 1,
102 GLX_DEPTH_SIZE, 0,
103 GLX_DOUBLEBUFFER, 0,
104 GLX_STENCIL_SIZE, 0,
105 None
106 },
107 {
108 /* Double buffered, without depth buffer */
109 GLX_RENDER_TYPE, GLX_RGBA_BIT,
110 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
111 GLX_RED_SIZE, 1,
112 GLX_GREEN_SIZE, 1,
113 GLX_BLUE_SIZE, 1,
114 GLX_DEPTH_SIZE, 0,
115 GLX_DOUBLEBUFFER, 1,
116 GLX_STENCIL_SIZE, 0,
117 None
118 }
119 };
120 Bool largest = True;
121 Bool preserve = False;
122 FBCONFIG *fbConfigs;
123 PBUFFER pBuffer = None;
124 int nConfigs;
125 int i;
126 int attempt;
127
128 for (attempt=0; attempt<NUM_FB_CONFIGS; attempt++) {
129
130 /* Get list of possible frame buffer configurations */
131 fbConfigs = ChooseFBConfig(dpy, screen, fbAttribs[attempt], &nConfigs);
132 if (nConfigs==0 || !fbConfigs) {
133 printf("Note: glXChooseFBConfig(%s) failed\n", fbString[attempt]);
134 continue;
135 }
136
137 #if 0 /*DEBUG*/
138 for (i=0;i<nConfigs;i++) {
139 printf("Config %d\n", i);
140 PrintFBConfigInfo(dpy, screen, fbConfigs[i], 0);
141 }
142 #endif
143
144 /* Create the pbuffer using first fbConfig in the list that works. */
145 for (i=0;i<nConfigs;i++) {
146 pBuffer = CreatePbuffer(dpy, screen, fbConfigs[i], width, height, preserve, largest);
147 if (pBuffer) {
148 gFBconfig = fbConfigs[i];
149 gWidth = width;
150 gHeight = height;
151 break;
152 }
153 }
154
155 if (pBuffer!=None) {
156 break;
157 }
158 }
159
160 if (pBuffer) {
161 printf("Using: %s\n", fbString[attempt]);
162 }
163
164 XFree(fbConfigs);
165
166 return pBuffer;
167 #undef NUM_FB_CONFIGS
168 }
169
170
171
172 /*
173 * Do all the X / GLX setup stuff.
174 */
175 static int
176 Setup(int width, int height)
177 {
178 int pbSupport;
179 XVisualInfo *visInfo;
180
181 /* Open the X display */
182 gDpy = XOpenDisplay(NULL);
183 if (!gDpy) {
184 printf("Error: couldn't open default X display.\n");
185 return 0;
186 }
187
188 /* Get default screen */
189 gScreen = DefaultScreen(gDpy);
190
191 /* Test that pbuffers are available */
192 pbSupport = QueryPbuffers(gDpy, gScreen);
193 if (pbSupport == 1) {
194 printf("Using GLX 1.3 Pbuffers\n");
195 }
196 else if (pbSupport == 2) {
197 printf("Using SGIX Pbuffers\n");
198 }
199 else {
200 printf("Error: pbuffers not available on this screen\n");
201 XCloseDisplay(gDpy);
202 return 0;
203 }
204
205 /* Create Pbuffer */
206 gPBuffer = MakePbuffer( gDpy, gScreen, width, height );
207 if (gPBuffer==None) {
208 printf("Error: couldn't create pbuffer\n");
209 XCloseDisplay(gDpy);
210 return 0;
211 }
212
213 /* Get corresponding XVisualInfo */
214 visInfo = GetVisualFromFBConfig(gDpy, gScreen, gFBconfig);
215 if (!visInfo) {
216 printf("Error: can't get XVisualInfo from FBconfig\n");
217 XCloseDisplay(gDpy);
218 return 0;
219 }
220
221 /* Create GLX context */
222 glCtx = glXCreateContext(gDpy, visInfo, NULL, True);
223 if (!glCtx) {
224 /* try indirect */
225 glCtx = glXCreateContext(gDpy, visInfo, NULL, False);
226 if (!glCtx) {
227 printf("Error: Couldn't create GLXContext\n");
228 XFree(visInfo);
229 XCloseDisplay(gDpy);
230 return 0;
231 }
232 else {
233 printf("Warning: using indirect GLXContext\n");
234 }
235 }
236
237 /* Bind context to pbuffer */
238 if (!glXMakeCurrent(gDpy, gPBuffer, glCtx)) {
239 printf("Error: glXMakeCurrent failed\n");
240 XFree(visInfo);
241 XCloseDisplay(gDpy);
242 return 0;
243 }
244
245 return 1; /* Success!! */
246 }
247
248
249
250 /* One-time GL setup */
251 static void
252 InitGL(void)
253 {
254 static GLfloat pos[4] = {0.0, 0.0, 10.0, 0.0};
255 glEnable(GL_LIGHTING);
256 glEnable(GL_LIGHT0);
257 glLightfv(GL_LIGHT0, GL_POSITION, pos);
258 glEnable(GL_NORMALIZE);
259 glEnable(GL_DEPTH_TEST);
260 glEnable(GL_CULL_FACE);
261
262 glViewport(0, 0, gWidth, gHeight);
263 glMatrixMode( GL_PROJECTION );
264 glLoadIdentity();
265 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
266 glMatrixMode( GL_MODELVIEW );
267 glLoadIdentity();
268 glTranslatef( 0.0, 0.0, -15.0 );
269 }
270
271
272 /* Return random float in [0,1] */
273 static float
274 Random(void)
275 {
276 int i = rand();
277 return (float) (i % 1000) / 1000.0;
278 }
279
280
281 static void
282 RandomColor(void)
283 {
284 GLfloat c[4];
285 c[0] = Random();
286 c[1] = Random();
287 c[2] = Random();
288 c[3] = 1.0;
289 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c);
290 }
291
292
293 /* This function borrowed from Mark Kilgard's GLUT */
294 static void
295 drawBox(GLfloat x0, GLfloat x1, GLfloat y0, GLfloat y1,
296 GLfloat z0, GLfloat z1, GLenum type)
297 {
298 static GLfloat n[6][3] =
299 {
300 {-1.0, 0.0, 0.0},
301 {0.0, 1.0, 0.0},
302 {1.0, 0.0, 0.0},
303 {0.0, -1.0, 0.0},
304 {0.0, 0.0, 1.0},
305 {0.0, 0.0, -1.0}
306 };
307 static GLint faces[6][4] =
308 {
309 {0, 1, 2, 3},
310 {3, 2, 6, 7},
311 {7, 6, 5, 4},
312 {4, 5, 1, 0},
313 {5, 6, 2, 1},
314 {7, 4, 0, 3}
315 };
316 GLfloat v[8][3], tmp;
317 GLint i;
318
319 if (x0 > x1) {
320 tmp = x0;
321 x0 = x1;
322 x1 = tmp;
323 }
324 if (y0 > y1) {
325 tmp = y0;
326 y0 = y1;
327 y1 = tmp;
328 }
329 if (z0 > z1) {
330 tmp = z0;
331 z0 = z1;
332 z1 = tmp;
333 }
334 v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
335 v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
336 v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
337 v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
338 v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
339 v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
340
341 for (i = 0; i < 6; i++) {
342 glBegin(type);
343 glNormal3fv(&n[i][0]);
344 glVertex3fv(&v[faces[i][0]][0]);
345 glVertex3fv(&v[faces[i][1]][0]);
346 glVertex3fv(&v[faces[i][2]][0]);
347 glVertex3fv(&v[faces[i][3]][0]);
348 glEnd();
349 }
350 }
351
352
353
354 /* Render a scene */
355 static void
356 Render(void)
357 {
358 int NumBoxes = 100;
359 int i;
360
361 glClearColor(0.2, 0.2, 0.9, 0.0);
362 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
363
364 for (i=0;i<NumBoxes;i++) {
365 float tx = -2.0 + 4.0 * Random();
366 float ty = -2.0 + 4.0 * Random();
367 float tz = 4.0 - 16.0 * Random();
368 float sx = 0.1 + Random() * 0.4;
369 float sy = 0.1 + Random() * 0.4;
370 float sz = 0.1 + Random() * 0.4;
371 float rx = Random();
372 float ry = Random();
373 float rz = Random();
374 float ra = Random() * 360.0;
375 glPushMatrix();
376 glTranslatef(tx, ty, tz);
377 glRotatef(ra, rx, ry, rz);
378 glScalef(sx, sy, sz);
379 RandomColor();
380 drawBox(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0, GL_POLYGON);
381 glPopMatrix();
382 }
383
384 glFinish();
385 }
386
387
388
389 static void
390 WriteFile(const char *filename)
391 {
392 FILE *f;
393 GLubyte *image;
394 int i;
395
396 image = malloc(gWidth * gHeight * 3 * sizeof(GLubyte));
397 if (!image) {
398 printf("Error: couldn't allocate image buffer\n");
399 return;
400 }
401
402 glPixelStorei(GL_PACK_ALIGNMENT, 1);
403 glReadPixels(0, 0, gWidth, gHeight, GL_RGB, GL_UNSIGNED_BYTE, image);
404
405 f = fopen(filename, "w");
406 if (!f) {
407 printf("Couldn't open image file: %s\n", filename);
408 return;
409 }
410 fprintf(f,"P6\n");
411 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
412 fprintf(f,"%i %i\n", gWidth, gHeight);
413 fprintf(f,"255\n");
414 fclose(f);
415 f = fopen(filename, "ab"); /* now append binary data */
416 if (!f) {
417 printf("Couldn't append to image file: %s\n", filename);
418 return;
419 }
420
421 for (i=0;i<gHeight;i++) {
422 GLubyte *rowPtr;
423 /* Remember, OpenGL images are bottom to top. Have to reverse. */
424 rowPtr = image + (gHeight-1-i) * gWidth*3;
425 fwrite(rowPtr, 1, gWidth*3, f);
426 }
427
428 fclose(f);
429 free(image);
430
431 printf("Wrote %d by %d image file: %s\n", gWidth, gHeight, filename);
432 }
433
434
435
436 /*
437 * Print message describing command line parameters.
438 */
439 static void
440 Usage(const char *appName)
441 {
442 printf("Usage:\n");
443 printf(" %s width height imgfile\n", appName);
444 printf("Where imgfile is a ppm file\n");
445 }
446
447
448
449 int
450 main(int argc, char *argv[])
451 {
452 if (argc!=4) {
453 Usage(argv[0]);
454 }
455 else {
456 int width = atoi(argv[1]);
457 int height = atoi(argv[2]);
458 char *fileName = argv[3];
459 if (width<=0) {
460 printf("Error: width parameter must be at least 1.\n");
461 return 1;
462 }
463 if (height<=0) {
464 printf("Error: height parameter must be at least 1.\n");
465 return 1;
466 }
467 if (!Setup(width, height)) {
468 return 1;
469 }
470 InitGL();
471 Render();
472 WriteFile(fileName);
473 DestroyPbuffer(gDpy, gScreen, gPBuffer);
474 }
475 return 0;
476 }
477