Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

miniglxtest.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Test the mini GLX interface.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <GL/gl.h>
  9. #define USE_MINI_GLX 1
  10. #if USE_MINI_GLX
  11. #include <GL/miniglx.h>
  12. #else
  13. #include <GL/glx.h>
  14. #endif
  15. #include "../util/writebuf.c"
  16. static GLXContext ctx;
  17. static GLuint NumFrames = 100;
  18. static GLuint NumDisplays = 1;
  19. static GLboolean Texture = GL_FALSE;
  20. static GLboolean SingleBuffer = GL_FALSE;
  21. static GLboolean Sleeps = GL_TRUE;
  22. static void
  23. rect(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
  24. {
  25. glBegin(GL_QUADS);
  26. glTexCoord2f(0, 0); glColor3f(0, 0, 1); glVertex2f(x1, y1);
  27. glTexCoord2f(1, 0); glColor3f(1, 0, 0); glVertex2f(x2, y1);
  28. glTexCoord2f(1, 1); glColor3f(0, 1, 0); glVertex2f(x2, y2);
  29. glTexCoord2f(0, 1); glColor3f(0, 0, 0); glVertex2f(x1, y2);
  30. glEnd();
  31. }
  32. static void
  33. redraw(Display *dpy, Window w, int rot)
  34. {
  35. GLfloat a;
  36. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  37. glPushMatrix();
  38. glRotatef(rot, 0, 0, 1);
  39. glScalef(.5, .5, .5);
  40. for (a = 0.0; a < 360.0; a += 30.0) {
  41. glPushMatrix();
  42. glRotatef(a, 0, 0, 1);
  43. glRotatef(40, 1, 0, 0);
  44. glColor3f(a / 360.0, 1-a/360.0, 0);
  45. rect(0.3, -0.25, 1.5, 0.25);
  46. glPopMatrix();
  47. }
  48. glPopMatrix();
  49. if (SingleBuffer)
  50. glFlush();
  51. else
  52. glXSwapBuffers(dpy, w);
  53. }
  54. static Window
  55. make_window(Display *dpy, unsigned int width, unsigned int height)
  56. {
  57. int attrib_single[] = { GLX_RGBA,
  58. GLX_RED_SIZE, 1,
  59. GLX_GREEN_SIZE, 1,
  60. GLX_BLUE_SIZE, 1,
  61. GLX_DEPTH_SIZE, 1,
  62. None };
  63. int attrib_double[] = { GLX_RGBA,
  64. GLX_RED_SIZE, 1,
  65. GLX_GREEN_SIZE, 1,
  66. GLX_BLUE_SIZE, 1,
  67. GLX_DEPTH_SIZE, 1,
  68. GLX_DOUBLEBUFFER,
  69. None };
  70. int *attrib = SingleBuffer ? attrib_single : attrib_double;
  71. int scrnum = 0;
  72. XSetWindowAttributes attr;
  73. unsigned long mask;
  74. Window root;
  75. Window win;
  76. XVisualInfo *visinfo;
  77. root = RootWindow(dpy, scrnum);
  78. if (!(visinfo = glXChooseVisual(dpy, scrnum, attrib))) {
  79. printf("Error: couldn't get an RGB, Double-buffered visual\n");
  80. exit(1);
  81. }
  82. if (!(ctx = glXCreateContext(dpy, visinfo, NULL, True))) {
  83. printf("Error: glXCreateContext failed\n");
  84. exit(1);
  85. }
  86. /* window attributes */
  87. attr.background_pixel = 0;
  88. attr.border_pixel = 0;
  89. attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
  90. attr.event_mask = StructureNotifyMask | ExposureMask;
  91. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  92. win = XCreateWindow(dpy, root, 0, 0, width, height,
  93. 0, visinfo->depth, InputOutput,
  94. visinfo->visual, mask, &attr);
  95. if (!win) {
  96. printf("Error: XCreateWindow failed\n");
  97. exit(1);
  98. }
  99. glXMakeCurrent(dpy, win, ctx);
  100. glViewport(0, 0, width, height);
  101. return win;
  102. }
  103. static void
  104. event_loop(Display *dpy, Window win)
  105. {
  106. int i;
  107. printf("Drawing %d frames\n", NumFrames);
  108. for (i = 0; i < NumFrames; i++) {
  109. redraw(dpy, win, -i*2);
  110. if (i == 1) {
  111. glReadBuffer(GL_BACK);
  112. WritePPMFile("frame1.ppm", 800,600);
  113. }
  114. if (Sleeps) {
  115. usleep(20000);
  116. }
  117. }
  118. }
  119. static int
  120. runtest(void)
  121. {
  122. Display *dpy;
  123. Window win;
  124. dpy = XOpenDisplay(NULL);
  125. if (!dpy) {
  126. printf("Error: XOpenDisplay failed\n");
  127. return 1;
  128. }
  129. win = make_window(dpy, 800, 600);
  130. srand(getpid());
  131. /* init GL state */
  132. glClearColor(0.5, 0.5, 0.5, 1.0);
  133. glEnable(GL_DEPTH_TEST);
  134. if (Texture) {
  135. GLubyte image[16][16][4];
  136. GLint i, j;
  137. for (i = 0; i < 16; i++) {
  138. for (j = 0; j < 16; j++) {
  139. if (((i / 2) ^ (j / 2)) & 1) {
  140. image[i][j][0] = 255;
  141. image[i][j][1] = 255;
  142. image[i][j][2] = 255;
  143. image[i][j][3] = 255;
  144. }
  145. else {
  146. image[i][j][0] = 128;
  147. image[i][j][1] = 128;
  148. image[i][j][2] = 128;
  149. image[i][j][3] = 128;
  150. }
  151. }
  152. }
  153. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0,
  154. GL_RGBA, GL_UNSIGNED_BYTE, image);
  155. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  156. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  157. glEnable(GL_TEXTURE_2D);
  158. }
  159. if (SingleBuffer) {
  160. glDrawBuffer(GL_FRONT);
  161. glReadBuffer(GL_FRONT);
  162. }
  163. else {
  164. glDrawBuffer(GL_BACK);
  165. }
  166. XMapWindow(dpy, win);
  167. /* wait for window to get mapped */
  168. {
  169. XEvent e;
  170. while (1) {
  171. XNextEvent(dpy, &e);
  172. if (e.type == MapNotify && e.xmap.window == win) {
  173. break;
  174. }
  175. }
  176. }
  177. event_loop(dpy, win);
  178. glXDestroyContext(dpy, ctx);
  179. XDestroyWindow(dpy, win);
  180. XCloseDisplay(dpy);
  181. return 0;
  182. }
  183. static void
  184. usage(void)
  185. {
  186. printf("Usage:\n");
  187. printf(" -f N render N frames (default %d)\n", NumFrames);
  188. printf(" -d N do N display cycles\n");
  189. printf(" -t texturing\n");
  190. printf(" -s single buffering\n");
  191. printf(" -n no usleep() delay\n");
  192. }
  193. static void
  194. parse_args(int argc, char *argv[])
  195. {
  196. int i;
  197. for (i = 1; i < argc; i++) {
  198. if (strcmp(argv[i], "-f") == 0) {
  199. NumFrames = atoi(argv[i + 1]);
  200. i++;
  201. }
  202. else if (strcmp(argv[i], "-d") == 0) {
  203. NumDisplays = atoi(argv[i + 1]);
  204. i++;
  205. }
  206. else if (strcmp(argv[i], "-n") == 0) {
  207. Sleeps = GL_FALSE;
  208. }
  209. else if (strcmp(argv[i], "-s") == 0) {
  210. SingleBuffer = GL_TRUE;
  211. }
  212. else if (strcmp(argv[i], "-t") == 0) {
  213. Texture = GL_TRUE;
  214. }
  215. else {
  216. usage();
  217. exit(1);
  218. }
  219. }
  220. }
  221. int
  222. main(int argc, char *argv[])
  223. {
  224. int i;
  225. parse_args(argc, argv);
  226. for (i = 0; i < NumDisplays; i++) {
  227. if (runtest() != 0)
  228. break;
  229. }
  230. return 0;
  231. }