Allolib  1.0
C++ Components For Interactive Multimedia
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
al_DistAtten.hpp
1 #ifndef INCLUDE_AL_DIST_ATTEN_HPP
2 #define INCLUDE_AL_DIST_ATTEN_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology, UCSB.
8  Copyright (C) 2012. The Regents of the University of California.
9  All rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13 
14  Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16 
17  Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 
21  Neither the name of the University of California nor the names of its
22  contributors may be used to endorse or promote products derived from
23  this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36 
37 
38  File description:
39  Distance attentuation functions for calculating sound pressure levels,
40  fog density, etc. within a space.
41 
42  File author(s):
43  Lance Putnam, 2015, putnam.lance@gmail.com
44 */
45 
46 #include <math.h>
47 
48 namespace al {
49 
52  ATTEN_NONE = 0,
56 };
57 
61 template <class T = float>
62 class DistAtten {
63  public:
68  DistAtten(T nearClip = T(0.1), T farClip = T(20),
70  : mNear(nearClip), mFar(farClip), mFarBias(farBias), mLaw(law) {
71  setScale();
72  }
73 
75  T nearClip() const { return mNear; }
76 
78  T farClip() const { return mFar; }
79 
81  T farBias() const { return mFarBias; }
82 
84  AttenuationLaw law() const { return mLaw; }
85 
88  mLaw = v;
89  return setScale();
90  }
91 
94  mNear = v;
95  return setScale();
96  }
97 
100  mFar = v;
101  return setScale();
102  }
103 
106  mFarBias = v;
107  return setScale();
108  }
109 
111  T attenuation(T dist) const {
112  // No attenuation if below near distance
113  if (dist <= mNear) return T(1);
114 
115  switch (mLaw) {
116  case ATTEN_LINEAR:
117  return dist < mFar ? T(1) - mScale * (dist - mNear) : mFarBias;
118 
119  case ATTEN_INVERSE:
120  return mNear / (mNear + mScale * (dist - mNear));
121 
122  case ATTEN_INVERSE_SQUARE: {
123  T nearSqr = mNear * mNear;
124  return nearSqr / (nearSqr + mScale * (dist * dist - nearSqr));
125  }
126 
127  default:
128  return T(1);
129  }
130  }
131 
132  protected:
133  T mNear, mFar; // clipping planes
134  T mFarBias; // bias on far clip (linear model only)
135  T mScale;
136  AttenuationLaw mLaw;
137 
138  DistAtten& setScale() {
139  switch (mLaw) {
140  case ATTEN_LINEAR:
141  mScale = (T(1) - mFarBias) / (mFar - mNear);
142  break;
143 
144  // Note: For inverse laws, the attenuation factor at far clip is
145  // hard-coded. For INVERSE_SQUARE, it is the square of the value for
146  // INVERSE. This ensures a correct inverse power relationship.
147  case ATTEN_INVERSE:
148  mScale = (mNear / T(0.25) - mNear) / (mFar - mNear);
149  break;
150  case ATTEN_INVERSE_SQUARE: {
151  T nearSqr = mNear * mNear;
152  mScale = (nearSqr / T(0.25 * 0.25) - nearSqr) / (mFar * mFar - nearSqr);
153  } break;
154  default:
155  mScale = 1;
156  }
157  return *this;
158  }
159 };
160 
161 /* Other possible attenuation laws where
162 
163  dN = (distance-mNearClip) / (mClipRange);
164 
165 max/cosm:
166  curve = (1.-dN)*(1.-dN);
167 hydrogen bond:
168  curve = ((d + C) / (d*d + d + C))^2; // e.g. C=2
169 sigmoid:
170  curve = 1 - tanh(M_PI * dN*dN);
171 
172 biasing:
173  mAmpFar + curve*(1.-mAmpFar)
174 */
175 
176 } // namespace al
177 
178 #endif
T farClip() const
Get far clip distance.
DistAtten & law(AttenuationLaw v)
Set attenuation law.
DistAtten & farBias(T v)
Set bias at far clip distance (linear model only)
T nearClip() const
Get near clip distance.
T attenuation(T dist) const
Get attenuation factor for a given distance.
DistAtten(T nearClip=T(0.1), T farClip=T(20), AttenuationLaw law=ATTEN_INVERSE, T farBias=T(0))
AttenuationLaw law() const
Get attenuation law.
DistAtten & nearClip(T v)
Set near clip distance.
T farBias() const
Get far bias.
DistAtten & farClip(T v)
Set far clip distance.
Definition: al_App.hpp:23
AttenuationLaw
Distance to attenuation laws.
@ ATTEN_INVERSE
@ ATTEN_INVERSE_SQUARE
@ ATTEN_NONE
@ ATTEN_LINEAR
T dist(const Vec< N, T > &a, const Vec< N, U > &b)
Returns distance between two vectors.
Definition: al_Vec.hpp:676