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 | |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 19 | // The highest z value can't be higher than (CASTER_Z_CAP_RATIO * light.z) |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 20 | #define CASTER_Z_CAP_RATIO 0.95f |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 21 | |
| 22 | // When there is no umbra, then just fake the umbra using |
| 23 | // centroid * (1 - FAKE_UMBRA_SIZE_RATIO) + outline * FAKE_UMBRA_SIZE_RATIO |
| 24 | #define FAKE_UMBRA_SIZE_RATIO 0.05f |
| 25 | |
| 26 | // When the polygon is about 90 vertices, the penumbra + umbra can reach 270 rays. |
| 27 | // That is consider pretty fine tessllated polygon so far. |
| 28 | // This is just to prevent using too much some memory when edge slicing is not |
| 29 | // needed any more. |
| 30 | #define FINE_TESSELLATED_POLYGON_RAY_NUMBER 270 |
| 31 | /** |
| 32 | * Extra vertices for the corner for smoother corner. |
| 33 | * Only for outer loop. |
| 34 | * Note that we use such extra memory to avoid an extra loop. |
| 35 | */ |
| 36 | // For half circle, we could add EXTRA_VERTEX_PER_PI vertices. |
| 37 | // Set to 1 if we don't want to have any. |
| 38 | #define SPOT_EXTRA_CORNER_VERTEX_PER_PI 18 |
| 39 | |
| 40 | // For the whole polygon, the sum of all the deltas b/t normals is 2 * M_PI, |
| 41 | // therefore, the maximum number of extra vertices will be twice bigger. |
| 42 | #define SPOT_MAX_EXTRA_CORNER_VERTEX_NUMBER (2 * SPOT_EXTRA_CORNER_VERTEX_PER_PI) |
| 43 | |
| 44 | // For each RADIANS_DIVISOR, we would allocate one more vertex b/t the normals. |
| 45 | #define SPOT_CORNER_RADIANS_DIVISOR (M_PI / SPOT_EXTRA_CORNER_VERTEX_PER_PI) |
| 46 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 47 | |
| 48 | #include <math.h> |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 49 | #include <stdlib.h> |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 50 | #include <utils/Log.h> |
| 51 | |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 52 | #include "ShadowTessellator.h" |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 53 | #include "SpotShadow.h" |
| 54 | #include "Vertex.h" |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 55 | #include "utils/MathUtils.h" |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 56 | |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 57 | // TODO: After we settle down the new algorithm, we can remove the old one and |
| 58 | // its utility functions. |
| 59 | // Right now, we still need to keep it for comparison purpose and future expansion. |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 60 | namespace android { |
| 61 | namespace uirenderer { |
| 62 | |
ztenghui | 9122b1b | 2014-10-03 11:21:11 -0700 | [diff] [blame] | 63 | static const float EPSILON = 1e-7; |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 64 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 65 | /** |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 66 | * For each polygon's vertex, the light center will project it to the receiver |
| 67 | * as one of the outline vertex. |
| 68 | * For each outline vertex, we need to store the position and normal. |
| 69 | * Normal here is defined against the edge by the current vertex and the next vertex. |
| 70 | */ |
| 71 | struct OutlineData { |
| 72 | Vector2 position; |
| 73 | Vector2 normal; |
| 74 | float radius; |
| 75 | }; |
| 76 | |
| 77 | /** |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 78 | * For each vertex, we need to keep track of its angle, whether it is penumbra or |
| 79 | * umbra, and its corresponding vertex index. |
| 80 | */ |
| 81 | struct SpotShadow::VertexAngleData { |
| 82 | // The angle to the vertex from the centroid. |
| 83 | float mAngle; |
| 84 | // True is the vertex comes from penumbra, otherwise it comes from umbra. |
| 85 | bool mIsPenumbra; |
| 86 | // The index of the vertex described by this data. |
| 87 | int mVertexIndex; |
| 88 | void set(float angle, bool isPenumbra, int index) { |
| 89 | mAngle = angle; |
| 90 | mIsPenumbra = isPenumbra; |
| 91 | mVertexIndex = index; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | /** |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 96 | * Calculate the angle between and x and a y coordinate. |
| 97 | * The atan2 range from -PI to PI. |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 98 | */ |
Chris Craik | b79a3e3 | 2014-03-11 12:20:17 -0700 | [diff] [blame] | 99 | static float angle(const Vector2& point, const Vector2& center) { |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 100 | return atan2(point.y - center.y, point.x - center.x); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Calculate the intersection of a ray with the line segment defined by two points. |
| 105 | * |
| 106 | * Returns a negative value in error conditions. |
| 107 | |
| 108 | * @param rayOrigin The start of the ray |
| 109 | * @param dx The x vector of the ray |
| 110 | * @param dy The y vector of the ray |
| 111 | * @param p1 The first point defining the line segment |
| 112 | * @param p2 The second point defining the line segment |
| 113 | * @return The distance along the ray if it intersects with the line segment, negative if otherwise |
| 114 | */ |
Chris Craik | b79a3e3 | 2014-03-11 12:20:17 -0700 | [diff] [blame] | 115 | static float rayIntersectPoints(const Vector2& rayOrigin, float dx, float dy, |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 116 | const Vector2& p1, const Vector2& p2) { |
| 117 | // The math below is derived from solving this formula, basically the |
| 118 | // intersection point should stay on both the ray and the edge of (p1, p2). |
| 119 | // solve([p1x+t*(p2x-p1x)=dx*t2+px,p1y+t*(p2y-p1y)=dy*t2+py],[t,t2]); |
| 120 | |
ztenghui | 9122b1b | 2014-10-03 11:21:11 -0700 | [diff] [blame] | 121 | float divisor = (dx * (p1.y - p2.y) + dy * p2.x - dy * p1.x); |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 122 | if (divisor == 0) return -1.0f; // error, invalid divisor |
| 123 | |
| 124 | #if DEBUG_SHADOW |
ztenghui | 9122b1b | 2014-10-03 11:21:11 -0700 | [diff] [blame] | 125 | float interpVal = (dx * (p1.y - rayOrigin.y) + dy * rayOrigin.x - dy * p1.x) / divisor; |
ztenghui | 99af942 | 2014-03-14 14:35:54 -0700 | [diff] [blame] | 126 | if (interpVal < 0 || interpVal > 1) { |
| 127 | ALOGW("rayIntersectPoints is hitting outside the segment %f", interpVal); |
| 128 | } |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 129 | #endif |
| 130 | |
ztenghui | 9122b1b | 2014-10-03 11:21:11 -0700 | [diff] [blame] | 131 | float distance = (p1.x * (rayOrigin.y - p2.y) + p2.x * (p1.y - rayOrigin.y) + |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 132 | rayOrigin.x * (p2.y - p1.y)) / divisor; |
| 133 | |
| 134 | return distance; // may be negative in error cases |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 138 | * Sort points by their X coordinates |
| 139 | * |
| 140 | * @param points the points as a Vector2 array. |
| 141 | * @param pointsLength the number of vertices of the polygon. |
| 142 | */ |
| 143 | void SpotShadow::xsort(Vector2* points, int pointsLength) { |
| 144 | quicksortX(points, 0, pointsLength - 1); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * compute the convex hull of a collection of Points |
| 149 | * |
| 150 | * @param points the points as a Vector2 array. |
| 151 | * @param pointsLength the number of vertices of the polygon. |
| 152 | * @param retPoly pre allocated array of floats to put the vertices |
| 153 | * @return the number of points in the polygon 0 if no intersection |
| 154 | */ |
| 155 | int SpotShadow::hull(Vector2* points, int pointsLength, Vector2* retPoly) { |
| 156 | xsort(points, pointsLength); |
| 157 | int n = pointsLength; |
| 158 | Vector2 lUpper[n]; |
| 159 | lUpper[0] = points[0]; |
| 160 | lUpper[1] = points[1]; |
| 161 | |
| 162 | int lUpperSize = 2; |
| 163 | |
| 164 | for (int i = 2; i < n; i++) { |
| 165 | lUpper[lUpperSize] = points[i]; |
| 166 | lUpperSize++; |
| 167 | |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 168 | while (lUpperSize > 2 && !ccw( |
| 169 | lUpper[lUpperSize - 3].x, lUpper[lUpperSize - 3].y, |
| 170 | lUpper[lUpperSize - 2].x, lUpper[lUpperSize - 2].y, |
| 171 | lUpper[lUpperSize - 1].x, lUpper[lUpperSize - 1].y)) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 172 | // Remove the middle point of the three last |
| 173 | lUpper[lUpperSize - 2].x = lUpper[lUpperSize - 1].x; |
| 174 | lUpper[lUpperSize - 2].y = lUpper[lUpperSize - 1].y; |
| 175 | lUpperSize--; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | Vector2 lLower[n]; |
| 180 | lLower[0] = points[n - 1]; |
| 181 | lLower[1] = points[n - 2]; |
| 182 | |
| 183 | int lLowerSize = 2; |
| 184 | |
| 185 | for (int i = n - 3; i >= 0; i--) { |
| 186 | lLower[lLowerSize] = points[i]; |
| 187 | lLowerSize++; |
| 188 | |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 189 | while (lLowerSize > 2 && !ccw( |
| 190 | lLower[lLowerSize - 3].x, lLower[lLowerSize - 3].y, |
| 191 | lLower[lLowerSize - 2].x, lLower[lLowerSize - 2].y, |
| 192 | lLower[lLowerSize - 1].x, lLower[lLowerSize - 1].y)) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 193 | // Remove the middle point of the three last |
| 194 | lLower[lLowerSize - 2] = lLower[lLowerSize - 1]; |
| 195 | lLowerSize--; |
| 196 | } |
| 197 | } |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 198 | |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 199 | // output points in CW ordering |
| 200 | const int total = lUpperSize + lLowerSize - 2; |
| 201 | int outIndex = total - 1; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 202 | for (int i = 0; i < lUpperSize; i++) { |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 203 | retPoly[outIndex] = lUpper[i]; |
| 204 | outIndex--; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | for (int i = 1; i < lLowerSize - 1; i++) { |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 208 | retPoly[outIndex] = lLower[i]; |
| 209 | outIndex--; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 210 | } |
| 211 | // 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] | 212 | return total; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 216 | * Test whether the 3 points form a counter clockwise turn. |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 217 | * |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 218 | * @return true if a right hand turn |
| 219 | */ |
ztenghui | 9122b1b | 2014-10-03 11:21:11 -0700 | [diff] [blame] | 220 | bool SpotShadow::ccw(float ax, float ay, float bx, float by, |
| 221 | float cx, float cy) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 222 | return (bx - ax) * (cy - ay) - (by - ay) * (cx - ax) > EPSILON; |
| 223 | } |
| 224 | |
| 225 | /** |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 226 | * Sort points about a center point |
| 227 | * |
| 228 | * @param poly The in and out polyogon as a Vector2 array. |
| 229 | * @param polyLength The number of vertices of the polygon. |
| 230 | * @param center the center ctr[0] = x , ctr[1] = y to sort around. |
| 231 | */ |
| 232 | void SpotShadow::sort(Vector2* poly, int polyLength, const Vector2& center) { |
| 233 | quicksortCirc(poly, 0, polyLength - 1, center); |
| 234 | } |
| 235 | |
| 236 | /** |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 237 | * Swap points pointed to by i and j |
| 238 | */ |
| 239 | void SpotShadow::swap(Vector2* points, int i, int j) { |
| 240 | Vector2 temp = points[i]; |
| 241 | points[i] = points[j]; |
| 242 | points[j] = temp; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * quick sort implementation about the center. |
| 247 | */ |
| 248 | void SpotShadow::quicksortCirc(Vector2* points, int low, int high, |
| 249 | const Vector2& center) { |
| 250 | int i = low, j = high; |
| 251 | int p = low + (high - low) / 2; |
| 252 | float pivot = angle(points[p], center); |
| 253 | while (i <= j) { |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 254 | while (angle(points[i], center) > pivot) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 255 | i++; |
| 256 | } |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 257 | while (angle(points[j], center) < pivot) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 258 | j--; |
| 259 | } |
| 260 | |
| 261 | if (i <= j) { |
| 262 | swap(points, i, j); |
| 263 | i++; |
| 264 | j--; |
| 265 | } |
| 266 | } |
| 267 | if (low < j) quicksortCirc(points, low, j, center); |
| 268 | if (i < high) quicksortCirc(points, i, high, center); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Sort points by x axis |
| 273 | * |
| 274 | * @param points points to sort |
| 275 | * @param low start index |
| 276 | * @param high end index |
| 277 | */ |
| 278 | void SpotShadow::quicksortX(Vector2* points, int low, int high) { |
| 279 | int i = low, j = high; |
| 280 | int p = low + (high - low) / 2; |
| 281 | float pivot = points[p].x; |
| 282 | while (i <= j) { |
| 283 | while (points[i].x < pivot) { |
| 284 | i++; |
| 285 | } |
| 286 | while (points[j].x > pivot) { |
| 287 | j--; |
| 288 | } |
| 289 | |
| 290 | if (i <= j) { |
| 291 | swap(points, i, j); |
| 292 | i++; |
| 293 | j--; |
| 294 | } |
| 295 | } |
| 296 | if (low < j) quicksortX(points, low, j); |
| 297 | if (i < high) quicksortX(points, i, high); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Test whether a point is inside the polygon. |
| 302 | * |
| 303 | * @param testPoint the point to test |
| 304 | * @param poly the polygon |
| 305 | * @return true if the testPoint is inside the poly. |
| 306 | */ |
| 307 | bool SpotShadow::testPointInsidePolygon(const Vector2 testPoint, |
| 308 | const Vector2* poly, int len) { |
| 309 | bool c = false; |
ztenghui | 9122b1b | 2014-10-03 11:21:11 -0700 | [diff] [blame] | 310 | float testx = testPoint.x; |
| 311 | float testy = testPoint.y; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 312 | for (int i = 0, j = len - 1; i < len; j = i++) { |
ztenghui | 9122b1b | 2014-10-03 11:21:11 -0700 | [diff] [blame] | 313 | float startX = poly[j].x; |
| 314 | float startY = poly[j].y; |
| 315 | float endX = poly[i].x; |
| 316 | float endY = poly[i].y; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 317 | |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 318 | if (((endY > testy) != (startY > testy)) |
| 319 | && (testx < (startX - endX) * (testy - endY) |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 320 | / (startY - endY) + endX)) { |
| 321 | c = !c; |
| 322 | } |
| 323 | } |
| 324 | return c; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Make the polygon turn clockwise. |
| 329 | * |
| 330 | * @param polygon the polygon as a Vector2 array. |
| 331 | * @param len the number of points of the polygon |
| 332 | */ |
| 333 | void SpotShadow::makeClockwise(Vector2* polygon, int len) { |
| 334 | if (polygon == 0 || len == 0) { |
| 335 | return; |
| 336 | } |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 337 | if (!ShadowTessellator::isClockwise(polygon, len)) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 338 | reverse(polygon, len); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 343 | * Reverse the polygon |
| 344 | * |
| 345 | * @param polygon the polygon as a Vector2 array |
| 346 | * @param len the number of points of the polygon |
| 347 | */ |
| 348 | void SpotShadow::reverse(Vector2* polygon, int len) { |
| 349 | int n = len / 2; |
| 350 | for (int i = 0; i < n; i++) { |
| 351 | Vector2 tmp = polygon[i]; |
| 352 | int k = len - 1 - i; |
| 353 | polygon[i] = polygon[k]; |
| 354 | polygon[k] = tmp; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 359 | * Compute a horizontal circular polygon about point (x , y , height) of radius |
| 360 | * (size) |
| 361 | * |
| 362 | * @param points number of the points of the output polygon. |
| 363 | * @param lightCenter the center of the light. |
| 364 | * @param size the light size. |
| 365 | * @param ret result polygon. |
| 366 | */ |
| 367 | void SpotShadow::computeLightPolygon(int points, const Vector3& lightCenter, |
| 368 | float size, Vector3* ret) { |
| 369 | // TODO: Caching all the sin / cos values and store them in a look up table. |
| 370 | for (int i = 0; i < points; i++) { |
ztenghui | 9122b1b | 2014-10-03 11:21:11 -0700 | [diff] [blame] | 371 | float angle = 2 * i * M_PI / points; |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 372 | ret[i].x = cosf(angle) * size + lightCenter.x; |
| 373 | ret[i].y = sinf(angle) * size + lightCenter.y; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 374 | ret[i].z = lightCenter.z; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 379 | * From light center, project one vertex to the z=0 surface and get the outline. |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 380 | * |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 381 | * @param outline The result which is the outline position. |
| 382 | * @param lightCenter The center of light. |
| 383 | * @param polyVertex The input polygon's vertex. |
| 384 | * |
| 385 | * @return float The ratio of (polygon.z / light.z - polygon.z) |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 386 | */ |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 387 | float SpotShadow::projectCasterToOutline(Vector2& outline, |
| 388 | const Vector3& lightCenter, const Vector3& polyVertex) { |
| 389 | float lightToPolyZ = lightCenter.z - polyVertex.z; |
| 390 | float ratioZ = CASTER_Z_CAP_RATIO; |
| 391 | if (lightToPolyZ != 0) { |
| 392 | // If any caster's vertex is almost above the light, we just keep it as 95% |
| 393 | // of the height of the light. |
ztenghui | 3bd3fa1 | 2014-08-25 14:42:27 -0700 | [diff] [blame] | 394 | ratioZ = MathUtils::clamp(polyVertex.z / lightToPolyZ, 0.0f, CASTER_Z_CAP_RATIO); |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | outline.x = polyVertex.x - ratioZ * (lightCenter.x - polyVertex.x); |
| 398 | outline.y = polyVertex.y - ratioZ * (lightCenter.y - polyVertex.y); |
| 399 | return ratioZ; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Generate the shadow spot light of shape lightPoly and a object poly |
| 404 | * |
| 405 | * @param isCasterOpaque whether the caster is opaque |
| 406 | * @param lightCenter the center of the light |
| 407 | * @param lightSize the radius of the light |
| 408 | * @param poly x,y,z vertexes of a convex polygon that occludes the light source |
| 409 | * @param polyLength number of vertexes of the occluding polygon |
| 410 | * @param shadowTriangleStrip return an (x,y,alpha) triangle strip representing the shadow. Return |
| 411 | * empty strip if error. |
| 412 | */ |
| 413 | void SpotShadow::createSpotShadow(bool isCasterOpaque, const Vector3& lightCenter, |
| 414 | float lightSize, const Vector3* poly, int polyLength, const Vector3& polyCentroid, |
| 415 | VertexBuffer& shadowTriangleStrip) { |
ztenghui | 3bd3fa1 | 2014-08-25 14:42:27 -0700 | [diff] [blame] | 416 | if (CC_UNLIKELY(lightCenter.z <= 0)) { |
| 417 | ALOGW("Relative Light Z is not positive. No spot shadow!"); |
| 418 | return; |
| 419 | } |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 420 | if (CC_UNLIKELY(polyLength < 3)) { |
| 421 | #if DEBUG_SHADOW |
| 422 | ALOGW("Invalid polygon length. No spot shadow!"); |
| 423 | #endif |
| 424 | return; |
| 425 | } |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 426 | OutlineData outlineData[polyLength]; |
| 427 | Vector2 outlineCentroid; |
| 428 | // Calculate the projected outline for each polygon's vertices from the light center. |
| 429 | // |
| 430 | // O Light |
| 431 | // / |
| 432 | // / |
| 433 | // . Polygon vertex |
| 434 | // / |
| 435 | // / |
| 436 | // O Outline vertices |
| 437 | // |
| 438 | // Ratio = (Poly - Outline) / (Light - Poly) |
| 439 | // Outline.x = Poly.x - Ratio * (Light.x - Poly.x) |
| 440 | // Outline's radius / Light's radius = Ratio |
| 441 | |
| 442 | // Compute the last outline vertex to make sure we can get the normal and outline |
| 443 | // in one single loop. |
| 444 | projectCasterToOutline(outlineData[polyLength - 1].position, lightCenter, |
| 445 | poly[polyLength - 1]); |
| 446 | |
| 447 | // Take the outline's polygon, calculate the normal for each outline edge. |
| 448 | int currentNormalIndex = polyLength - 1; |
| 449 | int nextNormalIndex = 0; |
| 450 | |
| 451 | for (int i = 0; i < polyLength; i++) { |
| 452 | float ratioZ = projectCasterToOutline(outlineData[i].position, |
| 453 | lightCenter, poly[i]); |
| 454 | outlineData[i].radius = ratioZ * lightSize; |
| 455 | |
| 456 | outlineData[currentNormalIndex].normal = ShadowTessellator::calculateNormal( |
| 457 | outlineData[currentNormalIndex].position, |
| 458 | outlineData[nextNormalIndex].position); |
| 459 | currentNormalIndex = (currentNormalIndex + 1) % polyLength; |
| 460 | nextNormalIndex++; |
| 461 | } |
| 462 | |
| 463 | projectCasterToOutline(outlineCentroid, lightCenter, polyCentroid); |
| 464 | |
| 465 | int penumbraIndex = 0; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 466 | // Then each polygon's vertex produce at minmal 2 penumbra vertices. |
| 467 | // Since the size can be dynamic here, we keep track of the size and update |
| 468 | // the real size at the end. |
| 469 | int allocatedPenumbraLength = 2 * polyLength + SPOT_MAX_EXTRA_CORNER_VERTEX_NUMBER; |
| 470 | Vector2 penumbra[allocatedPenumbraLength]; |
| 471 | int totalExtraCornerSliceNumber = 0; |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 472 | |
| 473 | Vector2 umbra[polyLength]; |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 474 | |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 475 | // When centroid is covered by all circles from outline, then we consider |
| 476 | // the umbra is invalid, and we will tune down the shadow strength. |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 477 | bool hasValidUmbra = true; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 478 | // We need the minimal of RaitoVI to decrease the spot shadow strength accordingly. |
| 479 | float minRaitoVI = FLT_MAX; |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 480 | |
| 481 | for (int i = 0; i < polyLength; i++) { |
| 482 | // Generate all the penumbra's vertices only using the (outline vertex + normal * radius) |
| 483 | // There is no guarantee that the penumbra is still convex, but for |
| 484 | // each outline vertex, it will connect to all its corresponding penumbra vertices as |
| 485 | // triangle fans. And for neighber penumbra vertex, it will be a trapezoid. |
| 486 | // |
| 487 | // Penumbra Vertices marked as Pi |
| 488 | // Outline Vertices marked as Vi |
| 489 | // (P3) |
| 490 | // (P2) | ' (P4) |
| 491 | // (P1)' | | ' |
| 492 | // ' | | ' |
| 493 | // (P0) ------------------------------------------------(P5) |
| 494 | // | (V0) |(V1) |
| 495 | // | | |
| 496 | // | | |
| 497 | // | | |
| 498 | // | | |
| 499 | // | | |
| 500 | // | | |
| 501 | // | | |
| 502 | // | | |
| 503 | // (V3)-----------------------------------(V2) |
| 504 | int preNormalIndex = (i + polyLength - 1) % polyLength; |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 505 | |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 506 | const Vector2& previousNormal = outlineData[preNormalIndex].normal; |
| 507 | const Vector2& currentNormal = outlineData[i].normal; |
| 508 | |
| 509 | // Depending on how roundness we want for each corner, we can subdivide |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 510 | // further here and/or introduce some heuristic to decide how much the |
| 511 | // subdivision should be. |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 512 | int currentExtraSliceNumber = ShadowTessellator::getExtraVertexNumber( |
| 513 | previousNormal, currentNormal, SPOT_CORNER_RADIANS_DIVISOR); |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 514 | |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 515 | int currentCornerSliceNumber = 1 + currentExtraSliceNumber; |
| 516 | totalExtraCornerSliceNumber += currentExtraSliceNumber; |
| 517 | #if DEBUG_SHADOW |
| 518 | ALOGD("currentExtraSliceNumber should be %d", currentExtraSliceNumber); |
| 519 | ALOGD("currentCornerSliceNumber should be %d", currentCornerSliceNumber); |
| 520 | ALOGD("totalCornerSliceNumber is %d", totalExtraCornerSliceNumber); |
| 521 | #endif |
| 522 | if (CC_UNLIKELY(totalExtraCornerSliceNumber > SPOT_MAX_EXTRA_CORNER_VERTEX_NUMBER)) { |
| 523 | currentCornerSliceNumber = 1; |
| 524 | } |
| 525 | for (int k = 0; k <= currentCornerSliceNumber; k++) { |
| 526 | Vector2 avgNormal = |
| 527 | (previousNormal * (currentCornerSliceNumber - k) + currentNormal * k) / |
| 528 | currentCornerSliceNumber; |
| 529 | avgNormal.normalize(); |
| 530 | penumbra[penumbraIndex++] = outlineData[i].position + |
| 531 | avgNormal * outlineData[i].radius; |
| 532 | } |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 533 | |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 534 | |
| 535 | // Compute the umbra by the intersection from the outline's centroid! |
| 536 | // |
| 537 | // (V) ------------------------------------ |
| 538 | // | ' | |
| 539 | // | ' | |
| 540 | // | ' (I) | |
| 541 | // | ' | |
| 542 | // | ' (C) | |
| 543 | // | | |
| 544 | // | | |
| 545 | // | | |
| 546 | // | | |
| 547 | // ------------------------------------ |
| 548 | // |
| 549 | // Connect a line b/t the outline vertex (V) and the centroid (C), it will |
| 550 | // intersect with the outline vertex's circle at point (I). |
| 551 | // Now, ratioVI = VI / VC, ratioIC = IC / VC |
| 552 | // Then the intersetion point can be computed as Ixy = Vxy * ratioIC + Cxy * ratioVI; |
| 553 | // |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 554 | // When all of the outline circles cover the the outline centroid, (like I is |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 555 | // on the other side of C), there is no real umbra any more, so we just fake |
| 556 | // a small area around the centroid as the umbra, and tune down the spot |
| 557 | // shadow's umbra strength to simulate the effect the whole shadow will |
| 558 | // become lighter in this case. |
| 559 | // The ratio can be simulated by using the inverse of maximum of ratioVI for |
| 560 | // all (V). |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 561 | float distOutline = (outlineData[i].position - outlineCentroid).length(); |
ztenghui | 3bd3fa1 | 2014-08-25 14:42:27 -0700 | [diff] [blame] | 562 | if (CC_UNLIKELY(distOutline == 0)) { |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 563 | // If the outline has 0 area, then there is no spot shadow anyway. |
| 564 | ALOGW("Outline has 0 area, no spot shadow!"); |
| 565 | return; |
| 566 | } |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 567 | |
| 568 | float ratioVI = outlineData[i].radius / distOutline; |
| 569 | minRaitoVI = MathUtils::min(minRaitoVI, ratioVI); |
| 570 | if (ratioVI >= (1 - FAKE_UMBRA_SIZE_RATIO)) { |
| 571 | ratioVI = (1 - FAKE_UMBRA_SIZE_RATIO); |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 572 | } |
| 573 | // When we know we don't have valid umbra, don't bother to compute the |
| 574 | // values below. But we can't skip the loop yet since we want to know the |
| 575 | // maximum ratio. |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 576 | float ratioIC = 1 - ratioVI; |
| 577 | umbra[i] = outlineData[i].position * ratioIC + outlineCentroid * ratioVI; |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 578 | } |
| 579 | |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 580 | hasValidUmbra = (minRaitoVI <= 1.0); |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 581 | float shadowStrengthScale = 1.0; |
| 582 | if (!hasValidUmbra) { |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 583 | #if DEBUG_SHADOW |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 584 | ALOGW("The object is too close to the light or too small, no real umbra!"); |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 585 | #endif |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 586 | for (int i = 0; i < polyLength; i++) { |
| 587 | umbra[i] = outlineData[i].position * FAKE_UMBRA_SIZE_RATIO + |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 588 | outlineCentroid * (1 - FAKE_UMBRA_SIZE_RATIO); |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 589 | } |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 590 | shadowStrengthScale = 1.0 / minRaitoVI; |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 591 | } |
| 592 | |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 593 | int penumbraLength = penumbraIndex; |
| 594 | int umbraLength = polyLength; |
| 595 | |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 596 | #if DEBUG_SHADOW |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 597 | ALOGD("penumbraLength is %d , allocatedPenumbraLength %d", penumbraLength, allocatedPenumbraLength); |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 598 | dumpPolygon(poly, polyLength, "input poly"); |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 599 | dumpPolygon(penumbra, penumbraLength, "penumbra"); |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 600 | dumpPolygon(umbra, umbraLength, "umbra"); |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 601 | ALOGD("hasValidUmbra is %d and shadowStrengthScale is %f", hasValidUmbra, shadowStrengthScale); |
| 602 | #endif |
| 603 | |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 604 | // The penumbra and umbra needs to be in convex shape to keep consistency |
| 605 | // and quality. |
| 606 | // Since we are still shooting rays to penumbra, it needs to be convex. |
| 607 | // Umbra can be represented as a fan from the centroid, but visually umbra |
| 608 | // looks nicer when it is convex. |
| 609 | Vector2 finalUmbra[umbraLength]; |
| 610 | Vector2 finalPenumbra[penumbraLength]; |
| 611 | int finalUmbraLength = hull(umbra, umbraLength, finalUmbra); |
| 612 | int finalPenumbraLength = hull(penumbra, penumbraLength, finalPenumbra); |
| 613 | |
| 614 | generateTriangleStrip(isCasterOpaque, shadowStrengthScale, finalPenumbra, |
| 615 | finalPenumbraLength, finalUmbra, finalUmbraLength, poly, polyLength, |
| 616 | shadowTriangleStrip, outlineCentroid); |
| 617 | |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 618 | } |
| 619 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 620 | /** |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 621 | * This is only for experimental purpose. |
| 622 | * After intersections are calculated, we could smooth the polygon if needed. |
| 623 | * So far, we don't think it is more appealing yet. |
| 624 | * |
| 625 | * @param level The level of smoothness. |
| 626 | * @param rays The total number of rays. |
| 627 | * @param rayDist (In and Out) The distance for each ray. |
| 628 | * |
| 629 | */ |
| 630 | void SpotShadow::smoothPolygon(int level, int rays, float* rayDist) { |
| 631 | for (int k = 0; k < level; k++) { |
| 632 | for (int i = 0; i < rays; i++) { |
| 633 | float p1 = rayDist[(rays - 1 + i) % rays]; |
| 634 | float p2 = rayDist[i]; |
| 635 | float p3 = rayDist[(i + 1) % rays]; |
| 636 | rayDist[i] = (p1 + p2 * 2 + p3) / 4; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 641 | // Index pair is meant for storing the tessellation information for the penumbra |
| 642 | // area. One index must come from exterior tangent of the circles, the other one |
| 643 | // must come from the interior tangent of the circles. |
| 644 | struct IndexPair { |
| 645 | int outerIndex; |
| 646 | int innerIndex; |
| 647 | }; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 648 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 649 | // For one penumbra vertex, find the cloest umbra vertex and return its index. |
| 650 | inline int getClosestUmbraIndex(const Vector2& pivot, const Vector2* polygon, int polygonLength) { |
| 651 | float minLengthSquared = FLT_MAX; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 652 | int resultIndex = -1; |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 653 | bool hasDecreased = false; |
| 654 | // Starting with some negative offset, assuming both umbra and penumbra are starting |
| 655 | // at the same angle, this can help to find the result faster. |
| 656 | // Normally, loop 3 times, we can find the closest point. |
| 657 | int offset = polygonLength - 2; |
| 658 | for (int i = 0; i < polygonLength; i++) { |
| 659 | int currentIndex = (i + offset) % polygonLength; |
| 660 | float currentLengthSquared = (pivot - polygon[currentIndex]).lengthSquared(); |
| 661 | if (currentLengthSquared < minLengthSquared) { |
| 662 | if (minLengthSquared != FLT_MAX) { |
| 663 | hasDecreased = true; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 664 | } |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 665 | minLengthSquared = currentLengthSquared; |
| 666 | resultIndex = currentIndex; |
| 667 | } else if (currentLengthSquared > minLengthSquared && hasDecreased) { |
| 668 | // Early break b/c we have found the closet one and now the length |
| 669 | // is increasing again. |
| 670 | break; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 673 | if(resultIndex == -1) { |
| 674 | ALOGE("resultIndex is -1, the polygon must be invalid!"); |
| 675 | resultIndex = 0; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 676 | } |
| 677 | return resultIndex; |
| 678 | } |
| 679 | |
ztenghui | 3932063 | 2014-11-12 10:56:15 -0800 | [diff] [blame] | 680 | // Allow some epsilon here since the later ray intersection did allow for some small |
| 681 | // floating point error, when the intersection point is slightly outside the segment. |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 682 | inline bool sameDirections(bool isPositiveCross, float a, float b) { |
| 683 | if (isPositiveCross) { |
ztenghui | 3932063 | 2014-11-12 10:56:15 -0800 | [diff] [blame] | 684 | return a >= -EPSILON && b >= -EPSILON; |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 685 | } else { |
ztenghui | 3932063 | 2014-11-12 10:56:15 -0800 | [diff] [blame] | 686 | return a <= EPSILON && b <= EPSILON; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 687 | } |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 688 | } |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 689 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 690 | // Find the right polygon edge to shoot the ray at. |
| 691 | inline int findPolyIndex(bool isPositiveCross, int startPolyIndex, const Vector2& umbraDir, |
| 692 | const Vector2* polyToCentroid, int polyLength) { |
| 693 | // Make sure we loop with a bound. |
| 694 | for (int i = 0; i < polyLength; i++) { |
| 695 | int currentIndex = (i + startPolyIndex) % polyLength; |
| 696 | const Vector2& currentToCentroid = polyToCentroid[currentIndex]; |
| 697 | const Vector2& nextToCentroid = polyToCentroid[(currentIndex + 1) % polyLength]; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 698 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 699 | float currentCrossUmbra = currentToCentroid.cross(umbraDir); |
| 700 | float umbraCrossNext = umbraDir.cross(nextToCentroid); |
| 701 | if (sameDirections(isPositiveCross, currentCrossUmbra, umbraCrossNext)) { |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 702 | #if DEBUG_SHADOW |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 703 | ALOGD("findPolyIndex loop %d times , index %d", i, currentIndex ); |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 704 | #endif |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 705 | return currentIndex; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 706 | } |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 707 | } |
| 708 | LOG_ALWAYS_FATAL("Can't find the right polygon's edge from startPolyIndex %d", startPolyIndex); |
| 709 | return -1; |
| 710 | } |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 711 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 712 | // Generate the index pair for penumbra / umbra vertices, and more penumbra vertices |
| 713 | // if needed. |
| 714 | inline void genNewPenumbraAndPairWithUmbra(const Vector2* penumbra, int penumbraLength, |
| 715 | const Vector2* umbra, int umbraLength, Vector2* newPenumbra, int& newPenumbraIndex, |
| 716 | IndexPair* verticesPair, int& verticesPairIndex) { |
| 717 | // In order to keep everything in just one loop, we need to pre-compute the |
| 718 | // closest umbra vertex for the last penumbra vertex. |
| 719 | int previousClosestUmbraIndex = getClosestUmbraIndex(penumbra[penumbraLength - 1], |
| 720 | umbra, umbraLength); |
| 721 | for (int i = 0; i < penumbraLength; i++) { |
| 722 | const Vector2& currentPenumbraVertex = penumbra[i]; |
| 723 | // For current penumbra vertex, starting from previousClosestUmbraIndex, |
| 724 | // then check the next one until the distance increase. |
| 725 | // The last one before the increase is the umbra vertex we need to pair with. |
ztenghui | 3932063 | 2014-11-12 10:56:15 -0800 | [diff] [blame] | 726 | float currentLengthSquared = |
| 727 | (currentPenumbraVertex - umbra[previousClosestUmbraIndex]).lengthSquared(); |
| 728 | int currentClosestUmbraIndex = previousClosestUmbraIndex; |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 729 | int indexDelta = 0; |
| 730 | for (int j = 1; j < umbraLength; j++) { |
| 731 | int newUmbraIndex = (previousClosestUmbraIndex + j) % umbraLength; |
| 732 | float newLengthSquared = (currentPenumbraVertex - umbra[newUmbraIndex]).lengthSquared(); |
| 733 | if (newLengthSquared > currentLengthSquared) { |
ztenghui | 3932063 | 2014-11-12 10:56:15 -0800 | [diff] [blame] | 734 | // currentClosestUmbraIndex is the umbra vertex's index which has |
| 735 | // currently found smallest distance, so we can simply break here. |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 736 | break; |
| 737 | } else { |
| 738 | currentLengthSquared = newLengthSquared; |
| 739 | indexDelta++; |
ztenghui | 3932063 | 2014-11-12 10:56:15 -0800 | [diff] [blame] | 740 | currentClosestUmbraIndex = newUmbraIndex; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 741 | } |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 742 | } |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 743 | |
| 744 | if (indexDelta > 1) { |
| 745 | // For those umbra don't have penumbra, generate new penumbra vertices by interpolation. |
| 746 | // |
| 747 | // Assuming Pi for penumbra vertices, and Ui for umbra vertices. |
| 748 | // In the case like below P1 paired with U1 and P2 paired with U5. |
| 749 | // U2 to U4 are unpaired umbra vertices. |
| 750 | // |
| 751 | // P1 P2 |
| 752 | // | | |
| 753 | // U1 U2 U3 U4 U5 |
| 754 | // |
| 755 | // We will need to generate 3 more penumbra vertices P1.1, P1.2, P1.3 |
| 756 | // to pair with U2 to U4. |
| 757 | // |
| 758 | // P1 P1.1 P1.2 P1.3 P2 |
| 759 | // | | | | | |
| 760 | // U1 U2 U3 U4 U5 |
| 761 | // |
| 762 | // That distance ratio b/t Ui to U1 and Ui to U5 decides its paired penumbra |
| 763 | // vertex's location. |
| 764 | int newPenumbraNumber = indexDelta - 1; |
| 765 | |
| 766 | float accumulatedDeltaLength[newPenumbraNumber]; |
| 767 | float totalDeltaLength = 0; |
| 768 | |
| 769 | // To save time, cache the previous umbra vertex info outside the loop |
| 770 | // and update each loop. |
| 771 | Vector2 previousClosestUmbra = umbra[previousClosestUmbraIndex]; |
| 772 | Vector2 skippedUmbra; |
| 773 | // Use umbra data to precompute the length b/t unpaired umbra vertices, |
| 774 | // and its ratio against the total length. |
| 775 | for (int k = 0; k < indexDelta; k++) { |
| 776 | int skippedUmbraIndex = (previousClosestUmbraIndex + k + 1) % umbraLength; |
| 777 | skippedUmbra = umbra[skippedUmbraIndex]; |
| 778 | float currentDeltaLength = (skippedUmbra - previousClosestUmbra).length(); |
| 779 | |
| 780 | totalDeltaLength += currentDeltaLength; |
| 781 | accumulatedDeltaLength[k] = totalDeltaLength; |
| 782 | |
| 783 | previousClosestUmbra = skippedUmbra; |
| 784 | } |
| 785 | |
| 786 | const Vector2& previousPenumbra = penumbra[(i + penumbraLength - 1) % penumbraLength]; |
| 787 | // Then for each unpaired umbra vertex, create a new penumbra by the ratio, |
| 788 | // and pair them togehter. |
| 789 | for (int k = 0; k < newPenumbraNumber; k++) { |
| 790 | float weightForCurrentPenumbra = 1.0f; |
| 791 | if (totalDeltaLength != 0.0f) { |
| 792 | weightForCurrentPenumbra = accumulatedDeltaLength[k] / totalDeltaLength; |
| 793 | } |
| 794 | float weightForPreviousPenumbra = 1.0f - weightForCurrentPenumbra; |
| 795 | |
| 796 | Vector2 interpolatedPenumbra = currentPenumbraVertex * weightForCurrentPenumbra + |
| 797 | previousPenumbra * weightForPreviousPenumbra; |
| 798 | |
| 799 | int skippedUmbraIndex = (previousClosestUmbraIndex + k + 1) % umbraLength; |
Andreas Gampe | 1272887 | 2014-11-10 20:54:07 -0800 | [diff] [blame] | 800 | verticesPair[verticesPairIndex].outerIndex = newPenumbraIndex; |
| 801 | verticesPair[verticesPairIndex].innerIndex = skippedUmbraIndex; |
| 802 | verticesPairIndex++; |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 803 | newPenumbra[newPenumbraIndex++] = interpolatedPenumbra; |
| 804 | } |
| 805 | } |
Andreas Gampe | 1272887 | 2014-11-10 20:54:07 -0800 | [diff] [blame] | 806 | verticesPair[verticesPairIndex].outerIndex = newPenumbraIndex; |
| 807 | verticesPair[verticesPairIndex].innerIndex = currentClosestUmbraIndex; |
| 808 | verticesPairIndex++; |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 809 | newPenumbra[newPenumbraIndex++] = currentPenumbraVertex; |
| 810 | |
| 811 | previousClosestUmbraIndex = currentClosestUmbraIndex; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | // Precompute all the polygon's vector, return true if the reference cross product is positive. |
| 816 | inline bool genPolyToCentroid(const Vector2* poly2d, int polyLength, |
| 817 | const Vector2& centroid, Vector2* polyToCentroid) { |
| 818 | for (int j = 0; j < polyLength; j++) { |
| 819 | polyToCentroid[j] = poly2d[j] - centroid; |
ztenghui | 3932063 | 2014-11-12 10:56:15 -0800 | [diff] [blame] | 820 | // Normalize these vectors such that we can use epsilon comparison after |
| 821 | // computing their cross products with another normalized vector. |
| 822 | polyToCentroid[j].normalize(); |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 823 | } |
| 824 | float refCrossProduct = 0; |
| 825 | for (int j = 0; j < polyLength; j++) { |
| 826 | refCrossProduct = polyToCentroid[j].cross(polyToCentroid[(j + 1) % polyLength]); |
| 827 | if (refCrossProduct != 0) { |
| 828 | break; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 832 | return refCrossProduct > 0; |
| 833 | } |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 834 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 835 | // For one umbra vertex, shoot an ray from centroid to it. |
| 836 | // If the ray hit the polygon first, then return the intersection point as the |
| 837 | // closer vertex. |
| 838 | inline Vector2 getCloserVertex(const Vector2& umbraVertex, const Vector2& centroid, |
| 839 | const Vector2* poly2d, int polyLength, const Vector2* polyToCentroid, |
| 840 | bool isPositiveCross, int& previousPolyIndex) { |
| 841 | Vector2 umbraToCentroid = umbraVertex - centroid; |
| 842 | float distanceToUmbra = umbraToCentroid.length(); |
| 843 | umbraToCentroid = umbraToCentroid / distanceToUmbra; |
| 844 | |
| 845 | // previousPolyIndex is updated for each item such that we can minimize the |
| 846 | // looping inside findPolyIndex(); |
| 847 | previousPolyIndex = findPolyIndex(isPositiveCross, previousPolyIndex, |
| 848 | umbraToCentroid, polyToCentroid, polyLength); |
| 849 | |
| 850 | float dx = umbraToCentroid.x; |
| 851 | float dy = umbraToCentroid.y; |
| 852 | float distanceToIntersectPoly = rayIntersectPoints(centroid, dx, dy, |
| 853 | poly2d[previousPolyIndex], poly2d[(previousPolyIndex + 1) % polyLength]); |
| 854 | if (distanceToIntersectPoly < 0) { |
| 855 | distanceToIntersectPoly = 0; |
| 856 | } |
| 857 | |
| 858 | // Pick the closer one as the occluded area vertex. |
| 859 | Vector2 closerVertex; |
| 860 | if (distanceToIntersectPoly < distanceToUmbra) { |
| 861 | closerVertex.x = centroid.x + dx * distanceToIntersectPoly; |
| 862 | closerVertex.y = centroid.y + dy * distanceToIntersectPoly; |
| 863 | } else { |
| 864 | closerVertex = umbraVertex; |
| 865 | } |
| 866 | |
| 867 | return closerVertex; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | /** |
| 871 | * Generate a triangle strip given two convex polygon |
| 872 | **/ |
John Reck | f088c34 | 2014-12-12 09:49:29 -0800 | [diff] [blame] | 873 | void SpotShadow::generateTriangleStrip(bool isCasterOpaque, float shadowStrengthScale, |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 874 | Vector2* penumbra, int penumbraLength, Vector2* umbra, int umbraLength, |
| 875 | const Vector3* poly, int polyLength, VertexBuffer& shadowTriangleStrip, |
| 876 | const Vector2& centroid) { |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 877 | bool hasOccludedUmbraArea = false; |
| 878 | Vector2 poly2d[polyLength]; |
| 879 | |
| 880 | if (isCasterOpaque) { |
| 881 | for (int i = 0; i < polyLength; i++) { |
| 882 | poly2d[i].x = poly[i].x; |
| 883 | poly2d[i].y = poly[i].y; |
| 884 | } |
| 885 | // Make sure the centroid is inside the umbra, otherwise, fall back to the |
| 886 | // approach as if there is no occluded umbra area. |
| 887 | if (testPointInsidePolygon(centroid, poly2d, polyLength)) { |
| 888 | hasOccludedUmbraArea = true; |
| 889 | } |
| 890 | } |
| 891 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 892 | // For each penumbra vertex, find its corresponding closest umbra vertex index. |
| 893 | // |
| 894 | // Penumbra Vertices marked as Pi |
| 895 | // Umbra Vertices marked as Ui |
| 896 | // (P3) |
| 897 | // (P2) | ' (P4) |
| 898 | // (P1)' | | ' |
| 899 | // ' | | ' |
| 900 | // (P0) ------------------------------------------------(P5) |
| 901 | // | (U0) |(U1) |
| 902 | // | | |
| 903 | // | |(U2) (P5.1) |
| 904 | // | | |
| 905 | // | | |
| 906 | // | | |
| 907 | // | | |
| 908 | // | | |
| 909 | // | | |
| 910 | // (U4)-----------------------------------(U3) (P6) |
| 911 | // |
| 912 | // At least, like P0, P1, P2, they will find the matching umbra as U0. |
| 913 | // If we jump over some umbra vertex without matching penumbra vertex, then |
| 914 | // we will generate some new penumbra vertex by interpolation. Like P6 is |
| 915 | // matching U3, but U2 is not matched with any penumbra vertex. |
| 916 | // So interpolate P5.1 out and match U2. |
| 917 | // In this way, every umbra vertex will have a matching penumbra vertex. |
| 918 | // |
| 919 | // The total pair number can be as high as umbraLength + penumbraLength. |
| 920 | const int maxNewPenumbraLength = umbraLength + penumbraLength; |
| 921 | IndexPair verticesPair[maxNewPenumbraLength]; |
| 922 | int verticesPairIndex = 0; |
| 923 | |
| 924 | // Cache all the existing penumbra vertices and newly interpolated vertices into a |
| 925 | // a new array. |
| 926 | Vector2 newPenumbra[maxNewPenumbraLength]; |
| 927 | int newPenumbraIndex = 0; |
| 928 | |
| 929 | // For each penumbra vertex, find its closet umbra vertex by comparing the |
| 930 | // neighbor umbra vertices. |
| 931 | genNewPenumbraAndPairWithUmbra(penumbra, penumbraLength, umbra, umbraLength, newPenumbra, |
| 932 | newPenumbraIndex, verticesPair, verticesPairIndex); |
| 933 | ShadowTessellator::checkOverflow(verticesPairIndex, maxNewPenumbraLength, "Spot pair"); |
| 934 | ShadowTessellator::checkOverflow(newPenumbraIndex, maxNewPenumbraLength, "Spot new penumbra"); |
| 935 | #if DEBUG_SHADOW |
| 936 | for (int i = 0; i < umbraLength; i++) { |
| 937 | ALOGD("umbra i %d, [%f, %f]", i, umbra[i].x, umbra[i].y); |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 938 | } |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 939 | for (int i = 0; i < newPenumbraIndex; i++) { |
| 940 | ALOGD("new penumbra i %d, [%f, %f]", i, newPenumbra[i].x, newPenumbra[i].y); |
| 941 | } |
| 942 | for (int i = 0; i < verticesPairIndex; i++) { |
| 943 | ALOGD("index i %d, [%d, %d]", i, verticesPair[i].outerIndex, verticesPair[i].innerIndex); |
| 944 | } |
| 945 | #endif |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 946 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 947 | // For the size of vertex buffer, we need 3 rings, one has newPenumbraSize, |
| 948 | // one has umbraLength, the last one has at most umbraLength. |
| 949 | // |
| 950 | // For the size of index buffer, the umbra area needs (2 * umbraLength + 2). |
| 951 | // The penumbra one can vary a bit, but it is bounded by (2 * verticesPairIndex + 2). |
| 952 | // And 2 more for jumping between penumbra to umbra. |
| 953 | const int newPenumbraLength = newPenumbraIndex; |
| 954 | const int totalVertexCount = newPenumbraLength + umbraLength * 2; |
| 955 | const int totalIndexCount = 2 * umbraLength + 2 * verticesPairIndex + 6; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 956 | AlphaVertex* shadowVertices = |
| 957 | shadowTriangleStrip.alloc<AlphaVertex>(totalVertexCount); |
| 958 | uint16_t* indexBuffer = |
| 959 | shadowTriangleStrip.allocIndices<uint16_t>(totalIndexCount); |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 960 | int vertexBufferIndex = 0; |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 961 | int indexBufferIndex = 0; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 962 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 963 | // Fill the IB and VB for the penumbra area. |
| 964 | for (int i = 0; i < newPenumbraLength; i++) { |
| 965 | AlphaVertex::set(&shadowVertices[vertexBufferIndex++], newPenumbra[i].x, |
| 966 | newPenumbra[i].y, 0.0f); |
| 967 | } |
| 968 | for (int i = 0; i < umbraLength; i++) { |
| 969 | AlphaVertex::set(&shadowVertices[vertexBufferIndex++], umbra[i].x, umbra[i].y, |
| 970 | M_PI); |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 971 | } |
| 972 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 973 | for (int i = 0; i < verticesPairIndex; i++) { |
| 974 | indexBuffer[indexBufferIndex++] = verticesPair[i].outerIndex; |
| 975 | // All umbra index need to be offseted by newPenumbraSize. |
| 976 | indexBuffer[indexBufferIndex++] = verticesPair[i].innerIndex + newPenumbraLength; |
| 977 | } |
| 978 | indexBuffer[indexBufferIndex++] = verticesPair[0].outerIndex; |
| 979 | indexBuffer[indexBufferIndex++] = verticesPair[0].innerIndex + newPenumbraLength; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 980 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 981 | // Now fill the IB and VB for the umbra area. |
| 982 | // First duplicated the index from previous strip and the first one for the |
| 983 | // degenerated triangles. |
| 984 | indexBuffer[indexBufferIndex] = indexBuffer[indexBufferIndex - 1]; |
| 985 | indexBufferIndex++; |
| 986 | indexBuffer[indexBufferIndex++] = newPenumbraLength + 0; |
| 987 | // Save the first VB index for umbra area in order to close the loop. |
| 988 | int savedStartIndex = vertexBufferIndex; |
| 989 | |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 990 | if (hasOccludedUmbraArea) { |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 991 | // Precompute all the polygon's vector, and the reference cross product, |
| 992 | // in order to find the right polygon edge for the ray to intersect. |
| 993 | Vector2 polyToCentroid[polyLength]; |
| 994 | bool isPositiveCross = genPolyToCentroid(poly2d, polyLength, centroid, polyToCentroid); |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 995 | |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 996 | // Because both the umbra and polygon are going in the same direction, |
| 997 | // we can save the previous polygon index to make sure we have less polygon |
| 998 | // vertex to compute for each ray. |
| 999 | int previousPolyIndex = 0; |
| 1000 | for (int i = 0; i < umbraLength; i++) { |
| 1001 | // Shoot a ray from centroid to each umbra vertices and pick the one with |
| 1002 | // shorter distance to the centroid, b/t the umbra vertex or the intersection point. |
| 1003 | Vector2 closerVertex = getCloserVertex(umbra[i], centroid, poly2d, polyLength, |
| 1004 | polyToCentroid, isPositiveCross, previousPolyIndex); |
| 1005 | |
| 1006 | // We already stored the umbra vertices, just need to add the occlued umbra's ones. |
| 1007 | indexBuffer[indexBufferIndex++] = newPenumbraLength + i; |
| 1008 | indexBuffer[indexBufferIndex++] = vertexBufferIndex; |
| 1009 | AlphaVertex::set(&shadowVertices[vertexBufferIndex++], |
| 1010 | closerVertex.x, closerVertex.y, M_PI); |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 1011 | } |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 1012 | } else { |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 1013 | // If there is no occluded umbra at all, then draw the triangle fan |
| 1014 | // starting from the centroid to all umbra vertices. |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 1015 | int lastCentroidIndex = vertexBufferIndex; |
| 1016 | AlphaVertex::set(&shadowVertices[vertexBufferIndex++], centroid.x, |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 1017 | centroid.y, M_PI); |
| 1018 | for (int i = 0; i < umbraLength; i++) { |
| 1019 | indexBuffer[indexBufferIndex++] = newPenumbraLength + i; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 1020 | indexBuffer[indexBufferIndex++] = lastCentroidIndex; |
| 1021 | } |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 1022 | } |
ztenghui | d2dcd6f | 2014-10-29 16:04:29 -0700 | [diff] [blame] | 1023 | // Closing the umbra area triangle's loop here. |
| 1024 | indexBuffer[indexBufferIndex++] = newPenumbraLength; |
| 1025 | indexBuffer[indexBufferIndex++] = savedStartIndex; |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 1026 | |
| 1027 | // At the end, update the real index and vertex buffer size. |
| 1028 | shadowTriangleStrip.updateVertexCount(vertexBufferIndex); |
| 1029 | shadowTriangleStrip.updateIndexCount(indexBufferIndex); |
| 1030 | ShadowTessellator::checkOverflow(vertexBufferIndex, totalVertexCount, "Spot Vertex Buffer"); |
| 1031 | ShadowTessellator::checkOverflow(indexBufferIndex, totalIndexCount, "Spot Index Buffer"); |
| 1032 | |
| 1033 | shadowTriangleStrip.setMode(VertexBuffer::kIndices); |
| 1034 | shadowTriangleStrip.computeBounds<AlphaVertex>(); |
| 1035 | } |
| 1036 | |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 1037 | #if DEBUG_SHADOW |
| 1038 | |
| 1039 | #define TEST_POINT_NUMBER 128 |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 1040 | /** |
| 1041 | * Calculate the bounds for generating random test points. |
| 1042 | */ |
| 1043 | void SpotShadow::updateBound(const Vector2 inVector, Vector2& lowerBound, |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 1044 | Vector2& upperBound) { |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 1045 | if (inVector.x < lowerBound.x) { |
| 1046 | lowerBound.x = inVector.x; |
| 1047 | } |
| 1048 | |
| 1049 | if (inVector.y < lowerBound.y) { |
| 1050 | lowerBound.y = inVector.y; |
| 1051 | } |
| 1052 | |
| 1053 | if (inVector.x > upperBound.x) { |
| 1054 | upperBound.x = inVector.x; |
| 1055 | } |
| 1056 | |
| 1057 | if (inVector.y > upperBound.y) { |
| 1058 | upperBound.y = inVector.y; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * For debug purpose, when things go wrong, dump the whole polygon data. |
| 1064 | */ |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 1065 | void SpotShadow::dumpPolygon(const Vector2* poly, int polyLength, const char* polyName) { |
| 1066 | for (int i = 0; i < polyLength; i++) { |
| 1067 | ALOGD("polygon %s i %d x %f y %f", polyName, i, poly[i].x, poly[i].y); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * For debug purpose, when things go wrong, dump the whole polygon data. |
| 1073 | */ |
| 1074 | void SpotShadow::dumpPolygon(const Vector3* poly, int polyLength, const char* polyName) { |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 1075 | for (int i = 0; i < polyLength; i++) { |
| 1076 | ALOGD("polygon %s i %d x %f y %f", polyName, i, poly[i].x, poly[i].y); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | /** |
| 1081 | * Test whether the polygon is convex. |
| 1082 | */ |
| 1083 | bool SpotShadow::testConvex(const Vector2* polygon, int polygonLength, |
| 1084 | const char* name) { |
| 1085 | bool isConvex = true; |
| 1086 | for (int i = 0; i < polygonLength; i++) { |
| 1087 | Vector2 start = polygon[i]; |
| 1088 | Vector2 middle = polygon[(i + 1) % polygonLength]; |
| 1089 | Vector2 end = polygon[(i + 2) % polygonLength]; |
| 1090 | |
ztenghui | 9122b1b | 2014-10-03 11:21:11 -0700 | [diff] [blame] | 1091 | float delta = (float(middle.x) - start.x) * (float(end.y) - start.y) - |
| 1092 | (float(middle.y) - start.y) * (float(end.x) - start.x); |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 1093 | bool isCCWOrCoLinear = (delta >= EPSILON); |
| 1094 | |
| 1095 | if (isCCWOrCoLinear) { |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 1096 | 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] | 1097 | "middle (x %f, y %f) and end (x %f, y %f) , delta is %f !!!", |
| 1098 | name, start.x, start.y, middle.x, middle.y, end.x, end.y, delta); |
| 1099 | isConvex = false; |
| 1100 | break; |
| 1101 | } |
| 1102 | } |
| 1103 | return isConvex; |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * Test whether or not the polygon (intersection) is within the 2 input polygons. |
| 1108 | * Using Marte Carlo method, we generate a random point, and if it is inside the |
| 1109 | * intersection, then it must be inside both source polygons. |
| 1110 | */ |
| 1111 | void SpotShadow::testIntersection(const Vector2* poly1, int poly1Length, |
| 1112 | const Vector2* poly2, int poly2Length, |
| 1113 | const Vector2* intersection, int intersectionLength) { |
| 1114 | // Find the min and max of x and y. |
ztenghui | c50a03d | 2014-08-21 13:47:54 -0700 | [diff] [blame] | 1115 | Vector2 lowerBound = {FLT_MAX, FLT_MAX}; |
| 1116 | Vector2 upperBound = {-FLT_MAX, -FLT_MAX}; |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 1117 | for (int i = 0; i < poly1Length; i++) { |
| 1118 | updateBound(poly1[i], lowerBound, upperBound); |
| 1119 | } |
| 1120 | for (int i = 0; i < poly2Length; i++) { |
| 1121 | updateBound(poly2[i], lowerBound, upperBound); |
| 1122 | } |
| 1123 | |
| 1124 | bool dumpPoly = false; |
| 1125 | for (int k = 0; k < TEST_POINT_NUMBER; k++) { |
| 1126 | // Generate a random point between minX, minY and maxX, maxY. |
ztenghui | 9122b1b | 2014-10-03 11:21:11 -0700 | [diff] [blame] | 1127 | float randomX = rand() / float(RAND_MAX); |
| 1128 | float randomY = rand() / float(RAND_MAX); |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 1129 | |
| 1130 | Vector2 testPoint; |
| 1131 | testPoint.x = lowerBound.x + randomX * (upperBound.x - lowerBound.x); |
| 1132 | testPoint.y = lowerBound.y + randomY * (upperBound.y - lowerBound.y); |
| 1133 | |
| 1134 | // If the random point is in both poly 1 and 2, then it must be intersection. |
| 1135 | if (testPointInsidePolygon(testPoint, intersection, intersectionLength)) { |
| 1136 | if (!testPointInsidePolygon(testPoint, poly1, poly1Length)) { |
| 1137 | dumpPoly = true; |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 1138 | ALOGW("(Error Type 1): one point (%f, %f) in the intersection is" |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 1139 | " not in the poly1", |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 1140 | testPoint.x, testPoint.y); |
| 1141 | } |
| 1142 | |
| 1143 | if (!testPointInsidePolygon(testPoint, poly2, poly2Length)) { |
| 1144 | dumpPoly = true; |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 1145 | ALOGW("(Error Type 1): one point (%f, %f) in the intersection is" |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 1146 | " not in the poly2", |
ztenghui | f5ca8b4 | 2014-01-27 15:53:28 -0800 | [diff] [blame] | 1147 | testPoint.x, testPoint.y); |
| 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | if (dumpPoly) { |
| 1153 | dumpPolygon(intersection, intersectionLength, "intersection"); |
| 1154 | for (int i = 1; i < intersectionLength; i++) { |
| 1155 | Vector2 delta = intersection[i] - intersection[i - 1]; |
| 1156 | ALOGD("Intersetion i, %d Vs i-1 is delta %f", i, delta.lengthSquared()); |
| 1157 | } |
| 1158 | |
| 1159 | dumpPolygon(poly1, poly1Length, "poly 1"); |
| 1160 | dumpPolygon(poly2, poly2Length, "poly 2"); |
| 1161 | } |
| 1162 | } |
| 1163 | #endif |
| 1164 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 1165 | }; // namespace uirenderer |
| 1166 | }; // namespace android |