summaryrefslogtreecommitdiff
path: root/progs/xdemos/shape.c
blob: 5ff09708be6b71091430ac4b1f2c49255ab7b5e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

/*
 * Example of using the X "shape" extension with OpenGL:  render a spinning
 * cube inside of a non-rectangular window.
 *
 * Press ESC to exit.  Press up/down to change window shape.
 *
 * To compile add "shape" to the PROGS list in Makefile.
 *
 * Brian Paul
 * June 16, 1997
 *
 * This program is in the public domain.
 */


#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/extensions/shape.h>
#include <GL/glx.h>

#ifndef PI
#define PI 3.1415926
#endif


static int Width=500, Height=500;

static float Xangle = 0.0, Yangle = 0.0;
static int Sides = 5;
static int MinSides = 3;
static int MaxSides = 20;


/* return current time (in seconds) */
static double
current_time(void)
{
   struct timeval tv;
#ifdef __VMS
   (void) gettimeofday(&tv, NULL );
#else
   struct timezone tz;
   (void) gettimeofday(&tv, &tz);
#endif
   return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
}


/*
 * Draw the OpenGL stuff and do a SwapBuffers.
 */
static void display(Display *dpy, Window win)
{
   float scale = 1.7;

   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   glPushMatrix();

   glScalef(scale, scale, scale);
   glRotatef(Xangle, 1.0, 0.0, 0.0);
   glRotatef(Yangle, 0.0, 1.0, 0.0);

   /*
    * wireframe box
    */
   glColor3f(1.0, 1.0, 1.0);
   glBegin(GL_LINE_LOOP);
   glVertex3f(-1.0, -1.0, -1.0);
   glVertex3f( 1.0, -1.0, -1.0);
   glVertex3f( 1.0,  1.0, -1.0);
   glVertex3f(-1.0,  1.0, -1.0);
   glEnd();

   glBegin(GL_LINE_LOOP);
   glVertex3f(-1.0, -1.0, 1.0);
   glVertex3f( 1.0, -1.0, 1.0);
   glVertex3f( 1.0,  1.0, 1.0);
   glVertex3f(-1.0,  1.0, 1.0);
   glEnd();

   glBegin(GL_LINES);
   glVertex3f(-1.0, -1.0, -1.0);   glVertex3f(-1.0, -1.0, 1.0);
   glVertex3f( 1.0, -1.0, -1.0);   glVertex3f( 1.0, -1.0, 1.0);
   glVertex3f( 1.0,  1.0, -1.0);   glVertex3f( 1.0,  1.0, 1.0);
   glVertex3f(-1.0,  1.0, -1.0);   glVertex3f(-1.0,  1.0, 1.0);
   glEnd();

   /*
    * Solid box
    */
   glPushMatrix();
   glScalef(0.75, 0.75, 0.75);

   glColor3f(1, 0, 0);
   glBegin(GL_POLYGON);
   glVertex3f(1, -1, -1);
   glVertex3f(1,  1, -1);
   glVertex3f(1,  1,  1);
   glVertex3f(1, -1,  1);
   glEnd();

   glColor3f(0, 1, 1);
   glBegin(GL_POLYGON);
   glVertex3f(-1, -1, -1);
   glVertex3f(-1,  1, -1);
   glVertex3f(-1,  1,  1);
   glVertex3f(-1, -1,  1);
   glEnd();

   glColor3f(0, 1, 0);
   glBegin(GL_POLYGON);
   glVertex3f(-1, 1, -1);
   glVertex3f( 1, 1, -1);
   glVertex3f( 1, 1,  1);
   glVertex3f(-1, 1,  1);
   glEnd();

   glColor3f(1, 0, 1);
   glBegin(GL_POLYGON);
   glVertex3f(-1, -1, -1);
   glVertex3f( 1, -1, -1);
   glVertex3f( 1, -1,  1);
   glVertex3f(-1, -1,  1);
   glEnd();

   glColor3f(0, 0, 1);
   glBegin(GL_POLYGON);
   glVertex3f(-1, -1, 1);
   glVertex3f( 1, -1, 1);
   glVertex3f( 1,  1, 1);
   glVertex3f(-1,  1, 1);
   glEnd();

   glColor3f(1, 1, 0);
   glBegin(GL_POLYGON);
   glVertex3f(-1, -1, -1);
   glVertex3f( 1, -1, -1);
   glVertex3f( 1,  1, -1);
   glVertex3f(-1,  1, -1);
   glEnd();
   glPopMatrix();


   glPopMatrix();

   glXSwapBuffers(dpy, win);
}


/*
 * This is called when we have to recompute the window shape bitmask.
 * We just generate an n-sided regular polygon here but any other shape
 * would be possible.
 */
static void make_shape_mask(Display *dpy, Window win, int width, int height,
                            int sides)
{
   Pixmap shapeMask;
   XGCValues xgcv;
   GC gc;

   /* allocate 1-bit deep pixmap and a GC */
   shapeMask = XCreatePixmap(dpy, win, width, height, 1);
   gc = XCreateGC(dpy, shapeMask, 0, &xgcv);

   /* clear shapeMask to zeros */
   XSetForeground(dpy, gc, 0);
   XFillRectangle(dpy, shapeMask, gc, 0, 0, width, height);

   /* draw mask */
   XSetForeground(dpy, gc, 1);
   {
      int cx = width / 2;
      int cy = height / 2;
      float angle = 0.0;
      float step = 2.0 * PI / sides;
      float radius = width / 2;
      int i;
      XPoint points[100];
      for (i=0;i<sides;i++) {
         int x = cx + radius * sin(angle);
         int y = cy - radius * cos(angle);
         points[i].x = x;
         points[i].y = y;
         angle += step;
      }
      XFillPolygon(dpy, shapeMask, gc, points, sides, Convex, CoordModeOrigin);
   }

   /* This is the only SHAPE extension call- simple! */
   XShapeCombineMask(dpy, win, ShapeBounding, 0, 0, shapeMask, ShapeSet);

   XFreeGC(dpy, gc);
   XFreePixmap(dpy, shapeMask);
}


