aboutsummaryrefslogtreecommitdiffstats
path: root/progs/glsl/texaaline.c
blob: 4ad5a761c67f04f4f77bfd995c5a4ba8fd36d2ef (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
/**
 * AA lines with texture mapped quads
 *
 * Brian Paul
 * 9 Feb 2008
 */


#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glew.h>
#include <GL/glut.h>

#ifndef M_PI
#define M_PI 3.1415926535
#endif

static GLint WinWidth = 300, WinHeight = 300;
static GLint win = 0;
static GLfloat Width = 8.;

/*
 * Quad strip for line from v0 to v1:
 *
 1   3                     5   7
 +---+---------------------+---+
 |                             |
 |   *v0                 v1*   |
 |                             |
 +---+---------------------+---+
 0   2                     4   6
 */
static void
QuadLine(const GLfloat *v0, const GLfloat *v1, GLfloat width)
{
   GLfloat dx = v1[0] - v0[0];
   GLfloat dy = v1[1] - v0[1];
   GLfloat len = sqrt(dx*dx + dy*dy);
   float dx0, dx1, dx2, dx3, dx4, dx5, dx6, dx7;
   float dy0, dy1, dy2, dy3, dy4, dy5, dy6, dy7;

   dx /= len;
   dy /= len;

   width *= 0.5;  /* half width */
   dx = dx * (width + 0.0);
   dy = dy * (width + 0.0);

   dx0 = -dx+dy;  dy0 = -dy-dx;
   dx1 = -dx-dy;  dy1 = -dy+dx;

   dx2 =   0+dy;  dy2 = -dx+0;
   dx3 =   0-dy;  dy3 = +dx+0;

   dx4 =   0+dy;  dy4 = -dx+0;
   dx5 =   0-dy;  dy5 = +dx+0;

   dx6 =  dx+dy;  dy6 =  dy-dx;
   dx7 =  dx-dy;  dy7 =  dy+dx;

   /*
   printf("dx, dy = %g, %g\n", dx, dy);
   printf("  dx0, dy0: %g, %g\n", dx0, dy0);
   printf("  dx1, dy1: %g, %g\n", dx1, dy1);
   printf("  dx2, dy2: %g, %g\n", dx2, dy2);
   printf("  dx3, dy3: %g, %g\n", dx3, dy3);
   */

   glBegin(GL_QUAD_STRIP);
   glTexCoord2f(0, 0);
   glVertex2f(v0[0] + dx0, v0[1] + dy0);
   glTexCoord2f(0, 1);
   glVertex2f(v0[0] + dx1, v0[1] + dy1);

   glTexCoord2f(0.5, 0);
   glVertex2f(v0[0] + dx2, v0[1] + dy2);
   glTexCoord2f(0.5, 1);
   glVertex2f(v0[0] + dx3, v0[1] + dy3);

   glTexCoord2f(0.5, 0);
   glVertex2f(v1[0] + dx2, v1[1] + dy2);
   glTexCoord2f(0.5, 1);
   glVertex2f(v1[0] + dx3, v1[1] + dy3);

   glTexCoord2f(1, 0);
   glVertex2f(v1[0] + dx6, v1[1] + dy6);
   glTexCoord2f(1, 1);
   glVertex2f(v1[0] + dx7, v1[1] + dy7);
   glEnd();
}


static float Cos(float a)
{
   return cos(a * M_PI / 180.);
}

static float Sin(float a)
{
   return sin(a * M_PI / 180.);
}

static void
Redisplay(void)
{
   float cx = 0.5 * WinWidth, cy = 0.5 * WinHeight;
   float len = 0.5 * WinWidth - 20.0;
   int i;

   glClear(GL_COLOR_BUFFER_BIT);

   glColor3f(1, 1, 1);

   glEnable(GL_BLEND);
   glEnable(GL_TEXTURE_2D);

   for (i = 0; i < 360; i+=5) {
      float v0[2], v1[2];
      v0[0] = cx + 40 * Cos(i);
      v0[1] = cy + 40 * Sin(i);
      v1[0] = cx + len * Cos(i);
      v1[1] = cy + len * Sin(i);
      QuadLine(v0, v1, Width);
   }

   {
      float v0[2], v1[2], x;
      for (x = 0; x < 1.0; x += 0.2) {
         v0[0] = cx + x;
         v0[1] = cy + x * 40 - 20;
         v1[0] = cx + x + 5.0;
         v1[1] = cy + x * 40 - 20;
         QuadLine(v0, v1, Width);
      }
   }

   glDisable(GL_BLEND);
   glDisable(GL_TEXTURE_2D);

   glutSwapBuffers();
}


static void
Reshape(int width, int height)
{
   WinWidth = width;
   WinHeight = height;
   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0, width, 0, height, -1, 1);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}


