|
| 1 | +/* |
| 2 | +=========================================================================== |
| 3 | +Copyright (C) 1999-2005 Id Software, Inc. |
| 4 | +Copyright (C) 2006-2011 Robert Beckebans <trebor_7@users.sourceforge.net> |
| 5 | +
|
| 6 | +This file is part of Daemon source code. |
| 7 | +
|
| 8 | +Daemon source code is free software; you can redistribute it |
| 9 | +and/or modify it under the terms of the GNU General Public License as |
| 10 | +published by the Free Software Foundation; either version 2 of the License, |
| 11 | +or (at your option) any later version. |
| 12 | +
|
| 13 | +Daemon source code is distributed in the hope that it will be |
| 14 | +useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +GNU General Public License for more details. |
| 17 | +
|
| 18 | +You should have received a copy of the GNU General Public License |
| 19 | +along with Daemon source code; if not, write to the Free Software |
| 20 | +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | +=========================================================================== |
| 22 | +*/ |
| 23 | + |
| 24 | +float FogTable(float s) |
| 25 | +{ |
| 26 | + return sqrt(floor(s * 255.0) / 255); |
| 27 | +} |
| 28 | + |
| 29 | +float FogFactor(float s, float t) |
| 30 | +{ |
| 31 | + s -= 1.0f / 512.0f; |
| 32 | + |
| 33 | + if ( s < 0 ) |
| 34 | + { |
| 35 | + return 0; |
| 36 | + } |
| 37 | + |
| 38 | + if ( t < 1.0f / 32.0f ) |
| 39 | + { |
| 40 | + return 0; |
| 41 | + } |
| 42 | + |
| 43 | + if ( t < 31.0f / 32.0f ) |
| 44 | + { |
| 45 | + s *= ( t - 1.0f / 32.0f ) / ( 30.0f / 32.0f ); |
| 46 | + } |
| 47 | + |
| 48 | + s *= 8; |
| 49 | + |
| 50 | + if ( s > 1.0f ) |
| 51 | + { |
| 52 | + s = 1.0f; |
| 53 | + } |
| 54 | + |
| 55 | + return FogTable(s); |
| 56 | +} |
| 57 | + |
| 58 | +float GetFogAlpha(float s, float t) |
| 59 | +{ |
| 60 | + float sfloor = floor(s * 256 + 0.5) - 0.5; |
| 61 | + float sceil = sfloor + 1; |
| 62 | + sfloor = clamp(sfloor, 0.5, 255.5) / 256; |
| 63 | + sceil = clamp(sceil, 0.5, 255.5) / 256; |
| 64 | + float smix = sfloor < sceil ? (s - sfloor) * 256 : 0.5; |
| 65 | + |
| 66 | + float tfloor = floor(t * 32 + 0.5) - 0.5; |
| 67 | + float tceil = tfloor + 1; |
| 68 | + tfloor = clamp(tfloor, 0.5, 31.5) / 32; |
| 69 | + tceil = clamp(tceil, 0.5, 31.5) / 32; |
| 70 | + float tmix = tfloor < tceil ? (t - tfloor) * 32 : 0.5; |
| 71 | + |
| 72 | + float f00 = FogFactor(sfloor, tfloor); |
| 73 | + float f01 = FogFactor(sfloor, tceil); |
| 74 | + float f10 = FogFactor(sceil, tfloor); |
| 75 | + float f11 = FogFactor(sceil, tceil); |
| 76 | + return mix(mix(f00, f01, tmix), mix(f10, f11, tmix), smix); |
| 77 | +} |
0 commit comments