/* oolite-dust.vertex Vertex shader for interplanetary dust. © 2010-2013 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. */ /* 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)) #define OODUST_SIZE (float(2000)) */ /* “Warpiness”: 0 for dots when not warping and the near end of lines when warping, 1 for the far end of lines when warping. (Note that both “ends” have the same input coordinates, and the shader applies the warp effect.) */ attribute float aWarpiness; uniform vec4 uWarp; // Player position, offset by -OODUST_SIZE/2 on each coordinate. uniform vec4 uOffsetPlayerPosition; varying vec4 vColor; void main(void) { /* Dust particles are arranged in a cube of 2000×2000×2000 metres (OO_DUST_SIZE) around the universe origin. The following code offsets each particle to be somewhere in a cube of the same size around the player, creating an infinite repeating pattern. */ vec4 position = mod(gl_Vertex - uOffsetPlayerPosition, OODUST_SIZE) + uOffsetPlayerPosition; position = position - uWarp * aWarpiness; // Project the dust, and find its distance from the camera. position.w = 1.0; position = gl_ModelViewProjectionMatrix * position; gl_Position = position; float distance = length(position); /* 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); }