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