summaryrefslogtreecommitdiffstats
path: root/hacks/glx/dropshadow.c
blob: 0a5855cc996d548260bf5483e7d33aa13263c2bf (plain) (blame)
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
/* dropshadow.c, Copyright (c) 2009 Jens Kilian <jjk@acm.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdlib.h>

#include "dropshadow.h"

/* (Alpha) texture data for drop shadow.
 */
static int drop_shadow_width = 32;
static int drop_shadow_height = 32;
static unsigned char drop_shadow_data[] =
{
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 1, 1, 1, 3, 3, 4, 4, 4, 4, 4, 4,
  4, 4, 4, 4, 4, 4, 3, 3, 1, 1, 1, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 1, 1, 3, 4, 6, 7, 9, 10, 10, 10, 10, 10,
  10, 10, 10, 10, 10, 9, 7, 6, 4, 3, 1, 1, 0, 0, 0, 0,
  0, 0, 0, 1, 1, 3, 5, 9, 13, 16, 19, 19, 21, 21, 22, 22,
  22, 22, 21, 21, 19, 19, 16, 13, 9, 5, 3, 1, 1, 0, 0, 0,
  0, 0, 1, 1, 3, 5, 10, 16, 22, 28, 32, 35, 37, 37, 38, 38,
  38, 38, 37, 37, 35, 32, 28, 22, 16, 10, 5, 3, 1, 1, 0, 0,
  0, 0, 1, 1, 4, 9, 16, 25, 34, 43, 50, 55, 58, 59, 60, 60,
  60, 60, 59, 58, 55, 50, 43, 34, 25, 16, 9, 4, 1, 1, 0, 0,
  0, 0, 1, 3, 6, 13, 22, 34, 48, 61, 70, 77, 80, 82, 83, 84,
  84, 83, 82, 80, 77, 70, 61, 48, 34, 22, 13, 6, 3, 1, 0, 0,
  0, 0, 1, 3, 7, 16, 28, 43, 61, 76, 88, 97, 102, 103, 104, 104,
  104, 104, 103, 102, 97, 88, 76, 61, 43, 28, 16, 7, 3, 1, 0, 0,
  0, 1, 1, 4, 9, 19, 32, 51, 70, 88, 103, 112, 117, 120, 121, 121,
  121, 121, 120, 117, 112, 103, 88, 70, 51, 32, 19, 9, 4, 1, 1, 0,
  0, 1, 1, 4, 10, 20, 35, 55, 77, 97, 112, 122, 128, 130, 132, 133,
  133, 132, 130, 128, 122, 112, 97, 77, 55, 35, 20, 10, 4, 1, 1, 0,
  0, 1, 1, 4, 10, 21, 37, 58, 80, 101, 117, 128, 134, 137, 138, 139,
  139, 138, 137, 134, 128, 117, 101, 80, 58, 37, 21, 10, 4, 1, 0, 0,
  0, 0, 1, 4, 10, 21, 38, 59, 82, 103, 119, 130, 137, 139, 141, 142,
  142, 141, 139, 137, 130, 119, 103, 82, 59, 38, 21, 10, 4, 1, 0, 0,
  0, 0, 1, 4, 10, 22, 38, 59, 83, 104, 121, 132, 139, 141, 142, 142,
  142, 142, 141, 139, 132, 121, 104, 83, 59, 38, 22, 10, 4, 1, 0, 0,
  0, 0, 1, 4, 10, 22, 38, 60, 84, 104, 121, 133, 139, 142, 142, 142,
  142, 142, 142, 139, 133, 121, 104, 84, 60, 38, 22, 10, 4, 1, 0, 0,
  0, 0, 1, 4, 10, 22, 38, 60, 84, 104, 121, 133, 139, 142, 142, 142,
  142, 142, 142, 139, 133, 121, 104, 84, 60, 38, 22, 10, 4, 1, 0, 0,
  0, 0, 1, 4, 10, 22, 38, 59, 83, 104, 121, 132, 139, 141, 142, 142,
  142, 142, 141, 139, 132, 121, 104, 83, 59, 38, 22, 10, 4, 1, 0, 0,
  0, 0, 1, 4, 10, 21, 38, 59, 82, 103, 119, 130, 137, 139, 141, 142,
  142, 141, 139, 137, 130, 119, 103, 82, 59, 38, 21, 10, 4, 1, 0, 0,
  0, 1, 1, 4, 10, 21, 37, 58, 80, 101, 118, 128, 134, 137, 139, 139,
  139, 139, 137, 134, 128, 117, 102, 80, 58, 37, 21, 10, 4, 1, 0, 0,
  0, 1, 1, 4, 10, 20, 35, 55, 77, 97, 112, 122, 128, 130, 132, 133,
  133, 132, 130, 128, 122, 112, 97, 77, 55, 35, 20, 10, 4, 1, 1, 0,
  0, 1, 1, 4, 9, 19, 32, 51, 70, 88, 103, 112, 117, 120, 121, 121,
  121, 121, 120, 117, 112, 103, 88, 70, 51, 32, 19, 9, 4, 1, 1, 0,
  0, 0, 1, 3, 7, 16, 28, 43, 61, 76, 88, 97, 102, 103, 104, 104,
  104, 104, 103, 102, 97, 88, 76, 61, 43, 28, 16, 7, 3, 1, 0, 0,
  0, 0, 1, 3, 6, 13, 22, 34, 48, 61, 70, 77, 80, 82, 83, 84,
  84, 83, 82, 80, 77, 70, 61, 48, 34, 22, 13, 6, 3, 1, 0, 0,
  0, 0, 1, 1, 4, 9, 16, 25, 34, 43, 50, 55, 58, 59, 60, 60,
  60, 60, 59, 58, 55, 50, 43, 34, 25, 16, 9, 4, 1, 1, 0, 0,
  0, 0, 1, 1, 3, 5, 10, 16, 22, 28, 32, 35, 37, 37, 38, 38,
  38, 38, 37, 37, 35, 32, 28, 22, 16, 10, 5, 3, 1, 1, 0, 0,
  0, 0, 0, 1, 1, 3, 5, 9, 13, 16, 19, 19, 21, 21, 22, 22,
  22, 22, 21, 21, 19, 19, 16, 13, 9, 5, 3, 1, 1, 0, 0, 0,
  0, 0, 0, 0, 1, 1, 3, 4, 6, 7, 9, 10, 10, 10, 10, 10,
  10, 10, 10, 10, 10, 9, 7, 6, 4, 3, 1, 1, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 1, 1, 1, 3, 3, 4, 4, 4, 4, 4, 4,
  4, 4, 4, 4, 4, 4, 3, 3, 1, 1, 1, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

GLuint
init_drop_shadow(void)
{
    GLuint t;

    glGenTextures (1, &t);
    if (t <= 0) abort();

    glBindTexture (GL_TEXTURE_2D, t);
#if 0
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    gluBuild2DMipmaps (GL_TEXTURE_2D, GL_ALPHA,
                       drop_shadow_width, drop_shadow_height,
                       GL_ALPHA, GL_UNSIGNED_BYTE,
                       drop_shadow_data);
#else
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, 
                  drop_shadow_width, drop_shadow_height, 0,
                  GL_ALPHA, GL_UNSIGNED_BYTE,
                  drop_shadow_data);
#endif

    return t;
}

