|
1 | 1 | /* |
2 | 2 | =========================================================================== |
3 | | -Copyright (C) 1999-2005 Id Software, Inc. |
4 | | -Copyright (C) 2006-2011 Robert Beckebans <trebor_7@users.sourceforge.net> |
5 | 3 |
|
6 | | -This file is part of Daemon source code. |
| 4 | +Daemon BSD Source Code |
| 5 | +Copyright (c) 2025 Daemon Developers |
| 6 | +All rights reserved. |
| 7 | +
|
| 8 | +This file is part of the Daemon BSD Source Code (Daemon Source Code). |
| 9 | +
|
| 10 | +Redistribution and use in source and binary forms, with or without |
| 11 | +modification, are permitted provided that the following conditions are met: |
| 12 | + * Redistributions of source code must retain the above copyright |
| 13 | + notice, this list of conditions and the following disclaimer. |
| 14 | + * Redistributions in binary form must reproduce the above copyright |
| 15 | + notice, this list of conditions and the following disclaimer in the |
| 16 | + documentation and/or other materials provided with the distribution. |
| 17 | + * Neither the name of the Daemon developers nor the |
| 18 | + names of its contributors may be used to endorse or promote products |
| 19 | + derived from this software without specific prior written permission. |
| 20 | +
|
| 21 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 22 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | +DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY |
| 25 | +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 26 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 27 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 28 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
7 | 31 |
|
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 | 32 | =========================================================================== |
22 | 33 | */ |
23 | 34 |
|
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 | 35 | float GetFogAlpha(float s, float t) |
59 | 36 | { |
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; |
| 37 | + t = clamp(t, 0.0, 1.0); |
65 | 38 |
|
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; |
| 39 | + float x = min(1, s * t); |
71 | 40 |
|
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); |
| 41 | + // sqrt(x) is bad near 0 because it increases too quickly resulting in sharp edges. |
| 42 | + // x ≤ 1/32: √32 * x |
| 43 | + // x ≥ 1/32: √x |
| 44 | + return min(sqrt(32.0) * x, sqrt(x)); |
77 | 45 | } |
0 commit comments