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.

overlay.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * GLX overlay test/demo.
  3. *
  4. * Brian Paul
  5. * 18 July 2005
  6. */
  7. #include <GL/gl.h>
  8. #include <GL/glx.h>
  9. #include <X11/keysym.h>
  10. #include <assert.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. static int WinWidth = 300, WinHeight = 300;
  14. static Window NormalWindow = 0;
  15. static Window OverlayWindow = 0;
  16. static GLXContext NormalContext = 0;
  17. static GLXContext OverlayContext = 0;
  18. static GLboolean RGBOverlay = GL_FALSE;
  19. static GLfloat Angle = 0.0;
  20. static void
  21. RedrawNormal(Display *dpy)
  22. {
  23. glXMakeCurrent(dpy, NormalWindow, NormalContext);
  24. glViewport(0, 0, WinWidth, WinHeight);
  25. glMatrixMode(GL_PROJECTION);
  26. glLoadIdentity();
  27. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  28. glMatrixMode(GL_MODELVIEW);
  29. glClearColor(0.5, 0.5, 0.5, 1.0);
  30. glClear(GL_COLOR_BUFFER_BIT);
  31. glColor3f(1.0, 1.0, 0.0);
  32. glPushMatrix();
  33. glRotatef(Angle, 0, 0, 1);
  34. glRectf(-0.8, -0.8, 0.8, 0.8);
  35. glPopMatrix();
  36. glXSwapBuffers(dpy, NormalWindow);
  37. }
  38. static void
  39. RedrawOverlay(Display *dpy)
  40. {
  41. glXMakeCurrent(dpy, OverlayWindow, OverlayContext);
  42. glViewport(0, 0, WinWidth, WinHeight);
  43. glMatrixMode(GL_PROJECTION);
  44. glLoadIdentity();
  45. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  46. glMatrixMode(GL_MODELVIEW);
  47. glClear(GL_COLOR_BUFFER_BIT);
  48. if (RGBOverlay) {
  49. glColor3f(0.0, 1.0, 1.0);
  50. }
  51. else {
  52. glIndexi(2);
  53. }
  54. glBegin(GL_LINES);
  55. glVertex2f(-1, -1);
  56. glVertex2f(1, 1);
  57. glVertex2f(1, -1);
  58. glVertex2f(-1, 1);
  59. glEnd();
  60. glXSwapBuffers(dpy, OverlayWindow);
  61. }
  62. static Window
  63. MakeWindow(Display *dpy, XVisualInfo *visinfo, Window parent,
  64. unsigned int width, unsigned int height)
  65. {
  66. int scrnum;
  67. XSetWindowAttributes attr;
  68. unsigned long mask;
  69. Window root;
  70. Window win;
  71. scrnum = DefaultScreen(dpy);
  72. root = RootWindow(dpy, scrnum);
  73. /* window attributes */
  74. attr.background_pixel = 0;
  75. attr.border_pixel = 0;
  76. attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
  77. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  78. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  79. win = XCreateWindow(dpy, parent, 0, 0, width, height,
  80. 0, visinfo->depth, InputOutput,
  81. visinfo->visual, mask, &attr);
  82. return win;
  83. }
  84. static void
  85. MakeNormalWindow(Display *dpy)
  86. {
  87. int attrib[] = { GLX_RGBA,
  88. GLX_RED_SIZE, 1,
  89. GLX_GREEN_SIZE, 1,
  90. GLX_BLUE_SIZE, 1,
  91. GLX_DOUBLEBUFFER,
  92. None };
  93. int scrnum;
  94. Window root;
  95. XVisualInfo *visinfo;
  96. scrnum = DefaultScreen(dpy);
  97. root = RootWindow(dpy, scrnum);
  98. visinfo = glXChooseVisual(dpy, scrnum, attrib);
  99. if (!visinfo) {
  100. printf("Error: couldn't get an RGB, Double-buffered visual\n");
  101. exit(1);
  102. }
  103. NormalWindow = MakeWindow(dpy, visinfo, root, WinWidth, WinHeight);
  104. assert(NormalWindow);
  105. NormalContext = glXCreateContext(dpy, visinfo, NULL, True);
  106. assert(NormalContext);
  107. }
  108. static void
  109. MakeOverlayWindow(Display *dpy)
  110. {
  111. int rgbAttribs[] = {
  112. GLX_RGBA,
  113. GLX_RED_SIZE, 1,
  114. GLX_GREEN_SIZE, 1,
  115. GLX_BLUE_SIZE, 1,
  116. GLX_DOUBLEBUFFER,
  117. GLX_LEVEL, 1,
  118. None
  119. };
  120. int indexAttribs[] = {
  121. /*GLX_RGBA, leave this out */
  122. GLX_RED_SIZE, 1,
  123. GLX_GREEN_SIZE, 1,
  124. GLX_BLUE_SIZE, 1,
  125. GLX_DOUBLEBUFFER,
  126. GLX_LEVEL, 1,
  127. None
  128. };
  129. int scrnum;
  130. Window root;
  131. XVisualInfo *visinfo;
  132. scrnum = DefaultScreen(dpy);
  133. root = RootWindow(dpy, scrnum);
  134. visinfo = glXChooseVisual(dpy, scrnum, rgbAttribs);
  135. if (visinfo) {
  136. printf("Found RGB overlay visual 0x%x\n", (int) visinfo->visualid);
  137. RGBOverlay = GL_TRUE;
  138. }
  139. else {
  140. visinfo = glXChooseVisual(dpy, scrnum, indexAttribs);
  141. if (visinfo) {
  142. printf("Found Color Index overlay visual 0x%x\n",
  143. (int) visinfo->visualid);
  144. /* XXX setup the colormap entries! */
  145. }
  146. else {
  147. printf("Error: couldn't get an overlay visual!\n");
  148. exit(1);
  149. }
  150. }
  151. OverlayWindow = MakeWindow(dpy, visinfo, NormalWindow, WinWidth, WinHeight);
  152. assert(OverlayWindow);
  153. OverlayContext = glXCreateContext(dpy, visinfo, NULL, True);
  154. assert(OverlayContext);
  155. }
  156. static void
  157. EventLoop(Display *dpy)
  158. {
  159. XEvent event;
  160. while (1) {
  161. XNextEvent(dpy, &event);
  162. switch (event.type) {
  163. case Expose:
  164. RedrawNormal(dpy);
  165. RedrawOverlay(dpy);
  166. break;
  167. case ConfigureNotify:
  168. WinWidth = event.xconfigure.width;
  169. WinHeight = event.xconfigure.height;
  170. if (event.xconfigure.window == NormalWindow)
  171. XResizeWindow(dpy, OverlayWindow, WinWidth, WinHeight);
  172. break;
  173. case KeyPress:
  174. {
  175. char buffer[10];
  176. int r, code;
  177. code = XLookupKeysym(&event.xkey, 0);
  178. r = XLookupString(&event.xkey, buffer, sizeof(buffer),
  179. NULL, NULL);
  180. if (buffer[0] == 27) {
  181. /* escape */
  182. return;
  183. }
  184. else if (buffer[0] == ' ') {
  185. Angle += 5.0;
  186. RedrawNormal(dpy);
  187. }
  188. }
  189. break;
  190. default:
  191. ; /* nothing */
  192. }
  193. }
  194. }
  195. int
  196. main(int argc, char *argv[])
  197. {
  198. Display *dpy = XOpenDisplay(NULL);
  199. assert(dpy);
  200. MakeNormalWindow(dpy);
  201. MakeOverlayWindow(dpy);
  202. XMapWindow(dpy, NormalWindow);
  203. XMapWindow(dpy, OverlayWindow);
  204. EventLoop(dpy);
  205. glXDestroyContext(dpy, OverlayContext);
  206. glXDestroyContext(dpy, NormalContext);
  207. XDestroyWindow(dpy, OverlayWindow);
  208. XDestroyWindow(dpy, NormalWindow);
  209. return 0;
  210. }