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.

wincopy.c 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* $Id: wincopy.c,v 1.1 1999/11/25 17:41:51 brianp Exp $ */
  2. /*
  3. * Mesa 3-D graphics library
  4. * Version: 3.3
  5. *
  6. * Copyright (C) 1999 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. /*
  26. * This program opens two GLX windows, renders into one and uses
  27. * glCopyPixels to copy the image from the first window into the
  28. * second by means of the GLX 1.3 function glxMakeContextCurrent().
  29. * This function works just like the glXMakeCurrentReadSGI() function
  30. * in the GLX_SGI_make_current_read extension.
  31. */
  32. #include <GL/gl.h>
  33. #include <GL/glx.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <unistd.h>
  37. #ifdef GLX_VERSION_1_3
  38. static Display *Dpy;
  39. static int ScrNum;
  40. static GLXContext Context;
  41. static Window Win[2]; /* Win[0] = source, Win[1] = dest */
  42. static GLint Width[2], Height[2];
  43. static GLfloat Angle = 0.0;
  44. static Window
  45. CreateWindow(Display *dpy, int scrnum, XVisualInfo *visinfo,
  46. int xpos, int ypos, int width, int height,
  47. const char *name)
  48. {
  49. Window win;
  50. XSetWindowAttributes attr;
  51. unsigned long mask;
  52. Window root;
  53. root = RootWindow(dpy, scrnum);
  54. /* window attributes */
  55. attr.background_pixel = 0;
  56. attr.border_pixel = 0;
  57. attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
  58. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  59. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  60. win = XCreateWindow(dpy, root, xpos, ypos, width, height,
  61. 0, visinfo->depth, InputOutput,
  62. visinfo->visual, mask, &attr);
  63. if (win) {
  64. XSizeHints sizehints;
  65. sizehints.x = xpos;
  66. sizehints.y = ypos;
  67. sizehints.width = width;
  68. sizehints.height = height;
  69. sizehints.flags = USSize | USPosition;
  70. XSetNormalHints(dpy, win, &sizehints);
  71. XSetStandardProperties(dpy, win, name, name,
  72. None, (char **)NULL, 0, &sizehints);
  73. XMapWindow(dpy, win);
  74. }
  75. return win;
  76. }
  77. static void
  78. Redraw(void)
  79. {
  80. /* make the first window the current one */
  81. if (!glXMakeContextCurrent(Dpy, Win[0], Win[0], Context)) {
  82. printf("glXMakeContextCurrent failed in Redraw()\n");
  83. return;
  84. }
  85. Angle += 1.0;
  86. glViewport(0, 0, Width[0], Height[0]);
  87. glMatrixMode(GL_PROJECTION);
  88. glLoadIdentity();
  89. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  90. glMatrixMode(GL_MODELVIEW);
  91. glShadeModel(GL_FLAT);
  92. glClearColor(0.5, 0.5, 0.5, 1.0);
  93. glClear(GL_COLOR_BUFFER_BIT);
  94. /* draw blue quad */
  95. glColor3f(0.3, 0.3, 1.0);
  96. glPushMatrix();
  97. glRotatef(Angle, 0, 0, 1);
  98. glBegin(GL_POLYGON);
  99. glVertex2f(-0.5, -0.25);
  100. glVertex2f( 0.5, -0.25);
  101. glVertex2f( 0.5, 0.25);
  102. glVertex2f(-0.5, 0.25);
  103. glEnd();
  104. glPopMatrix();
  105. glXSwapBuffers(Dpy, Win[0]);
  106. /* copy image from window 0 to window 1 */
  107. if (!glXMakeContextCurrent(Dpy, Win[1], Win[0], Context)) {
  108. printf("glXMakeContextCurrent failed in Redraw()\n");
  109. return;
  110. }
  111. /* raster pos setup */
  112. glViewport(0, 0, Width[1], Height[1]);
  113. glPushMatrix();
  114. glLoadIdentity();
  115. glMatrixMode(GL_PROJECTION);
  116. glPushMatrix();
  117. glLoadIdentity();
  118. glOrtho(-1, 1, -1, 1, -1, 1);
  119. glRasterPos2f(-1, -1);
  120. /* copy the image between windows */
  121. glDrawBuffer(GL_FRONT);
  122. glCopyPixels(0, 0, Width[0], Height[0], GL_COLOR);
  123. glDrawBuffer(GL_BACK);
  124. glPopMatrix();
  125. glMatrixMode(GL_MODELVIEW);
  126. glPopMatrix();
  127. }
  128. static void
  129. Resize(Window win, unsigned int width, unsigned int height)
  130. {
  131. int i;
  132. if (win == Win[0]) {
  133. i = 0;
  134. }
  135. else {
  136. i = 1;
  137. }
  138. Width[i] = width;
  139. Height[i] = height;
  140. if (!glXMakeCurrent(Dpy, Win[i], Context)) {
  141. printf("glXMakeCurrent failed in Resize()\n");
  142. return;
  143. }
  144. }
  145. static void
  146. EventLoop(void)
  147. {
  148. XEvent event;
  149. while (1) {
  150. if (XPending(Dpy) > 0) {
  151. XNextEvent( Dpy, &event );
  152. switch (event.type) {
  153. case Expose:
  154. Redraw();
  155. break;
  156. case ConfigureNotify:
  157. Resize(event.xany.window, event.xconfigure.width, event.xconfigure.height);
  158. break;
  159. case KeyPress:
  160. return;
  161. default:
  162. /*no-op*/ ;
  163. }
  164. }
  165. else {
  166. /* animate */
  167. Redraw();
  168. }
  169. }
  170. }
  171. static void
  172. Init(void)
  173. {
  174. XVisualInfo *visinfo;
  175. int attrib[] = { GLX_RGBA,
  176. GLX_RED_SIZE, 1,
  177. GLX_GREEN_SIZE, 1,
  178. GLX_BLUE_SIZE, 1,
  179. GLX_DOUBLEBUFFER,
  180. None };
  181. Dpy = XOpenDisplay(NULL);
  182. if (!Dpy) {
  183. printf("Couldn't open default display!\n");
  184. exit(1);
  185. }
  186. ScrNum = DefaultScreen(Dpy);
  187. visinfo = glXChooseVisual(Dpy, ScrNum, attrib);
  188. if (!visinfo) {
  189. printf("Unable to find RGB, double-buffered visual\n");
  190. exit(1);
  191. }
  192. Context = glXCreateContext(Dpy, visinfo, NULL, True);
  193. if (!Context) {
  194. printf("Couldn't create GLX context\n");
  195. exit(1);
  196. }
  197. Win[0] = CreateWindow(Dpy, ScrNum, visinfo,
  198. 0, 0, 300, 300, "source window");
  199. Win[1] = CreateWindow(Dpy, ScrNum, visinfo,
  200. 350, 0, 300, 300, "dest window");
  201. }
  202. int
  203. main(int argc, char *argv[])
  204. {
  205. Init();
  206. EventLoop();
  207. return 0;
  208. }
  209. #else
  210. int
  211. main(int argc, char *argv[])
  212. {
  213. printf("This program requires GLX 1.3!\n");
  214. return 0;
  215. }
  216. #endif /* GLX_VERSION_1_3 */