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.

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