2 * AA lines with texture mapped quads
21 static GLint WinWidth
= 300, WinHeight
= 300;
23 static GLfloat Width
= 8.;
26 * Quad strip for line from v0 to v1:
29 +---+---------------------+---+
33 +---+---------------------+---+
37 QuadLine(const GLfloat
*v0
, const GLfloat
*v1
, GLfloat width
)
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
;
48 width
*= 0.5; /* half width */
49 dx
= dx
* (width
+ 0.0);
50 dy
= dy
* (width
+ 0.0);
52 dx0
= -dx
+dy
; dy0
= -dy
-dx
;
53 dx1
= -dx
-dy
; dy1
= -dy
+dx
;
55 dx2
= 0+dy
; dy2
= -dx
+0;
56 dx3
= 0-dy
; dy3
= +dx
+0;
58 dx4
= 0+dy
; dy4
= -dx
+0;
59 dx5
= 0-dy
; dy5
= +dx
+0;
61 dx6
= dx
+dy
; dy6
= dy
-dx
;
62 dx7
= dx
-dy
; dy7
= dy
+dx
;
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);
72 glBegin(GL_QUAD_STRIP
);
74 glVertex2f(v0
[0] + dx0
, v0
[1] + dy0
);
76 glVertex2f(v0
[0] + dx1
, v0
[1] + dy1
);
79 glVertex2f(v0
[0] + dx2
, v0
[1] + dy2
);
81 glVertex2f(v0
[0] + dx3
, v0
[1] + dy3
);
84 glVertex2f(v1
[0] + dx2
, v1
[1] + dy2
);
86 glVertex2f(v1
[0] + dx3
, v1
[1] + dy3
);
89 glVertex2f(v1
[0] + dx6
, v1
[1] + dy6
);
91 glVertex2f(v1
[0] + dx7
, v1
[1] + dy7
);
96 static float Cos(float a
)
98 return cos(a
* M_PI
/ 180.);
101 static float Sin(float a
)
103 return sin(a
* M_PI
/ 180.);
111 glClear(GL_COLOR_BUFFER_BIT
);
116 glEnable(GL_TEXTURE_2D
);
118 for (i
= 0; i
< 360; i
+=5) {
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
);
128 float v0
[2], v1
[2], x
;
129 for (x
= 0; x
< 1.0; x
+= 0.2) {
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
);
139 glDisable(GL_TEXTURE_2D
);
146 Reshape(int width
, int height
)
148 glViewport(0, 0, width
, height
);
149 glMatrixMode(GL_PROJECTION
);
151 glOrtho(0, width
, 0, height
, -1, 1);
152 glMatrixMode(GL_MODELVIEW
);
160 glutDestroyWindow(win
);
165 Key(unsigned char key
, int x
, int y
)
186 printf("Width = %g\n", Width
);
192 ramp4(GLint i
, GLint size
)
198 else if (i
>= size
- 5) {
199 d
= 1.0 - (i
- (size
- 5)) / 4.0;
208 ramp2(GLint i
, GLint size
)
214 else if (i
>= size
- 3) {
215 d
= 1.0 - (i
- (size
- 3)) / 2.0;
224 ramp1(GLint i
, GLint size
)
227 if (i
== 0 || i
== size
-1) {
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....
247 GLfloat tex
[SZ
][SZ
]; /* alpha tex */
249 for (i
= 0; i
< SZ
; i
++) {
250 for (j
= 0; j
< SZ
; j
++) {
252 float k
= (SZ
-1) / 2.0;
253 float dx
= fabs(i
- k
) / k
;
254 float dy
= fabs(j
- k
) / k
;
262 float d
= ramp1(i
, SZ
) * ramp1(j
, SZ
);
263 printf("%d, %d: %g\n", i
, j
, d
);
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
);
283 GLfloat tex
[SZ
][SZ
]; /* alpha tex */
286 glPixelStorei(GL_UNPACK_ROW_LENGTH
, SZ
);
287 for (level
= 0; level
< 7; level
++) {
288 int sz
= 1 << (6 - level
);
290 for (i
= 0; i
< sz
; i
++) {
291 for (j
= 0; j
< sz
; j
++) {
297 tex
[i
][j
] = ramp1(i
, sz
) * ramp1(j
, sz
);
301 glTexImage2D(GL_TEXTURE_2D
, level
, GL_ALPHA
,
302 sz
, sz
, 0, GL_ALPHA
, GL_FLOAT
, tex
);
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);
312 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
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
);
334 glClearColor(0.3f
, 0.3f
, 0.3f
, 0.0f
);
336 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
338 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
340 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
350 ParseOptions(int argc
, char *argv
[])
356 main(int argc
, char *argv
[])
358 glutInit(&argc
, argv
);
359 glutInitWindowPosition( 0, 0);
360 glutInitWindowSize(WinWidth
, WinHeight
);
361 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
362 win
= glutCreateWindow(argv
[0]);
364 glutReshapeFunc(Reshape
);
365 glutKeyboardFunc(Key
);
366 glutDisplayFunc(Redisplay
);
367 ParseOptions(argc
, argv
);