ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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" |
| 18 | |
| 19 | #define SHADOW_SHRINK_SCALE 0.1f |
| 20 | |
| 21 | #include <math.h> |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 25 | #include "ShadowTessellator.h" |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 26 | #include "SpotShadow.h" |
| 27 | #include "Vertex.h" |
| 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 32 | static const double EPSILON = 1e-7; |
| 33 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 34 | /** |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 35 | * Calculate the angle between and x and a y coordinate. |
| 36 | * The atan2 range from -PI to PI. |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 37 | */ |
Chris Craik | b79a3e3 | 2014-03-11 12:20:17 -0700 | [diff] [blame] | 38 | static float angle(const Vector2& point, const Vector2& center) { |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 39 | return atan2(point.y - center.y, point.x - center.x); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Calculate the intersection of a ray with the line segment defined by two points. |
| 44 | * |
| 45 | * Returns a negative value in error conditions. |
| 46 | |
| 47 | * @param rayOrigin The start of the ray |
| 48 | * @param dx The x vector of the ray |
| 49 | * @param dy The y vector of the ray |
| 50 | * @param p1 The first point defining the line segment |
| 51 | * @param p2 The second point defining the line segment |
| 52 | * @return The distance along the ray if it intersects with the line segment, negative if otherwise |
| 53 | */ |
Chris Craik | b79a3e3 | 2014-03-11 12:20:17 -0700 | [diff] [blame] | 54 | static float rayIntersectPoints(const Vector2& rayOrigin, float dx, float dy, |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 55 | const Vector2& p1, const Vector2& p2) { |
| 56 | // The math below is derived from solving this formula, basically the |
| 57 | // intersection point should stay on both the ray and the edge of (p1, p2). |
| 58 | // solve([p1x+t*(p2x-p1x)=dx*t2+px,p1y+t*(p2y-p1y)=dy*t2+py],[t,t2]); |
| 59 | |
| 60 | double divisor = (dx * (p1.y - p2.y) + dy * p2.x - dy * p1.x); |
| 61 | if (divisor == 0) return -1.0f; // error, invalid divisor |
| 62 | |
| 63 | #if DEBUG_SHADOW |
| 64 | double interpVal = (dx * (p1.y - rayOrigin.y) + dy * rayOrigin.x - dy * p1.x) / divisor; |
ztenghui | 99af942 | 2014-03-14 14:35:54 -0700 | [diff] [blame] | 65 | if (interpVal < 0 || interpVal > 1) { |
| 66 | ALOGW("rayIntersectPoints is hitting outside the segment %f", interpVal); |
| 67 | } |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 68 | #endif |
| 69 | |
| 70 | double distance = (p1.x * (rayOrigin.y - p2.y) + p2.x * (p1.y - rayOrigin.y) + |
| 71 | rayOrigin.x * (p2.y - p1.y)) / divisor; |
| 72 | |
| 73 | return distance; // may be negative in error cases |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 77 | * Sort points by their X coordinates |
| 78 | * |
| 79 | * @param points the points as a Vector2 array. |
| 80 | * @param pointsLength the number of vertices of the polygon. |
| 81 | */ |
| 82 | void SpotShadow::xsort(Vector2* points, int pointsLength) { |
| 83 | quicksortX(points, 0, pointsLength - 1); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * compute the convex hull of a collection of Points |
| 88 | * |
| 89 | * @param points the points as a Vector2 array. |
| 90 | * @param pointsLength the number of vertices of the polygon. |
| 91 | * @param retPoly pre allocated array of floats to put the vertices |
| 92 | * @return the number of points in the polygon 0 if no intersection |
| 93 | */ |
| 94 | int SpotShadow::hull(Vector2* points, int pointsLength, Vector2* retPoly) { |
| 95 | xsort(points, pointsLength); |
| 96 | int n = pointsLength; |
| 97 | Vector2 lUpper[n]; |
| 98 | lUpper[0] = points[0]; |
| 99 | lUpper[1] = points[1]; |
| 100 | |
| 101 | int lUpperSize = 2; |
| 102 | |
| 103 | for (int i = 2; i < n; i++) { |
| 104 | lUpper[lUpperSize] = points[i]; |
| 105 | lUpperSize++; |
| 106 | |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 107 | while (lUpperSize > 2 && !ccw( |
| 108 | lUpper[lUpperSize - 3].x, lUpper[lUpperSize - 3].y, |
| 109 | lUpper[lUpperSize - 2].x, lUpper[lUpperSize - 2].y, |
| 110 | lUpper[lUpperSize - 1].x, lUpper[lUpperSize - 1].y)) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 111 | // Remove the middle point of the three last |
| 112 | lUpper[lUpperSize - 2].x = lUpper[lUpperSize - 1].x; |
| 113 | lUpper[lUpperSize - 2].y = lUpper[lUpperSize - 1].y; |
| 114 | lUpperSize--; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | Vector2 lLower[n]; |
| 119 | lLower[0] = points[n - 1]; |
| 120 | lLower[1] = points[n - 2]; |
| 121 | |
| 122 | int lLowerSize = 2; |
| 123 | |
| 124 | for (int i = n - 3; i >= 0; i--) { |
| 125 | lLower[lLowerSize] = points[i]; |
| 126 | lLowerSize++; |
| 127 | |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 128 | while (lLowerSize > 2 && !ccw( |
| 129 | lLower[lLowerSize - 3].x, lLower[lLowerSize - 3].y, |
| 130 | lLower[lLowerSize - 2].x, lLower[lLowerSize - 2].y, |
| 131 | lLower[lLowerSize - 1].x, lLower[lLowerSize - 1].y)) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 132 | // Remove the middle point of the three last |
| 133 | lLower[lLowerSize - 2] = lLower[lLowerSize - 1]; |
| 134 | lLowerSize--; |
| 135 | } |
| 136 | } |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 137 | |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 138 | // output points in CW ordering |
| 139 | const int total = lUpperSize + lLowerSize - 2; |
| 140 | int outIndex = total - 1; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 141 | for (int i = 0; i < lUpperSize; i++) { |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 142 | retPoly[outIndex] = lUpper[i]; |
| 143 | outIndex--; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | for (int i = 1; i < lLowerSize - 1; i++) { |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 147 | retPoly[outIndex] = lLower[i]; |
| 148 | outIndex--; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 149 | } |
| 150 | // TODO: Add test harness which verify that all the points are inside the hull. |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 151 | return total; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 155 | * Test whether the 3 points form a counter clockwise turn. |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 156 | * |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 157 | * @return true if a right hand turn |
| 158 | */ |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 159 | bool SpotShadow::ccw(double ax, double ay, double bx, double by, |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 160 | double cx, double cy) { |
| 161 | return (bx - ax) * (cy - ay) - (by - ay) * (cx - ax) > EPSILON; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Calculates the intersection of poly1 with poly2 and put in poly2. |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 166 | * Note that both poly1 and poly2 must be in CW order already! |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 167 | * |
| 168 | * @param poly1 The 1st polygon, as a Vector2 array. |
| 169 | * @param poly1Length The number of vertices of 1st polygon. |
| 170 | * @param poly2 The 2nd and output polygon, as a Vector2 array. |
| 171 | * @param poly2Length The number of vertices of 2nd polygon. |
| 172 | * @return number of vertices in output polygon as poly2. |
| 173 | */ |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 174 | int SpotShadow::intersection(const Vector2* poly1, int poly1Length, |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 175 | Vector2* poly2, int poly2Length) { |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 176 | #if DEBUG_SHADOW |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 177 | if (!ShadowTessellator::isClockwise(poly1, poly1Length)) { |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 178 | ALOGW("Poly1 is not clockwise! Intersection is wrong!"); |
| 179 | } |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 180 | if (!ShadowTessellator::isClockwise(poly2, poly2Length)) { |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 181 | ALOGW("Poly2 is not clockwise! Intersection is wrong!"); |
| 182 | } |
| 183 | #endif |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 184 | Vector2 poly[poly1Length * poly2Length + 2]; |
| 185 | int count = 0; |
| 186 | int pcount = 0; |
| 187 | |
| 188 | // If one vertex from one polygon sits inside another polygon, add it and |
| 189 | // count them. |
| 190 | for (int i = 0; i < poly1Length; i++) { |
| 191 | if (testPointInsidePolygon(poly1[i], poly2, poly2Length)) { |
| 192 | poly[count] = poly1[i]; |
| 193 | count++; |
| 194 | pcount++; |
| 195 | |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | int insidePoly2 = pcount; |
| 200 | for (int i = 0; i < poly2Length; i++) { |
| 201 | if (testPointInsidePolygon(poly2[i], poly1, poly1Length)) { |
| 202 | poly[count] = poly2[i]; |
| 203 | count++; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | int insidePoly1 = count - insidePoly2; |
| 208 | // If all vertices from poly1 are inside poly2, then just return poly1. |
| 209 | if (insidePoly2 == poly1Length) { |
| 210 | memcpy(poly2, poly1, poly1Length * sizeof(Vector2)); |
| 211 | return poly1Length; |
| 212 | } |
| 213 | |
| 214 | // If all vertices from poly2 are inside poly1, then just return poly2. |
| 215 | if (insidePoly1 == poly2Length) { |
| 216 | return poly2Length; |
| 217 | } |
| 218 | |
| 219 | // Since neither polygon fully contain the other one, we need to add all the |
| 220 | // intersection points. |
| 221 | Vector2 intersection; |
| 222 | for (int i = 0; i < poly2Length; i++) { |
| 223 | for (int j = 0; j < poly1Length; j++) { |
| 224 | int poly2LineStart = i; |
| 225 | int poly2LineEnd = ((i + 1) % poly2Length); |
| 226 | int poly1LineStart = j; |
| 227 | int poly1LineEnd = ((j + 1) % poly1Length); |
| 228 | bool found = lineIntersection( |
| 229 | poly2[poly2LineStart].x, poly2[poly2LineStart].y, |
| 230 | poly2[poly2LineEnd].x, poly2[poly2LineEnd].y, |
| 231 | poly1[poly1LineStart].x, poly1[poly1LineStart].y, |
| 232 | poly1[poly1LineEnd].x, poly1[poly1LineEnd].y, |
| 233 | intersection); |
| 234 | if (found) { |
| 235 | poly[count].x = intersection.x; |
| 236 | poly[count].y = intersection.y; |
| 237 | count++; |
| 238 | } else { |
| 239 | Vector2 delta = poly2[i] - poly1[j]; |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 240 | if (delta.lengthSquared() < EPSILON) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 241 | poly[count] = poly2[i]; |
| 242 | count++; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (count == 0) { |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | // Sort the result polygon around the center. |
| 253 | Vector2 center(0.0f, 0.0f); |
| 254 | for (int i = 0; i < count; i++) { |
| 255 | center += poly[i]; |
| 256 | } |
| 257 | center /= count; |
| 258 | sort(poly, count, center); |
| 259 | |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 260 | #if DEBUG_SHADOW |
| 261 | // Since poly2 is overwritten as the result, we need to save a copy to do |
| 262 | // our verification. |
| 263 | Vector2 oldPoly2[poly2Length]; |
| 264 | int oldPoly2Length = poly2Length; |
| 265 | memcpy(oldPoly2, poly2, sizeof(Vector2) * poly2Length); |
| 266 | #endif |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 267 | |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 268 | // Filter the result out from poly and put it into poly2. |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 269 | poly2[0] = poly[0]; |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 270 | int lastOutputIndex = 0; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 271 | for (int i = 1; i < count; i++) { |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 272 | Vector2 delta = poly[i] - poly2[lastOutputIndex]; |
| 273 | if (delta.lengthSquared() >= EPSILON) { |
| 274 | poly2[++lastOutputIndex] = poly[i]; |
| 275 | } else { |
| 276 | // If the vertices are too close, pick the inner one, because the |
| 277 | // inner one is more likely to be an intersection point. |
| 278 | Vector2 delta1 = poly[i] - center; |
| 279 | Vector2 delta2 = poly2[lastOutputIndex] - center; |
| 280 | if (delta1.lengthSquared() < delta2.lengthSquared()) { |
| 281 | poly2[lastOutputIndex] = poly[i]; |
| 282 | } |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 285 | int resultLength = lastOutputIndex + 1; |
| 286 | |
| 287 | #if DEBUG_SHADOW |
| 288 | testConvex(poly2, resultLength, "intersection"); |
| 289 | testConvex(poly1, poly1Length, "input poly1"); |
| 290 | testConvex(oldPoly2, oldPoly2Length, "input poly2"); |
| 291 | |
| 292 | testIntersection(poly1, poly1Length, oldPoly2, oldPoly2Length, poly2, resultLength); |
| 293 | #endif |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 294 | |
| 295 | return resultLength; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Sort points about a center point |
| 300 | * |
| 301 | * @param poly The in and out polyogon as a Vector2 array. |
| 302 | * @param polyLength The number of vertices of the polygon. |
| 303 | * @param center the center ctr[0] = x , ctr[1] = y to sort around. |
| 304 | */ |
| 305 | void SpotShadow::sort(Vector2* poly, int polyLength, const Vector2& center) { |
| 306 | quicksortCirc(poly, 0, polyLength - 1, center); |
| 307 | } |
| 308 | |
| 309 | /** |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 310 | * Swap points pointed to by i and j |
| 311 | */ |
| 312 | void SpotShadow::swap(Vector2* points, int i, int j) { |
| 313 | Vector2 temp = points[i]; |
| 314 | points[i] = points[j]; |
| 315 | points[j] = temp; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * quick sort implementation about the center. |
| 320 | */ |
| 321 | void SpotShadow::quicksortCirc(Vector2* points, int low, int high, |
| 322 | const Vector2& center) { |
| 323 | int i = low, j = high; |
| 324 | int p = low + (high - low) / 2; |
| 325 | float pivot = angle(points[p], center); |
| 326 | while (i <= j) { |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 327 | while (angle(points[i], center) > pivot) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 328 | i++; |
| 329 | } |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 330 | while (angle(points[j], center) < pivot) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 331 | j--; |
| 332 | } |
| 333 | |
| 334 | if (i <= j) { |
| 335 | swap(points, i, j); |
| 336 | i++; |
| 337 | j--; |
| 338 | } |
| 339 | } |
| 340 | if (low < j) quicksortCirc(points, low, j, center); |
| 341 | if (i < high) quicksortCirc(points, i, high, center); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Sort points by x axis |
| 346 | * |
| 347 | * @param points points to sort |
| 348 | * @param low start index |
| 349 | * @param high end index |
| 350 | */ |
| 351 | void SpotShadow::quicksortX(Vector2* points, int low, int high) { |
| 352 | int i = low, j = high; |
| 353 | int p = low + (high - low) / 2; |
| 354 | float pivot = points[p].x; |
| 355 | while (i <= j) { |
| 356 | while (points[i].x < pivot) { |
| 357 | i++; |
| 358 | } |
| 359 | while (points[j].x > pivot) { |
| 360 | j--; |
| 361 | } |
| 362 | |
| 363 | if (i <= j) { |
| 364 | swap(points, i, j); |
| 365 | i++; |
| 366 | j--; |
| 367 | } |
| 368 | } |
| 369 | if (low < j) quicksortX(points, low, j); |
| 370 | if (i < high) quicksortX(points, i, high); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Test whether a point is inside the polygon. |
| 375 | * |
| 376 | * @param testPoint the point to test |
| 377 | * @param poly the polygon |
| 378 | * @return true if the testPoint is inside the poly. |
| 379 | */ |
| 380 | bool SpotShadow::testPointInsidePolygon(const Vector2 testPoint, |
| 381 | const Vector2* poly, int len) { |
| 382 | bool c = false; |
| 383 | double testx = testPoint.x; |
| 384 | double testy = testPoint.y; |
| 385 | for (int i = 0, j = len - 1; i < len; j = i++) { |
| 386 | double startX = poly[j].x; |
| 387 | double startY = poly[j].y; |
| 388 | double endX = poly[i].x; |
| 389 | double endY = poly[i].y; |
| 390 | |
| 391 | if (((endY > testy) != (startY > testy)) && |
| 392 | (testx < (startX - endX) * (testy - endY) |
| 393 | / (startY - endY) + endX)) { |
| 394 | c = !c; |
| 395 | } |
| 396 | } |
| 397 | return c; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Make the polygon turn clockwise. |
| 402 | * |
| 403 | * @param polygon the polygon as a Vector2 array. |
| 404 | * @param len the number of points of the polygon |
| 405 | */ |
| 406 | void SpotShadow::makeClockwise(Vector2* polygon, int len) { |
| 407 | if (polygon == 0 || len == 0) { |
| 408 | return; |
| 409 | } |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 410 | if (!ShadowTessellator::isClockwise(polygon, len)) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 411 | reverse(polygon, len); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /** |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 416 | * Reverse the polygon |
| 417 | * |
| 418 | * @param polygon the polygon as a Vector2 array |
| 419 | * @param len the number of points of the polygon |
| 420 | */ |
| 421 | void SpotShadow::reverse(Vector2* polygon, int len) { |
| 422 | int n = len / 2; |
| 423 | for (int i = 0; i < n; i++) { |
| 424 | Vector2 tmp = polygon[i]; |
| 425 | int k = len - 1 - i; |
| 426 | polygon[i] = polygon[k]; |
| 427 | polygon[k] = tmp; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Intersects two lines in parametric form. This function is called in a tight |
| 433 | * loop, and we need double precision to get things right. |
| 434 | * |
| 435 | * @param x1 the x coordinate point 1 of line 1 |
| 436 | * @param y1 the y coordinate point 1 of line 1 |
| 437 | * @param x2 the x coordinate point 2 of line 1 |
| 438 | * @param y2 the y coordinate point 2 of line 1 |
| 439 | * @param x3 the x coordinate point 1 of line 2 |
| 440 | * @param y3 the y coordinate point 1 of line 2 |
| 441 | * @param x4 the x coordinate point 2 of line 2 |
| 442 | * @param y4 the y coordinate point 2 of line 2 |
| 443 | * @param ret the x,y location of the intersection |
| 444 | * @return true if it found an intersection |
| 445 | */ |
| 446 | inline bool SpotShadow::lineIntersection(double x1, double y1, double x2, double y2, |
| 447 | double x3, double y3, double x4, double y4, Vector2& ret) { |
| 448 | double d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); |
| 449 | if (d == 0.0) return false; |
| 450 | |
| 451 | double dx = (x1 * y2 - y1 * x2); |
| 452 | double dy = (x3 * y4 - y3 * x4); |
| 453 | double x = (dx * (x3 - x4) - (x1 - x2) * dy) / d; |
| 454 | double y = (dx * (y3 - y4) - (y1 - y2) * dy) / d; |
| 455 | |
| 456 | // The intersection should be in the middle of the point 1 and point 2, |
| 457 | // likewise point 3 and point 4. |
| 458 | if (((x - x1) * (x - x2) > EPSILON) |
| 459 | || ((x - x3) * (x - x4) > EPSILON) |
| 460 | || ((y - y1) * (y - y2) > EPSILON) |
| 461 | || ((y - y3) * (y - y4) > EPSILON)) { |
| 462 | // Not interesected |
| 463 | return false; |
| 464 | } |
| 465 | ret.x = x; |
| 466 | ret.y = y; |
| 467 | return true; |
| 468 | |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Compute a horizontal circular polygon about point (x , y , height) of radius |
| 473 | * (size) |
| 474 | * |
| 475 | * @param points number of the points of the output polygon. |
| 476 | * @param lightCenter the center of the light. |
| 477 | * @param size the light size. |
| 478 | * @param ret result polygon. |
| 479 | */ |
| 480 | void SpotShadow::computeLightPolygon(int points, const Vector3& lightCenter, |
| 481 | float size, Vector3* ret) { |
| 482 | // TODO: Caching all the sin / cos values and store them in a look up table. |
| 483 | for (int i = 0; i < points; i++) { |
| 484 | double angle = 2 * i * M_PI / points; |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 485 | ret[i].x = cosf(angle) * size + lightCenter.x; |
| 486 | ret[i].y = sinf(angle) * size + lightCenter.y; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 487 | ret[i].z = lightCenter.z; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Generate the shadow from a spot light. |
| 493 | * |
| 494 | * @param poly x,y,z vertexes of a convex polygon that occludes the light source |
| 495 | * @param polyLength number of vertexes of the occluding polygon |
| 496 | * @param lightCenter the center of the light |
| 497 | * @param lightSize the radius of the light source |
| 498 | * @param lightVertexCount the vertex counter for the light polygon |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 499 | * @param shadowTriangleStrip return an (x,y,alpha) triangle strip representing the shadow. Return |
| 500 | * empty strip if error. |
| 501 | * |
| 502 | */ |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 503 | VertexBufferMode SpotShadow::createSpotShadow(bool isCasterOpaque, const Vector3* poly, |
| 504 | int polyLength, const Vector3& lightCenter, float lightSize, |
| 505 | int lightVertexCount, VertexBuffer& retStrips) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 506 | Vector3 light[lightVertexCount * 3]; |
| 507 | computeLightPolygon(lightVertexCount, lightCenter, lightSize, light); |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 508 | computeSpotShadow(isCasterOpaque, light, lightVertexCount, lightCenter, poly, |
| 509 | polyLength, retStrips); |
| 510 | return kVertexBufferMode_TwoPolyRingShadow; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Generate the shadow spot light of shape lightPoly and a object poly |
| 515 | * |
| 516 | * @param lightPoly x,y,z vertex of a convex polygon that is the light source |
| 517 | * @param lightPolyLength number of vertexes of the light source polygon |
| 518 | * @param poly x,y,z vertexes of a convex polygon that occludes the light source |
| 519 | * @param polyLength number of vertexes of the occluding polygon |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 520 | * @param shadowTriangleStrip return an (x,y,alpha) triangle strip representing the shadow. Return |
| 521 | * empty strip if error. |
| 522 | */ |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 523 | void SpotShadow::computeSpotShadow(bool isCasterOpaque, const Vector3* lightPoly, |
| 524 | int lightPolyLength, const Vector3& lightCenter, const Vector3* poly, |
| 525 | int polyLength, VertexBuffer& shadowTriangleStrip) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 526 | // Point clouds for all the shadowed vertices |
| 527 | Vector2 shadowRegion[lightPolyLength * polyLength]; |
| 528 | // Shadow polygon from one point light. |
| 529 | Vector2 outline[polyLength]; |
| 530 | Vector2 umbraMem[polyLength * lightPolyLength]; |
| 531 | Vector2* umbra = umbraMem; |
| 532 | |
| 533 | int umbraLength = 0; |
| 534 | |
| 535 | // Validate input, receiver is always at z = 0 plane. |
| 536 | bool inputPolyPositionValid = true; |
| 537 | for (int i = 0; i < polyLength; i++) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 538 | if (poly[i].z >= lightPoly[0].z) { |
| 539 | inputPolyPositionValid = false; |
Chris Craik | b79a3e3 | 2014-03-11 12:20:17 -0700 | [diff] [blame] | 540 | ALOGW("polygon above the light"); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 541 | break; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | // If the caster's position is invalid, don't draw anything. |
| 546 | if (!inputPolyPositionValid) { |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | // Calculate the umbra polygon based on intersections of all outlines |
| 551 | int k = 0; |
| 552 | for (int j = 0; j < lightPolyLength; j++) { |
| 553 | int m = 0; |
| 554 | for (int i = 0; i < polyLength; i++) { |
ztenghui | 28c3ea0 | 2014-03-18 15:58:57 -0700 | [diff] [blame] | 555 | // After validating the input, deltaZ is guaranteed to be positive. |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 556 | float deltaZ = lightPoly[j].z - poly[i].z; |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 557 | float ratioZ = lightPoly[j].z / deltaZ; |
| 558 | float x = lightPoly[j].x - ratioZ * (lightPoly[j].x - poly[i].x); |
| 559 | float y = lightPoly[j].y - ratioZ * (lightPoly[j].y - poly[i].y); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 560 | |
| 561 | Vector2 newPoint = Vector2(x, y); |
| 562 | shadowRegion[k] = newPoint; |
| 563 | outline[m] = newPoint; |
| 564 | |
| 565 | k++; |
| 566 | m++; |
| 567 | } |
| 568 | |
| 569 | // For the first light polygon's vertex, use the outline as the umbra. |
| 570 | // Later on, use the intersection of the outline and existing umbra. |
| 571 | if (umbraLength == 0) { |
| 572 | for (int i = 0; i < polyLength; i++) { |
| 573 | umbra[i] = outline[i]; |
| 574 | } |
| 575 | umbraLength = polyLength; |
| 576 | } else { |
| 577 | int col = ((j * 255) / lightPolyLength); |
| 578 | umbraLength = intersection(outline, polyLength, umbra, umbraLength); |
| 579 | if (umbraLength == 0) { |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | // Generate the penumbra area using the hull of all shadow regions. |
| 586 | int shadowRegionLength = k; |
| 587 | Vector2 penumbra[k]; |
| 588 | int penumbraLength = hull(shadowRegion, shadowRegionLength, penumbra); |
| 589 | |
ztenghui | 5176c97 | 2014-01-31 17:17:55 -0800 | [diff] [blame] | 590 | Vector2 fakeUmbra[polyLength]; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 591 | if (umbraLength < 3) { |
ztenghui | 5176c97 | 2014-01-31 17:17:55 -0800 | [diff] [blame] | 592 | // If there is no real umbra, make a fake one. |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 593 | for (int i = 0; i < polyLength; i++) { |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 594 | float deltaZ = lightCenter.z - poly[i].z; |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 595 | float ratioZ = lightCenter.z / deltaZ; |
| 596 | float x = lightCenter.x - ratioZ * (lightCenter.x - poly[i].x); |
| 597 | float y = lightCenter.y - ratioZ * (lightCenter.y - poly[i].y); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 598 | |
ztenghui | 5176c97 | 2014-01-31 17:17:55 -0800 | [diff] [blame] | 599 | fakeUmbra[i].x = x; |
| 600 | fakeUmbra[i].y = y; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | // Shrink the centroid's shadow by 10%. |
| 604 | // TODO: Study the magic number of 10%. |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 605 | Vector2 shadowCentroid = |
| 606 | ShadowTessellator::centroid2d(fakeUmbra, polyLength); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 607 | for (int i = 0; i < polyLength; i++) { |
ztenghui | 5176c97 | 2014-01-31 17:17:55 -0800 | [diff] [blame] | 608 | fakeUmbra[i] = shadowCentroid * (1.0f - SHADOW_SHRINK_SCALE) + |
| 609 | fakeUmbra[i] * SHADOW_SHRINK_SCALE; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 610 | } |
| 611 | #if DEBUG_SHADOW |
| 612 | ALOGD("No real umbra make a fake one, centroid2d = %f , %f", |
| 613 | shadowCentroid.x, shadowCentroid.y); |
| 614 | #endif |
| 615 | // Set the fake umbra, whose size is the same as the original polygon. |
ztenghui | 5176c97 | 2014-01-31 17:17:55 -0800 | [diff] [blame] | 616 | umbra = fakeUmbra; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 617 | umbraLength = polyLength; |
| 618 | } |
| 619 | |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 620 | generateTriangleStrip(isCasterOpaque, penumbra, penumbraLength, umbra, |
| 621 | umbraLength, poly, polyLength, shadowTriangleStrip); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /** |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 625 | * Converts a polygon specified with CW vertices into an array of distance-from-centroid values. |
| 626 | * |
| 627 | * Returns false in error conditions |
| 628 | * |
| 629 | * @param poly Array of vertices. Note that these *must* be CW. |
| 630 | * @param polyLength The number of vertices in the polygon. |
| 631 | * @param polyCentroid The centroid of the polygon, from which rays will be cast |
| 632 | * @param rayDist The output array for the calculated distances, must be SHADOW_RAY_COUNT in size |
| 633 | */ |
| 634 | bool convertPolyToRayDist(const Vector2* poly, int polyLength, const Vector2& polyCentroid, |
| 635 | float* rayDist) { |
| 636 | const int rays = SHADOW_RAY_COUNT; |
| 637 | const float step = M_PI * 2 / rays; |
| 638 | |
| 639 | const Vector2* lastVertex = &(poly[polyLength - 1]); |
| 640 | float startAngle = angle(*lastVertex, polyCentroid); |
| 641 | |
| 642 | // Start with the ray that's closest to and less than startAngle |
| 643 | int rayIndex = floor((startAngle - EPSILON) / step); |
| 644 | rayIndex = (rayIndex + rays) % rays; // ensure positive |
| 645 | |
| 646 | for (int polyIndex = 0; polyIndex < polyLength; polyIndex++) { |
| 647 | /* |
| 648 | * For a given pair of vertices on the polygon, poly[i-1] and poly[i], the rays that |
| 649 | * intersect these will be those that are between the two angles from the centroid that the |
| 650 | * vertices define. |
| 651 | * |
| 652 | * Because the polygon vertices are stored clockwise, the closest ray with an angle |
| 653 | * *smaller* than that defined by angle(poly[i], centroid) will be the first ray that does |
| 654 | * not intersect with poly[i-1], poly[i]. |
| 655 | */ |
| 656 | float currentAngle = angle(poly[polyIndex], polyCentroid); |
| 657 | |
| 658 | // find first ray that will not intersect the line segment poly[i-1] & poly[i] |
| 659 | int firstRayIndexOnNextSegment = floor((currentAngle - EPSILON) / step); |
| 660 | firstRayIndexOnNextSegment = (firstRayIndexOnNextSegment + rays) % rays; // ensure positive |
| 661 | |
| 662 | // Iterate through all rays that intersect with poly[i-1], poly[i] line segment. |
| 663 | // This may be 0 rays. |
| 664 | while (rayIndex != firstRayIndexOnNextSegment) { |
| 665 | float distanceToIntersect = rayIntersectPoints(polyCentroid, |
| 666 | cos(rayIndex * step), |
| 667 | sin(rayIndex * step), |
| 668 | *lastVertex, poly[polyIndex]); |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 669 | if (distanceToIntersect < 0) { |
| 670 | #if DEBUG_SHADOW |
| 671 | ALOGW("ERROR: convertPolyToRayDist failed"); |
| 672 | #endif |
| 673 | return false; // error case, abort |
| 674 | } |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 675 | |
| 676 | rayDist[rayIndex] = distanceToIntersect; |
| 677 | |
| 678 | rayIndex = (rayIndex - 1 + rays) % rays; |
| 679 | } |
| 680 | lastVertex = &poly[polyIndex]; |
| 681 | } |
| 682 | |
| 683 | return true; |
| 684 | } |
| 685 | |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 686 | int SpotShadow::calculateOccludedUmbra(const Vector2* umbra, int umbraLength, |
| 687 | const Vector3* poly, int polyLength, Vector2* occludedUmbra) { |
| 688 | // Occluded umbra area is computed as the intersection of the projected 2D |
| 689 | // poly and umbra. |
| 690 | for (int i = 0; i < polyLength; i++) { |
| 691 | occludedUmbra[i].x = poly[i].x; |
| 692 | occludedUmbra[i].y = poly[i].y; |
| 693 | } |
| 694 | |
| 695 | // Both umbra and incoming polygon are guaranteed to be CW, so we can call |
| 696 | // intersection() directly. |
| 697 | return intersection(umbra, umbraLength, |
| 698 | occludedUmbra, polyLength); |
| 699 | } |
| 700 | |
| 701 | #define OCLLUDED_UMBRA_SHRINK_FACTOR 0.95f |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 702 | /** |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 703 | * Generate a triangle strip given two convex polygons |
| 704 | * |
| 705 | * @param penumbra The outer polygon x,y vertexes |
| 706 | * @param penumbraLength The number of vertexes in the outer polygon |
| 707 | * @param umbra The inner outer polygon x,y vertexes |
| 708 | * @param umbraLength The number of vertexes in the inner polygon |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 709 | * @param shadowTriangleStrip return an (x,y,alpha) triangle strip representing the shadow. Return |
| 710 | * empty strip if error. |
| 711 | **/ |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 712 | void SpotShadow::generateTriangleStrip(bool isCasterOpaque, const Vector2* penumbra, |
| 713 | int penumbraLength, const Vector2* umbra, int umbraLength, |
| 714 | const Vector3* poly, int polyLength, VertexBuffer& shadowTriangleStrip) { |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 715 | const int rays = SHADOW_RAY_COUNT; |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 716 | const int size = 2 * rays; |
| 717 | const float step = M_PI * 2 / rays; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 718 | // Centroid of the umbra. |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 719 | Vector2 centroid = ShadowTessellator::centroid2d(umbra, umbraLength); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 720 | #if DEBUG_SHADOW |
| 721 | ALOGD("centroid2d = %f , %f", centroid.x, centroid.y); |
| 722 | #endif |
| 723 | // Intersection to the penumbra. |
| 724 | float penumbraDistPerRay[rays]; |
| 725 | // Intersection to the umbra. |
| 726 | float umbraDistPerRay[rays]; |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 727 | // Intersection to the occluded umbra area. |
| 728 | float occludedUmbraDistPerRay[rays]; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 729 | |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 730 | // convert CW polygons to ray distance encoding, aborting on conversion failure |
| 731 | if (!convertPolyToRayDist(umbra, umbraLength, centroid, umbraDistPerRay)) return; |
| 732 | if (!convertPolyToRayDist(penumbra, penumbraLength, centroid, penumbraDistPerRay)) return; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 733 | |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 734 | bool hasOccludedUmbraArea = false; |
| 735 | if (isCasterOpaque) { |
| 736 | Vector2 occludedUmbra[polyLength + umbraLength]; |
| 737 | int occludedUmbraLength = calculateOccludedUmbra(umbra, umbraLength, poly, polyLength, |
| 738 | occludedUmbra); |
| 739 | // Make sure the centroid is inside the umbra, otherwise, fall back to the |
| 740 | // approach as if there is no occluded umbra area. |
| 741 | if (testPointInsidePolygon(centroid, occludedUmbra, occludedUmbraLength)) { |
| 742 | hasOccludedUmbraArea = true; |
| 743 | // Shrink the occluded umbra area to avoid pixel level artifacts. |
| 744 | for (int i = 0; i < occludedUmbraLength; i ++) { |
| 745 | occludedUmbra[i] = centroid + (occludedUmbra[i] - centroid) * |
| 746 | OCLLUDED_UMBRA_SHRINK_FACTOR; |
| 747 | } |
| 748 | if (!convertPolyToRayDist(occludedUmbra, occludedUmbraLength, centroid, |
| 749 | occludedUmbraDistPerRay)) { |
| 750 | return; |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | AlphaVertex* shadowVertices = |
| 756 | shadowTriangleStrip.alloc<AlphaVertex>(SHADOW_VERTEX_COUNT); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 757 | |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 758 | // Calculate the vertices (x, y, alpha) in the shadow area. |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 759 | AlphaVertex centroidXYA; |
| 760 | AlphaVertex::set(¢roidXYA, centroid.x, centroid.y, 1.0f); |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 761 | for (int rayIndex = 0; rayIndex < rays; rayIndex++) { |
| 762 | float dx = cosf(step * rayIndex); |
| 763 | float dy = sinf(step * rayIndex); |
| 764 | |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 765 | // penumbra ring |
| 766 | float penumbraDistance = penumbraDistPerRay[rayIndex]; |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 767 | AlphaVertex::set(&shadowVertices[rayIndex], |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 768 | dx * penumbraDistance + centroid.x, |
| 769 | dy * penumbraDistance + centroid.y, 0.0f); |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 770 | |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 771 | // umbra ring |
| 772 | float umbraDistance = umbraDistPerRay[rayIndex]; |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 773 | AlphaVertex::set(&shadowVertices[rays + rayIndex], |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 774 | dx * umbraDistance + centroid.x, dy * umbraDistance + centroid.y, 1.0f); |
| 775 | |
| 776 | // occluded umbra ring |
| 777 | if (hasOccludedUmbraArea) { |
| 778 | float occludedUmbraDistance = occludedUmbraDistPerRay[rayIndex]; |
| 779 | AlphaVertex::set(&shadowVertices[2 * rays + rayIndex], |
| 780 | dx * occludedUmbraDistance + centroid.x, |
| 781 | dy * occludedUmbraDistance + centroid.y, 1.0f); |
| 782 | } else { |
| 783 | // Put all vertices of the occluded umbra ring at the centroid. |
| 784 | shadowVertices[2 * rays + rayIndex] = centroidXYA; |
| 785 | } |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 786 | } |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /** |
| 790 | * This is only for experimental purpose. |
| 791 | * After intersections are calculated, we could smooth the polygon if needed. |
| 792 | * So far, we don't think it is more appealing yet. |
| 793 | * |
| 794 | * @param level The level of smoothness. |
| 795 | * @param rays The total number of rays. |
| 796 | * @param rayDist (In and Out) The distance for each ray. |
| 797 | * |
| 798 | */ |
| 799 | void SpotShadow::smoothPolygon(int level, int rays, float* rayDist) { |
| 800 | for (int k = 0; k < level; k++) { |
| 801 | for (int i = 0; i < rays; i++) { |
| 802 | float p1 = rayDist[(rays - 1 + i) % rays]; |
| 803 | float p2 = rayDist[i]; |
| 804 | float p3 = rayDist[(i + 1) % rays]; |
| 805 | rayDist[i] = (p1 + p2 * 2 + p3) / 4; |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 810 | #if DEBUG_SHADOW |
| 811 | |
| 812 | #define TEST_POINT_NUMBER 128 |
| 813 | |
| 814 | /** |
| 815 | * Calculate the bounds for generating random test points. |
| 816 | */ |
| 817 | void SpotShadow::updateBound(const Vector2 inVector, Vector2& lowerBound, |
| 818 | Vector2& upperBound ) { |
| 819 | if (inVector.x < lowerBound.x) { |
| 820 | lowerBound.x = inVector.x; |
| 821 | } |
| 822 | |
| 823 | if (inVector.y < lowerBound.y) { |
| 824 | lowerBound.y = inVector.y; |
| 825 | } |
| 826 | |
| 827 | if (inVector.x > upperBound.x) { |
| 828 | upperBound.x = inVector.x; |
| 829 | } |
| 830 | |
| 831 | if (inVector.y > upperBound.y) { |
| 832 | upperBound.y = inVector.y; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * For debug purpose, when things go wrong, dump the whole polygon data. |
| 838 | */ |
| 839 | static void dumpPolygon(const Vector2* poly, int polyLength, const char* polyName) { |
| 840 | for (int i = 0; i < polyLength; i++) { |
| 841 | ALOGD("polygon %s i %d x %f y %f", polyName, i, poly[i].x, poly[i].y); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * Test whether the polygon is convex. |
| 847 | */ |
| 848 | bool SpotShadow::testConvex(const Vector2* polygon, int polygonLength, |
| 849 | const char* name) { |
| 850 | bool isConvex = true; |
| 851 | for (int i = 0; i < polygonLength; i++) { |
| 852 | Vector2 start = polygon[i]; |
| 853 | Vector2 middle = polygon[(i + 1) % polygonLength]; |
| 854 | Vector2 end = polygon[(i + 2) % polygonLength]; |
| 855 | |
| 856 | double delta = (double(middle.x) - start.x) * (double(end.y) - start.y) - |
| 857 | (double(middle.y) - start.y) * (double(end.x) - start.x); |
| 858 | bool isCCWOrCoLinear = (delta >= EPSILON); |
| 859 | |
| 860 | if (isCCWOrCoLinear) { |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 861 | ALOGW("(Error Type 2): polygon (%s) is not a convex b/c start (x %f, y %f)," |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 862 | "middle (x %f, y %f) and end (x %f, y %f) , delta is %f !!!", |
| 863 | name, start.x, start.y, middle.x, middle.y, end.x, end.y, delta); |
| 864 | isConvex = false; |
| 865 | break; |
| 866 | } |
| 867 | } |
| 868 | return isConvex; |
| 869 | } |
| 870 | |
| 871 | /** |
| 872 | * Test whether or not the polygon (intersection) is within the 2 input polygons. |
| 873 | * Using Marte Carlo method, we generate a random point, and if it is inside the |
| 874 | * intersection, then it must be inside both source polygons. |
| 875 | */ |
| 876 | void SpotShadow::testIntersection(const Vector2* poly1, int poly1Length, |
| 877 | const Vector2* poly2, int poly2Length, |
| 878 | const Vector2* intersection, int intersectionLength) { |
| 879 | // Find the min and max of x and y. |
| 880 | Vector2 lowerBound(FLT_MAX, FLT_MAX); |
| 881 | Vector2 upperBound(-FLT_MAX, -FLT_MAX); |
| 882 | for (int i = 0; i < poly1Length; i++) { |
| 883 | updateBound(poly1[i], lowerBound, upperBound); |
| 884 | } |
| 885 | for (int i = 0; i < poly2Length; i++) { |
| 886 | updateBound(poly2[i], lowerBound, upperBound); |
| 887 | } |
| 888 | |
| 889 | bool dumpPoly = false; |
| 890 | for (int k = 0; k < TEST_POINT_NUMBER; k++) { |
| 891 | // Generate a random point between minX, minY and maxX, maxY. |
| 892 | double randomX = rand() / double(RAND_MAX); |
| 893 | double randomY = rand() / double(RAND_MAX); |
| 894 | |
| 895 | Vector2 testPoint; |
| 896 | testPoint.x = lowerBound.x + randomX * (upperBound.x - lowerBound.x); |
| 897 | testPoint.y = lowerBound.y + randomY * (upperBound.y - lowerBound.y); |
| 898 | |
| 899 | // If the random point is in both poly 1 and 2, then it must be intersection. |
| 900 | if (testPointInsidePolygon(testPoint, intersection, intersectionLength)) { |
| 901 | if (!testPointInsidePolygon(testPoint, poly1, poly1Length)) { |
| 902 | dumpPoly = true; |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 903 | ALOGW("(Error Type 1): one point (%f, %f) in the intersection is" |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 904 | " not in the poly1", |
| 905 | testPoint.x, testPoint.y); |
| 906 | } |
| 907 | |
| 908 | if (!testPointInsidePolygon(testPoint, poly2, poly2Length)) { |
| 909 | dumpPoly = true; |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 910 | ALOGW("(Error Type 1): one point (%f, %f) in the intersection is" |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 911 | " not in the poly2", |
| 912 | testPoint.x, testPoint.y); |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | if (dumpPoly) { |
| 918 | dumpPolygon(intersection, intersectionLength, "intersection"); |
| 919 | for (int i = 1; i < intersectionLength; i++) { |
| 920 | Vector2 delta = intersection[i] - intersection[i - 1]; |
| 921 | ALOGD("Intersetion i, %d Vs i-1 is delta %f", i, delta.lengthSquared()); |
| 922 | } |
| 923 | |
| 924 | dumpPolygon(poly1, poly1Length, "poly 1"); |
| 925 | dumpPolygon(poly2, poly2Length, "poly 2"); |
| 926 | } |
| 927 | } |
| 928 | #endif |
| 929 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 930 | }; // namespace uirenderer |
| 931 | }; // namespace android |