Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pbdemo.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * This program demonstrates how to do "off-screen" rendering using
  3. * the GLX pixel buffer extension.
  4. *
  5. * Written by Brian Paul for the "OpenGL and Window System Integration"
  6. * course presented at SIGGRAPH '97. Updated on 5 October 2002.
  7. *
  8. * Usage:
  9. * pbuffers width height imgfile
  10. * Where:
  11. * width is the width, in pixels, of the image to generate.
  12. * height is the height, in pixels, of the image to generate.
  13. * imgfile is the name of the PPM image file to write.
  14. *
  15. *
  16. * This demo draws 3-D boxes with random orientation. A pbuffer with
  17. * a depth (Z) buffer is prefered but if such a pbuffer can't be created
  18. * we use a non-depth-buffered config.
  19. *
  20. * On machines such as the SGI Indigo you may have to reconfigure your
  21. * display/X server to enable pbuffers. Look in the /usr/gfx/ucode/MGRAS/vof/
  22. * directory for display configurationswith the _pbuf suffix. Use
  23. * setmon -x <vof> to configure your X server and display for pbuffers.
  24. *
  25. * O2 systems seem to support pbuffers well.
  26. *
  27. * IR systems (at least 1RM systems) don't have single-buffered, RGBA,
  28. * Z-buffered pbuffer configs. BUT, they DO have DOUBLE-buffered, RGBA,
  29. * Z-buffered pbuffers. Note how we try four different fbconfig attribute
  30. * lists below!
  31. */
  32. #include <assert.h>
  33. #include <string.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <X11/Xlib.h>
  37. #include "pbutil.h"
  38. /* Some ugly global vars */
  39. static Display *gDpy = NULL;
  40. static int gScreen = 0;
  41. static FBCONFIG gFBconfig = 0;
  42. static PBUFFER gPBuffer = 0;
  43. static int gWidth, gHeight;
  44. static GLXContext glCtx;
  45. /*
  46. * Create the pbuffer and return a GLXPbuffer handle.
  47. *
  48. * We loop over a list of fbconfigs trying to create
  49. * a pixel buffer. We return the first pixel buffer which we successfully
  50. * create.
  51. */
  52. static PBUFFER
  53. MakePbuffer( Display *dpy, int screen, int width, int height )
  54. {
  55. #define NUM_FB_CONFIGS 4
  56. const char fbString[NUM_FB_CONFIGS][100] = {
  57. "Single Buffered, depth buffer",
  58. "Double Buffered, depth buffer",
  59. "Single Buffered, no depth buffer",
  60. "Double Buffered, no depth buffer"
  61. };
  62. int fbAttribs[NUM_FB_CONFIGS][100] = {
  63. {
  64. /* Single buffered, with depth buffer */
  65. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  66. GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
  67. GLX_RED_SIZE, 1,
  68. GLX_GREEN_SIZE, 1,
  69. GLX_BLUE_SIZE, 1,
  70. GLX_DEPTH_SIZE, 1,
  71. GLX_DOUBLEBUFFER, 0,
  72. GLX_STENCIL_SIZE, 0,
  73. None
  74. },
  75. {
  76. /* Double buffered, with depth buffer */
  77. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  78. GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
  79. GLX_RED_SIZE, 1,
  80. GLX_GREEN_SIZE, 1,
  81. GLX_BLUE_SIZE, 1,
  82. GLX_DEPTH_SIZE, 1,
  83. GLX_DOUBLEBUFFER, 1,
  84. GLX_STENCIL_SIZE, 0,
  85. None
  86. },
  87. {
  88. /* Single bufferd, without depth buffer */
  89. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  90. GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
  91. GLX_RED_SIZE, 1,
  92. GLX_GREEN_SIZE, 1,
  93. GLX_BLUE_SIZE, 1,
  94. GLX_DEPTH_SIZE, 0,
  95. GLX_DOUBLEBUFFER, 0,
  96. GLX_STENCIL_SIZE, 0,
  97. None
  98. },
  99. {
  100. /* Double bufferd, without depth buffer */
  101. GLX_RENDER_TYPE, GLX_RGBA_BIT,
  102. GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
  103. GLX_RED_SIZE, 1,
  104. GLX_GREEN_SIZE, 1,
  105. GLX_BLUE_SIZE, 1,
  106. GLX_DEPTH_SIZE, 0,
  107. GLX_DOUBLEBUFFER, 1,
  108. GLX_STENCIL_SIZE, 0,
  109. None
  110. }
  111. };
  112. Bool largest = True;
  113. Bool preserve = False;
  114. FBCONFIG *fbConfigs;
  115. PBUFFER pBuffer = None;
  116. int nConfigs;
  117. int i;
  118. int attempt;
  119. for (attempt=0; attempt<NUM_FB_CONFIGS; attempt++) {
  120. /* Get list of possible frame buffer configurations */
  121. fbConfigs = ChooseFBConfig(dpy, screen, fbAttribs[attempt], &nConfigs);
  122. if (nConfigs==0 || !fbConfigs) {
  123. printf("Error: glXChooseFBConfig failed\n");
  124. XCloseDisplay(dpy);
  125. return 0;
  126. }
  127. #if 0 /*DEBUG*/
  128. for (i=0;i<nConfigs;i++) {
  129. printf("Config %d\n", i);
  130. PrintFBConfigInfo(dpy, screen, fbConfigs[i], 0);
  131. }
  132. #endif
  133. /* Create the pbuffer using first fbConfig in the list that works. */
  134. for (i=0;i<nConfigs;i++) {
  135. pBuffer = CreatePbuffer(dpy, screen, fbConfigs[i], width, height, preserve, largest);
  136. if (pBuffer) {
  137. gFBconfig = fbConfigs[i];
  138. gWidth = width;
  139. gHeight = height;
  140. break;
  141. }
  142. }
  143. if (pBuffer!=None) {
  144. break;
  145. }
  146. }
  147. if (pBuffer) {
  148. printf("Using: %s\n", fbString[attempt]);
  149. }
  150. XFree(fbConfigs);
  151. return pBuffer;
  152. #undef NUM_FB_CONFIGS
  153. }
  154. /*
  155. * Do all the X / GLX setup stuff.
  156. */
  157. static int
  158. Setup(int width, int height)
  159. {
  160. int pbSupport;
  161. XVisualInfo *visInfo;
  162. /* Open the X display */
  163. gDpy = XOpenDisplay(NULL);
  164. if (!gDpy) {
  165. printf("Error: couldn't open default X display.\n");
  166. return 0;
  167. }
  168. /* Get default screen */
  169. gScreen = DefaultScreen(gDpy);
  170. /* Test that pbuffers are available */
  171. pbSupport = QueryPbuffers(gDpy, gScreen);
  172. if (pbSupport == 1) {
  173. printf("Using GLX 1.3 Pbuffers\n");
  174. }
  175. else if (pbSupport == 2) {
  176. printf("Using SGIX Pbuffers\n");
  177. }
  178. else {
  179. printf("Error: pbuffers not available on this screen\n");
  180. XCloseDisplay(gDpy);
  181. return 0;
  182. }
  183. /* Create Pbuffer */
  184. gPBuffer = MakePbuffer( gDpy, gScreen, width, height );
  185. if (gPBuffer==None) {
  186. printf("Error: couldn't create pbuffer\n");
  187. XCloseDisplay(gDpy);
  188. return 0;
  189. }
  190. /* Get corresponding XVisualInfo */
  191. visInfo = GetVisualFromFBConfig(gDpy, gScreen, gFBconfig);
  192. if (!visInfo) {
  193. printf("Error: can't get XVisualInfo from FBconfig\n");
  194. XCloseDisplay(gDpy);
  195. return 0;
  196. }
  197. /* Create GLX context */
  198. glCtx = glXCreateContext(gDpy, visInfo, NULL, True);
  199. if (!glCtx) {
  200. /* try indirect */
  201. glCtx = glXCreateContext(gDpy, visInfo, NULL, False);
  202. if (!glCtx) {
  203. printf("Error: Couldn't create GLXContext\n");
  204. XFree(visInfo);
  205. XCloseDisplay(gDpy);
  206. return 0;
  207. }
  208. else {
  209. printf("Warning: using indirect GLXContext\n");
  210. }
  211. }
  212. /* Bind context to pbuffer */
  213. if (!glXMakeCurrent(gDpy, gPBuffer, glCtx)) {
  214. printf("Error: glXMakeCurrent failed\n");
  215. XFree(visInfo);
  216. XCloseDisplay(gDpy);
  217. return 0;
  218. }
  219. return 1; /* Success!! */
  220. }
  221. /* One-time GL setup */
  222. static void
  223. InitGL(void)
  224. {
  225. static GLfloat pos[4] = {0.0, 0.0, 10.0, 0.0};
  226. glEnable(GL_LIGHTING);
  227. glEnable(GL_LIGHT0);
  228. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  229. glEnable(GL_NORMALIZE);
  230. glEnable(GL_DEPTH_TEST);
  231. glEnable(GL_CULL_FACE);
  232. glViewport(0, 0, gWidth, gHeight);
  233. glMatrixMode( GL_PROJECTION );
  234. glLoadIdentity();
  235. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  236. glMatrixMode( GL_MODELVIEW );
  237. glLoadIdentity();
  238. glTranslatef( 0.0, 0.0, -15.0 );
  239. }
  240. /* Return random float in [0,1] */
  241. static float
  242. Random(void)
  243. {
  244. int i = rand();
  245. return (float) (i % 1000) / 1000.0;
  246. }
  247. static void
  248. RandomColor(void)
  249. {
  250. GLfloat c[4];
  251. c[0] = Random();
  252. c[1] = Random();
  253. c[2] = Random();
  254. c[3] = 1.0;
  255. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c);
  256. }
  257. /* This function borrowed from Mark Kilgard's GLUT */
  258. static void
  259. drawBox(GLfloat x0, GLfloat x1, GLfloat y0, GLfloat y1,
  260. GLfloat z0, GLfloat z1, GLenum type)
  261. {
  262. static GLfloat n[6][3] =
  263. {
  264. {-1.0, 0.0, 0.0},
  265. {0.0, 1.0, 0.0},
  266. {1.0, 0.0, 0.0},
  267. {0.0, -1.0, 0.0},
  268. {0.0, 0.0, 1.0},
  269. {0.0, 0.0, -1.0}
  270. };
  271. static GLint faces[6][4] =
  272. {
  273. {0, 1, 2, 3},
  274. {3, 2, 6, 7},
  275. {7, 6, 5, 4},
  276. {4, 5, 1, 0},
  277. {5, 6, 2, 1},
  278. {7, 4, 0, 3}
  279. };
  280. GLfloat v[8][3], tmp;
  281. GLint i;
  282. if (x0 > x1) {
  283. tmp = x0;
  284. x0 = x1;
  285. x1 = tmp;
  286. }
  287. if (y0 > y1) {
  288. tmp = y0;
  289. y0 = y1;
  290. y1 = tmp;
  291. }
  292. if (z0 > z1) {
  293. tmp = z0;
  294. z0 = z1;
  295. z1 = tmp;
  296. }
  297. v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  298. v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  299. v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  300. v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  301. v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  302. v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  303. for (i = 0; i < 6; i++) {
  304. glBegin(type);
  305. glNormal3fv(&n[i][0]);
  306. glVertex3fv(&v[faces[i][0]][0]);
  307. glVertex3fv(&v[faces[i][1]][0]);
  308. glVertex3fv(&v[faces[i][2]][0]);
  309. glVertex3fv(&v[faces[i][3]][0]);
  310. glEnd();
  311. }
  312. }
  313. /* Render a scene */
  314. static void
  315. Render(void)
  316. {
  317. int NumBoxes = 100;
  318. int i;
  319. glClearColor(0.2, 0.2, 0.9, 0.0);
  320. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  321. for (i=0;i<NumBoxes;i++) {
  322. float tx = -2.0 + 4.0 * Random();
  323. float ty = -2.0 + 4.0 * Random();
  324. float tz = 4.0 - 16.0 * Random();
  325. float sx = 0.1 + Random() * 0.4;
  326. float sy = 0.1 + Random() * 0.4;
  327. float sz = 0.1 + Random() * 0.4;
  328. float rx = Random();
  329. float ry = Random();
  330. float rz = Random();
  331. float ra = Random() * 360.0;
  332. glPushMatrix();
  333. glTranslatef(tx, ty, tz);
  334. glRotatef(ra, rx, ry, rz);
  335. glScalef(sx, sy, sz);
  336. RandomColor();
  337. drawBox(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0, GL_POLYGON);
  338. glPopMatrix();
  339. }
  340. glFinish();
  341. }
  342. static void
  343. WriteFile(const char *filename)
  344. {
  345. FILE *f;
  346. GLubyte *image;
  347. int i;
  348. image = malloc(gWidth * gHeight * 3 * sizeof(GLubyte));
  349. if (!image) {
  350. printf("Error: couldn't allocate image buffer\n");
  351. return;
  352. }
  353. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  354. glReadPixels(0, 0, gWidth, gHeight, GL_RGB, GL_UNSIGNED_BYTE, image);
  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. for (i=0;i<gHeight;i++) {
  371. GLubyte *rowPtr;
  372. /* Remember, OpenGL images are bottom to top. Have to reverse. */
  373. rowPtr = image + (gHeight-1-i) * gWidth*3;
  374. fwrite(rowPtr, 1, gWidth*3, f);
  375. }
  376. fclose(f);
  377. free(image);
  378. printf("Wrote %d by %d image file: %s\n", gWidth, gHeight, filename);
  379. }
  380. /*
  381. * Print message describing command line parameters.
  382. */
  383. static void
  384. Usage(const char *appName)
  385. {
  386. printf("Usage:\n");
  387. printf(" %s width height imgfile\n", appName);
  388. printf("Where imgfile is a ppm file\n");
  389. }
  390. int
  391. main(int argc, char *argv[])
  392. {
  393. if (argc!=4) {
  394. Usage(argv[0]);
  395. }
  396. else {
  397. int width = atoi(argv[1]);
  398. int height = atoi(argv[2]);
  399. char *fileName = argv[3];
  400. if (width<=0) {
  401. printf("Error: width parameter must be at least 1.\n");
  402. return 1;
  403. }
  404. if (height<=0) {
  405. printf("Error: height parameter must be at least 1.\n");
  406. return 1;
  407. }
  408. if (!Setup(width, height)) {
  409. return 1;
  410. }
  411. InitGL();
  412. Render();
  413. WriteFile(fileName);
  414. DestroyPbuffer(gDpy, gScreen, gPBuffer);
  415. }
  416. return 0;
  417. }