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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 buffered, 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 buffered, 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("Note: glXChooseFBConfig(%s) failed\n", fbString[attempt]);
  124. continue;
  125. }
  126. #if 0 /*DEBUG*/
  127. for (i=0;i<nConfigs;i++) {
  128. printf("Config %d\n", i);
  129. PrintFBConfigInfo(dpy, screen, fbConfigs[i], 0);
  130. }
  131. #endif
  132. /* Create the pbuffer using first fbConfig in the list that works. */
  133. for (i=0;i<nConfigs;i++) {
  134. pBuffer = CreatePbuffer(dpy, screen, fbConfigs[i], width, height, preserve, largest);
  135. if (pBuffer) {
  136. gFBconfig = fbConfigs[i];
  137. gWidth = width;
  138. gHeight = height;
  139. break;
  140. }
  141. }
  142. if (pBuffer!=None) {
  143. break;
  144. }
  145. }
  146. if (pBuffer) {
  147. printf("Using: %s\n", fbString[attempt]);
  148. }
  149. XFree(fbConfigs);
  150. return pBuffer;
  151. #undef NUM_FB_CONFIGS
  152. }
  153. /*
  154. * Do all the X / GLX setup stuff.
  155. */
  156. static int
  157. Setup(int width, int height)
  158. {
  159. int pbSupport;
  160. XVisualInfo *visInfo;
  161. /* Open the X display */
  162. gDpy = XOpenDisplay(NULL);
  163. if (!gDpy) {
  164. printf("Error: couldn't open default X display.\n");
  165. return 0;
  166. }
  167. /* Get default screen */
  168. gScreen = DefaultScreen(gDpy);
  169. /* Test that pbuffers are available */
  170. pbSupport = QueryPbuffers(gDpy, gScreen);
  171. if (pbSupport == 1) {
  172. printf("Using GLX 1.3 Pbuffers\n");
  173. }
  174. else if (pbSupport == 2) {
  175. printf("Using SGIX Pbuffers\n");
  176. }
  177. else {
  178. printf("Error: pbuffers not available on this screen\n");
  179. XCloseDisplay(gDpy);
  180. return 0;
  181. }
  182. /* Create Pbuffer */
  183. gPBuffer = MakePbuffer( gDpy, gScreen, width, height );
  184. if (gPBuffer==None) {
  185. printf("Error: couldn't create pbuffer\n");
  186. XCloseDisplay(gDpy);
  187. return 0;
  188. }
  189. /* Get corresponding XVisualInfo */
  190. visInfo = GetVisualFromFBConfig(gDpy, gScreen, gFBconfig);
  191. if (!visInfo) {
  192. printf("Error: can't get XVisualInfo from FBconfig\n");
  193. XCloseDisplay(gDpy);
  194. return 0;
  195. }
  196. /* Create GLX context */
  197. glCtx = glXCreateContext(gDpy, visInfo, NULL, True);
  198. if (!glCtx) {
  199. /* try indirect */
  200. glCtx = glXCreateContext(gDpy, visInfo, NULL, False);
  201. if (!glCtx) {
  202. printf("Error: Couldn't create GLXContext\n");
  203. XFree(visInfo);
  204. XCloseDisplay(gDpy);
  205. return 0;
  206. }
  207. else {
  208. printf("Warning: using indirect GLXContext\n");
  209. }
  210. }
  211. /* Bind context to pbuffer */
  212. if (!glXMakeCurrent(gDpy, gPBuffer, glCtx)) {
  213. printf("Error: glXMakeCurrent failed\n");
  214. XFree(visInfo);
  215. XCloseDisplay(gDpy);
  216. return 0;
  217. }
  218. return 1; /* Success!! */
  219. }
  220. /* One-time GL setup */
  221. static void
  222. InitGL(void)
  223. {
  224. static GLfloat pos[4] = {0.0, 0.0, 10.0, 0.0};
  225. glEnable(GL_LIGHTING);
  226. glEnable(GL_LIGHT0);
  227. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  228. glEnable(GL_NORMALIZE);
  229. glEnable(GL_DEPTH_TEST);
  230. glEnable(GL_CULL_FACE);
  231. glViewport(0, 0, gWidth, gHeight);
  232. glMatrixMode( GL_PROJECTION );
  233. glLoadIdentity();
  234. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  235. glMatrixMode( GL_MODELVIEW );
  236. glLoadIdentity();
  237. glTranslatef( 0.0, 0.0, -15.0 );
  238. }
  239. /* Return random float in [0,1] */
  240. static float
  241. Random(void)
  242. {
  243. int i = rand();
  244. return (float) (i % 1000) / 1000.0;
  245. }
  246. static void
  247. RandomColor(void)
  248. {
  249. GLfloat c[4];
  250. c[0] = Random();
  251. c[1] = Random();
  252. c[2] = Random();
  253. c[3] = 1.0;
  254. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c);
  255. }
  256. /* This function borrowed from Mark Kilgard's GLUT */
  257. static void
  258. drawBox(GLfloat x0, GLfloat x1, GLfloat y0, GLfloat y1,
  259. GLfloat z0, GLfloat z1, GLenum type)
  260. {
  261. static GLfloat n[6][3] =
  262. {
  263. {-1.0, 0.0, 0.0},
  264. {0.0, 1.0, 0.0},
  265. {1.0, 0.0, 0.0},
  266. {0.0, -1.0, 0.0},
  267. {0.0, 0.0, 1.0},
  268. {0.0, 0.0, -1.0}
  269. };
  270. static GLint faces[6][4] =
  271. {
  272. {0, 1, 2, 3},
  273. {3, 2, 6, 7},
  274. {7, 6, 5, 4},
  275. {4, 5, 1, 0},
  276. {5, 6, 2, 1},
  277. {7, 4, 0, 3}
  278. };
  279. GLfloat v[8][3], tmp;
  280. GLint i;
  281. if (x0 > x1) {
  282. tmp = x0;
  283. x0 = x1;
  284. x1 = tmp;
  285. }
  286. if (y0 > y1) {
  287. tmp = y0;
  288. y0 = y1;
  289. y1 = tmp;
  290. }
  291. if (z0 > z1) {
  292. tmp = z0;
  293. z0 = z1;
  294. z1 = tmp;
  295. }
  296. v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
  297. v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
  298. v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  299. v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  300. v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
  301. v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;
  302. for (i = 0; i < 6; i++) {
  303. glBegin(type);
  304. glNormal3fv(&n[i][0]);
  305. glVertex3fv(&v[faces[i][0]][0]);
  306. glVertex3fv(&v[faces[i][1]][0]);
  307. glVertex3fv(&v[faces[i][2]][0]);
  308. glVertex3fv(&v[faces[i][3]][0]);
  309. glEnd();
  310. }
  311. }
  312. /* Render a scene */
  313. static void
  314. Render(void)
  315. {
  316. int NumBoxes = 100;
  317. int i;
  318. glClearColor(0.2, 0.2, 0.9, 0.0);
  319. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  320. for (i=0;i<NumBoxes;i++) {
  321. float tx = -2.0 + 4.0 * Random();
  322. float ty = -2.0 + 4.0 * Random();
  323. float tz = 4.0 - 16.0 * Random();
  324. float sx = 0.1 + Random() * 0.4;
  325. float sy = 0.1 + Random() * 0.4;
  326. float sz = 0.1 + Random() * 0.4;
  327. float rx = Random();
  328. float ry = Random();
  329. float rz = Random();
  330. float ra = Random() * 360.0;
  331. glPushMatrix();
  332. glTranslatef(tx, ty, tz);
  333. glRotatef(ra, rx, ry, rz);
  334. glScalef(sx, sy, sz);
  335. RandomColor();
  336. drawBox(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0, GL_POLYGON);
  337. glPopMatrix();
  338. }
  339. glFinish();
  340. }
  341. static void
  342. WriteFile(const char *filename)
  343. {
  344. FILE *f;
  345. GLubyte *image;
  346. int i;
  347. image = malloc(gWidth * gHeight * 3 * sizeof(GLubyte));
  348. if (!image) {
  349. printf("Error: couldn't allocate image buffer\n");
  350. return;
  351. }
  352. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  353. glReadPixels(0, 0, gWidth, gHeight, GL_RGB, GL_UNSIGNED_BYTE, image);
  354. f = fopen(filename, "w");
  355. if (!f) {
  356. printf("Couldn't open image file: %s\n", filename);
  357. return;
  358. }
  359. fprintf(f,"P6\n");
  360. fprintf(f,"# ppm-file created by %s\n", "trdemo2");
  361. fprintf(f,"%i %i\n", gWidth, gHeight);
  362. fprintf(f,"255\n");
  363. fclose(f);
  364. f = fopen(filename, "ab"); /* now append binary data */
  365. if (!f) {
  366. printf("Couldn't append to image file: %s\n", filename);
  367. return;
  368. }
  369. for (i=0;i<gHeight;i++) {
  370. GLubyte *rowPtr;
  371. /* Remember, OpenGL images are bottom to top. Have to reverse. */
  372. rowPtr = image + (gHeight-1-i) * gWidth*3;
  373. fwrite(rowPtr, 1, gWidth*3, f);
  374. }
  375. fclose(f);
  376. free(image);
  377. printf("Wrote %d by %d image file: %s\n", gWidth, gHeight, filename);
  378. }
  379. /*
  380. * Print message describing command line parameters.
  381. */
  382. static void
  383. Usage(const char *appName)
  384. {
  385. printf("Usage:\n");
  386. printf(" %s width height imgfile\n", appName);
  387. printf("Where imgfile is a ppm file\n");
  388. }
  389. int
  390. main(int argc, char *argv[])
  391. {
  392. if (argc!=4) {
  393. Usage(argv[0]);
  394. }
  395. else {
  396. int width = atoi(argv[1]);
  397. int height = atoi(argv[2]);
  398. char *fileName = argv[3];
  399. if (width<=0) {
  400. printf("Error: width parameter must be at least 1.\n");
  401. return 1;
  402. }
  403. if (height<=0) {
  404. printf("Error: height parameter must be at least 1.\n");
  405. return 1;
  406. }
  407. if (!Setup(width, height)) {
  408. return 1;
  409. }
  410. InitGL();
  411. Render();
  412. WriteFile(fileName);
  413. DestroyPbuffer(gDpy, gScreen, gPBuffer);
  414. }
  415. return 0;
  416. }