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.

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