Debian Codegraph @

Shaders/clouds-box.vert

raw
1 // -*-C++-*-
2 #version 120
3
4 varying float fogFactor;
5
6 //attribute vec3 usrAttr3;
7 //attribute vec3 usrAttr4;
8
9 //float textureIndexX = usrAttr3.r;
10 //float textureIndexY = usrAttr3.g;
11 //float wScale = usrAttr3.b;
12 //float hScale = usrAttr4.r;
13 //float shade = usrAttr4.g;
14 //float cloud_height = usrAttr4.b;
15
16 void main(void)
17 {
18
19 float shade = 0.9;
20 float cloud_height = 1000.0;
21
22 gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
23 //gl_TexCoord[0] = gl_MultiTexCoord0 + vec4(textureIndexX, textureIndexY, 0.0, 0.0);
24 vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
25 vec4 l = gl_ModelViewMatrixInverse * vec4(0.0,0.0,1.0,1.0);
26 vec3 u = normalize(ep.xyz - l.xyz);
27
28 // Find a rotation matrix that rotates 1,0,0 into u. u, r and w are
29 // the columns of that matrix.
30 vec3 absu = abs(u);
31 vec3 r = normalize(vec3(-u.y, u.x, 0.0));
32 vec3 w = cross(u, r);
33
34 // Do the matrix multiplication by [ u r w pos]. Assume no
35 // scaling in the homogeneous component of pos.
36 gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
37 gl_Position.xyz = gl_Vertex.x * u;
38 gl_Position.xyz += gl_Vertex.y * r * 1.0;
39 gl_Position.xyz += gl_Vertex.z * w * 1.0;
40 //gl_Position.xyz += gl_Vertex.y * r * wScale;
41 //gl_Position.xyz += gl_Vertex.z * w * hScale;
42 gl_Position.xyz += gl_Color.xyz;
43
44 // Determine a lighting normal based on the vertex position from the
45 // center of the cloud, so that sprite on the opposite side of the cloud to the sun are darker.
46 float n = dot(normalize(-gl_LightSource[0].position.xyz),
47 normalize(mat3x3(gl_ModelViewMatrix) * (- gl_Position.xyz)));;
48
49 // Determine the position - used for fog and shading calculations
50 vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Position);
51 float fogCoord = abs(ecPosition.z);
52 float fract = smoothstep(0.0, cloud_height, gl_Position.z + cloud_height);
53
54 // Final position of the sprite
55 gl_Position = gl_ModelViewProjectionMatrix * gl_Position;
56
57 // Determine the shading of the sprite based on its vertical position and position relative to the sun.
58 n = min(smoothstep(-0.5, 0.0, n), fract);
59 // Determine the shading based on a mixture from the backlight to the front
60 vec4 backlight = gl_LightSource[0].diffuse * shade;
61
62 gl_FrontColor = mix(backlight, gl_LightSource[0].diffuse, n);
63 gl_FrontColor += gl_FrontLightModelProduct.sceneColor;
64
65 // As we get within 100m of the sprite, it is faded out. Equally at large distances it also fades out.
66 gl_FrontColor.a = min(smoothstep(10.0, 100.0, fogCoord), 1.0 - smoothstep(25000.0, 30000.0, fogCoord));
67 gl_BackColor = gl_FrontColor;
68
69 // Fog doesn't affect clouds as much as other objects.
70 fogFactor = exp( -gl_Fog.density * fogCoord * 0.2);
71 fogFactor = clamp(fogFactor, 0.0, 1.0);
72 }