Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

glxpbdemo.c 9.4KB

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