blob: 2573209336cc6b09f798bd6e5c13c4b6b91408bc [file] [log] [blame]
Brian Paul21666e32002-10-05 18:30:13 +00001
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 */
Brian Paul21666e32002-10-05 18:30:13 +000044static Display *gDpy = NULL;
45static int gScreen = 0;
Brian Paulf72e4422005-01-04 00:58:29 +000046static FBCONFIG gFBconfig = 0;
47static PBUFFER gPBuffer = 0;
Brian Paul21666e32002-10-05 18:30:13 +000048static int gWidth, gHeight;
Brian Paulf72e4422005-01-04 00:58:29 +000049static GLXContext glCtx;
Brian Paul21666e32002-10-05 18:30:13 +000050
51
52
53/*
Brian Paulf72e4422005-01-04 00:58:29 +000054 * Create the pbuffer and return a GLXPbuffer handle.
Brian Paul18a94902004-02-02 15:38:26 +000055 *
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.
Brian Paul21666e32002-10-05 18:30:13 +000059 */
Brian Paulf72e4422005-01-04 00:58:29 +000060static PBUFFER
Brian Paul21666e32002-10-05 18:30:13 +000061MakePbuffer( Display *dpy, int screen, int width, int height )
62{
63#define NUM_FB_CONFIGS 4
Brian Paul18a94902004-02-02 15:38:26 +000064 const char fbString[NUM_FB_CONFIGS][100] = {
Brian Paul21666e32002-10-05 18:30:13 +000065 "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 */
Brian Paulf72e4422005-01-04 00:58:29 +000073 GLX_RENDER_TYPE, GLX_RGBA_BIT,
74 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
Brian Paul21666e32002-10-05 18:30:13 +000075 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 */
Brian Paulf72e4422005-01-04 00:58:29 +000085 GLX_RENDER_TYPE, GLX_RGBA_BIT,
86 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
Brian Paul21666e32002-10-05 18:30:13 +000087 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 {
Brianffc633c2007-07-24 09:19:22 -060096 /* Single buffered, without depth buffer */
Brian Paulf72e4422005-01-04 00:58:29 +000097 GLX_RENDER_TYPE, GLX_RGBA_BIT,
98 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
Brian Paul21666e32002-10-05 18:30:13 +000099 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 {
Brianffc633c2007-07-24 09:19:22 -0600108 /* Double buffered, without depth buffer */
Brian Paulf72e4422005-01-04 00:58:29 +0000109 GLX_RENDER_TYPE, GLX_RGBA_BIT,
110 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
Brian Paul21666e32002-10-05 18:30:13 +0000111 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 };
Brian Paulf72e4422005-01-04 00:58:29 +0000120 Bool largest = True;
121 Bool preserve = False;
122 FBCONFIG *fbConfigs;
123 PBUFFER pBuffer = None;
Brian Paul21666e32002-10-05 18:30:13 +0000124 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 */
Brian Paulf72e4422005-01-04 00:58:29 +0000131 fbConfigs = ChooseFBConfig(dpy, screen, fbAttribs[attempt], &nConfigs);
Brian Paul21666e32002-10-05 18:30:13 +0000132 if (nConfigs==0 || !fbConfigs) {
Brianffc633c2007-07-24 09:19:22 -0600133 printf("Note: glXChooseFBConfig(%s) failed\n", fbString[attempt]);
134 continue;
Brian Paul21666e32002-10-05 18:30:13 +0000135 }
136
Brian Paulf72e4422005-01-04 00:58:29 +0000137#if 0 /*DEBUG*/
Brian Paul21666e32002-10-05 18:30:13 +0000138 for (i=0;i<nConfigs;i++) {
139 printf("Config %d\n", i);
Brian Paulf72e4422005-01-04 00:58:29 +0000140 PrintFBConfigInfo(dpy, screen, fbConfigs[i], 0);
Brian Paul21666e32002-10-05 18:30:13 +0000141 }
142#endif
143
144 /* Create the pbuffer using first fbConfig in the list that works. */
145 for (i=0;i<nConfigs;i++) {
Brian Paul846a6b02009-10-07 09:10:48 -0600146 pBuffer = CreatePbuffer(dpy, screen, fbConfigs[i], width, height, largest, preserve);
Brian Paul21666e32002-10-05 18:30:13 +0000147 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 */
175static int
176Setup(int width, int height)
177{
Brian Paulf72e4422005-01-04 00:58:29 +0000178 int pbSupport;
Brian Paul21666e32002-10-05 18:30:13 +0000179 XVisualInfo *visInfo;
Brian Paul21666e32002-10-05 18:30:13 +0000180
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 */
Brian Paulf72e4422005-01-04 00:58:29 +0000192 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 {
Brian Paul21666e32002-10-05 18:30:13 +0000200 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
Brian Paulf36425b2009-10-07 09:11:23 -0600213 /* Test drawable queries */
214 {
215 unsigned int v;
216 glXQueryDrawable( gDpy, gPBuffer, GLX_WIDTH, &v);
217 printf("GLX_WIDTH = %u\n", v);
218 glXQueryDrawable( gDpy, gPBuffer, GLX_HEIGHT, &v);
219 printf("GLX_HEIGHT = %u\n", v);
220 glXQueryDrawable( gDpy, gPBuffer, GLX_PRESERVED_CONTENTS, &v);
221 printf("GLX_PRESERVED_CONTENTS = %u\n", v);
222 glXQueryDrawable( gDpy, gPBuffer, GLX_LARGEST_PBUFFER, &v);
223 printf("GLX_LARGEST_PBUFFER = %u\n", v);
224 glXQueryDrawable( gDpy, gPBuffer, GLX_FBCONFIG_ID, &v);
225 printf("GLX_FBCONFIG_ID = %u\n", v);
226 }
227
Brian Paul21666e32002-10-05 18:30:13 +0000228 /* Get corresponding XVisualInfo */
Brian Paulf72e4422005-01-04 00:58:29 +0000229 visInfo = GetVisualFromFBConfig(gDpy, gScreen, gFBconfig);
Brian Paul21666e32002-10-05 18:30:13 +0000230 if (!visInfo) {
231 printf("Error: can't get XVisualInfo from FBconfig\n");
232 XCloseDisplay(gDpy);
233 return 0;
234 }
235
236 /* Create GLX context */
237 glCtx = glXCreateContext(gDpy, visInfo, NULL, True);
238 if (!glCtx) {
239 /* try indirect */
240 glCtx = glXCreateContext(gDpy, visInfo, NULL, False);
241 if (!glCtx) {
242 printf("Error: Couldn't create GLXContext\n");
243 XFree(visInfo);
244 XCloseDisplay(gDpy);
245 return 0;
246 }
247 else {
248 printf("Warning: using indirect GLXContext\n");
249 }
250 }
251
252 /* Bind context to pbuffer */
253 if (!glXMakeCurrent(gDpy, gPBuffer, glCtx)) {
254 printf("Error: glXMakeCurrent failed\n");
255 XFree(visInfo);
256 XCloseDisplay(gDpy);
257 return 0;
258 }
259
260 return 1; /* Success!! */
Brian Paul21666e32002-10-05 18:30:13 +0000261}
262
263
264
265/* One-time GL setup */
266static void
267InitGL(void)
268{
269 static GLfloat pos[4] = {0.0, 0.0, 10.0, 0.0};
270 glEnable(GL_LIGHTING);
271 glEnable(GL_LIGHT0);
272 glLightfv(GL_LIGHT0, GL_POSITION, pos);
273 glEnable(GL_NORMALIZE);
274 glEnable(GL_DEPTH_TEST);
275 glEnable(GL_CULL_FACE);
276
277 glViewport(0, 0, gWidth, gHeight);
278 glMatrixMode( GL_PROJECTION );
279 glLoadIdentity();
280 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
281 glMatrixMode( GL_MODELVIEW );
282 glLoadIdentity();
283 glTranslatef( 0.0, 0.0, -15.0 );
Brian Paul21666e32002-10-05 18:30:13 +0000284}
285
286
287/* Return random float in [0,1] */
288static float
289Random(void)
290{
291 int i = rand();
292 return (float) (i % 1000) / 1000.0;
293}
294
295
296static void
297RandomColor(void)
298{
299 GLfloat c[4];
300 c[0] = Random();
301 c[1] = Random();
302 c[2] = Random();
303 c[3] = 1.0;
304 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c);
305}
306
307
308/* This function borrowed from Mark Kilgard's GLUT */
309static void
310drawBox(GLfloat x0, GLfloat x1, GLfloat y0, GLfloat y1,
311 GLfloat z0, GLfloat z1, GLenum type)
312{
313 static GLfloat n[6][3] =
314 {
315 {-1.0, 0.0, 0.0},
316 {0.0, 1.0, 0.0},
317 {1.0, 0.0, 0.0},
318 {0.0, -1.0, 0.0},
319 {0.0, 0.0, 1.0},
320 {0.0, 0.0, -1.0}
321 };
322 static GLint faces[6][4] =
323 {
324 {0, 1, 2, 3},
325 {3, 2, 6, 7},
326 {7, 6, 5, 4},
327 {4, 5, 1, 0},
328 {5, 6, 2, 1},
329 {7, 4, 0, 3}
330 };
331 GLfloat v[8][3], tmp;
332 GLint i;
333
334 if (x0 > x1) {
335 tmp = x0;
336 x0 = x1;
337 x1 = tmp;
338 }
339 if (y0 > y1) {
340 tmp = y0;
341 y0 = y1;
342 y1 = tmp;
343 }
344 if (z0 > z1) {
345 tmp = z0;
346 z0 = z1;
347 z1 = tmp;
348 }
349 v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
350 v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
351 v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
352 v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
353 v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
354 v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
355
356 for (i = 0; i < 6; i++) {
357 glBegin(type);
358 glNormal3fv(&n[i][0]);
359 glVertex3fv(&v[faces[i][0]][0]);
360 glVertex3fv(&v[faces[i][1]][0]);
361 glVertex3fv(&v[faces[i][2]][0]);
362 glVertex3fv(&v[faces[i][3]][0]);
363 glEnd();
364 }
365}
366
367
368
369/* Render a scene */
370static void
371Render(void)
372{
373 int NumBoxes = 100;
374 int i;
375
Brian Paul21666e32002-10-05 18:30:13 +0000376 glClearColor(0.2, 0.2, 0.9, 0.0);
377 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
378
379 for (i=0;i<NumBoxes;i++) {
380 float tx = -2.0 + 4.0 * Random();
381 float ty = -2.0 + 4.0 * Random();
382 float tz = 4.0 - 16.0 * Random();
383 float sx = 0.1 + Random() * 0.4;
384 float sy = 0.1 + Random() * 0.4;
385 float sz = 0.1 + Random() * 0.4;
386 float rx = Random();
387 float ry = Random();
388 float rz = Random();
389 float ra = Random() * 360.0;
390 glPushMatrix();
391 glTranslatef(tx, ty, tz);
392 glRotatef(ra, rx, ry, rz);
393 glScalef(sx, sy, sz);
394 RandomColor();
395 drawBox(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0, GL_POLYGON);
396 glPopMatrix();
397 }
398
399 glFinish();
400}
401
402
403
404static void
405WriteFile(const char *filename)
406{
407 FILE *f;
408 GLubyte *image;
409 int i;
410
411 image = malloc(gWidth * gHeight * 3 * sizeof(GLubyte));
412 if (!image) {
413 printf("Error: couldn't allocate image buffer\n");
414 return;
415 }
416
417 glPixelStorei(GL_PACK_ALIGNMENT, 1);
418 glReadPixels(0, 0, gWidth, gHeight, GL_RGB, GL_UNSIGNED_BYTE, image);
419
420 f = fopen(filename, "w");
421 if (!f) {
422 printf("Couldn't open image file: %s\n", filename);
423 return;
424 }
425 fprintf(f,"P6\n");
426 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
427 fprintf(f,"%i %i\n", gWidth, gHeight);
428 fprintf(f,"255\n");
429 fclose(f);
430 f = fopen(filename, "ab"); /* now append binary data */
431 if (!f) {
432 printf("Couldn't append to image file: %s\n", filename);
433 return;
434 }
435
436 for (i=0;i<gHeight;i++) {
437 GLubyte *rowPtr;
438 /* Remember, OpenGL images are bottom to top. Have to reverse. */
439 rowPtr = image + (gHeight-1-i) * gWidth*3;
440 fwrite(rowPtr, 1, gWidth*3, f);
441 }
442
443 fclose(f);
444 free(image);
445
446 printf("Wrote %d by %d image file: %s\n", gWidth, gHeight, filename);
447}
448
449
450
451/*
452 * Print message describing command line parameters.
453 */
454static void
455Usage(const char *appName)
456{
457 printf("Usage:\n");
458 printf(" %s width height imgfile\n", appName);
459 printf("Where imgfile is a ppm file\n");
460}
461
462
463
464int
465main(int argc, char *argv[])
466{
467 if (argc!=4) {
468 Usage(argv[0]);
469 }
470 else {
471 int width = atoi(argv[1]);
472 int height = atoi(argv[2]);
473 char *fileName = argv[3];
474 if (width<=0) {
475 printf("Error: width parameter must be at least 1.\n");
476 return 1;
477 }
478 if (height<=0) {
479 printf("Error: height parameter must be at least 1.\n");
480 return 1;
481 }
482 if (!Setup(width, height)) {
483 return 1;
484 }
Brian Paulf72e4422005-01-04 00:58:29 +0000485 InitGL();
Brian Paul21666e32002-10-05 18:30:13 +0000486 Render();
487 WriteFile(fileName);
Brian Paulf72e4422005-01-04 00:58:29 +0000488 DestroyPbuffer(gDpy, gScreen, gPBuffer);
Brian Paul21666e32002-10-05 18:30:13 +0000489 }
490 return 0;
491}
492