Debian Codegraph @

Shaders/3dcloud.vert

raw
1 // -*-C++-*-
2 #version 120
3
4 varying float fogFactor;
5 varying vec4 cloudColor;
6
7 uniform float range; // From /sim/rendering/clouds3d-vis-range
8 uniform float detail_range; // From /sim/rendering/clouds3d_detail-range
9
10 attribute vec3 usrAttr1;
11 attribute vec3 usrAttr2;
12
13 float shade_factor = usrAttr1.g;
14 float cloud_height = usrAttr1.b;
15 float bottom_factor = usrAttr2.r;
16 float middle_factor = usrAttr2.g;
17 float top_factor = usrAttr2.b;
18
19 void main(void)
20 {
21 gl_TexCoord[0] = gl_MultiTexCoord0;
22 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
23 vec4 l = gl_ModelViewMatrixInverse * vec4(0.0,0.0,1.0,1.0);
24 vec3 u = normalize(ep.xyz - l.xyz);
25
26 // Find a rotation matrix that rotates 1,0,0 into u. u, r and w are
27 // the columns of that matrix.
28 vec3 absu = abs(u);
29 vec3 r = normalize(vec3(-u.y, u.x, 0.0));
30 vec3 w = cross(u, r);
31
32 // Do the matrix multiplication by [ u r w pos]. Assume no
33 // scaling in the homogeneous component of pos.
34 gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
35 gl_Position.xyz = gl_Vertex.x * u;
36 gl_Position.xyz += gl_Vertex.y * r;
37 gl_Position.xyz += gl_Vertex.z * w;
38 // Apply Z scaling to allow sprites to be squashed in the z-axis
39 gl_Position.z = gl_Position.z * gl_Color.w;
40
41 // Now shift the sprite to the correct position in the cloud.
42 gl_Position.xyz += gl_Color.xyz;
43
44 // Determine the position - used for fog and shading calculations
45 float fogCoord = length(vec3(gl_ModelViewMatrix * vec4(gl_Color.x, gl_Color.y, gl_Color.z, 1.0)));
46 float center_dist = length(vec3(gl_ModelViewMatrix * vec4(0.0,0.0,0.0,1.0)));
47
48 if ((fogCoord > detail_range) && (fogCoord > center_dist) && (shade_factor < 0.7)) {
49 // More than detail_range away, so discard all sprites on opposite side of
50 // cloud center by shifting them beyond the view fustrum
51 gl_Position = vec4(0.0,0.0,10.0,1.0);
52 cloudColor = vec4(0.0);
53 } else {
54 // Determine a lighting normal based on the vertex position from the
55 // center of the cloud, so that sprite on the opposite side of the cloud to the sun are darker.
56 float n = dot(normalize(-gl_LightSource[0].position.xyz),
57 normalize(vec3(gl_ModelViewMatrix * vec4(- gl_Position.x, - gl_Position.y, - gl_Position.z, 0.0))));
58
59 // Determine the shading of the vertex. We shade it based on it's position
60 // in the cloud relative to the sun, and it's vertical position in the cloud.
61 float shade = mix(shade_factor, top_factor, smoothstep(-0.3, 0.3, n));
62 //if (n < 0) {
63 // shade = mix(top_factor, shade_factor, abs(n));
64 //}
65
66 if (gl_Position.z < 0.5 * cloud_height) {
67 shade = min(shade, mix(bottom_factor, middle_factor, gl_Position.z * 2.0 / cloud_height));
68 } else {
69 shade = min(shade, mix(middle_factor, top_factor, gl_Position.z * 2.0 / cloud_height - 1.0));
70 }
71
72 // Final position of the sprite
73 gl_Position = gl_ModelViewProjectionMatrix * gl_Position;
74 cloudColor = gl_LightSource[0].diffuse * shade + gl_FrontLightModelProduct.sceneColor;
75
76 if ((fogCoord > (0.9 * detail_range)) && (fogCoord > center_dist) && (shade_factor < 0.7)) {
77 // cloudlet is almost at the detail range, so fade it out.
78 cloudColor.a = 1.0 - smoothstep(0.9 * detail_range, detail_range, fogCoord);
79 } else {
80 // As we get within 100m of the sprite, it is faded out. Equally at large distances it also fades out.
81 cloudColor.a = min(smoothstep(10.0, 100.0, fogCoord), 1.0 - smoothstep(0.9 * range, range, fogCoord));
82 }
83
84 //gl_BackColor = cloudColor;
85
86 // Fog doesn't affect clouds as much as other objects.
87 fogFactor = exp( -gl_Fog.density * fogCoord * 0.5);
88 fogFactor = clamp(fogFactor, 0.0, 1.0);
89 }
90 }