source: trunk/src/opengl/util/ellipse_functions.glsl@ 440

Last change on this file since 440 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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