static void
CleanUp(void)
{
   glutDestroyWindow(win);
}


static void
Key(unsigned char key, int x, int y)
{
  (void) x;
  (void) y;

   switch(key) {
   case 'w':
      Width -= 0.5;
      break;
   case 'W':
      Width += 0.5;
      break;
   case 27:
      CleanUp();
      exit(0);
      break;
   }
#if 0
   if (Width < 3)
      Width = 3;
#endif
   printf("Width = %g\n", Width);
   glutPostRedisplay();
}


static float
ramp4(GLint i, GLint size)
{
   float d;
   if (i < 4 ) {
      d = i / 4.0;
   }
   else if (i >= size - 5) {
      d = 1.0 - (i - (size - 5)) / 4.0;
   }
   else {
      d = 1.0;
   }
   return d;
}

static float
ramp2(GLint i, GLint size)
{
   float d;
   if (i < 2 ) {
      d = i / 2.0;
   }
   else if (i >= size - 3) {
      d = 1.0 - (i - (size - 3)) / 2.0;
   }
   else {
      d = 1.0;
   }
   return d;
}

static float
ramp1(GLint i, GLint size)
{
   float d;
   if (i == 0 || i == size-1) {
      d = 0.0;
   }
   else {
      d = 1.0;
   }
   return d;
}


/**
 * Make an alpha texture for antialiasing lines.
 * Just a linear fall-off ramp for now.
 * Should have a number of different textures for different line widths.
 * Could try a bell-like-curve....
 */
static void
MakeTexture(void)
{
#define SZ 8
   GLfloat tex[SZ][SZ];  /* alpha tex */
   int i, j;
   for (i = 0; i < SZ; i++) {
      for (j = 0; j < SZ; j++) {
#if 0
         float k = (SZ-1) / 2.0;
         float dx = fabs(i - k) / k;
         float dy = fabs(j - k) / k;
         float d;

         dx = 1.0 - dx;
         dy = 1.0 - dy;
         d = dx * dy;

#else
         float d = ramp1(i, SZ) * ramp1(j, SZ);
         printf("%d, %d: %g\n", i, j, d);
#endif
         tex[i][j] = d;
      }
   }

   glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, SZ, SZ, 0, GL_ALPHA, GL_FLOAT, tex);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
#undef SZ
}


static void
MakeMipmap(void)
{
#define SZ 64
   GLfloat tex[SZ][SZ];  /* alpha tex */
   int level;

   glPixelStorei(GL_UNPACK_ROW_LENGTH, SZ);
   for (level = 0; level < 7; level++) {
      int sz = 1 << (6 - level);
      int i, j;
      for (i = 0; i < sz; i++) {
         for (j = 0; j < sz; j++) {
            if (level == 6)
               tex[i][j] = 1.0;
            else if (level == 5)
               tex[i][j] = 0.5;
            else
               tex[i][j] = ramp1(i, sz) * ramp1(j, sz);
         }
      }

      glTexImage2D(GL_TEXTURE_2D, level, GL_ALPHA,
                   sz, sz, 0, GL_ALPHA, GL_FLOAT, tex);
   }

   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   /*
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 4);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 5);
   */

   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
#undef SZ
}


static void
Init(void)
{
   const char *version;

   (void) MakeTexture;
   (void) ramp4;
   (void) ramp2;

   version = (const char *) glGetString(GL_VERSION);
   if (version[0] != '2' || version[1] != '.') {
      printf("This program requires OpenGL 2.x, found %s\n", version);
      exit(1);
   }

   glClearColor(0.3f, 0.3f, 0.3f, 0.0f);

   printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));

   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
#if 0
   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
#elif 0
   MakeTexture();
#else
   MakeMipmap();
#endif
}


static void
ParseOptions(int argc, char *argv[])
{
}


int
main(int argc, char *argv[])
{
   glutInit(&argc, argv);
   glutInitWindowSize(WinWidth, WinHeight);
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
   win = glutCreateWindow(argv[0]);
   glewInit();
   glutReshapeFunc(Reshape);
   glutKeyboardFunc(Key);
   glutDisplayFunc(Redisplay);
   ParseOptions(argc, argv);
   Init();
   glutMainLoop();
   return 0;
}