ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 17 | #include <math.h> |
| 18 | #include <utils/Log.h> |
Lazar Trsic | 1240752 | 2015-06-23 11:46:49 +0200 | [diff] [blame] | 19 | #include <utils/MathUtils.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 20 | #include <utils/Trace.h> |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 21 | |
| 22 | #include "AmbientShadow.h" |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 23 | #include "Properties.h" |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 24 | #include "ShadowTessellator.h" |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 25 | #include "SpotShadow.h" |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 26 | #include "Vector.h" |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 31 | void ShadowTessellator::tessellateAmbientShadow(bool isCasterOpaque, const Vector3* casterPolygon, |
| 32 | int casterVertexCount, const Vector3& centroid3d, |
| 33 | const Rect& casterBounds, const Rect& localClip, |
| 34 | float maxZ, VertexBuffer& shadowVertexBuffer) { |
Chris Craik | 87f9df8 | 2014-03-07 14:34:42 -0800 | [diff] [blame] | 35 | ATRACE_CALL(); |
| 36 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 37 | // A bunch of parameters to tweak the shadow. |
| 38 | // TODO: Allow some of these changable by debug settings or APIs. |
ztenghui | 8def74d | 2014-10-01 16:10:16 -0700 | [diff] [blame] | 39 | float heightFactor = 1.0f / 128; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 40 | const float geomFactor = 64; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 41 | |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 42 | if (CC_UNLIKELY(Properties::overrideAmbientRatio > 0.0f)) { |
| 43 | heightFactor *= Properties::overrideAmbientRatio; |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 44 | } |
| 45 | |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 46 | Rect ambientShadowBounds(casterBounds); |
| 47 | ambientShadowBounds.outset(maxZ * geomFactor * heightFactor); |
| 48 | |
| 49 | if (!localClip.intersects(ambientShadowBounds)) { |
| 50 | #if DEBUG_SHADOW |
| 51 | ALOGD("Ambient shadow is out of clip rect!"); |
| 52 | #endif |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 53 | return; |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 54 | } |
| 55 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 56 | AmbientShadow::createAmbientShadow(isCasterOpaque, casterPolygon, casterVertexCount, centroid3d, |
| 57 | heightFactor, geomFactor, shadowVertexBuffer); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 58 | } |
| 59 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 60 | void ShadowTessellator::tessellateSpotShadow(bool isCasterOpaque, const Vector3* casterPolygon, |
| 61 | int casterVertexCount, const Vector3& casterCentroid, |
| 62 | const mat4& receiverTransform, |
| 63 | const Vector3& lightCenter, int lightRadius, |
| 64 | const Rect& casterBounds, const Rect& localClip, |
| 65 | VertexBuffer& shadowVertexBuffer) { |
Chris Craik | 87f9df8 | 2014-03-07 14:34:42 -0800 | [diff] [blame] | 66 | ATRACE_CALL(); |
| 67 | |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 68 | Vector3 adjustedLightCenter(lightCenter); |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 69 | if (CC_UNLIKELY(Properties::overrideLightPosY > 0)) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 70 | adjustedLightCenter.y = -Properties::overrideLightPosY; // negated since this shifts up |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 71 | } |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 72 | if (CC_UNLIKELY(Properties::overrideLightPosZ > 0)) { |
| 73 | adjustedLightCenter.z = Properties::overrideLightPosZ; |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 74 | } |
| 75 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 76 | #if DEBUG_SHADOW |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 77 | ALOGD("light center %f %f %f %d", adjustedLightCenter.x, adjustedLightCenter.y, |
| 78 | adjustedLightCenter.z, lightRadius); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 79 | #endif |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 80 | if (isnan(adjustedLightCenter.x) || isnan(adjustedLightCenter.y) || |
| 81 | isnan(adjustedLightCenter.z)) { |
Teng-Hui Zhu | cf22d18 | 2015-11-04 16:57:53 -0800 | [diff] [blame] | 82 | return; |
| 83 | } |
Chris Craik | 3197cde | 2014-01-16 14:03:39 -0800 | [diff] [blame] | 84 | |
| 85 | // light position (because it's in local space) needs to compensate for receiver transform |
| 86 | // TODO: should apply to light orientation, not just position |
| 87 | Matrix4 reverseReceiverTransform; |
| 88 | reverseReceiverTransform.loadInverse(receiverTransform); |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 89 | reverseReceiverTransform.mapPoint3d(adjustedLightCenter); |
Chris Craik | 3197cde | 2014-01-16 14:03:39 -0800 | [diff] [blame] | 90 | |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 91 | if (CC_UNLIKELY(Properties::overrideLightRadius > 0)) { |
| 92 | lightRadius = Properties::overrideLightRadius; |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 93 | } |
| 94 | |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 95 | // Now light and caster are both in local space, we will check whether |
| 96 | // the shadow is within the clip area. |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 97 | Rect lightRect = Rect(adjustedLightCenter.x - lightRadius, adjustedLightCenter.y - lightRadius, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 98 | adjustedLightCenter.x + lightRadius, adjustedLightCenter.y + lightRadius); |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 99 | lightRect.unionWith(localClip); |
| 100 | if (!lightRect.intersects(casterBounds)) { |
| 101 | #if DEBUG_SHADOW |
| 102 | ALOGD("Spot shadow is out of clip rect!"); |
| 103 | #endif |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 104 | return; |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 105 | } |
| 106 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 107 | SpotShadow::createSpotShadow(isCasterOpaque, adjustedLightCenter, lightRadius, casterPolygon, |
| 108 | casterVertexCount, casterCentroid, shadowVertexBuffer); |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 109 | |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 110 | #if DEBUG_SHADOW |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 111 | if (shadowVertexBuffer.getVertexCount() <= 0) { |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 112 | ALOGD("Spot shadow generation failed %d", shadowVertexBuffer.getVertexCount()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 113 | } |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 114 | #endif |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 115 | } |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 116 | |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 117 | /** |
| 118 | * Calculate the centroid of a 2d polygon. |
| 119 | * |
| 120 | * @param poly The polygon, which is represented in a Vector2 array. |
| 121 | * @param polyLength The length of the polygon in terms of number of vertices. |
| 122 | * @return the centroid of the polygon. |
| 123 | */ |
| 124 | Vector2 ShadowTessellator::centroid2d(const Vector2* poly, int polyLength) { |
| 125 | double sumx = 0; |
| 126 | double sumy = 0; |
| 127 | int p1 = polyLength - 1; |
| 128 | double area = 0; |
| 129 | for (int p2 = 0; p2 < polyLength; p2++) { |
| 130 | double x1 = poly[p1].x; |
| 131 | double y1 = poly[p1].y; |
| 132 | double x2 = poly[p2].x; |
| 133 | double y2 = poly[p2].y; |
| 134 | double a = (x1 * y2 - x2 * y1); |
| 135 | sumx += (x1 + x2) * a; |
| 136 | sumy += (y1 + y2) * a; |
| 137 | area += a; |
| 138 | p1 = p2; |
| 139 | } |
| 140 | |
| 141 | Vector2 centroid = poly[0]; |
| 142 | if (area != 0) { |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 143 | centroid = (Vector2){static_cast<float>(sumx / (3 * area)), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 144 | static_cast<float>(sumy / (3 * area))}; |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 145 | } else { |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 146 | ALOGW("Area is 0 while computing centroid!"); |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 147 | } |
| 148 | return centroid; |
| 149 | } |
| 150 | |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 151 | // Make sure p1 -> p2 is going CW around the poly. |
| 152 | Vector2 ShadowTessellator::calculateNormal(const Vector2& p1, const Vector2& p2) { |
| 153 | Vector2 result = p2 - p1; |
| 154 | if (result.x != 0 || result.y != 0) { |
| 155 | result.normalize(); |
| 156 | // Calculate the normal , which is CCW 90 rotate to the delta. |
| 157 | float tempy = result.y; |
| 158 | result.y = result.x; |
| 159 | result.x = -tempy; |
| 160 | } |
| 161 | return result; |
| 162 | } |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 163 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 164 | int ShadowTessellator::getExtraVertexNumber(const Vector2& vector1, const Vector2& vector2, |
| 165 | float divisor) { |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 166 | // When there is no distance difference, there is no need for extra vertices. |
| 167 | if (vector1.lengthSquared() == 0 || vector2.lengthSquared() == 0) { |
| 168 | return 0; |
| 169 | } |
| 170 | // The formula is : |
| 171 | // extraNumber = floor(acos(dot(n1, n2)) / (M_PI / EXTRA_VERTEX_PER_PI)) |
| 172 | // The value ranges for each step are: |
| 173 | // dot( ) --- [-1, 1] |
| 174 | // acos( ) --- [0, M_PI] |
| 175 | // floor(...) --- [0, EXTRA_VERTEX_PER_PI] |
| 176 | float dotProduct = vector1.dot(vector2); |
Lazar Trsic | 1240752 | 2015-06-23 11:46:49 +0200 | [diff] [blame] | 177 | // make sure that dotProduct value is in acsof input range [-1, 1] |
| 178 | dotProduct = MathUtils::clamp(dotProduct, -1.0f, 1.0f); |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 179 | // TODO: Use look up table for the dotProduct to extraVerticesNumber |
| 180 | // computation, if needed. |
| 181 | float angle = acosf(dotProduct); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 182 | return (int)floor(angle / divisor); |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void ShadowTessellator::checkOverflow(int used, int total, const char* bufferName) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 186 | LOG_ALWAYS_FATAL_IF(used > total, "Error: %s overflow!!! used %d, total %d", bufferName, used, |
| 187 | total); |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 188 | } |
| 189 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 190 | }; // namespace uirenderer |
| 191 | }; // namespace android |