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.

shape.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Example of using the X "shape" extension with OpenGL: render a spinning
  3. * cube inside of a non-rectangular window.
  4. *
  5. * Press ESC to exit. Press up/down to change window shape.
  6. *
  7. * To compile add "shape" to the PROGS list in Makefile.
  8. *
  9. * Brian Paul
  10. * June 16, 1997
  11. *
  12. * This program is in the public domain.
  13. */
  14. #include <math.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <sys/time.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #include <X11/Xlib.h>
  21. #include <X11/Xutil.h>
  22. #include <X11/keysym.h>
  23. #include <X11/extensions/shape.h>
  24. #include <GL/glx.h>
  25. #ifndef PI
  26. #define PI 3.1415926
  27. #endif
  28. static int Width=500, Height=500;
  29. static float Xangle = 0.0, Yangle = 0.0;
  30. static int Sides = 5;
  31. static int MinSides = 3;
  32. static int MaxSides = 20;
  33. /* return current time (in seconds) */
  34. static double
  35. current_time(void)
  36. {
  37. struct timeval tv;
  38. #ifdef __VMS
  39. (void) gettimeofday(&tv, NULL );
  40. #else
  41. struct timezone tz;
  42. (void) gettimeofday(&tv, &tz);
  43. #endif
  44. return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
  45. }
  46. /*
  47. * Draw the OpenGL stuff and do a SwapBuffers.
  48. */
  49. static void display(Display *dpy, Window win)
  50. {
  51. float scale = 1.7;
  52. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  53. glPushMatrix();
  54. glScalef(scale, scale, scale);
  55. glRotatef(Xangle, 1.0, 0.0, 0.0);
  56. glRotatef(Yangle, 0.0, 1.0, 0.0);
  57. /*
  58. * wireframe box
  59. */
  60. glColor3f(1.0, 1.0, 1.0);
  61. glBegin(GL_LINE_LOOP);
  62. glVertex3f(-1.0, -1.0, -1.0);
  63. glVertex3f( 1.0, -1.0, -1.0);
  64. glVertex3f( 1.0, 1.0, -1.0);
  65. glVertex3f(-1.0, 1.0, -1.0);
  66. glEnd();
  67. glBegin(GL_LINE_LOOP);
  68. glVertex3f(-1.0, -1.0, 1.0);
  69. glVertex3f( 1.0, -1.0, 1.0);
  70. glVertex3f( 1.0, 1.0, 1.0);
  71. glVertex3f(-1.0, 1.0, 1.0);
  72. glEnd();
  73. glBegin(GL_LINES);
  74. glVertex3f(-1.0, -1.0, -1.0); glVertex3f(-1.0, -1.0, 1.0);
  75. glVertex3f( 1.0, -1.0, -1.0); glVertex3f( 1.0, -1.0, 1.0);
  76. glVertex3f( 1.0, 1.0, -1.0); glVertex3f( 1.0, 1.0, 1.0);
  77. glVertex3f(-1.0, 1.0, -1.0); glVertex3f(-1.0, 1.0, 1.0);
  78. glEnd();
  79. /*
  80. * Solid box
  81. */
  82. glPushMatrix();
  83. glScalef(0.75, 0.75, 0.75);
  84. glColor3f(1, 0, 0);
  85. glBegin(GL_POLYGON);
  86. glVertex3f(1, -1, -1);
  87. glVertex3f(1, 1, -1);
  88. glVertex3f(1, 1, 1);
  89. glVertex3f(1, -1, 1);
  90. glEnd();
  91. glColor3f(0, 1, 1);
  92. glBegin(GL_POLYGON);
  93. glVertex3f(-1, -1, -1);
  94. glVertex3f(-1, 1, -1);
  95. glVertex3f(-1, 1, 1);
  96. glVertex3f(-1, -1, 1);
  97. glEnd();
  98. glColor3f(0, 1, 0);
  99. glBegin(GL_POLYGON);
  100. glVertex3f(-1, 1, -1);
  101. glVertex3f( 1, 1, -1);
  102. glVertex3f( 1, 1, 1);
  103. glVertex3f(-1, 1, 1);
  104. glEnd();
  105. glColor3f(1, 0, 1);
  106. glBegin(GL_POLYGON);
  107. glVertex3f(-1, -1, -1);
  108. glVertex3f( 1, -1, -1);
  109. glVertex3f( 1, -1, 1);
  110. glVertex3f(-1, -1, 1);
  111. glEnd();
  112. glColor3f(0, 0, 1);
  113. glBegin(GL_POLYGON);
  114. glVertex3f(-1, -1, 1);
  115. glVertex3f( 1, -1, 1);
  116. glVertex3f( 1, 1, 1);
  117. glVertex3f(-1, 1, 1);
  118. glEnd();
  119. glColor3f(1, 1, 0);
  120. glBegin(GL_POLYGON);
  121. glVertex3f(-1, -1, -1);
  122. glVertex3f( 1, -1, -1);
  123. glVertex3f( 1, 1, -1);
  124. glVertex3f(-1, 1, -1);
  125. glEnd();
  126. glPopMatrix();
  127. glPopMatrix();
  128. glXSwapBuffers(dpy, win);
  129. }
  130. /*
  131. * This is called when we have to recompute the window shape bitmask.
  132. * We just generate an n-sided regular polygon here but any other shape
  133. * would be possible.
  134. */
  135. static void make_shape_mask(Display *dpy, Window win, int width, int height,
  136. int sides)
  137. {
  138. Pixmap shapeMask;
  139. XGCValues xgcv;
  140. GC gc;
  141. /* allocate 1-bit deep pixmap and a GC */
  142. shapeMask = XCreatePixmap(dpy, win, width, height, 1);
  143. gc = XCreateGC(dpy, shapeMask, 0, &xgcv);
  144. /* clear shapeMask to zeros */
  145. XSetForeground(dpy, gc, 0);
  146. XFillRectangle(dpy, shapeMask, gc, 0, 0, width, height);
  147. /* draw mask */
  148. XSetForeground(dpy, gc, 1);
  149. {
  150. int cx = width / 2;
  151. int cy = height / 2;
  152. float angle = 0.0;
  153. float step = 2.0 * PI / sides;
  154. float radius = width / 2;
  155. int i;
  156. XPoint points[100];
  157. for (i=0;i<sides;i++) {
  158. int x = cx + radius * sin(angle);
  159. int y = cy - radius * cos(angle);
  160. points[i].x = x;
  161. points[i].y = y;
  162. angle += step;
  163. }
  164. XFillPolygon(dpy, shapeMask, gc, points, sides, Convex, CoordModeOrigin);
  165. }
  166. /* This is the only SHAPE extension call- simple! */
  167. XShapeCombineMask(dpy, win, ShapeBounding, 0, 0, shapeMask, ShapeSet);
  168. XFreeGC(dpy, gc);
  169. XFreePixmap(dpy, shapeMask);
  170. }
  171. /*
  172. * Called when window is resized. Do OpenGL viewport and projection stuff.
  173. */
  174. static void reshape(int width, int height)
  175. {
  176. glViewport(0, 0, width, height);
  177. glMatrixMode(GL_PROJECTION);
  178. glLoadIdentity();
  179. glFrustum(-1.0, 1.0, -1.0, 1.0, 3.0, 20.0);
  180. glMatrixMode(GL_MODELVIEW);
  181. glLoadIdentity();
  182. glTranslatef(0.0, 0.0, -10.0);
  183. glEnable(GL_DEPTH_TEST);
  184. }
  185. /*
  186. * Process X events.
  187. */
  188. static void event_loop(Display *dpy, Window win)
  189. {
  190. while (1) {
  191. XEvent event;
  192. if (XPending(dpy)) {
  193. XNextEvent(dpy, &event);
  194. switch (event.type) {
  195. case Expose:
  196. display(dpy, event.xexpose.window);
  197. break;
  198. case ConfigureNotify:
  199. Width = event.xconfigure.width;
  200. Height = event.xconfigure.height,
  201. make_shape_mask(dpy, win, Width, Height, Sides);
  202. reshape(Width, Height);
  203. break;
  204. case KeyPress:
  205. {
  206. char buf[100];
  207. KeySym keySym;
  208. XComposeStatus stat;
  209. XLookupString(&event.xkey, buf, sizeof(buf), &keySym, &stat);
  210. switch (keySym) {
  211. case XK_Escape:
  212. exit(0);
  213. break;
  214. case XK_Up:
  215. Sides++;
  216. if (Sides>MaxSides) Sides = MaxSides;
  217. make_shape_mask(dpy, win, Width, Height, Sides);
  218. break;
  219. case XK_Down:
  220. Sides--;
  221. if (Sides<MinSides) Sides = MinSides;
  222. make_shape_mask(dpy, win, Width, Height, Sides);
  223. break;
  224. }
  225. }
  226. break;
  227. default:
  228. ;;
  229. }
  230. }
  231. else {
  232. static double t0 = -1.0;
  233. double dt, t = current_time();
  234. if (t0 < 0.0)
  235. t0 = t;
  236. dt = t - t0;
  237. Xangle += 90.0 * dt; /* 90 degrees per second */
  238. Yangle += 70.0 * dt;
  239. t0 = t;
  240. display(dpy, win);
  241. }
  242. }
  243. }
  244. /*
  245. * Allocate a "nice" colormap. This could be better (HP-CR support, etc).
  246. */
  247. static Colormap alloc_colormap(Display *dpy, Window parent, Visual *vis)
  248. {
  249. Screen *scr = DefaultScreenOfDisplay(dpy);
  250. int scrnum = DefaultScreen(dpy);
  251. if (MaxCmapsOfScreen(scr)==1 && vis==DefaultVisual(dpy, scrnum)) {
  252. /* The window and root are of the same visual type so */
  253. /* share the root colormap. */
  254. return DefaultColormap(dpy, scrnum);
  255. }
  256. else {
  257. return XCreateColormap(dpy, parent, vis, AllocNone);
  258. }
  259. }
  260. int main(int argc, char *argv[])
  261. {
  262. static int glAttribs[] = {
  263. GLX_DOUBLEBUFFER,
  264. GLX_RGBA,
  265. GLX_DEPTH_SIZE, 1,
  266. None
  267. };
  268. Display *dpy;
  269. XVisualInfo *visInfo;
  270. int scrn;
  271. Window root;
  272. Colormap cmap;
  273. Window win;
  274. XSetWindowAttributes winAttribs;
  275. unsigned long winAttribsMask;
  276. GLXContext glCtx;
  277. int ignore;
  278. const char *name = "OpenGL in a Shaped Window";
  279. dpy = XOpenDisplay(NULL);
  280. if (!dpy) {
  281. fprintf(stderr, "Couldn't open default display\n");
  282. return 1;
  283. }
  284. /* check that we can use the shape extension */
  285. if (!XQueryExtension(dpy, "SHAPE", &ignore, &ignore, &ignore )) {
  286. fprintf(stderr, "Display doesn't support shape extension\n");
  287. return 1;
  288. }
  289. scrn = DefaultScreen(dpy);
  290. root = RootWindow(dpy, scrn);
  291. visInfo = glXChooseVisual(dpy, scrn, glAttribs);
  292. if (!visInfo) {
  293. fprintf(stderr, "Couldn't get RGB, DB, Z visual\n");
  294. return 1;
  295. }
  296. glCtx = glXCreateContext(dpy, visInfo, 0, True);
  297. if (!glCtx) {
  298. fprintf(stderr, "Couldn't create GL context\n");
  299. return 1;
  300. }
  301. cmap = alloc_colormap(dpy, root, visInfo->visual);
  302. if (!cmap) {
  303. fprintf(stderr, "Couln't create colormap\n");
  304. return 1;
  305. }
  306. winAttribs.border_pixel = 0;
  307. winAttribs.colormap = cmap;
  308. winAttribs.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  309. winAttribsMask = CWBorderPixel | CWColormap | CWEventMask;
  310. win = XCreateWindow(dpy, root, 0, 0, Width, Height, 0,
  311. visInfo->depth, InputOutput,
  312. visInfo->visual,
  313. winAttribsMask, &winAttribs);
  314. {
  315. XSizeHints sizehints;
  316. /*
  317. sizehints.x = xpos;
  318. sizehints.y = ypos;
  319. sizehints.width = width;
  320. sizehints.height = height;
  321. */
  322. sizehints.flags = 0;
  323. XSetNormalHints(dpy, win, &sizehints);
  324. XSetStandardProperties(dpy, win, name, name,
  325. None, (char **)NULL, 0, &sizehints);
  326. }
  327. XMapWindow(dpy, win);
  328. glXMakeCurrent(dpy, win, glCtx);
  329. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  330. printf("Press ESC to exit.\n");
  331. printf("Press up/down to change window shape.\n");
  332. event_loop(dpy, win);
  333. return 0;
  334. }