blob: 611e7e594fdc4fd1dffe90d25851713aadcd14e6 [file] [log] [blame]
Brian Paul21666e32002-10-05 18:30:13 +00001/* $Id: pbdemo.c,v 1.1 2002/10/05 18:30:13 brianp Exp $ */
2
3/*
4 * This program demonstrates how to do "off-screen" rendering using
5 * the GLX pixel buffer extension.
6 *
7 * Written by Brian Paul for the "OpenGL and Window System Integration"
8 * course presented at SIGGRAPH '97. Updated on 5 October 2002.
9 *
10 * Usage:
11 * pbuffers width height imgfile
12 * Where:
13 * width is the width, in pixels, of the image to generate.
14 * height is the height, in pixels, of the image to generate.
15 * imgfile is the name of the PPM image file to write.
16 *
17 *
18 * This demo draws 3-D boxes with random orientation. A pbuffer with
19 * a depth (Z) buffer is prefered but if such a pbuffer can't be created
20 * we use a non-depth-buffered config.
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 configurationswith 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 * IR systems (at least 1RM systems) don't have single-buffered, RGBA,
30 * Z-buffered pbuffer configs. BUT, they DO have DOUBLE-buffered, RGBA,
31 * Z-buffered pbuffers. Note how we try four different fbconfig attribute
32 * lists below!
33 */
34
35
36#include <assert.h>
37#include <string.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <X11/Xlib.h>
41#include "pbutil.h"
42
43
44/* Some ugly global vars */
45static GLXFBConfigSGIX gFBconfig = 0;
46static Display *gDpy = NULL;
47static int gScreen = 0;
48static GLXPbufferSGIX gPBuffer = 0;
49static int gWidth, gHeight;
50
51
52
53/*
54 * Create the pbuffer and return a GLXPbufferSGIX handle.
55 */
56static GLXPbufferSGIX
57MakePbuffer( Display *dpy, int screen, int width, int height )
58{
59#define NUM_FB_CONFIGS 4
60 char fbString[NUM_FB_CONFIGS][100] = {
61 "Single Buffered, depth buffer",
62 "Double Buffered, depth buffer",
63 "Single Buffered, no depth buffer",
64 "Double Buffered, no depth buffer"
65 };
66 int fbAttribs[NUM_FB_CONFIGS][100] = {
67 {
68 /* Single buffered, with depth buffer */
69 GLX_RENDER_TYPE_SGIX, GLX_RGBA_BIT_SGIX,
70 GLX_DRAWABLE_TYPE_SGIX, GLX_PIXMAP_BIT_SGIX,
71 GLX_RED_SIZE, 1,
72 GLX_GREEN_SIZE, 1,
73 GLX_BLUE_SIZE, 1,
74 GLX_DEPTH_SIZE, 1,
75 GLX_DOUBLEBUFFER, 0,
76 GLX_STENCIL_SIZE, 0,
77 None
78 },
79 {
80 /* Double buffered, with depth buffer */
81 GLX_RENDER_TYPE_SGIX, GLX_RGBA_BIT_SGIX,
82 GLX_DRAWABLE_TYPE_SGIX, GLX_PIXMAP_BIT_SGIX,
83 GLX_RED_SIZE, 1,
84 GLX_GREEN_SIZE, 1,
85 GLX_BLUE_SIZE, 1,
86 GLX_DEPTH_SIZE, 1,
87 GLX_DOUBLEBUFFER, 1,
88 GLX_STENCIL_SIZE, 0,
89 None
90 },
91 {
92 /* Single bufferd, without depth buffer */
93 GLX_RENDER_TYPE_SGIX, GLX_RGBA_BIT_SGIX,
94 GLX_DRAWABLE_TYPE_SGIX, GLX_PIXMAP_BIT_SGIX,
95 GLX_RED_SIZE, 1,
96 GLX_GREEN_SIZE, 1,
97 GLX_BLUE_SIZE, 1,
98 GLX_DEPTH_SIZE, 0,
99 GLX_DOUBLEBUFFER, 0,
100 GLX_STENCIL_SIZE, 0,
101 None
102 },
103 {
104 /* Double bufferd, without depth buffer */
105 GLX_RENDER_TYPE_SGIX, GLX_RGBA_BIT_SGIX,
106 GLX_DRAWABLE_TYPE_SGIX, GLX_PIXMAP_BIT_SGIX,
107 GLX_RED_SIZE, 1,
108 GLX_GREEN_SIZE, 1,
109 GLX_BLUE_SIZE, 1,
110 GLX_DEPTH_SIZE, 0,
111 GLX_DOUBLEBUFFER, 1,
112 GLX_STENCIL_SIZE, 0,
113 None
114 }
115 };
116 int pbAttribs[] = {
117 GLX_LARGEST_PBUFFER_SGIX, True,
118 GLX_PRESERVED_CONTENTS_SGIX, False,
119 None
120 };
121 GLXFBConfigSGIX *fbConfigs;
122 GLXPbufferSGIX pBuffer = None;
123 int nConfigs;
124 int i;
125 int attempt;
126
127 for (attempt=0; attempt<NUM_FB_CONFIGS; attempt++) {
128
129 /* Get list of possible frame buffer configurations */
130 fbConfigs = glXChooseFBConfigSGIX(dpy, screen, fbAttribs[attempt], &nConfigs);
131 if (nConfigs==0 || !fbConfigs) {
132 printf("Error: glxChooseFBConfigSGIX failed\n");
133 XCloseDisplay(dpy);
134 return 0;
135 }
136
137#ifdef DEBUG
138 for (i=0;i<nConfigs;i++) {
139 printf("Config %d\n", i);
140 PrintFBConfigInfo(dpy, 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, fbConfigs[i], width, height, pbAttribs);
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 */
175static int
176Setup(int width, int height)
177{
178#if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer)
179 XVisualInfo *visInfo;
180 GLXContext glCtx;
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 if (!QueryPbuffers(gDpy, gScreen)) {
194 printf("Error: pbuffers not available on this screen\n");
195 XCloseDisplay(gDpy);
196 return 0;
197 }
198
199 /* Create Pbuffer */
200 gPBuffer = MakePbuffer( gDpy, gScreen, width, height );
201 if (gPBuffer==None) {
202 printf("Error: couldn't create pbuffer\n");
203 XCloseDisplay(gDpy);
204 return 0;
205 }
206
207 /* Get corresponding XVisualInfo */
208 visInfo = glXGetVisualFromFBConfigSGIX(gDpy, gFBconfig);
209 if (!visInfo) {
210 printf("Error: can't get XVisualInfo from FBconfig\n");
211 XCloseDisplay(gDpy);
212 return 0;
213 }
214
215 /* Create GLX context */
216 glCtx = glXCreateContext(gDpy, visInfo, NULL, True);
217 if (!glCtx) {
218 /* try indirect */
219 glCtx = glXCreateContext(gDpy, visInfo, NULL, False);
220 if (!glCtx) {
221 printf("Error: Couldn't create GLXContext\n");
222 XFree(visInfo);
223 XCloseDisplay(gDpy);
224 return 0;
225 }
226 else {
227 printf("Warning: using indirect GLXContext\n");
228 }
229 }
230
231 /* Bind context to pbuffer */
232 if (!glXMakeCurrent(gDpy, gPBuffer, glCtx)) {
233 printf("Error: glXMakeCurrent failed\n");
234 XFree(visInfo);
235 XCloseDisplay(gDpy);
236 return 0;
237 }
238
239 return 1; /* Success!! */
240#else
241 printf("Error: GLX_SGIX_fbconfig and/or GLX_SGIX_pbuffer extensions not"
242 " available at compile-time.\n");
243 return 0;
244#endif
245}
246
247
248
249/* One-time GL setup */
250static void
251InitGL(void)
252{
253 static GLfloat pos[4] = {0.0, 0.0, 10.0, 0.0};
254 glEnable(GL_LIGHTING);
255 glEnable(GL_LIGHT0);
256 glLightfv(GL_LIGHT0, GL_POSITION, pos);
257 glEnable(GL_NORMALIZE);
258 glEnable(GL_DEPTH_TEST);
259 glEnable(GL_CULL_FACE);
260
261 glViewport(0, 0, gWidth, gHeight);
262 glMatrixMode( GL_PROJECTION );
263 glLoadIdentity();
264 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
265 glMatrixMode( GL_MODELVIEW );
266 glLoadIdentity();
267 glTranslatef( 0.0, 0.0, -15.0 );
268
269}
270
271
272/* Return random float in [0,1] */
273static float
274Random(void)
275{
276 int i = rand();
277 return (float) (i % 1000) / 1000.0;
278}
279
280
281static void
282RandomColor(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 */
294static void
295drawBox(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 */
355static void
356Render(void)
357{
358 int NumBoxes = 100;
359 int i;
360
361 InitGL();
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
390static void
391WriteFile(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 */
440static void
441Usage(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
450int
451main(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 Render();
472 WriteFile(fileName);
473 glXDestroyGLXPbufferSGIX( gDpy, gPBuffer );
474 }
475 return 0;
476}
477