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

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