Debian Codegraph @

avogadro/rendering/sphere_ao_render_fs.glsl

raw
1 //#define CONTOUR_LINES
2 //#define TOON_SHADING
3 #version 400
4 precision highp float;
5 // normalized corner
6 in vec2 v_corner;
7 // color
8 in vec3 v_color;
9 // position in eye-coordinate space
10 in vec4 v_eyePos;
11 // sphere radius
12 in float v_radius;
13 // AO tile offset
14 in vec2 v_tileOffset;
15
16 out vec4 outColor;
17 // inverse model-view matrix
18 uniform mat4 u_modelView;
19 uniform mat3 u_invModelView;
20 // the projection matrix
21 uniform mat4 u_projection;
22 // the texture sampler
23 uniform sampler2D u_tex;
24 uniform float u_texScale;
25
26 #ifdef CONTOUR_LINES
27 const float contourWidth = 0.3;
28 #endif
29
30 #ifdef TOON_SHADING
31 const float levels = 4.0;
32 #endif
33
34 vec2 sphereSurfaceToTextureCoord(in vec3 coord)
35 {
36 vec3 absCoord = abs(coord);
37 float d = absCoord.x + absCoord.y + absCoord.z;
38 return (coord.z <= 0.0) ? coord.xy / d : sign(coord.xy) * (1.0 - absCoord.yx / d);
39 }
40
41 float cosine(in vec3 a, in vec3 b)
42 {
43 float cos_alpha = max(0.0, dot(a, b));
44 #ifdef TOON_SHADING
45 cos_alpha = floor(cos_alpha * levels) / levels;
46 #endif
47 return cos_alpha;
48 }
49
50 void main()
51 {
52 // figure out if we are inside our sphere
53 float zz = 1.0 - v_corner.x * v_corner.x - v_corner.y * v_corner.y;
54 #ifdef CONTOUR_LINES
55 if (zz <= -sqrt(contourWidth)) {
56 // fragment outside sphere + contour radius
57 discard;
58 }
59 if (zz <= 0.0) {
60 // fragment is part of the contour
61 float xi = abs(zz); // [0, contourWidth]
62 // eta determines how much the contours are pushed back
63 float eta = 0.0004;
64 outColor = vec4(0.0, 0.0, 0.0, 1.0); // black
65 gl_FragDepth = gl_FragCoord.z - 10e-10 + eta * xi;
66 return;
67 }
68 #else
69 if (zz <= 0.0)
70 discard;
71 #endif
72
73 // compute normal in eye coods
74 vec3 N = normalize(vec3(v_corner, sqrt(zz)));
75
76 // compute the pixel's depth
77 vec4 pos = v_eyePos;
78 pos.z += N.z * v_radius; // radius is 1.0
79 pos = u_projection * pos;
80 gl_FragDepth = (pos.z / pos.w + 1.0) / 2.0;
81
82 // transform to normal to model-space
83 vec3 modelN = N;
84 modelN = normalize(u_invModelView * modelN);
85 // determine (u, v) texture coordinates using gnomonic projection
86 vec2 uv = sphereSurfaceToTextureCoord(modelN); // [-1, 1]
87
88
89 uv = v_tileOffset + uv * u_texScale;
90
91 //gl_FragColor = vec4(v_color, 1.0) * texture2D(u_tex, uv);
92
93 // direction of light source
94 vec3 L = normalize(vec3(0, 1, 1));
95 // eye direction
96 vec3 E = vec3(0, 0, 1);
97
98 // angle between normal and light direction
99 float cos_alpha = cosine(N, L);
100
101 // compute ambient color
102 vec3 ambient = 0.4 * v_color;
103 // compute diffuse color
104 vec3 diffuse = 0.55 * v_color * cos_alpha;
105 // compute specular color (approximate Fresnel reflection)
106 vec3 H = normalize(L + E); // halfway vector between N and E
107 float cos_beta = cosine(N, H);
108 vec3 specular = 0.5 * (vec3(1, 1, 1) - v_color)* pow(cos_beta, 20.0);
109
110 // final color
111 vec3 color = ambient + diffuse + specular;
112 outColor = 1.2 * vec4(color, 1.0) * texture(u_tex, uv); // AO + Phong reflection [+ contours]
113 //gl_FragColor = vec4(color, 1.0); // Phong reflection [+ contours]
114 //gl_FragColor = 1.2 * texture2D(u_tex, uv); // AO [+ contours]
115 //gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); // contours + white atoms
116 }