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.

miniglxtest.c 5.9KB

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