void
draw_drop_shadow (GLuint t,
                  GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat h,
                  GLfloat r)
{
  /* Inner and outer boundaries of shadow. */
  const GLfloat li = x,     lo = li - r;
  const GLfloat ri = x + w, ro = ri + r;
  const GLfloat bi = y,     bo = bi - r;
  const GLfloat ti = y + h, to = ti + r;

  glEnable (GL_BLEND);
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  glEnable (GL_TEXTURE_2D);
  glBindTexture (GL_TEXTURE_2D, t);

  glBegin (GL_QUADS);

  /* There's likely a better way to do this... */
  glTexCoord2f (0.0, 0.0); glVertex3f (lo, bo, z);
  glTexCoord2f (0.5, 0.0); glVertex3f (li, bo, z);
  glTexCoord2f (0.5, 0.5); glVertex3f (li, bi, z);
  glTexCoord2f (0.0, 0.5); glVertex3f (lo, bi, z);

  glTexCoord2f (0.5, 0.0); glVertex3f (li, bo, z);
  glTexCoord2f (0.5, 0.0); glVertex3f (ri, bo, z);
  glTexCoord2f (0.5, 0.5); glVertex3f (ri, bi, z);
  glTexCoord2f (0.5, 0.5); glVertex3f (li, bi, z);

  glTexCoord2f (0.5, 0.0); glVertex3f (ri, bo, z);
  glTexCoord2f (1.0, 0.0); glVertex3f (ro, bo, z);
  glTexCoord2f (1.0, 0.5); glVertex3f (ro, bi, z);
  glTexCoord2f (0.5, 0.5); glVertex3f (ri, bi, z);

  glTexCoord2f (0.5, 0.5); glVertex3f (ri, bi, z);
  glTexCoord2f (1.0, 0.5); glVertex3f (ro, bi, z);
  glTexCoord2f (1.0, 0.5); glVertex3f (ro, ti, z);
  glTexCoord2f (0.5, 0.5); glVertex3f (ri, ti, z);

  glTexCoord2f (0.5, 0.5); glVertex3f (ri, ti, z);
  glTexCoord2f (1.0, 0.5); glVertex3f (ro, ti, z);
  glTexCoord2f (1.0, 1.0); glVertex3f (ro, to, z);
  glTexCoord2f (0.5, 1.0); glVertex3f (ri, to, z);

  glTexCoord2f (0.5, 0.5); glVertex3f (li, ti, z);
  glTexCoord2f (0.5, 0.5); glVertex3f (ri, ti, z);
  glTexCoord2f (0.5, 1.0); glVertex3f (ri, to, z);
  glTexCoord2f (0.5, 1.0); glVertex3f (li, to, z);

  glTexCoord2f (0.0, 0.5); glVertex3f (lo, ti, z);
  glTexCoord2f (0.5, 0.5); glVertex3f (li, ti, z);
  glTexCoord2f (0.5, 1.0); glVertex3f (li, to, z);
  glTexCoord2f (0.0, 1.0); glVertex3f (lo, to, z);

  glTexCoord2f (0.0, 0.5); glVertex3f (lo, bi, z);
  glTexCoord2f (0.5, 0.5); glVertex3f (li, bi, z);
  glTexCoord2f (0.5, 0.5); glVertex3f (li, ti, z);
  glTexCoord2f (0.0, 0.5); glVertex3f (lo, ti, z);

  glEnd();
}