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 | |
| 17 | #define LOG_TAG "OpenGLRenderer" |
Chris Craik | 87f9df8 | 2014-03-07 14:34:42 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_VIEW |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 19 | |
| 20 | #include <math.h> |
| 21 | #include <utils/Log.h> |
Chris Craik | 87f9df8 | 2014-03-07 14:34:42 -0800 | [diff] [blame] | 22 | #include <utils/Trace.h> |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 23 | |
| 24 | #include "AmbientShadow.h" |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 25 | #include "Caches.h" |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 26 | #include "ShadowTessellator.h" |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 27 | #include "SpotShadow.h" |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 32 | void ShadowTessellator::tessellateAmbientShadow(bool isCasterOpaque, |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 33 | const Vector3* casterPolygon, int casterVertexCount, |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 34 | const Vector3& centroid3d, const Rect& casterBounds, |
| 35 | const Rect& localClip, float maxZ, VertexBuffer& shadowVertexBuffer) { |
Chris Craik | 87f9df8 | 2014-03-07 14:34:42 -0800 | [diff] [blame] | 36 | ATRACE_CALL(); |
| 37 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 38 | // A bunch of parameters to tweak the shadow. |
| 39 | // TODO: Allow some of these changable by debug settings or APIs. |
ztenghui | 8def74d | 2014-10-01 16:10:16 -0700 | [diff] [blame] | 40 | float heightFactor = 1.0f / 128; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 41 | const float geomFactor = 64; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 42 | |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 43 | Caches& caches = Caches::getInstance(); |
| 44 | if (CC_UNLIKELY(caches.propertyAmbientRatio > 0.0f)) { |
| 45 | heightFactor *= caches.propertyAmbientRatio; |
| 46 | } |
| 47 | |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 48 | Rect ambientShadowBounds(casterBounds); |
| 49 | ambientShadowBounds.outset(maxZ * geomFactor * heightFactor); |
| 50 | |
| 51 | if (!localClip.intersects(ambientShadowBounds)) { |
| 52 | #if DEBUG_SHADOW |
| 53 | ALOGD("Ambient shadow is out of clip rect!"); |
| 54 | #endif |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 55 | return; |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 58 | AmbientShadow::createAmbientShadow(isCasterOpaque, casterPolygon, |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 59 | casterVertexCount, centroid3d, heightFactor, geomFactor, |
| 60 | shadowVertexBuffer); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 63 | void ShadowTessellator::tessellateSpotShadow(bool isCasterOpaque, |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 64 | const Vector3* casterPolygon, int casterVertexCount, const Vector3& casterCentroid, |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 65 | const mat4& receiverTransform, const Vector3& lightCenter, int lightRadius, |
| 66 | const Rect& casterBounds, const Rect& localClip, VertexBuffer& shadowVertexBuffer) { |
Chris Craik | 87f9df8 | 2014-03-07 14:34:42 -0800 | [diff] [blame] | 67 | ATRACE_CALL(); |
| 68 | |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 69 | Caches& caches = Caches::getInstance(); |
| 70 | |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 71 | Vector3 adjustedLightCenter(lightCenter); |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 72 | if (CC_UNLIKELY(caches.propertyLightPosY > 0)) { |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 73 | adjustedLightCenter.y = - caches.propertyLightPosY; // negated since this shifts up |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 74 | } |
| 75 | if (CC_UNLIKELY(caches.propertyLightPosZ > 0)) { |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 76 | adjustedLightCenter.z = caches.propertyLightPosZ; |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 77 | } |
| 78 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 79 | #if DEBUG_SHADOW |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 80 | ALOGD("light center %f %f %f", |
| 81 | adjustedLightCenter.x, adjustedLightCenter.y, adjustedLightCenter.z); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 82 | #endif |
Chris Craik | 3197cde | 2014-01-16 14:03:39 -0800 | [diff] [blame] | 83 | |
| 84 | // light position (because it's in local space) needs to compensate for receiver transform |
| 85 | // TODO: should apply to light orientation, not just position |
| 86 | Matrix4 reverseReceiverTransform; |
| 87 | reverseReceiverTransform.loadInverse(receiverTransform); |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 88 | reverseReceiverTransform.mapPoint3d(adjustedLightCenter); |
Chris Craik | 3197cde | 2014-01-16 14:03:39 -0800 | [diff] [blame] | 89 | |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 90 | if (CC_UNLIKELY(caches.propertyLightDiameter > 0)) { |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 91 | lightRadius = caches.propertyLightDiameter; |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame] | 92 | } |
| 93 | |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 94 | // Now light and caster are both in local space, we will check whether |
| 95 | // the shadow is within the clip area. |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 96 | Rect lightRect = Rect(adjustedLightCenter.x - lightRadius, adjustedLightCenter.y - lightRadius, |
| 97 | adjustedLightCenter.x + lightRadius, adjustedLightCenter.y + lightRadius); |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 98 | lightRect.unionWith(localClip); |
| 99 | if (!lightRect.intersects(casterBounds)) { |
| 100 | #if DEBUG_SHADOW |
| 101 | ALOGD("Spot shadow is out of clip rect!"); |
| 102 | #endif |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 103 | return; |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 104 | } |
| 105 | |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 106 | SpotShadow::createSpotShadow(isCasterOpaque, adjustedLightCenter, lightRadius, |
| 107 | casterPolygon, casterVertexCount, casterCentroid, shadowVertexBuffer); |
| 108 | |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 109 | #if DEBUG_SHADOW |
| 110 | if(shadowVertexBuffer.getVertexCount() <= 0) { |
| 111 | ALOGD("Spot shadow generation failed %d", shadowVertexBuffer.getVertexCount()); |
| 112 | } |
| 113 | #endif |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 114 | } |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 115 | |
| 116 | void ShadowTessellator::generateShadowIndices(uint16_t* shadowIndices) { |
| 117 | int currentIndex = 0; |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 118 | const int rays = SHADOW_RAY_COUNT; |
| 119 | // For the penumbra area. |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 120 | for (int layer = 0; layer < 2; layer ++) { |
| 121 | int baseIndex = layer * rays; |
| 122 | for (int i = 0; i < rays; i++) { |
| 123 | shadowIndices[currentIndex++] = i + baseIndex; |
| 124 | shadowIndices[currentIndex++] = rays + i + baseIndex; |
| 125 | } |
| 126 | // To close the loop, back to the ray 0. |
| 127 | shadowIndices[currentIndex++] = 0 + baseIndex; |
| 128 | // Note this is the same as the first index of next layer loop. |
| 129 | shadowIndices[currentIndex++] = rays + baseIndex; |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 130 | } |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 131 | |
| 132 | #if DEBUG_SHADOW |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 133 | if (currentIndex != MAX_SHADOW_INDEX_COUNT) { |
| 134 | ALOGW("vertex index count is wrong. current %d, expected %d", |
| 135 | currentIndex, MAX_SHADOW_INDEX_COUNT); |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 136 | } |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 137 | for (int i = 0; i < MAX_SHADOW_INDEX_COUNT; i++) { |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 138 | ALOGD("vertex index is (%d, %d)", i, shadowIndices[i]); |
| 139 | } |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Calculate the centroid of a 2d polygon. |
| 145 | * |
| 146 | * @param poly The polygon, which is represented in a Vector2 array. |
| 147 | * @param polyLength The length of the polygon in terms of number of vertices. |
| 148 | * @return the centroid of the polygon. |
| 149 | */ |
| 150 | Vector2 ShadowTessellator::centroid2d(const Vector2* poly, int polyLength) { |
| 151 | double sumx = 0; |
| 152 | double sumy = 0; |
| 153 | int p1 = polyLength - 1; |
| 154 | double area = 0; |
| 155 | for (int p2 = 0; p2 < polyLength; p2++) { |
| 156 | double x1 = poly[p1].x; |
| 157 | double y1 = poly[p1].y; |
| 158 | double x2 = poly[p2].x; |
| 159 | double y2 = poly[p2].y; |
| 160 | double a = (x1 * y2 - x2 * y1); |
| 161 | sumx += (x1 + x2) * a; |
| 162 | sumy += (y1 + y2) * a; |
| 163 | area += a; |
| 164 | p1 = p2; |
| 165 | } |
| 166 | |
| 167 | Vector2 centroid = poly[0]; |
| 168 | if (area != 0) { |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 169 | centroid = (Vector2){static_cast<float>(sumx / (3 * area)), |
| 170 | static_cast<float>(sumy / (3 * area))}; |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 171 | } else { |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 172 | ALOGW("Area is 0 while computing centroid!"); |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 173 | } |
| 174 | return centroid; |
| 175 | } |
| 176 | |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 177 | // Make sure p1 -> p2 is going CW around the poly. |
| 178 | Vector2 ShadowTessellator::calculateNormal(const Vector2& p1, const Vector2& p2) { |
| 179 | Vector2 result = p2 - p1; |
| 180 | if (result.x != 0 || result.y != 0) { |
| 181 | result.normalize(); |
| 182 | // Calculate the normal , which is CCW 90 rotate to the delta. |
| 183 | float tempy = result.y; |
| 184 | result.y = result.x; |
| 185 | result.x = -tempy; |
| 186 | } |
| 187 | return result; |
| 188 | } |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 189 | /** |
| 190 | * Test whether the polygon is order in clockwise. |
| 191 | * |
| 192 | * @param polygon the polygon as a Vector2 array |
| 193 | * @param len the number of points of the polygon |
| 194 | */ |
| 195 | bool ShadowTessellator::isClockwise(const Vector2* polygon, int len) { |
ztenghui | f11310f | 2014-05-14 13:48:27 -0700 | [diff] [blame] | 196 | if (len < 2 || polygon == NULL) { |
ztenghui | f11310f | 2014-05-14 13:48:27 -0700 | [diff] [blame] | 197 | return true; |
| 198 | } |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 199 | double sum = 0; |
| 200 | double p1x = polygon[len - 1].x; |
| 201 | double p1y = polygon[len - 1].y; |
| 202 | for (int i = 0; i < len; i++) { |
| 203 | |
| 204 | double p2x = polygon[i].x; |
| 205 | double p2y = polygon[i].y; |
| 206 | sum += p1x * p2y - p2x * p1y; |
| 207 | p1x = p2x; |
| 208 | p1y = p2y; |
| 209 | } |
| 210 | return sum < 0; |
| 211 | } |
| 212 | |
| 213 | bool ShadowTessellator::isClockwisePath(const SkPath& path) { |
| 214 | SkPath::Iter iter(path, false); |
| 215 | SkPoint pts[4]; |
| 216 | SkPath::Verb v; |
| 217 | |
| 218 | Vector<Vector2> arrayForDirection; |
| 219 | while (SkPath::kDone_Verb != (v = iter.next(pts))) { |
| 220 | switch (v) { |
| 221 | case SkPath::kMove_Verb: |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 222 | arrayForDirection.add((Vector2){pts[0].x(), pts[0].y()}); |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 223 | break; |
| 224 | case SkPath::kLine_Verb: |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 225 | arrayForDirection.add((Vector2){pts[1].x(), pts[1].y()}); |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 226 | break; |
| 227 | case SkPath::kQuad_Verb: |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 228 | arrayForDirection.add((Vector2){pts[1].x(), pts[1].y()}); |
| 229 | arrayForDirection.add((Vector2){pts[2].x(), pts[2].y()}); |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 230 | break; |
| 231 | case SkPath::kCubic_Verb: |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 232 | arrayForDirection.add((Vector2){pts[1].x(), pts[1].y()}); |
| 233 | arrayForDirection.add((Vector2){pts[2].x(), pts[2].y()}); |
| 234 | arrayForDirection.add((Vector2){pts[3].x(), pts[3].y()}); |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 235 | break; |
| 236 | default: |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return isClockwise(arrayForDirection.array(), arrayForDirection.size()); |
| 242 | } |
| 243 | |
| 244 | void ShadowTessellator::reverseVertexArray(Vertex* polygon, int len) { |
| 245 | int n = len / 2; |
| 246 | for (int i = 0; i < n; i++) { |
| 247 | Vertex tmp = polygon[i]; |
| 248 | int k = len - 1 - i; |
| 249 | polygon[i] = polygon[k]; |
| 250 | polygon[k] = tmp; |
| 251 | } |
| 252 | } |
| 253 | |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 254 | int ShadowTessellator::getExtraVertexNumber(const Vector2& vector1, |
| 255 | const Vector2& vector2, float divisor) { |
| 256 | // When there is no distance difference, there is no need for extra vertices. |
| 257 | if (vector1.lengthSquared() == 0 || vector2.lengthSquared() == 0) { |
| 258 | return 0; |
| 259 | } |
| 260 | // The formula is : |
| 261 | // extraNumber = floor(acos(dot(n1, n2)) / (M_PI / EXTRA_VERTEX_PER_PI)) |
| 262 | // The value ranges for each step are: |
| 263 | // dot( ) --- [-1, 1] |
| 264 | // acos( ) --- [0, M_PI] |
| 265 | // floor(...) --- [0, EXTRA_VERTEX_PER_PI] |
| 266 | float dotProduct = vector1.dot(vector2); |
| 267 | // TODO: Use look up table for the dotProduct to extraVerticesNumber |
| 268 | // computation, if needed. |
| 269 | float angle = acosf(dotProduct); |
| 270 | return (int) floor(angle / divisor); |
| 271 | } |
| 272 | |
| 273 | void ShadowTessellator::checkOverflow(int used, int total, const char* bufferName) { |
| 274 | LOG_ALWAYS_FATAL_IF(used > total, "Error: %s overflow!!! used %d, total %d", |
| 275 | bufferName, used, total); |
| 276 | } |
| 277 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 278 | }; // namespace uirenderer |
| 279 | }; // namespace android |