oolite/Resources/Shaders/oolite-dust.vertex

53 lines
1.7 KiB
Plaintext

/*
oolite-dust.vertex
Vertex shader for interplanetary dust.
© 2010 Jens Ayton
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
varying vec4 vColor;
/* The following macros are provided by Oolite, to ensure that they stay in
sync with the values used in non-shader mode.
#define OODUST_SCALE_MAX (float(2))
#define OODUST_SCALE_FACTOR (float(0.002))
*/
void main(void)
{
float distance = length(gl_ModelViewMatrix * gl_Vertex);
/* The original non-shader code fades to black using linear fog, with a
near plane of 500 and a far plane of 1000.
OODUST_SCALE_MAX is far plane/near plane, and OODUST_SCALE_FACTOR
is 1.0/(far plane - near plane).
This gives us a function such that f(x) ≥ 1 when x ≤ near plane,
f(x) ≤ 0 when x ≥ far plane, and a linear ramp in between, which we
then clamp to [0..1].
*/
float alpha = clamp(OODUST_SCALE_MAX - distance * OODUST_SCALE_FACTOR, 0.0, 1.0);
vColor = vec4(gl_Color.rgb, alpha * gl_Color.a);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}