r300: Moved the pipeline stages together for readability.
[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 bufferd, 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 bufferd, 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("Error: glXChooseFBConfig failed\n");
134 XCloseDisplay(dpy);
135 return 0;
136 }
137
138 #if 0 /*DEBUG*/
139 for (i=0;i<nConfigs;i++) {
140 printf("Config %d\n", i);
141 PrintFBConfigInfo(dpy, screen, fbConfigs[i], 0);
142 }
143 #endif
144
145 /* Create the pbuffer using first fbConfig in the list that works. */
146 for (i=0;i<nConfigs;i++) {
147 pBuffer = CreatePbuffer(dpy, screen, fbConfigs[i], width, height, preserve, largest);
148 if (pBuffer) {
149 gFBconfig = fbConfigs[i];
150 gWidth = width;
151 gHeight = height;
152 break;
153 }
154 }
155
156 if (pBuffer!=None) {
157 break;
158 }
159 }
160
161 if (pBuffer) {
162 printf("Using: %s\n", fbString[attempt]);
163 }
164
165 XFree(fbConfigs);
166
167 return pBuffer;
168 #undef NUM_FB_CONFIGS
169 }
170
171
172
173 /*
174 * Do all the X / GLX setup stuff.
175 */
176 static int
177 Setup(int width, int height)
178 {
179 int pbSupport;
180 XVisualInfo *visInfo;
181
182 /* Open the X display */
183 gDpy = XOpenDisplay(NULL);
184 if (!gDpy) {
185 printf("Error: couldn't open default X display.\n");
186 return 0;
187 }
188
189 /* Get default screen */
190 gScreen = DefaultScreen(gDpy);
191
192 /* Test that pbuffers are available */
193 pbSupport = QueryPbuffers(gDpy, gScreen);
194 if (pbSupport == 1) {
195 printf("Using GLX 1.3 Pbuffers\n");
196 }
197 else if (pbSupport == 2) {
198 printf("Using SGIX Pbuffers\n");
199 }
200 else {
201 printf("Error: pbuffers not available on this screen\n");
202 XCloseDisplay(gDpy);
203 return 0;
204 }
205
206 /* Create Pbuffer */
207 gPBuffer = MakePbuffer( gDpy, gScreen, width, height );
208 if (gPBuffer==None) {
209 printf("Error: couldn't create pbuffer\n");
210 XCloseDisplay(gDpy);
211 return 0;
212 }
213
214 /* Get corresponding XVisualInfo */
215 visInfo = GetVisualFromFBConfig(gDpy, gScreen, gFBconfig);
216 if (!visInfo) {
217 printf("Error: can't get XVisualInfo from FBconfig\n");
218 XCloseDisplay(gDpy);
219 return 0;
220 }
221
222 /* Create GLX context */
223 glCtx = glXCreateContext(gDpy, visInfo, NULL, True);
224 if (!glCtx) {
225 /* try indirect */
226 glCtx = glXCreateContext(gDpy, visInfo, NULL, False);
227 if (!glCtx) {
228 printf("Error: Couldn't create GLXContext\n");
229 XFree(visInfo);
230 XCloseDisplay(gDpy);
231 return 0;
232 }
233 else {
234 printf("Warning: using indirect GLXContext\n");
235 }
236 }
237
238 /* Bind context to pbuffer */
239 if (!glXMakeCurrent(gDpy, gPBuffer, glCtx)) {
240 printf("Error: glXMakeCurrent failed\n");
241 XFree(visInfo);
242 XCloseDisplay(gDpy);
243 return 0;
244 }
245
246 return 1; /* Success!! */
247 }
248
249
250
251 /* One-time GL setup */
252 static void
253 InitGL(void)
254 {
255 static GLfloat pos[4] = {0.0, 0.0, 10.0, 0.0};
256 glEnable(GL_LIGHTING);
257 glEnable(GL_LIGHT0);
258 glLightfv(GL_LIGHT0, GL_POSITION, pos);
259 glEnable(GL_NORMALIZE);
260 glEnable(GL_DEPTH_TEST);
261 glEnable(GL_CULL_FACE);
262
263 glViewport(0, 0, gWidth, gHeight);
264 glMatrixMode( GL_PROJECTION );
265 glLoadIdentity();
266 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
267 glMatrixMode( GL_MODELVIEW );
268 glLoadIdentity();
269 glTranslatef( 0.0, 0.0, -15.0 );
270 }
271
272
273 /* Return random float in [0,1] */
274 static float
275 Random(void)
276 {
277 int i = rand();
278 return (float) (i % 1000) / 1000.0;
279 }
280
281
282 static void
283 RandomColor(void)
284 {
285 GLfloat c[4];
286 c[0] = Random();
287 c[1] = Random();
288 c[2] = Random();
289 c[3] = 1.0;
290 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c);
291 }
292
293
294 /* This function borrowed from Mark Kilgard's GLUT */
295 static void
296 drawBox(GLfloat x0, GLfloat x1, GLfloat y0, GLfloat y1,
297 GLfloat z0, GLfloat z1, GLenum type)
298 {
299 static GLfloat n[6][3] =
300 {
301 {-1.0, 0.0, 0.0},
302 {0.0, 1.0, 0.0},
303 {1.0, 0.0, 0.0},
304 {0.0, -1.0, 0.0},
305 {0.0, 0.0, 1.0},
306 {0.0, 0.0, -1.0}
307 };
308 static GLint faces[6][4] =
309 {
310 {0, 1, 2, 3},
311 {3, 2, 6, 7},
312 {7, 6, 5, 4},
313 {4, 5, 1, 0},
314 {5, 6, 2, 1},
315 {7, 4, 0, 3}
316 };
317 GLfloat v[8][3], tmp;
318 GLint i;
319
320 if (x0 > x1) {
321 tmp = x0;
322 x0 = x1;
323 x1 = tmp;
324 }
325 if (y0 > y1) {
326 tmp = y0;
327 y0 = y1;
328 y1 = tmp;
329 }
330 if (z0 > z1) {
331 tmp = z0;
332 z0 = z1;
333 z1 = tmp;
334 }
335 v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
336 v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
337 v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
338 v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
339 v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
340 v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
341
342 for (i = 0; i < 6; i++) {
343 glBegin(type);
344 glNormal3fv(&n[i][0]);
345 glVertex3fv(&v[faces[i][0]][0]);
346 glVertex3fv(&v[faces[i][1]][0]);
347 glVertex3fv(&v[faces[i][2]][0]);
348 glVertex3fv(&v[faces[i][3]][0]);
349 glEnd();
350 }
351 }
352
353
354
355 /* Render a scene */
356 static void
357 Render(void)
358 {
359 int NumBoxes = 100;
360 int i;
361
362 glClearColor(0.2, 0.2, 0.9, 0.0);
363 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
364
365 for (i=0;i<NumBoxes;i++) {
366 float tx = -2.0 + 4.0 * Random();
367 float ty = -2.0 + 4.0 * Random();
368 float tz = 4.0 - 16.0 * Random();
369 float sx = 0.1 + Random() * 0.4;
370 float sy = 0.1 + Random() * 0.4;
371 float sz = 0.1 + Random() * 0.4;
372 float rx = Random();
373 float ry = Random();
374 float rz = Random();
375 float ra = Random() * 360.0;
376 glPushMatrix();
377 glTranslatef(tx, ty, tz);
378 glRotatef(ra, rx, ry, rz);
379 glScalef(sx, sy, sz);
380 RandomColor();
381 drawBox(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0, GL_POLYGON);
382 glPopMatrix();
383 }
384
385 glFinish();
386 }
387
388
389
390 static void
391 WriteFile(const char *filename)
392 {
393 FILE *f;
394 GLubyte *image;
395 int i;
396
397 image = malloc(gWidth * gHeight * 3 * sizeof(GLubyte));
398 if (!image) {
399 printf("Error: couldn't allocate image buffer\n");
400 return;
401 }
402
403 glPixelStorei(GL_PACK_ALIGNMENT, 1);
404 glReadPixels(0, 0, gWidth, gHeight, GL_RGB, GL_UNSIGNED_BYTE, image);
405
406 f = fopen(filename, "w");
407 if (!f) {
408 printf("Couldn't open image file: %s\n", filename);
409 return;
410 }
411 fprintf(f,"P6\n");
412 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
413 fprintf(f,"%i %i\n", gWidth, gHeight);
414 fprintf(f,"255\n");
415 fclose(f);
416 f = fopen(filename, "ab"); /* now append binary data */
417 if (!f) {
418 printf("Couldn't append to image file: %s\n", filename);
419 return;
420 }
421
422 for (i=0;i<gHeight;i++) {
423 GLubyte *rowPtr;
424 /* Remember, OpenGL images are bottom to top. Have to reverse. */
425 rowPtr = image + (gHeight-1-i) * gWidth*3;
426 fwrite(rowPtr, 1, gWidth*3, f);
427 }
428
429 fclose(f);
430 free(image);
431
432 printf("Wrote %d by %d image file: %s\n", gWidth, gHeight, filename);
433 }
434
435
436
437 /*
438 * Print message describing command line parameters.
439 */
440 static void
441 Usage(const char *appName)
442 {
443 printf("Usage:\n");
444 printf(" %s width height imgfile\n", appName);
445 printf("Where imgfile is a ppm file\n");
446 }
447
448
449
450 int
451 main(int argc, char *argv[])
452 {
453 if (argc!=4) {
454 Usage(argv[0]);
455 }
456 else {
457 int width = atoi(argv[1]);
458 int height = atoi(argv[2]);
459 char *fileName = argv[3];
460 if (width<=0) {
461 printf("Error: width parameter must be at least 1.\n");
462 return 1;
463 }
464 if (height<=0) {
465 printf("Error: height parameter must be at least 1.\n");
466 return 1;
467 }
468 if (!Setup(width, height)) {
469 return 1;
470 }
471 InitGL();
472 Render();
473 WriteFile(fileName);
474 DestroyPbuffer(gDpy, gScreen, gPBuffer);
475 }
476 return 0;
477 }
478