2009-11-26 10:44:44 +00:00
|
|
|
// -*-C++-*-
|
2009-09-29 07:41:11 +00:00
|
|
|
#version 120
|
|
|
|
|
|
|
|
varying float fogFactor;
|
|
|
|
|
2011-08-21 21:09:04 +00:00
|
|
|
uniform float range; // From /sim/rendering/clouds3d-vis-range
|
|
|
|
|
2009-09-29 07:41:11 +00:00
|
|
|
attribute vec3 usrAttr1;
|
|
|
|
attribute vec3 usrAttr2;
|
2011-10-06 20:21:13 +00:00
|
|
|
attribute vec3 usrAttr3;
|
2009-09-29 07:41:11 +00:00
|
|
|
|
|
|
|
float textureIndexX = usrAttr1.r;
|
|
|
|
float textureIndexY = usrAttr1.g;
|
|
|
|
float wScale = usrAttr1.b;
|
|
|
|
float hScale = usrAttr2.r;
|
2011-10-06 20:21:13 +00:00
|
|
|
float shade_factor = usrAttr2.g;
|
2009-09-29 07:41:11 +00:00
|
|
|
float cloud_height = usrAttr2.b;
|
2011-10-13 21:39:27 +00:00
|
|
|
float bottom_factor = usrAttr3.r;
|
|
|
|
float middle_factor = usrAttr3.g;
|
|
|
|
float top_factor = usrAttr3.b;
|
2009-09-29 07:41:11 +00:00
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
|
|
|
gl_TexCoord[0] = gl_MultiTexCoord0 + vec4(textureIndexX, textureIndexY, 0.0, 0.0);
|
|
|
|
vec4 ep = gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0);
|
|
|
|
vec4 l = gl_ModelViewMatrixInverse * vec4(0.0,0.0,1.0,1.0);
|
|
|
|
vec3 u = normalize(ep.xyz - l.xyz);
|
|
|
|
|
|
|
|
// Find a rotation matrix that rotates 1,0,0 into u. u, r and w are
|
|
|
|
// the columns of that matrix.
|
|
|
|
vec3 absu = abs(u);
|
2011-07-04 18:20:26 +00:00
|
|
|
vec3 r = normalize(vec3(-u.y, u.x, 0.0));
|
2009-09-29 07:41:11 +00:00
|
|
|
vec3 w = cross(u, r);
|
|
|
|
|
|
|
|
// Do the matrix multiplication by [ u r w pos]. Assume no
|
|
|
|
// scaling in the homogeneous component of pos.
|
|
|
|
gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
|
2009-11-26 16:15:45 +00:00
|
|
|
gl_Position.xyz = gl_Vertex.x * u;
|
|
|
|
gl_Position.xyz += gl_Vertex.y * r * wScale;
|
|
|
|
gl_Position.xyz += gl_Vertex.z * w * hScale;
|
2011-08-07 12:47:17 +00:00
|
|
|
// Apply Z scaling to allow sprites to be squashed in the z-axis
|
|
|
|
gl_Position.z = gl_Position.z * gl_Color.w;
|
2009-09-29 07:41:11 +00:00
|
|
|
|
2011-10-06 20:21:13 +00:00
|
|
|
// Now shift the sprite to the correct position in the cloud.
|
|
|
|
gl_Position.xyz += gl_Color.xyz;
|
|
|
|
|
2009-09-29 07:41:11 +00:00
|
|
|
// Determine a lighting normal based on the vertex position from the
|
|
|
|
// center of the cloud, so that sprite on the opposite side of the cloud to the sun are darker.
|
2011-06-28 18:03:59 +00:00
|
|
|
float n = dot(normalize(-gl_LightSource[0].position.xyz),
|
2011-10-06 20:21:13 +00:00
|
|
|
normalize(vec3(gl_ModelViewMatrix * vec4(- gl_Position.x, - gl_Position.y, - gl_Position.z, 0.0))));
|
2009-09-29 07:41:11 +00:00
|
|
|
|
|
|
|
// Determine the position - used for fog and shading calculations
|
|
|
|
vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Position);
|
|
|
|
float fogCoord = abs(ecPosition.z);
|
2011-08-21 21:09:04 +00:00
|
|
|
|
2011-10-06 20:21:13 +00:00
|
|
|
// Determine the shading of the vertex. We shade it based on it's position
|
|
|
|
// in the cloud relative to the sun, and it's vertical position in the cloud.
|
2011-10-13 21:39:27 +00:00
|
|
|
float shade = mix(shade_factor, top_factor, smoothstep(-0.3, 0.3, n));
|
2011-10-06 20:21:13 +00:00
|
|
|
//if (n < 0) {
|
|
|
|
// shade = mix(top_factor, shade_factor, abs(n));
|
|
|
|
//}
|
|
|
|
|
2011-10-13 21:39:27 +00:00
|
|
|
if (gl_Position.z < 0.5 * cloud_height) {
|
|
|
|
shade = min(shade, mix(bottom_factor, middle_factor, gl_Position.z * 2.0 / cloud_height));
|
|
|
|
} else {
|
|
|
|
shade = min(shade, mix(middle_factor, top_factor, gl_Position.z * 2.0 / cloud_height - 1.0));
|
|
|
|
}
|
2011-10-06 20:21:13 +00:00
|
|
|
|
|
|
|
//float h = gl_Position.z / cloud_height;
|
|
|
|
//if (h < 0.5) {
|
|
|
|
// shade = min(shade, mix(bottom_factor, middle_factor, smoothstep(0.0, 0.5, h)));
|
|
|
|
//} else {
|
|
|
|
// shade = min(shade, mix(middle_factor, top_factor, smoothstep(2.0 * (h - 0.5)));
|
|
|
|
// }
|
|
|
|
|
2009-09-29 07:41:11 +00:00
|
|
|
// Final position of the sprite
|
|
|
|
gl_Position = gl_ModelViewProjectionMatrix * gl_Position;
|
2011-08-21 21:09:04 +00:00
|
|
|
|
2011-10-06 20:21:13 +00:00
|
|
|
gl_FrontColor = gl_LightSource[0].diffuse * shade + gl_FrontLightModelProduct.sceneColor;
|
2009-09-29 07:41:11 +00:00
|
|
|
|
|
|
|
// As we get within 100m of the sprite, it is faded out. Equally at large distances it also fades out.
|
2011-10-13 21:39:27 +00:00
|
|
|
gl_FrontColor.a = min(smoothstep(10.0, 100.0, fogCoord), 1.0 - smoothstep(range*0.9, range, fogCoord));
|
2009-09-29 07:41:11 +00:00
|
|
|
gl_BackColor = gl_FrontColor;
|
|
|
|
|
|
|
|
// Fog doesn't affect clouds as much as other objects.
|
|
|
|
fogFactor = exp( -gl_Fog.density * fogCoord * 0.5);
|
|
|
|
fogFactor = clamp(fogFactor, 0.0, 1.0);
|
|
|
|
}
|