2 * AA lines with texture mapped quads
15 #include "glut_wrap.h"
18 #define M_PI 3.1415926535
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.);
109 float cx
= 0.5 * WinWidth
, cy
= 0.5 * WinHeight
;
110 float len
= 0.5 * WinWidth
- 20.0;
113 glClear(GL_COLOR_BUFFER_BIT
);
118 glEnable(GL_TEXTURE_2D
);
120 for (i
= 0; i
< 360; i
+=5) {
122 v0
[0] = cx
+ 40 * Cos(i
);
123 v0
[1] = cy
+ 40 * Sin(i
);
124 v1
[0] = cx
+ len
* Cos(i
);
125 v1
[1] = cy
+ len
* Sin(i
);
126 QuadLine(v0
, v1
, Width
);
130 float v0
[2], v1
[2], x
;
131 for (x
= 0; x
< 1.0; x
+= 0.2) {
133 v0
[1] = cy
+ x
* 40 - 20;
134 v1
[0] = cx
+ x
+ 5.0;
135 v1
[1] = cy
+ x
* 40 - 20;
136 QuadLine(v0
, v1
, Width
);
141 glDisable(GL_TEXTURE_2D
);
148 Reshape(int width
, int height
)
152 glViewport(0, 0, width
, height
);
153 glMatrixMode(GL_PROJECTION
);
155 glOrtho(0, width
, 0, height
, -1, 1);
156 glMatrixMode(GL_MODELVIEW
);
164 glutDestroyWindow(win
);
169 Key(unsigned char key
, int x
, int y
)
190 printf("Width = %g\n", Width
);
196 ramp4(GLint i
, GLint size
)
202 else if (i
>= size
- 5) {
203 d
= 1.0 - (i
- (size
- 5)) / 4.0;
212 ramp2(GLint i
, GLint size
)
218 else if (i
>= size
- 3) {
219 d
= 1.0 - (i
- (size
- 3)) / 2.0;
228 ramp1(GLint i
, GLint size
)
231 if (i
== 0 || i
== size
-1) {
242 * Make an alpha texture for antialiasing lines.
243 * Just a linear fall-off ramp for now.
244 * Should have a number of different textures for different line widths.
245 * Could try a bell-like-curve....
251 GLfloat tex
[SZ
][SZ
]; /* alpha tex */
253 for (i
= 0; i
< SZ
; i
++) {
254 for (j
= 0; j
< SZ
; j
++) {
256 float k
= (SZ
-1) / 2.0;
257 float dx
= fabs(i
- k
) / k
;
258 float dy
= fabs(j
- k
) / k
;
266 float d
= ramp1(i
, SZ
) * ramp1(j
, SZ
);
267 printf("%d, %d: %g\n", i
, j
, d
);
273 glTexImage2D(GL_TEXTURE_2D
, 0, GL_ALPHA
, SZ
, SZ
, 0, GL_ALPHA
, GL_FLOAT
, tex
);
274 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
275 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
276 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
277 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
278 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
287 GLfloat tex
[SZ
][SZ
]; /* alpha tex */
290 glPixelStorei(GL_UNPACK_ROW_LENGTH
, SZ
);
291 for (level
= 0; level
< 7; level
++) {
292 int sz
= 1 << (6 - level
);
294 for (i
= 0; i
< sz
; i
++) {
295 for (j
= 0; j
< sz
; j
++) {
301 tex
[i
][j
] = ramp1(i
, sz
) * ramp1(j
, sz
);
305 glTexImage2D(GL_TEXTURE_2D
, level
, GL_ALPHA
,
306 sz
, sz
, 0, GL_ALPHA
, GL_FLOAT
, tex
);
309 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
310 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
311 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
312 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
313 /* glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 4); */
314 /* glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 5); */
316 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
328 if (!GLEW_VERSION_2_0
) {
329 printf("This program requires OpenGL 2.x\n");
333 glClearColor(0.3f
, 0.3f
, 0.3f
, 0.0f
);
335 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
337 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
339 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
349 ParseOptions(int argc
, char *argv
[])
355 main(int argc
, char *argv
[])
357 glutInit(&argc
, argv
);
358 glutInitWindowSize(WinWidth
, WinHeight
);
359 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
360 win
= glutCreateWindow(argv
[0]);
362 glutReshapeFunc(Reshape
);
363 glutKeyboardFunc(Key
);
364 glutDisplayFunc(Redisplay
);
365 ParseOptions(argc
, argv
);