progs/glsl: compile with scons and glew
[mesa.git] / progs / glsl / texaaline.c
1 /**
2 * AA lines with texture mapped quads
3 *
4 * Brian Paul
5 * 9 Feb 2008
6 */
7
8
9 #include <assert.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <math.h>
14 #include <GL/glew.h>
15 #include <GL/gl.h>
16 #include <GL/glut.h>
17 #include <GL/glext.h>
18 #include "extfuncs.h"
19
20
21 static GLint WinWidth = 300, WinHeight = 300;
22 static GLint win = 0;
23 static GLfloat Width = 8.;
24
25 /*
26 * Quad strip for line from v0 to v1:
27 *
28 1 3 5 7
29 +---+---------------------+---+
30 | |
31 | *v0 v1* |
32 | |
33 +---+---------------------+---+
34 0 2 4 6
35 */
36 static void
37 QuadLine(const GLfloat *v0, const GLfloat *v1, GLfloat width)
38 {
39 GLfloat dx = v1[0] - v0[0];
40 GLfloat dy = v1[1] - v0[1];
41 GLfloat len = sqrt(dx*dx + dy*dy);
42 float dx0, dx1, dx2, dx3, dx4, dx5, dx6, dx7;
43 float dy0, dy1, dy2, dy3, dy4, dy5, dy6, dy7;
44
45 dx /= len;
46 dy /= len;
47
48 width *= 0.5; /* half width */
49 dx = dx * (width + 0.0);
50 dy = dy * (width + 0.0);
51
52 dx0 = -dx+dy; dy0 = -dy-dx;
53 dx1 = -dx-dy; dy1 = -dy+dx;
54
55 dx2 = 0+dy; dy2 = -dx+0;
56 dx3 = 0-dy; dy3 = +dx+0;
57
58 dx4 = 0+dy; dy4 = -dx+0;
59 dx5 = 0-dy; dy5 = +dx+0;
60
61 dx6 = dx+dy; dy6 = dy-dx;
62 dx7 = dx-dy; dy7 = dy+dx;
63
64 /*
65 printf("dx, dy = %g, %g\n", dx, dy);
66 printf(" dx0, dy0: %g, %g\n", dx0, dy0);
67 printf(" dx1, dy1: %g, %g\n", dx1, dy1);
68 printf(" dx2, dy2: %g, %g\n", dx2, dy2);
69 printf(" dx3, dy3: %g, %g\n", dx3, dy3);
70 */
71
72 glBegin(GL_QUAD_STRIP);
73 glTexCoord2f(0, 0);
74 glVertex2f(v0[0] + dx0, v0[1] + dy0);
75 glTexCoord2f(0, 1);
76 glVertex2f(v0[0] + dx1, v0[1] + dy1);
77
78 glTexCoord2f(0.5, 0);
79 glVertex2f(v0[0] + dx2, v0[1] + dy2);
80 glTexCoord2f(0.5, 1);
81 glVertex2f(v0[0] + dx3, v0[1] + dy3);
82
83 glTexCoord2f(0.5, 0);
84 glVertex2f(v1[0] + dx2, v1[1] + dy2);
85 glTexCoord2f(0.5, 1);
86 glVertex2f(v1[0] + dx3, v1[1] + dy3);
87
88 glTexCoord2f(1, 0);
89 glVertex2f(v1[0] + dx6, v1[1] + dy6);
90 glTexCoord2f(1, 1);
91 glVertex2f(v1[0] + dx7, v1[1] + dy7);
92 glEnd();
93 }
94
95
96 static float Cos(float a)
97 {
98 return cos(a * M_PI / 180.);
99 }
100
101 static float Sin(float a)
102 {
103 return sin(a * M_PI / 180.);
104 }
105
106 static void
107 Redisplay(void)
108 {
109 int i;
110
111 glClear(GL_COLOR_BUFFER_BIT);
112
113 glColor3f(1, 1, 1);
114
115 glEnable(GL_BLEND);
116 glEnable(GL_TEXTURE_2D);
117
118 for (i = 0; i < 360; i+=5) {
119 float v0[2], v1[2];
120 v0[0] = 150 + 40 * Cos(i);
121 v0[1] = 150 + 40 * Sin(i);
122 v1[0] = 150 + 130 * Cos(i);
123 v1[1] = 150 + 130 * Sin(i);
124 QuadLine(v0, v1, Width);
125 }
126
127 {
128 float v0[2], v1[2], x;
129 for (x = 0; x < 1.0; x += 0.2) {
130 v0[0] = 150 + x;
131 v0[1] = 150 + x * 40 - 20;
132 v1[0] = 150 + x + 5.0;
133 v1[1] = 150 + x * 40 - 20;
134 QuadLine(v0, v1, Width);
135 }
136 }
137
138 glDisable(GL_BLEND);
139 glDisable(GL_TEXTURE_2D);
140
141 glutSwapBuffers();
142 }
143
144
145 static void
146 Reshape(int width, int height)
147 {
148 glViewport(0, 0, width, height);
149 glMatrixMode(GL_PROJECTION);
150 glLoadIdentity();
151 glOrtho(0, width, 0, height, -1, 1);
152 glMatrixMode(GL_MODELVIEW);
153 glLoadIdentity();
154 }
155
156
157 static void
158 CleanUp(void)
159 {
160 glutDestroyWindow(win);
161 }
162
163
164 static void
165 Key(unsigned char key, int x, int y)
166 {
167 (void) x;
168 (void) y;
169
170 switch(key) {
171 case 'w':
172 Width -= 0.5;
173 break;
174 case 'W':
175 Width += 0.5;
176 break;
177 case 27:
178 CleanUp();
179 exit(0);
180 break;
181 }
182 #if 0
183 if (Width < 3)
184 Width = 3;
185 #endif
186 printf("Width = %g\n", Width);
187 glutPostRedisplay();
188 }
189
190
191 static float
192 ramp4(GLint i, GLint size)
193 {
194 float d;
195 if (i < 4 ) {
196 d = i / 4.0;
197 }
198 else if (i >= size - 5) {
199 d = 1.0 - (i - (size - 5)) / 4.0;
200 }
201 else {
202 d = 1.0;
203 }
204 return d;
205 }
206
207 static float
208 ramp2(GLint i, GLint size)
209 {
210 float d;
211 if (i < 2 ) {
212 d = i / 2.0;
213 }
214 else if (i >= size - 3) {
215 d = 1.0 - (i - (size - 3)) / 2.0;
216 }
217 else {
218 d = 1.0;
219 }
220 return d;
221 }
222
223 static float
224 ramp1(GLint i, GLint size)
225 {
226 float d;
227 if (i == 0 || i == size-1) {
228 d = 0.0;
229 }
230 else {
231 d = 1.0;
232 }
233 return d;
234 }
235
236
237 /**
238 * Make an alpha texture for antialiasing lines.
239 * Just a linear fall-off ramp for now.
240 * Should have a number of different textures for different line widths.
241 * Could try a bell-like-curve....
242 */
243 static void
244 MakeTexture(void)
245 {
246 #define SZ 8
247 GLfloat tex[SZ][SZ]; /* alpha tex */
248 int i, j;
249 for (i = 0; i < SZ; i++) {
250 for (j = 0; j < SZ; j++) {
251 #if 0
252 float k = (SZ-1) / 2.0;
253 float dx = fabs(i - k) / k;
254 float dy = fabs(j - k) / k;
255 float d;
256
257 dx = 1.0 - dx;
258 dy = 1.0 - dy;
259 d = dx * dy;
260
261 #else
262 float d = ramp1(i, SZ) * ramp1(j, SZ);
263 printf("%d, %d: %g\n", i, j, d);
264 #endif
265 tex[i][j] = d;
266 }
267 }
268
269 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, SZ, SZ, 0, GL_ALPHA, GL_FLOAT, tex);
270 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
271 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
272 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
273 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
274 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
275 #undef SZ
276 }
277
278
279 static void
280 MakeMipmap(void)
281 {
282 #define SZ 64
283 GLfloat tex[SZ][SZ]; /* alpha tex */
284 int level;
285
286 glPixelStorei(GL_UNPACK_ROW_LENGTH, SZ);
287 for (level = 0; level < 7; level++) {
288 int sz = 1 << (6 - level);
289 int i, j;
290 for (i = 0; i < sz; i++) {
291 for (j = 0; j < sz; j++) {
292 if (level == 6)
293 tex[i][j] = 1.0;
294 else if (level == 5)
295 tex[i][j] = 0.5;
296 else
297 tex[i][j] = ramp1(i, sz) * ramp1(j, sz);
298 }
299 }
300
301 glTexImage2D(GL_TEXTURE_2D, level, GL_ALPHA,
302 sz, sz, 0, GL_ALPHA, GL_FLOAT, tex);
303 }
304
305 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
306 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
307 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
308 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
309 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 4);
310 ////glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 5);
311
312 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
313 #undef SZ
314 }
315
316
317 static void
318 Init(void)
319 {
320 const char *version;
321
322 (void) MakeTexture;
323 (void) ramp4;
324 (void) ramp2;
325
326 version = (const char *) glGetString(GL_VERSION);
327 if (version[0] != '2' || version[1] != '.') {
328 printf("This program requires OpenGL 2.x, found %s\n", version);
329 exit(1);
330 }
331
332 GetExtensionFuncs();
333
334 glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
335
336 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
337
338 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
339 #if 0
340 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
341 #elif 0
342 MakeTexture();
343 #else
344 MakeMipmap();
345 #endif
346 }
347
348
349 static void
350 ParseOptions(int argc, char *argv[])
351 {
352 }
353
354
355 int
356 main(int argc, char *argv[])
357 {
358 glutInit(&argc, argv);
359 glutInitWindowPosition( 0, 0);
360 glutInitWindowSize(WinWidth, WinHeight);
361 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
362 win = glutCreateWindow(argv[0]);
363 glewInit();
364 glutReshapeFunc(Reshape);
365 glutKeyboardFunc(Key);
366 glutDisplayFunc(Redisplay);
367 ParseOptions(argc, argv);
368 Init();
369 glutMainLoop();
370 return 0;
371 }