Revert accidental commits from the xquartz tree
[mesa.git] / progs / xdemos / glxpbdemo.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 * Updated on 31 January 2004 to use native GLX by
10 * Andrew P. Lentvorski, Jr. <bsder@allcaps.org>
11 *
12 * Usage:
13 * glxpbdemo width height imgfile
14 * Where:
15 * width is the width, in pixels, of the image to generate.
16 * height is the height, in pixels, of the image to generate.
17 * imgfile is the name of the PPM image file to write.
18 *
19 *
20 * This demo draws 3-D boxes with random orientation.
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 configurations with 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 */
30
31 #include <string.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <X11/Xlib.h>
35 #include <GL/glx.h>
36
37 /* Some ugly global vars */
38 static GLXFBConfig gFBconfig = 0;
39 static Display *gDpy = NULL;
40 static int gScreen = 0;
41 static GLXPbuffer gPBuffer = 0;
42 static int gWidth, gHeight;
43
44
45 /*
46 * Test for appropriate version of GLX to run this program
47 * Input: dpy - the X display
48 * screen - screen number
49 * Return: 0 = GLX not available.
50 * 1 = GLX available.
51 */
52 static int
53 RuntimeQueryGLXVersion(Display *dpy, int screen)
54 {
55 #if defined(GLX_VERSION_1_3) || defined(GLX_VERSION_1_4)
56 char *glxversion;
57
58 glxversion = (char *) glXGetClientString(dpy, GLX_VERSION);
59 if (!(strstr(glxversion, "1.3") || strstr(glxversion, "1.4")))
60 return 0;
61
62 glxversion = (char *) glXQueryServerString(dpy, screen, GLX_VERSION);
63 if (!(strstr(glxversion, "1.3") || strstr(glxversion, "1.4")))
64 return 0;
65
66 return 1;
67 #else
68 return 0;
69 #endif
70 }
71
72
73
74 /*
75 * Create the pbuffer and return a GLXPbuffer handle.
76 */
77 static GLXPbuffer
78 MakePbuffer( Display *dpy, int screen, int width, int height )
79 {
80 GLXFBConfig *fbConfigs;
81 GLXFBConfig chosenFBConfig;
82 GLXPbuffer pBuffer = None;
83
84 int nConfigs;
85 int fbconfigid;
86
87 int fbAttribs[] = {
88 GLX_RENDER_TYPE, GLX_RGBA_BIT,
89 GLX_DEPTH_SIZE, 1,
90 GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT | GLX_PBUFFER_BIT,
91 None
92 };
93
94 int pbAttribs[] = {
95 GLX_PBUFFER_WIDTH, 0,
96 GLX_PBUFFER_HEIGHT, 0,
97 GLX_LARGEST_PBUFFER, False,
98 GLX_PRESERVED_CONTENTS, False,
99 None
100 };
101
102 pbAttribs[1] = width;
103 pbAttribs[3] = height;
104
105 fbConfigs = glXChooseFBConfig(dpy, screen, fbAttribs, &nConfigs);
106
107 if (0 == nConfigs || !fbConfigs) {
108 printf("Error: glxChooseFBConfig failed\n");
109 XFree(fbConfigs);
110 XCloseDisplay(dpy);
111 return 0;
112 }
113
114 chosenFBConfig = fbConfigs[0];
115
116 glXGetFBConfigAttrib(dpy, chosenFBConfig, GLX_FBCONFIG_ID, &fbconfigid);
117 printf("Chose 0x%x as fbconfigid\n", fbconfigid);
118
119 /* Create the pbuffer using first fbConfig in the list that works. */
120 pBuffer = glXCreatePbuffer(dpy, chosenFBConfig, pbAttribs);
121
122 if (pBuffer) {
123 gFBconfig = chosenFBConfig;
124 gWidth = width;
125 gHeight = height;
126 }
127
128 XFree(fbConfigs);
129
130 return pBuffer;
131 }
132
133
134
135 /*
136 * Do all the X / GLX setup stuff.
137 */
138 static int
139 Setup(int width, int height)
140 {
141 #if defined(GLX_VERSION_1_3) || defined(GLX_VERSION_1_4)
142 GLXContext glCtx;
143
144 /* Open the X display */
145 gDpy = XOpenDisplay(NULL);
146 if (!gDpy) {
147 printf("Error: couldn't open default X display.\n");
148 return 0;
149 }
150
151 /* Get default screen */
152 gScreen = DefaultScreen(gDpy);
153
154 /* Test that GLX is available */
155 if (!RuntimeQueryGLXVersion(gDpy, gScreen)) {
156 printf("Error: GLX 1.3 or 1.4 not available\n");
157 XCloseDisplay(gDpy);
158 return 0;
159 }
160
161 /* Create Pbuffer */
162 gPBuffer = MakePbuffer( gDpy, gScreen, width, height );
163 if (gPBuffer==None) {
164 printf("Error: couldn't create pbuffer\n");
165 XCloseDisplay(gDpy);
166 return 0;
167 }
168
169 /* Create GLX context */
170 glCtx = glXCreateNewContext(gDpy, gFBconfig, GLX_RGBA_TYPE, NULL, True);
171 if (glCtx) {
172 if (!glXIsDirect(gDpy, glCtx)) {
173 printf("Warning: using indirect GLXContext\n");
174 }
175 }
176 else {
177 printf("Error: Couldn't create GLXContext\n");
178 XCloseDisplay(gDpy);
179 return 0;
180 }
181
182 /* Bind context to pbuffer */
183 if (!glXMakeCurrent(gDpy, gPBuffer, glCtx)) {
184 printf("Error: glXMakeCurrent failed\n");
185 XCloseDisplay(gDpy);
186 return 0;
187 }
188
189 return 1; /* Success!! */
190 #else
191 printf("Error: GLX version 1.3 or 1.4 not available at compile time\n");
192 return 0;
193 #endif
194 }
195
196
197
198 /* One-time GL setup */
199 static void
200 InitGL(void)
201 {
202 static GLfloat pos[4] = {0.0, 0.0, 10.0, 0.0};
203 glEnable(GL_LIGHTING);
204 glEnable(GL_LIGHT0);
205 glLightfv(GL_LIGHT0, GL_POSITION, pos);
206 glEnable(GL_NORMALIZE);
207 glEnable(GL_DEPTH_TEST);
208 glEnable(GL_CULL_FACE);
209
210 glViewport(0, 0, gWidth, gHeight);
211 glMatrixMode( GL_PROJECTION );
212 glLoadIdentity();
213 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
214 glMatrixMode( GL_MODELVIEW );
215 glLoadIdentity();
216 glTranslatef( 0.0, 0.0, -15.0 );
217
218 }
219
220
221 /* Return random float in [0,1] */
222 static float
223 Random(void)
224 {
225 int i = rand();
226 return (float) (i % 1000) / 1000.0;
227 }
228
229
230 static void
231 RandomColor(void)
232 {
233 GLfloat c[4];
234 c[0] = Random();
235 c[1] = Random();
236 c[2] = Random();
237 c[3] = 1.0;
238 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c);
239 }
240
241
242 /* This function borrowed from Mark Kilgard's GLUT */
243 static void
244 drawBox(GLfloat x0, GLfloat x1, GLfloat y0, GLfloat y1,
245 GLfloat z0, GLfloat z1, GLenum type)
246 {
247 static GLfloat n[6][3] =
248 {
249 {-1.0, 0.0, 0.0},
250 {0.0, 1.0, 0.0},
251 {1.0, 0.0, 0.0},
252 {0.0, -1.0, 0.0},
253 {0.0, 0.0, 1.0},
254 {0.0, 0.0, -1.0}
255 };
256 static GLint faces[6][4] =
257 {
258 {0, 1, 2, 3},
259 {3, 2, 6, 7},
260 {7, 6, 5, 4},
261 {4, 5, 1, 0},
262 {5, 6, 2, 1},
263 {7, 4, 0, 3}
264 };
265 GLfloat v[8][3], tmp;
266 GLint i;
267
268 if (x0 > x1) {
269 tmp = x0;
270 x0 = x1;
271 x1 = tmp;
272 }
273 if (y0 > y1) {
274 tmp = y0;
275 y0 = y1;
276 y1 = tmp;
277 }
278 if (z0 > z1) {
279 tmp = z0;
280 z0 = z1;
281 z1 = tmp;
282 }
283 v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
284 v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
285 v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
286 v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
287 v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
288 v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
289
290 for (i = 0; i < 6; i++) {
291 glBegin(type);
292 glNormal3fv(&n[i][0]);
293 glVertex3fv(&v[faces[i][0]][0]);
294 glVertex3fv(&v[faces[i][1]][0]);
295 glVertex3fv(&v[faces[i][2]][0]);
296 glVertex3fv(&v[faces[i][3]][0]);
297 glEnd();
298 }
299 }
300
301
302
303 /* Render a scene */
304 static void
305 Render(void)
306 {
307 int NumBoxes = 100;
308 int i;
309
310 InitGL();
311 glClearColor(0.2, 0.2, 0.9, 0.0);
312 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
313
314 for (i=0;i<NumBoxes;i++) {
315 float tx = -2.0 + 4.0 * Random();
316 float ty = -2.0 + 4.0 * Random();
317 float tz = 4.0 - 16.0 * Random();
318 float sx = 0.1 + Random() * 0.4;
319 float sy = 0.1 + Random() * 0.4;
320 float sz = 0.1 + Random() * 0.4;
321 float rx = Random();
322 float ry = Random();
323 float rz = Random();
324 float ra = Random() * 360.0;
325 glPushMatrix();
326 glTranslatef(tx, ty, tz);
327 glRotatef(ra, rx, ry, rz);
328 glScalef(sx, sy, sz);
329 RandomColor();
330 drawBox(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0, GL_POLYGON);
331 glPopMatrix();
332 }
333
334 glFinish();
335 }
336
337
338
339 static void
340 WriteFile(const char *filename)
341 {
342 FILE *f;
343 GLubyte *image;
344 int i;
345
346 image = malloc(gWidth * gHeight * 3 * sizeof(GLubyte));
347 if (!image) {
348 printf("Error: couldn't allocate image buffer\n");
349 return;
350 }
351
352 glPixelStorei(GL_PACK_ALIGNMENT, 1);
353 glReadPixels(0, 0, gWidth, gHeight, GL_RGB, GL_UNSIGNED_BYTE, image);
354
355 f = fopen(filename, "w");
356 if (!f) {
357 printf("Couldn't open image file: %s\n", filename);
358 return;
359 }
360 fprintf(f,"P6\n");
361 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
362 fprintf(f,"%i %i\n", gWidth, gHeight);
363 fprintf(f,"255\n");
364 fclose(f);
365 f = fopen(filename, "ab"); /* now append binary data */
366 if (!f) {
367 printf("Couldn't append to image file: %s\n", filename);
368 return;
369 }
370
371 for (i=0;i<gHeight;i++) {
372 GLubyte *rowPtr;
373 /* Remember, OpenGL images are bottom to top. Have to reverse. */
374 rowPtr = image + (gHeight-1-i) * gWidth*3;
375 fwrite(rowPtr, 1, gWidth*3, f);
376 }
377
378 fclose(f);
379 free(image);
380
381 printf("Wrote %d by %d image file: %s\n", gWidth, gHeight, filename);
382 }
383
384
385
386 /*
387 * Print message describing command line parameters.
388 */
389 static void
390 Usage(const char *appName)
391 {
392 printf("Usage:\n");
393 printf(" %s width height imgfile\n", appName);
394 printf("Where imgfile is a ppm file\n");
395 }
396
397
398
399 int
400 main(int argc, char *argv[])
401 {
402 if (argc!=4) {
403 Usage(argv[0]);
404 }
405 else {
406 int width = atoi(argv[1]);
407 int height = atoi(argv[2]);
408 char *fileName = argv[3];
409 if (width<=0) {
410 printf("Error: width parameter must be at least 1.\n");
411 return 1;
412 }
413 if (height<=0) {
414 printf("Error: height parameter must be at least 1.\n");
415 return 1;
416 }
417 if (!Setup(width, height)) {
418 return 1;
419 }
420
421 printf("Setup completed\n");
422 Render();
423 printf("Render completed.\n");
424 WriteFile(fileName);
425 printf("File write completed.\n");
426
427 glXDestroyPbuffer( gDpy, gPBuffer );
428 }
429 return 0;
430 }
431