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