/*
 * Called when window is resized.  Do OpenGL viewport and projection stuff.
 */
static void reshape(int width, int height)
{
   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glFrustum(-1.0, 1.0, -1.0, 1.0, 3.0, 20.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glTranslatef(0.0, 0.0, -10.0);

   glEnable(GL_DEPTH_TEST);
}


/*
 * Process X events.
 */
static void event_loop(Display *dpy, Window win)
{
   while (1) {
      XEvent event;
      if (XPending(dpy)) {
         XNextEvent(dpy, &event);
         switch (event.type) {
            case Expose:
               display(dpy, event.xexpose.window);
               break;
            case ConfigureNotify:
               Width = event.xconfigure.width;
               Height = event.xconfigure.height,
               make_shape_mask(dpy, win, Width, Height, Sides);
               reshape(Width, Height);
               break;
            case KeyPress:
               {
                  char buf[100];
                  KeySym keySym;
                  XComposeStatus stat;
                  XLookupString(&event.xkey, buf, sizeof(buf), &keySym, &stat);
                  switch (keySym) {
                     case XK_Escape:
                        exit(0);
                        break;
                     case XK_Up:
                        Sides++;
                        if (Sides>MaxSides) Sides = MaxSides;
                        make_shape_mask(dpy, win, Width, Height, Sides);
                        break;
                     case XK_Down:
                        Sides--;
                        if (Sides<MinSides) Sides = MinSides;
                        make_shape_mask(dpy, win, Width, Height, Sides);
                        break;
                  }
               }
               break;
            default:
               ;;
         }
      }
      else {
         static double t0 = -1.0;
         double dt, t = current_time();
         if (t0 < 0.0)
            t0 = t;
         dt = t - t0;
         Xangle += 90.0 * dt;  /* 90 degrees per second */
         Yangle += 70.0 * dt;
         t0 = t;
         display(dpy, win);
      }
   }
}


/*
 * Allocate a "nice" colormap.  This could be better (HP-CR support, etc).
 */
static Colormap alloc_colormap(Display *dpy, Window parent, Visual *vis)
{
   Screen *scr = DefaultScreenOfDisplay(dpy);
   int scrnum = DefaultScreen(dpy);

   if (MaxCmapsOfScreen(scr)==1 && vis==DefaultVisual(dpy, scrnum)) {
      /* The window and root are of the same visual type so */
      /* share the root colormap. */
      return DefaultColormap(dpy, scrnum);
   }
   else {
      return XCreateColormap(dpy, parent, vis, AllocNone);
   }
}


int main(int argc, char *argv[])
{
   static int glAttribs[] = {
      GLX_DOUBLEBUFFER,
      GLX_RGBA,
      GLX_DEPTH_SIZE, 1,
      None
   };
   Display *dpy;
   XVisualInfo *visInfo;
   int scrn;
   Window root;
   Colormap cmap;
   Window win;
   XSetWindowAttributes winAttribs;
   unsigned long winAttribsMask;
   GLXContext glCtx;
   int ignore;
   const char *name = "OpenGL in a Shaped Window";

   dpy = XOpenDisplay(NULL);
   if (!dpy) {
      fprintf(stderr, "Couldn't open default display\n");
      return 1;
   }

   /* check that we can use the shape extension */
   if (!XQueryExtension(dpy, "SHAPE", &ignore, &ignore, &ignore )) {
      fprintf(stderr, "Display doesn't support shape extension\n");
      return 1;
   }

   scrn = DefaultScreen(dpy);

   root = RootWindow(dpy, scrn);

   visInfo = glXChooseVisual(dpy, scrn, glAttribs);
   if (!visInfo) {
      fprintf(stderr, "Couldn't get RGB, DB, Z visual\n");
      return 1;
   }

   glCtx = glXCreateContext(dpy, visInfo, 0, True);
   if (!glCtx) {
      fprintf(stderr, "Couldn't create GL context\n");
      return 1;
   }

   cmap = alloc_colormap(dpy, root, visInfo->visual);
   if (!cmap) {
      fprintf(stderr, "Couln't create colormap\n");
      return 1;
   }

   winAttribs.border_pixel = 0;
   winAttribs.colormap = cmap;
   winAttribs.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
   winAttribsMask = CWBorderPixel | CWColormap | CWEventMask;
   win = XCreateWindow(dpy, root, 0, 0, Width, Height, 0,
                       visInfo->depth, InputOutput,
                       visInfo->visual,
                       winAttribsMask, &winAttribs);

   {
      XSizeHints sizehints;
      /*
      sizehints.x = xpos;
      sizehints.y = ypos;
      sizehints.width  = width;
      sizehints.height = height;
      */
      sizehints.flags = 0;
      XSetNormalHints(dpy, win, &sizehints);
      XSetStandardProperties(dpy, win, name, name,
                              None, (char **)NULL, 0, &sizehints);
   }


   XMapWindow(dpy, win);

   glXMakeCurrent(dpy, win, glCtx);

   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
   printf("Press ESC to exit.\n");
   printf("Press up/down to change window shape.\n");

   event_loop(dpy, win);

   return 0;
}