renumber ARB_shader_objects and ARB_vertex_shader offsets
[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 XCloseDisplay(dpy);
110 return 0;
111 }
112
113 chosenFBConfig = fbConfigs[0];
114
115 glXGetFBConfigAttrib(dpy, chosenFBConfig, GLX_FBCONFIG_ID, &fbconfigid);
116 printf("Chose 0x%x as fbconfigid\n", fbconfigid);
117
118 /* Create the pbuffer using first fbConfig in the list that works. */
119 pBuffer = glXCreatePbuffer(dpy, chosenFBConfig, pbAttribs);
120
121 if (pBuffer) {
122 gFBconfig = chosenFBConfig;
123 gWidth = width;
124 gHeight = height;
125 }
126
127 XFree(fbConfigs);
128
129 return pBuffer;
130 }
131
132
133
134 /*
135 * Do all the X / GLX setup stuff.
136 */
137 static int
138 Setup(int width, int height)
139 {
140 #if defined(GLX_VERSION_1_3) || defined(GLX_VERSION_1_4)
141 XVisualInfo *visInfo;
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 XFree(visInfo);
179 XCloseDisplay(gDpy);
180 return 0;
181 }
182
183 /* Bind context to pbuffer */
184 if (!glXMakeCurrent(gDpy, gPBuffer, glCtx)) {
185 printf("Error: glXMakeCurrent failed\n");
186 XFree(visInfo);
187 XCloseDisplay(gDpy);
188 return 0;
189 }
190
191 return 1; /* Success!! */
192 #else
193 printf("Error: GLX version 1.3 or 1.4 not available at compile time\n");
194 return 0;
195 #endif
196 }
197
198
199
200 /* One-time GL setup */
201 static void
202 InitGL(void)
203 {
204 static GLfloat pos[4] = {0.0, 0.0, 10.0, 0.0};
205 glEnable(GL_LIGHTING);
206 glEnable(GL_LIGHT0);
207 glLightfv(GL_LIGHT0, GL_POSITION, pos);
208 glEnable(GL_NORMALIZE);
209 glEnable(GL_DEPTH_TEST);
210 glEnable(GL_CULL_FACE);
211
212 glViewport(0, 0, gWidth, gHeight);
213 glMatrixMode( GL_PROJECTION );
214 glLoadIdentity();
215 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
216 glMatrixMode( GL_MODELVIEW );
217 glLoadIdentity();
218 glTranslatef( 0.0, 0.0, -15.0 );
219
220 }
221
222
223 /* Return random float in [0,1] */
224 static float
225 Random(void)
226 {
227 int i = rand();
228 return (float) (i % 1000) / 1000.0;
229 }
230
231
232 static void
233 RandomColor(void)
234 {
235 GLfloat c[4];
236 c[0] = Random();
237 c[1] = Random();
238 c[2] = Random();
239 c[3] = 1.0;
240 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c);
241 }
242
243
244 /* This function borrowed from Mark Kilgard's GLUT */
245 static void
246 drawBox(GLfloat x0, GLfloat x1, GLfloat y0, GLfloat y1,
247 GLfloat z0, GLfloat z1, GLenum type)
248 {
249 static GLfloat n[6][3] =
250 {
251 {-1.0, 0.0, 0.0},
252 {0.0, 1.0, 0.0},
253 {1.0, 0.0, 0.0},
254 {0.0, -1.0, 0.0},
255 {0.0, 0.0, 1.0},
256 {0.0, 0.0, -1.0}
257 };
258 static GLint faces[6][4] =
259 {
260 {0, 1, 2, 3},
261 {3, 2, 6, 7},
262 {7, 6, 5, 4},
263 {4, 5, 1, 0},
264 {5, 6, 2, 1},
265 {7, 4, 0, 3}
266 };
267 GLfloat v[8][3], tmp;
268 GLint i;
269
270 if (x0 > x1) {
271 tmp = x0;
272 x0 = x1;
273 x1 = tmp;
274 }
275 if (y0 > y1) {
276 tmp = y0;
277 y0 = y1;
278 y1 = tmp;
279 }
280 if (z0 > z1) {
281 tmp = z0;
282 z0 = z1;
283 z1 = tmp;
284 }
285 v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
286 v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
287 v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
288 v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
289 v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
290 v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
291
292 for (i = 0; i < 6; i++) {
293 glBegin(type);
294 glNormal3fv(&n[i][0]);
295 glVertex3fv(&v[faces[i][0]][0]);
296 glVertex3fv(&v[faces[i][1]][0]);
297 glVertex3fv(&v[faces[i][2]][0]);
298 glVertex3fv(&v[faces[i][3]][0]);
299 glEnd();
300 }
301 }
302
303
304
305 /* Render a scene */
306 static void
307 Render(void)
308 {
309 int NumBoxes = 100;
310 int i;
311
312 InitGL();
313 glClearColor(0.2, 0.2, 0.9, 0.0);
314 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
315
316 for (i=0;i<NumBoxes;i++) {
317 float tx = -2.0 + 4.0 * Random();
318 float ty = -2.0 + 4.0 * Random();
319 float tz = 4.0 - 16.0 * Random();
320 float sx = 0.1 + Random() * 0.4;
321 float sy = 0.1 + Random() * 0.4;
322 float sz = 0.1 + Random() * 0.4;
323 float rx = Random();
324 float ry = Random();
325 float rz = Random();
326 float ra = Random() * 360.0;
327 glPushMatrix();
328 glTranslatef(tx, ty, tz);
329 glRotatef(ra, rx, ry, rz);
330 glScalef(sx, sy, sz);
331 RandomColor();
332 drawBox(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0, GL_POLYGON);
333 glPopMatrix();
334 }
335
336 glFinish();
337 }
338
339
340
341 static void
342 WriteFile(const char *filename)
343 {
344 FILE *f;
345 GLubyte *image;
346 int i;
347
348 image = malloc(gWidth * gHeight * 3 * sizeof(GLubyte));
349 if (!image) {
350 printf("Error: couldn't allocate image buffer\n");
351 return;
352 }
353
354 glPixelStorei(GL_PACK_ALIGNMENT, 1);
355 glReadPixels(0, 0, gWidth, gHeight, GL_RGB, GL_UNSIGNED_BYTE, image);
356
357 f = fopen(filename, "w");
358 if (!f) {
359 printf("Couldn't open image file: %s\n", filename);
360 return;
361 }
362 fprintf(f,"P6\n");
363 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
364 fprintf(f,"%i %i\n", gWidth, gHeight);
365 fprintf(f,"255\n");
366 fclose(f);
367 f = fopen(filename, "ab"); /* now append binary data */
368 if (!f) {
369 printf("Couldn't append to image file: %s\n", filename);
370 return;
371 }
372
373 for (i=0;i<gHeight;i++) {
374 GLubyte *rowPtr;
375 /* Remember, OpenGL images are bottom to top. Have to reverse. */
376 rowPtr = image + (gHeight-1-i) * gWidth*3;
377 fwrite(rowPtr, 1, gWidth*3, f);
378 }
379
380 fclose(f);
381 free(image);
382
383 printf("Wrote %d by %d image file: %s\n", gWidth, gHeight, filename);
384 }
385
386
387
388 /*
389 * Print message describing command line parameters.
390 */
391 static void
392 Usage(const char *appName)
393 {
394 printf("Usage:\n");
395 printf(" %s width height imgfile\n", appName);
396 printf("Where imgfile is a ppm file\n");
397 }
398
399
400
401 int
402 main(int argc, char *argv[])
403 {
404 if (argc!=4) {
405 Usage(argv[0]);
406 }
407 else {
408 int width = atoi(argv[1]);
409 int height = atoi(argv[2]);
410 char *fileName = argv[3];
411 if (width<=0) {
412 printf("Error: width parameter must be at least 1.\n");
413 return 1;
414 }
415 if (height<=0) {
416 printf("Error: height parameter must be at least 1.\n");
417 return 1;
418 }
419 if (!Setup(width, height)) {
420 return 1;
421 }
422
423 printf("Setup completed\n");
424 Render();
425 printf("Render completed.\n");
426 WriteFile(fileName);
427 printf("File write completed.\n");
428
429 glXDestroyPbuffer( gDpy, gPBuffer );
430 }
431 return 0;
432 }
433