Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* $Id: manywin.c,v 1.1 2000/06/13 19:41:30 brianp Exp $ */
  2. /*
  3. * Create N GLX windows/contexts and render to them in round-robin
  4. * order.
  5. *
  6. * Copyright (C) 2000 Brian Paul All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  22. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <GL/gl.h>
  26. #include <GL/glx.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. /*
  31. * Each display/window/context:
  32. */
  33. struct head {
  34. char DisplayName[1000];
  35. Display *Dpy;
  36. Window Win;
  37. GLXContext Context;
  38. float Angle;
  39. char Renderer[1000];
  40. char Vendor[1000];
  41. char Version[1000];
  42. };
  43. #define MAX_HEADS 200
  44. static struct head Heads[MAX_HEADS];
  45. static int NumHeads = 0;
  46. static void
  47. Error(const char *display, const char *msg)
  48. {
  49. fprintf(stderr, "Error on display %s - %s\n", display, msg);
  50. exit(1);
  51. }
  52. static struct head *
  53. AddHead(const char *displayName, const char *name)
  54. {
  55. Display *dpy;
  56. Window win;
  57. GLXContext ctx;
  58. int attrib[] = { GLX_RGBA,
  59. GLX_RED_SIZE, 1,
  60. GLX_GREEN_SIZE, 1,
  61. GLX_BLUE_SIZE, 1,
  62. GLX_DOUBLEBUFFER,
  63. None };
  64. int scrnum;
  65. XSetWindowAttributes attr;
  66. unsigned long mask;
  67. Window root;
  68. XVisualInfo *visinfo;
  69. int width = 90, height = 90;
  70. int xpos = 0, ypos = 0;
  71. if (NumHeads >= MAX_HEADS)
  72. return NULL;
  73. dpy = XOpenDisplay(displayName);
  74. if (!dpy) {
  75. Error(displayName, "Unable to open display");
  76. return NULL;
  77. }
  78. scrnum = DefaultScreen(dpy);
  79. root = RootWindow(dpy, scrnum);
  80. visinfo = glXChooseVisual(dpy, scrnum, attrib);
  81. if (!visinfo) {
  82. Error(displayName, "Unable to find RGB, double-buffered visual");
  83. return NULL;
  84. }
  85. /* window attributes */
  86. xpos = (NumHeads % 10) * 100;
  87. ypos = (NumHeads / 10) * 100;
  88. printf("%d, %d\n", xpos, ypos);
  89. attr.background_pixel = 0;
  90. attr.border_pixel = 0;
  91. attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
  92. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  93. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  94. win = XCreateWindow(dpy, root, xpos, ypos, width, height,
  95. 0, visinfo->depth, InputOutput,
  96. visinfo->visual, mask, &attr);
  97. if (!win) {
  98. Error(displayName, "Couldn't create window");
  99. return NULL;
  100. }
  101. {
  102. XSizeHints sizehints;
  103. sizehints.x = xpos;
  104. sizehints.y = ypos;
  105. sizehints.width = width;
  106. sizehints.height = height;
  107. sizehints.flags = USSize | USPosition;
  108. XSetNormalHints(dpy, win, &sizehints);
  109. XSetStandardProperties(dpy, win, name, name,
  110. None, (char **)NULL, 0, &sizehints);
  111. }
  112. ctx = glXCreateContext(dpy, visinfo, NULL, True);
  113. if (!ctx) {
  114. Error(displayName, "Couldn't create GLX context");
  115. return NULL;
  116. }
  117. XMapWindow(dpy, win);
  118. if (!glXMakeCurrent(dpy, win, ctx)) {
  119. Error(displayName, "glXMakeCurrent failed");
  120. printf("glXMakeCurrent failed in Redraw()\n");
  121. return;
  122. }
  123. /* save the info for this head */
  124. {
  125. struct head *h = &Heads[NumHeads];
  126. strcpy(h->DisplayName, name);
  127. h->Dpy = dpy;
  128. h->Win = win;
  129. h->Context = ctx;
  130. h->Angle = 0.0;
  131. strcpy(h->Version, (char *) glGetString(GL_VERSION));
  132. strcpy(h->Vendor, (char *) glGetString(GL_VENDOR));
  133. strcpy(h->Renderer, (char *) glGetString(GL_RENDERER));
  134. NumHeads++;
  135. return &Heads[NumHeads-1];
  136. }
  137. }
  138. static void
  139. Redraw(struct head *h)
  140. {
  141. if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
  142. Error(h->DisplayName, "glXMakeCurrent failed");
  143. printf("glXMakeCurrent failed in Redraw()\n");
  144. return;
  145. }
  146. h->Angle += 1.0;
  147. glShadeModel(GL_FLAT);
  148. glClearColor(0.5, 0.5, 0.5, 1.0);
  149. glClear(GL_COLOR_BUFFER_BIT);
  150. /* draw green triangle */
  151. glColor3f(0.0, 1.0, 0.0);
  152. glPushMatrix();
  153. glRotatef(h->Angle, 0, 0, 1);
  154. glBegin(GL_TRIANGLES);
  155. glVertex2f(0, 0.8);
  156. glVertex2f(-0.8, -0.7);
  157. glVertex2f(0.8, -0.7);
  158. glEnd();
  159. glPopMatrix();
  160. glXSwapBuffers(h->Dpy, h->Win);
  161. }
  162. static void
  163. Resize(const struct head *h, unsigned int width, unsigned int height)
  164. {
  165. if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
  166. Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
  167. return;
  168. }
  169. glFlush();
  170. glViewport(0, 0, width, height);
  171. glMatrixMode(GL_PROJECTION);
  172. glLoadIdentity();
  173. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  174. }
  175. static void
  176. EventLoop(void)
  177. {
  178. while (1) {
  179. int i;
  180. for (i = 0; i < NumHeads; i++) {
  181. struct head *h = &Heads[i];
  182. while (XPending(h->Dpy) > 0) {
  183. XEvent event;
  184. XNextEvent(h->Dpy, &event);
  185. if (event.xany.window == h->Win) {
  186. switch (event.type) {
  187. case Expose:
  188. Redraw(h);
  189. break;
  190. case ConfigureNotify:
  191. Resize(h, event.xconfigure.width, event.xconfigure.height);
  192. break;
  193. case KeyPress:
  194. return;
  195. default:
  196. /*no-op*/ ;
  197. }
  198. }
  199. else {
  200. printf("window mismatch\n");
  201. }
  202. }
  203. Redraw(h);
  204. }
  205. usleep(1);
  206. }
  207. }
  208. static void
  209. PrintInfo(const struct head *h)
  210. {
  211. printf("Name: %s\n", h->DisplayName);
  212. printf(" Display: 0x%x\n", h->Dpy);
  213. printf(" Window: 0x%x\n", h->Win);
  214. printf(" Context: 0x%x\n", h->Context);
  215. printf(" GL_VERSION: %s\n", h->Version);
  216. printf(" GL_VENDOR: %s\n", h->Vendor);
  217. printf(" GL_RENDERER: %s\n", h->Renderer);
  218. }
  219. int
  220. main(int argc, char *argv[])
  221. {
  222. int i;
  223. if (argc == 1) {
  224. struct head *h;
  225. printf("manywin: open N simultaneous glx windows\n");
  226. printf("Usage:\n");
  227. printf(" manywin numWindows\n");
  228. printf("Example:\n");
  229. printf(" manywin 10\n");
  230. return 0;
  231. }
  232. else {
  233. int n = atoi(argv[1]);
  234. printf("%d windows\n", n);
  235. for (i = 0; i < n; i++) {
  236. char name[100];
  237. struct head *h;
  238. sprintf(name, "%d", i);
  239. h = AddHead(":0", name);
  240. if (h) {
  241. PrintInfo(h);
  242. }
  243. }
  244. }
  245. EventLoop();
  246. return 0;
  247. }