source: trunk/src/opengl/util/ellipse_aa.glsl@ 1023

Last change on this file since 1023 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 1.4 KB
Line 
1uniform vec3 inv_matrix_m0;
2uniform vec3 inv_matrix_m1;
3uniform vec3 inv_matrix_m2;
4
5uniform vec2 ellipse_offset;
6
7// ellipse equation
8
9// s^2/a^2 + t^2/b^2 = 1
10//
11// implicit equation:
12// g(s,t) = 1 - s^2/r_s^2 - t^2/r_t^2
13
14// distance from ellipse:
15// grad = [dg/dx dg/dy]
16// d(s, t) ~= g(s, t) / |grad|
17
18// dg/dx = dg/ds * ds/dx + dg/dt * dt/dx
19// dg/dy = dg/ds * ds/dy + dg/dt * dt/dy
20
21float ellipse_aa()
22{
23 mat3 mat;
24
25 mat[0] = inv_matrix_m0;
26 mat[1] = inv_matrix_m1;
27 mat[2] = inv_matrix_m2;
28
29 vec3 hcoords = mat * vec3(gl_FragCoord.xy + ellipse_offset, 1);
30 float inv_w = 1.0 / hcoords.z;
31 vec2 st = hcoords.xy * inv_w;
32
33 vec4 xy = vec4(mat[0].xy, mat[1].xy);
34 vec2 h = vec2(mat[0].z, mat[1].z);
35
36 vec4 dstdxy = (xy.xzyw - h.xyxy * st.xxyy) * inv_w;
37
38 //dstdxy.x = (mat[0].x - mat[0].z * st.x) * inv_w; // ds/dx
39 //dstdxy.y = (mat[1].x - mat[1].z * st.x) * inv_w; // ds/dy
40 //dstdxy.z = (mat[0].y - mat[0].z * st.y) * inv_w; // dt/dx
41 //dstdxy.w = (mat[1].y - mat[1].z * st.y) * inv_w; // dt/dy
42
43 vec2 inv_r = gl_TexCoord[0].xy;
44 vec2 n = st * inv_r;
45 float g = 1.0 - dot(n, n);
46
47 vec2 dgdst = -2.0 * n * inv_r;
48
49 vec2 grad = vec2(dot(dgdst, dstdxy.xz),
50 dot(dgdst, dstdxy.yw));
51
52 return smoothstep(-0.5, 0.5, g * inversesqrt(dot(grad, grad)));
53}
54
55void main()
56{
57 gl_FragColor = ellipse_aa().xxxx;
58}
Note: See TracBrowser for help on using the repository browser.