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