blob: 9bce5699a278fc051f9663385dbd2b5fb9e31e56 [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 */
44static GLXFBConfigSGIX gFBconfig = 0;
45static Display *gDpy = NULL;
46static int gScreen = 0;
47static GLXPbufferSGIX gPBuffer = 0;
48static int gWidth, gHeight;
49
50
51
52/*
53 * Create the pbuffer and return a GLXPbufferSGIX handle.
54 */
55static GLXPbufferSGIX
56MakePbuffer( Display *dpy, int screen, int width, int height )
57{
58#define NUM_FB_CONFIGS 4
59 char fbString[NUM_FB_CONFIGS][100] = {
60 "Single Buffered, depth buffer",
61 "Double Buffered, depth buffer",
62 "Single Buffered, no depth buffer",
63 "Double Buffered, no depth buffer"
64 };
65 int fbAttribs[NUM_FB_CONFIGS][100] = {
66 {
67 /* Single buffered, with depth buffer */
68 GLX_RENDER_TYPE_SGIX, GLX_RGBA_BIT_SGIX,
69 GLX_DRAWABLE_TYPE_SGIX, GLX_PIXMAP_BIT_SGIX,
70 GLX_RED_SIZE, 1,
71 GLX_GREEN_SIZE, 1,
72 GLX_BLUE_SIZE, 1,
73 GLX_DEPTH_SIZE, 1,
74 GLX_DOUBLEBUFFER, 0,
75 GLX_STENCIL_SIZE, 0,
76 None
77 },
78 {
79 /* Double buffered, with depth buffer */
80 GLX_RENDER_TYPE_SGIX, GLX_RGBA_BIT_SGIX,
81 GLX_DRAWABLE_TYPE_SGIX, GLX_PIXMAP_BIT_SGIX,
82 GLX_RED_SIZE, 1,
83 GLX_GREEN_SIZE, 1,
84 GLX_BLUE_SIZE, 1,
85 GLX_DEPTH_SIZE, 1,
86 GLX_DOUBLEBUFFER, 1,
87 GLX_STENCIL_SIZE, 0,
88 None
89 },
90 {
91 /* Single bufferd, without depth buffer */
92 GLX_RENDER_TYPE_SGIX, GLX_RGBA_BIT_SGIX,
93 GLX_DRAWABLE_TYPE_SGIX, GLX_PIXMAP_BIT_SGIX,
94 GLX_RED_SIZE, 1,
95 GLX_GREEN_SIZE, 1,
96 GLX_BLUE_SIZE, 1,
97 GLX_DEPTH_SIZE, 0,
98 GLX_DOUBLEBUFFER, 0,
99 GLX_STENCIL_SIZE, 0,
100 None
101 },
102 {
103 /* Double bufferd, without depth buffer */
104 GLX_RENDER_TYPE_SGIX, GLX_RGBA_BIT_SGIX,
105 GLX_DRAWABLE_TYPE_SGIX, GLX_PIXMAP_BIT_SGIX,
106 GLX_RED_SIZE, 1,
107 GLX_GREEN_SIZE, 1,
108 GLX_BLUE_SIZE, 1,
109 GLX_DEPTH_SIZE, 0,
110 GLX_DOUBLEBUFFER, 1,
111 GLX_STENCIL_SIZE, 0,
112 None
113 }
114 };
115 int pbAttribs[] = {
116 GLX_LARGEST_PBUFFER_SGIX, True,
117 GLX_PRESERVED_CONTENTS_SGIX, False,
118 None
119 };
120 GLXFBConfigSGIX *fbConfigs;
121 GLXPbufferSGIX pBuffer = None;
122 int nConfigs;
123 int i;
124 int attempt;
125
126 for (attempt=0; attempt<NUM_FB_CONFIGS; attempt++) {
127
128 /* Get list of possible frame buffer configurations */
129 fbConfigs = glXChooseFBConfigSGIX(dpy, screen, fbAttribs[attempt], &nConfigs);
130 if (nConfigs==0 || !fbConfigs) {
131 printf("Error: glxChooseFBConfigSGIX failed\n");
132 XCloseDisplay(dpy);
133 return 0;
134 }
135
136#ifdef DEBUG
137 for (i=0;i<nConfigs;i++) {
138 printf("Config %d\n", i);
139 PrintFBConfigInfo(dpy, fbConfigs[i], 0);
140 }
141#endif
142
143 /* Create the pbuffer using first fbConfig in the list that works. */
144 for (i=0;i<nConfigs;i++) {
145 pBuffer = CreatePbuffer(dpy, fbConfigs[i], width, height, pbAttribs);
146 if (pBuffer) {
147 gFBconfig = fbConfigs[i];
148 gWidth = width;
149 gHeight = height;
150 break;
151 }
152 }
153
154 if (pBuffer!=None) {
155 break;
156 }
157 }
158
159 if (pBuffer) {
160 printf("Using: %s\n", fbString[attempt]);
161 }
162
163 XFree(fbConfigs);
164
165 return pBuffer;
166#undef NUM_FB_CONFIGS
167}
168
169
170
171/*
172 * Do all the X / GLX setup stuff.
173 */
174static int
175Setup(int width, int height)
176{
177#if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer)
178 XVisualInfo *visInfo;
179 GLXContext glCtx;
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 if (!QueryPbuffers(gDpy, gScreen)) {
193 printf("Error: pbuffers not available on this screen\n");
194 XCloseDisplay(gDpy);
195 return 0;
196 }
197
198 /* Create Pbuffer */
199 gPBuffer = MakePbuffer( gDpy, gScreen, width, height );
200 if (gPBuffer==None) {
201 printf("Error: couldn't create pbuffer\n");
202 XCloseDisplay(gDpy);
203 return 0;
204 }
205
206 /* Get corresponding XVisualInfo */
207 visInfo = glXGetVisualFromFBConfigSGIX(gDpy, gFBconfig);
208 if (!visInfo) {
209 printf("Error: can't get XVisualInfo from FBconfig\n");
210 XCloseDisplay(gDpy);
211 return 0;
212 }
213
214 /* Create GLX context */
215 glCtx = glXCreateContext(gDpy, visInfo, NULL, True);
216 if (!glCtx) {
217 /* try indirect */
218 glCtx = glXCreateContext(gDpy, visInfo, NULL, False);
219 if (!glCtx) {
220 printf("Error: Couldn't create GLXContext\n");
221 XFree(visInfo);
222 XCloseDisplay(gDpy);
223 return 0;
224 }
225 else {
226 printf("Warning: using indirect GLXContext\n");
227 }
228 }
229
230 /* Bind context to pbuffer */
231 if (!glXMakeCurrent(gDpy, gPBuffer, glCtx)) {
232 printf("Error: glXMakeCurrent failed\n");
233 XFree(visInfo);
234 XCloseDisplay(gDpy);
235 return 0;
236 }
237
238 return 1; /* Success!! */
239#else
240 printf("Error: GLX_SGIX_fbconfig and/or GLX_SGIX_pbuffer extensions not"
241 " available at compile-time.\n");
242 return 0;
243#endif
244}
245
246
247
248/* One-time GL setup */
249static void
250InitGL(void)
251{
252 static GLfloat pos[4] = {0.0, 0.0, 10.0, 0.0};
253 glEnable(GL_LIGHTING);
254 glEnable(GL_LIGHT0);
255 glLightfv(GL_LIGHT0, GL_POSITION, pos);
256 glEnable(GL_NORMALIZE);
257 glEnable(GL_DEPTH_TEST);
258 glEnable(GL_CULL_FACE);
259
260 glViewport(0, 0, gWidth, gHeight);
261 glMatrixMode( GL_PROJECTION );
262 glLoadIdentity();
263 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
264 glMatrixMode( GL_MODELVIEW );
265 glLoadIdentity();
266 glTranslatef( 0.0, 0.0, -15.0 );
267
268}
269
270
271/* Return random float in [0,1] */
272static float
273Random(void)
274{
275 int i = rand();
276 return (float) (i % 1000) / 1000.0;
277}
278
279
280static void
281RandomColor(void)
282{
283 GLfloat c[4];
284 c[0] = Random();
285 c[1] = Random();
286 c[2] = Random();
287 c[3] = 1.0;
288 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c);
289}
290
291
292/* This function borrowed from Mark Kilgard's GLUT */
293static void
294drawBox(GLfloat x0, GLfloat x1, GLfloat y0, GLfloat y1,
295 GLfloat z0, GLfloat z1, GLenum type)
296{
297 static GLfloat n[6][3] =
298 {
299 {-1.0, 0.0, 0.0},
300 {0.0, 1.0, 0.0},
301 {1.0, 0.0, 0.0},
302 {0.0, -1.0, 0.0},
303 {0.0, 0.0, 1.0},
304 {0.0, 0.0, -1.0}
305 };
306 static GLint faces[6][4] =
307 {
308 {0, 1, 2, 3},
309 {3, 2, 6, 7},
310 {7, 6, 5, 4},
311 {4, 5, 1, 0},
312 {5, 6, 2, 1},
313 {7, 4, 0, 3}
314 };
315 GLfloat v[8][3], tmp;
316 GLint i;
317
318 if (x0 > x1) {
319 tmp = x0;
320 x0 = x1;
321 x1 = tmp;
322 }
323 if (y0 > y1) {
324 tmp = y0;
325 y0 = y1;
326 y1 = tmp;
327 }
328 if (z0 > z1) {
329 tmp = z0;
330 z0 = z1;
331 z1 = tmp;
332 }
333 v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
334 v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
335 v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
336 v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
337 v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
338 v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
339
340 for (i = 0; i < 6; i++) {
341 glBegin(type);
342 glNormal3fv(&n[i][0]);
343 glVertex3fv(&v[faces[i][0]][0]);
344 glVertex3fv(&v[faces[i][1]][0]);
345 glVertex3fv(&v[faces[i][2]][0]);
346 glVertex3fv(&v[faces[i][3]][0]);
347 glEnd();
348 }
349}
350
351
352
353/* Render a scene */
354static void
355Render(void)
356{
357 int NumBoxes = 100;
358 int i;
359
360 InitGL();
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
389static void
390WriteFile(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 */
439static void
440Usage(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
449int
450main(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 Render();
471 WriteFile(fileName);
472 glXDestroyGLXPbufferSGIX( gDpy, gPBuffer );
473 }
474 return 0;
475}
476