| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 namespace blink { | 36 namespace blink { |
| 37 | 37 |
| 38 DistanceEffect::DistanceEffect() | 38 DistanceEffect::DistanceEffect() |
| 39 : model_(kModelInverse), | 39 : model_(kModelInverse), |
| 40 ref_distance_(1.0), | 40 ref_distance_(1.0), |
| 41 max_distance_(10000.0), | 41 max_distance_(10000.0), |
| 42 rolloff_factor_(1.0) {} | 42 rolloff_factor_(1.0) {} |
| 43 | 43 |
| 44 double DistanceEffect::Gain(double distance) { | 44 double DistanceEffect::Gain(double distance) { |
| 45 // Don't get closer than the reference distance or go beyond the maximum | |
| 46 // distance. | |
| 47 distance = clampTo(distance, ref_distance_, max_distance_); | |
| 48 | |
| 49 switch (model_) { | 45 switch (model_) { |
| 50 case kModelLinear: | 46 case kModelLinear: |
| 51 return LinearGain(distance); | 47 return LinearGain(distance); |
| 52 case kModelInverse: | 48 case kModelInverse: |
| 53 return InverseGain(distance); | 49 return InverseGain(distance); |
| 54 case kModelExponential: | 50 case kModelExponential: |
| 55 return ExponentialGain(distance); | 51 return ExponentialGain(distance); |
| 56 } | 52 } |
| 57 NOTREACHED(); | 53 NOTREACHED(); |
| 58 return 0.0; | 54 return 0.0; |
| 59 } | 55 } |
| 60 | 56 |
| 61 double DistanceEffect::LinearGain(double distance) { | 57 double DistanceEffect::LinearGain(double distance) { |
| 58 // Clamp refDistance and distance according to the spec. |
| 59 double dref = std::min(ref_distance_, max_distance_); |
| 60 double dmax = std::max(ref_distance_, max_distance_); |
| 61 distance = clampTo(distance, dref, dmax); |
| 62 |
| 63 if (dref == dmax) |
| 64 return 1 - rolloff_factor_; |
| 65 |
| 62 // We want a gain that decreases linearly from m_refDistance to | 66 // We want a gain that decreases linearly from m_refDistance to |
| 63 // m_maxDistance. The gain is 1 at m_refDistance. | 67 // m_maxDistance. The gain is 1 at m_refDistance. |
| 64 return (1.0 - clampTo(rolloff_factor_, 0.0, 1.0) * | 68 return (1.0 - clampTo(rolloff_factor_, 0.0, 1.0) * (distance - dref) / |
| 65 (distance - ref_distance_) / | 69 (dmax - dref)); |
| 66 (max_distance_ - ref_distance_)); | |
| 67 } | 70 } |
| 68 | 71 |
| 69 double DistanceEffect::InverseGain(double distance) { | 72 double DistanceEffect::InverseGain(double distance) { |
| 70 if (ref_distance_ == 0) | 73 if (ref_distance_ == 0) |
| 71 return 0; | 74 return 0; |
| 72 | 75 |
| 76 // Clamp distance according to spec |
| 77 distance = clampTo(distance, ref_distance_); |
| 78 |
| 73 return ref_distance_ / (ref_distance_ + clampTo(rolloff_factor_, 0.0) * | 79 return ref_distance_ / (ref_distance_ + clampTo(rolloff_factor_, 0.0) * |
| 74 (distance - ref_distance_)); | 80 (distance - ref_distance_)); |
| 75 } | 81 } |
| 76 | 82 |
| 77 double DistanceEffect::ExponentialGain(double distance) { | 83 double DistanceEffect::ExponentialGain(double distance) { |
| 78 if (ref_distance_ == 0) | 84 if (ref_distance_ == 0) |
| 79 return 0; | 85 return 0; |
| 80 | 86 |
| 87 // Clamp distance according to spec |
| 88 distance = clampTo(distance, ref_distance_); |
| 89 |
| 81 return pow(distance / ref_distance_, -clampTo(rolloff_factor_, 0.0)); | 90 return pow(distance / ref_distance_, -clampTo(rolloff_factor_, 0.0)); |
| 82 } | 91 } |
| 83 | 92 |
| 84 } // namespace blink | 93 } // namespace blink |
| OLD | NEW |