17b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/*
27b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Copyright (C) 2014 The Android Open Source Project
37b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
47b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Licensed under the Apache License, Version 2.0 (the "License");
57b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * you may not use this file except in compliance with the License.
67b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * You may obtain a copy of the License at
77b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
87b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *      http://www.apache.org/licenses/LICENSE-2.0
97b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
107b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Unless required by applicable law or agreed to in writing, software
117b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * distributed under the License is distributed on an "AS IS" BASIS,
127b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * See the License for the specific language governing permissions and
147b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * limitations under the License.
157b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
167b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
17512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui// The highest z value can't be higher than (CASTER_Z_CAP_RATIO * light.z)
18c50a03d78aaedd0003377e98710e7038bda330e9ztenghui#define CASTER_Z_CAP_RATIO 0.95f
19512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
20512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui// When there is no umbra, then just fake the umbra using
21512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui// centroid * (1 - FAKE_UMBRA_SIZE_RATIO) + outline * FAKE_UMBRA_SIZE_RATIO
22512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#define FAKE_UMBRA_SIZE_RATIO 0.05f
23512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
24512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui// When the polygon is about 90 vertices, the penumbra + umbra can reach 270 rays.
25512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui// That is consider pretty fine tessllated polygon so far.
26512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui// This is just to prevent using too much some memory when edge slicing is not
27512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui// needed any more.
28512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#define FINE_TESSELLATED_POLYGON_RAY_NUMBER 270
29512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui/**
30512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * Extra vertices for the corner for smoother corner.
31512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * Only for outer loop.
32512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * Note that we use such extra memory to avoid an extra loop.
33512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui */
34512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui// For half circle, we could add EXTRA_VERTEX_PER_PI vertices.
35512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui// Set to 1 if we don't want to have any.
36512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#define SPOT_EXTRA_CORNER_VERTEX_PER_PI 18
37512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
38512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui// For the whole polygon, the sum of all the deltas b/t normals is 2 * M_PI,
39512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui// therefore, the maximum number of extra vertices will be twice bigger.
40512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#define SPOT_MAX_EXTRA_CORNER_VERTEX_NUMBER  (2 * SPOT_EXTRA_CORNER_VERTEX_PER_PI)
41512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
42512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui// For each RADIANS_DIVISOR, we would allocate one more vertex b/t the normals.
43512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#define SPOT_CORNER_RADIANS_DIVISOR (M_PI / SPOT_EXTRA_CORNER_VERTEX_PER_PI)
44512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
45138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik#define PENUMBRA_ALPHA 0.0f
46138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik#define UMBRA_ALPHA 1.0f
477b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
489db58c031f8ffa102a6d585cb585bed3bdb911a9Chris Craik#include "SpotShadow.h"
497b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
5063d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui#include "ShadowTessellator.h"
517b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui#include "Vertex.h"
522dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson#include "VertexBuffer.h"
53c50a03d78aaedd0003377e98710e7038bda330e9ztenghui#include "utils/MathUtils.h"
547b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
559db58c031f8ffa102a6d585cb585bed3bdb911a9Chris Craik#include <algorithm>
569db58c031f8ffa102a6d585cb585bed3bdb911a9Chris Craik#include <math.h>
579db58c031f8ffa102a6d585cb585bed3bdb911a9Chris Craik#include <stdlib.h>
589db58c031f8ffa102a6d585cb585bed3bdb911a9Chris Craik#include <utils/Log.h>
599db58c031f8ffa102a6d585cb585bed3bdb911a9Chris Craik
60c50a03d78aaedd0003377e98710e7038bda330e9ztenghui// TODO: After we settle down the new algorithm, we can remove the old one and
61c50a03d78aaedd0003377e98710e7038bda330e9ztenghui// its utility functions.
62c50a03d78aaedd0003377e98710e7038bda330e9ztenghui// Right now, we still need to keep it for comparison purpose and future expansion.
637b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuinamespace android {
647b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuinamespace uirenderer {
657b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
669122b1b168d2a74d51517ed7282f4d6a8adea367ztenghuistatic const float EPSILON = 1e-7;
67726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik
68726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik/**
69c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * For each polygon's vertex, the light center will project it to the receiver
70c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * as one of the outline vertex.
71c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * For each outline vertex, we need to store the position and normal.
72c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * Normal here is defined against the edge by the current vertex and the next vertex.
73c50a03d78aaedd0003377e98710e7038bda330e9ztenghui */
74c50a03d78aaedd0003377e98710e7038bda330e9ztenghuistruct OutlineData {
75c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    Vector2 position;
76c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    Vector2 normal;
77c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    float radius;
78c50a03d78aaedd0003377e98710e7038bda330e9ztenghui};
79c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
80c50a03d78aaedd0003377e98710e7038bda330e9ztenghui/**
81512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * For each vertex, we need to keep track of its angle, whether it is penumbra or
82512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * umbra, and its corresponding vertex index.
83512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui */
84512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghuistruct SpotShadow::VertexAngleData {
85512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // The angle to the vertex from the centroid.
86512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    float mAngle;
87512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // True is the vertex comes from penumbra, otherwise it comes from umbra.
88512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    bool mIsPenumbra;
89512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // The index of the vertex described by this data.
90512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int mVertexIndex;
91512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    void set(float angle, bool isPenumbra, int index) {
92512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        mAngle = angle;
93512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        mIsPenumbra = isPenumbra;
94512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        mVertexIndex = index;
95512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
96512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui};
97512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
98512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui/**
99726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik * Calculate the angle between and x and a y coordinate.
100726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik * The atan2 range from -PI to PI.
101726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik */
102b79a3e301a8d89b9e1b1f6f3d7fd6aa56610a6f0Chris Craikstatic float angle(const Vector2& point, const Vector2& center) {
103726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    return atan2(point.y - center.y, point.x - center.x);
104726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik}
105726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik
1067b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
107726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik * Calculate the intersection of a ray with the line segment defined by two points.
1087b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
109726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik * Returns a negative value in error conditions.
110726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik
111726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik * @param rayOrigin The start of the ray
112726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik * @param dx The x vector of the ray
113726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik * @param dy The y vector of the ray
114726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik * @param p1 The first point defining the line segment
115726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik * @param p2 The second point defining the line segment
116726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik * @return The distance along the ray if it intersects with the line segment, negative if otherwise
1177b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
118b79a3e301a8d89b9e1b1f6f3d7fd6aa56610a6f0Chris Craikstatic float rayIntersectPoints(const Vector2& rayOrigin, float dx, float dy,
119726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        const Vector2& p1, const Vector2& p2) {
120726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    // The math below is derived from solving this formula, basically the
121726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    // intersection point should stay on both the ray and the edge of (p1, p2).
122726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    // solve([p1x+t*(p2x-p1x)=dx*t2+px,p1y+t*(p2y-p1y)=dy*t2+py],[t,t2]);
123726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik
1249122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui    float divisor = (dx * (p1.y - p2.y) + dy * p2.x - dy * p1.x);
125726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    if (divisor == 0) return -1.0f; // error, invalid divisor
126726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik
127726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik#if DEBUG_SHADOW
1289122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui    float interpVal = (dx * (p1.y - rayOrigin.y) + dy * rayOrigin.x - dy * p1.x) / divisor;
12999af9429cda84ad0af1d7fcecb580295b0046882ztenghui    if (interpVal < 0 || interpVal > 1) {
13099af9429cda84ad0af1d7fcecb580295b0046882ztenghui        ALOGW("rayIntersectPoints is hitting outside the segment %f", interpVal);
13199af9429cda84ad0af1d7fcecb580295b0046882ztenghui    }
132726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik#endif
133726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik
1349122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui    float distance = (p1.x * (rayOrigin.y - p2.y) + p2.x * (p1.y - rayOrigin.y) +
135726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik            rayOrigin.x * (p2.y - p1.y)) / divisor;
136726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik
137726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    return distance; // may be negative in error cases
1387b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
1397b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1407b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
1417b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Sort points by their X coordinates
1427b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
1437b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param points the points as a Vector2 array.
1447b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param pointsLength the number of vertices of the polygon.
1457b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
1467b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuivoid SpotShadow::xsort(Vector2* points, int pointsLength) {
1471e4209e3871493ccfb09e43634d4082f49c227beJohn Reck    auto cmp = [](const Vector2& a, const Vector2& b) -> bool {
1481e4209e3871493ccfb09e43634d4082f49c227beJohn Reck        return a.x < b.x;
1491e4209e3871493ccfb09e43634d4082f49c227beJohn Reck    };
1501e4209e3871493ccfb09e43634d4082f49c227beJohn Reck    std::sort(points, points + pointsLength, cmp);
1517b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
1527b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1537b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
1547b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * compute the convex hull of a collection of Points
1557b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
1567b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param points the points as a Vector2 array.
1577b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param pointsLength the number of vertices of the polygon.
1587b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param retPoly pre allocated array of floats to put the vertices
1597b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @return the number of points in the polygon 0 if no intersection
1607b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
1617b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuiint SpotShadow::hull(Vector2* points, int pointsLength, Vector2* retPoly) {
1627b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    xsort(points, pointsLength);
1637b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    int n = pointsLength;
1647b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    Vector2 lUpper[n];
1657b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    lUpper[0] = points[0];
1667b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    lUpper[1] = points[1];
1677b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1687b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    int lUpperSize = 2;
1697b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1707b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = 2; i < n; i++) {
1717b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        lUpper[lUpperSize] = points[i];
1727b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        lUpperSize++;
1737b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
174f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        while (lUpperSize > 2 && !ccw(
175f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                lUpper[lUpperSize - 3].x, lUpper[lUpperSize - 3].y,
176f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                lUpper[lUpperSize - 2].x, lUpper[lUpperSize - 2].y,
177f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                lUpper[lUpperSize - 1].x, lUpper[lUpperSize - 1].y)) {
1787b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            // Remove the middle point of the three last
1797b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            lUpper[lUpperSize - 2].x = lUpper[lUpperSize - 1].x;
1807b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            lUpper[lUpperSize - 2].y = lUpper[lUpperSize - 1].y;
1817b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            lUpperSize--;
1827b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        }
1837b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
1847b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1857b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    Vector2 lLower[n];
1867b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    lLower[0] = points[n - 1];
1877b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    lLower[1] = points[n - 2];
1887b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1897b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    int lLowerSize = 2;
1907b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1917b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = n - 3; i >= 0; i--) {
1927b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        lLower[lLowerSize] = points[i];
1937b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        lLowerSize++;
1947b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
195f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        while (lLowerSize > 2 && !ccw(
196f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                lLower[lLowerSize - 3].x, lLower[lLowerSize - 3].y,
197f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                lLower[lLowerSize - 2].x, lLower[lLowerSize - 2].y,
198f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                lLower[lLowerSize - 1].x, lLower[lLowerSize - 1].y)) {
1997b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            // Remove the middle point of the three last
2007b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            lLower[lLowerSize - 2] = lLower[lLowerSize - 1];
2017b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            lLowerSize--;
2027b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        }
2037b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
2047b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
205726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    // output points in CW ordering
206726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    const int total = lUpperSize + lLowerSize - 2;
207726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    int outIndex = total - 1;
2087b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = 0; i < lUpperSize; i++) {
209726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        retPoly[outIndex] = lUpper[i];
210726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        outIndex--;
2117b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
2127b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2137b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = 1; i < lLowerSize - 1; i++) {
214726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        retPoly[outIndex] = lLower[i];
215726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        outIndex--;
2167b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
2177b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    // TODO: Add test harness which verify that all the points are inside the hull.
218726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    return total;
2197b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
2207b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2217b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
222f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * Test whether the 3 points form a counter clockwise turn.
2237b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
2247b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @return true if a right hand turn
2257b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
2269122b1b168d2a74d51517ed7282f4d6a8adea367ztenghuibool SpotShadow::ccw(float ax, float ay, float bx, float by,
2279122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float cx, float cy) {
2287b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    return (bx - ax) * (cy - ay) - (by - ay) * (cx - ax) > EPSILON;
2297b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
2307b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2317b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
2327b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Sort points about a center point
2337b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
2347b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param poly The in and out polyogon as a Vector2 array.
2357b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param polyLength The number of vertices of the polygon.
2367b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param center the center ctr[0] = x , ctr[1] = y to sort around.
2377b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
2387b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuivoid SpotShadow::sort(Vector2* poly, int polyLength, const Vector2& center) {
2397b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    quicksortCirc(poly, 0, polyLength - 1, center);
2407b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
2417b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2427b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
2437b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Swap points pointed to by i and j
2447b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
2457b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuivoid SpotShadow::swap(Vector2* points, int i, int j) {
2467b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    Vector2 temp = points[i];
2477b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    points[i] = points[j];
2487b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    points[j] = temp;
2497b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
2507b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2517b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
2527b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * quick sort implementation about the center.
2537b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
2547b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuivoid SpotShadow::quicksortCirc(Vector2* points, int low, int high,
2557b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        const Vector2& center) {
2567b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    int i = low, j = high;
2577b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    int p = low + (high - low) / 2;
2587b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    float pivot = angle(points[p], center);
2597b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    while (i <= j) {
260726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        while (angle(points[i], center) > pivot) {
2617b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            i++;
2627b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        }
263726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        while (angle(points[j], center) < pivot) {
2647b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            j--;
2657b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        }
2667b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2677b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        if (i <= j) {
2687b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            swap(points, i, j);
2697b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            i++;
2707b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            j--;
2717b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        }
2727b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
2737b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    if (low < j) quicksortCirc(points, low, j, center);
2747b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    if (i < high) quicksortCirc(points, i, high, center);
2757b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
2767b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2777b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
2787b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Test whether a point is inside the polygon.
2797b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
2807b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param testPoint the point to test
2817b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param poly the polygon
2827b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @return true if the testPoint is inside the poly.
2837b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
2847b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuibool SpotShadow::testPointInsidePolygon(const Vector2 testPoint,
2857b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        const Vector2* poly, int len) {
2867b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    bool c = false;
2879122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui    float testx = testPoint.x;
2889122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui    float testy = testPoint.y;
2897b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = 0, j = len - 1; i < len; j = i++) {
2909122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float startX = poly[j].x;
2919122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float startY = poly[j].y;
2929122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float endX = poly[i].x;
2939122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float endY = poly[i].y;
2947b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
295512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        if (((endY > testy) != (startY > testy))
296512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            && (testx < (startX - endX) * (testy - endY)
2977b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui             / (startY - endY) + endX)) {
2987b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            c = !c;
2997b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        }
3007b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
3017b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    return c;
3027b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
3037b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
3047b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
3057b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Make the polygon turn clockwise.
3067b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
3077b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param polygon the polygon as a Vector2 array.
3087b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param len the number of points of the polygon
3097b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
3107b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuivoid SpotShadow::makeClockwise(Vector2* polygon, int len) {
311d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    if (polygon == nullptr  || len == 0) {
3127b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        return;
3137b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
3142e023f3827dfc0dfc1ed7c3dd54d02b4a993f0b4ztenghui    if (!ShadowTessellator::isClockwise(polygon, len)) {
3157b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        reverse(polygon, len);
3167b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
3177b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
3187b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
3197b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
3207b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Reverse the polygon
3217b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
3227b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param polygon the polygon as a Vector2 array
3237b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param len the number of points of the polygon
3247b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
3257b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuivoid SpotShadow::reverse(Vector2* polygon, int len) {
3267b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    int n = len / 2;
3277b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = 0; i < n; i++) {
3287b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        Vector2 tmp = polygon[i];
3297b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        int k = len - 1 - i;
3307b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        polygon[i] = polygon[k];
3317b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        polygon[k] = tmp;
3327b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
3337b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
3347b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
3357b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
3367b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Compute a horizontal circular polygon about point (x , y , height) of radius
3377b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * (size)
3387b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
3397b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param points number of the points of the output polygon.
3407b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param lightCenter the center of the light.
3417b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param size the light size.
3427b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param ret result polygon.
3437b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
3447b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuivoid SpotShadow::computeLightPolygon(int points, const Vector3& lightCenter,
3457b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        float size, Vector3* ret) {
3467b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    // TODO: Caching all the sin / cos values and store them in a look up table.
3477b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = 0; i < points; i++) {
3489122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float angle = 2 * i * M_PI / points;
349726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        ret[i].x = cosf(angle) * size + lightCenter.x;
350726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        ret[i].y = sinf(angle) * size + lightCenter.y;
3517b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        ret[i].z = lightCenter.z;
3527b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
3537b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
3547b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
3557b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
356512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * From light center, project one vertex to the z=0 surface and get the outline.
3577b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
358512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @param outline The result which is the outline position.
359512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @param lightCenter The center of light.
360512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @param polyVertex The input polygon's vertex.
361512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui *
362512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @return float The ratio of (polygon.z / light.z - polygon.z)
3637b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
364c50a03d78aaedd0003377e98710e7038bda330e9ztenghuifloat SpotShadow::projectCasterToOutline(Vector2& outline,
365c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        const Vector3& lightCenter, const Vector3& polyVertex) {
366c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    float lightToPolyZ = lightCenter.z - polyVertex.z;
367c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    float ratioZ = CASTER_Z_CAP_RATIO;
368c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    if (lightToPolyZ != 0) {
369c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // If any caster's vertex is almost above the light, we just keep it as 95%
370c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // of the height of the light.
3713bd3fa1f1d437e22aee35381a559dcee15a437ddztenghui        ratioZ = MathUtils::clamp(polyVertex.z / lightToPolyZ, 0.0f, CASTER_Z_CAP_RATIO);
372c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    }
373c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
374c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    outline.x = polyVertex.x - ratioZ * (lightCenter.x - polyVertex.x);
375c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    outline.y = polyVertex.y - ratioZ * (lightCenter.y - polyVertex.y);
376c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    return ratioZ;
377c50a03d78aaedd0003377e98710e7038bda330e9ztenghui}
378c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
379c50a03d78aaedd0003377e98710e7038bda330e9ztenghui/**
380c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * Generate the shadow spot light of shape lightPoly and a object poly
381c50a03d78aaedd0003377e98710e7038bda330e9ztenghui *
382c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * @param isCasterOpaque whether the caster is opaque
383c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * @param lightCenter the center of the light
384c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * @param lightSize the radius of the light
385c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * @param poly x,y,z vertexes of a convex polygon that occludes the light source
386c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * @param polyLength number of vertexes of the occluding polygon
387c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * @param shadowTriangleStrip return an (x,y,alpha) triangle strip representing the shadow. Return
388c50a03d78aaedd0003377e98710e7038bda330e9ztenghui *                            empty strip if error.
389c50a03d78aaedd0003377e98710e7038bda330e9ztenghui */
390c50a03d78aaedd0003377e98710e7038bda330e9ztenghuivoid SpotShadow::createSpotShadow(bool isCasterOpaque, const Vector3& lightCenter,
391c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        float lightSize, const Vector3* poly, int polyLength, const Vector3& polyCentroid,
392c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        VertexBuffer& shadowTriangleStrip) {
3933bd3fa1f1d437e22aee35381a559dcee15a437ddztenghui    if (CC_UNLIKELY(lightCenter.z <= 0)) {
3943bd3fa1f1d437e22aee35381a559dcee15a437ddztenghui        ALOGW("Relative Light Z is not positive. No spot shadow!");
3953bd3fa1f1d437e22aee35381a559dcee15a437ddztenghui        return;
3963bd3fa1f1d437e22aee35381a559dcee15a437ddztenghui    }
397512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    if (CC_UNLIKELY(polyLength < 3)) {
398512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#if DEBUG_SHADOW
399512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        ALOGW("Invalid polygon length. No spot shadow!");
400512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#endif
401512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        return;
402512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
403c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    OutlineData outlineData[polyLength];
404c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    Vector2 outlineCentroid;
405c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // Calculate the projected outline for each polygon's vertices from the light center.
406c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //
407c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //                       O     Light
408c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //                      /
409c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //                    /
410c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //                   .     Polygon vertex
411c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //                 /
412c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //               /
413c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //              O     Outline vertices
414c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //
415c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // Ratio = (Poly - Outline) / (Light - Poly)
416c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // Outline.x = Poly.x - Ratio * (Light.x - Poly.x)
417c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // Outline's radius / Light's radius = Ratio
418c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
419c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // Compute the last outline vertex to make sure we can get the normal and outline
420c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // in one single loop.
421c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    projectCasterToOutline(outlineData[polyLength - 1].position, lightCenter,
422c50a03d78aaedd0003377e98710e7038bda330e9ztenghui            poly[polyLength - 1]);
423c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
424c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // Take the outline's polygon, calculate the normal for each outline edge.
425c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    int currentNormalIndex = polyLength - 1;
426c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    int nextNormalIndex = 0;
427c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
428c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    for (int i = 0; i < polyLength; i++) {
429c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        float ratioZ = projectCasterToOutline(outlineData[i].position,
430c50a03d78aaedd0003377e98710e7038bda330e9ztenghui                lightCenter, poly[i]);
431c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        outlineData[i].radius = ratioZ * lightSize;
432c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
433c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        outlineData[currentNormalIndex].normal = ShadowTessellator::calculateNormal(
434c50a03d78aaedd0003377e98710e7038bda330e9ztenghui                outlineData[currentNormalIndex].position,
435c50a03d78aaedd0003377e98710e7038bda330e9ztenghui                outlineData[nextNormalIndex].position);
436c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        currentNormalIndex = (currentNormalIndex + 1) % polyLength;
437c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        nextNormalIndex++;
438c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    }
439c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
440c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    projectCasterToOutline(outlineCentroid, lightCenter, polyCentroid);
441c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
442c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    int penumbraIndex = 0;
443512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // Then each polygon's vertex produce at minmal 2 penumbra vertices.
444512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // Since the size can be dynamic here, we keep track of the size and update
445512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // the real size at the end.
446512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int allocatedPenumbraLength = 2 * polyLength + SPOT_MAX_EXTRA_CORNER_VERTEX_NUMBER;
447512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    Vector2 penumbra[allocatedPenumbraLength];
448512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int totalExtraCornerSliceNumber = 0;
449c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
450c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    Vector2 umbra[polyLength];
451c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
452512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // When centroid is covered by all circles from outline, then we consider
453512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // the umbra is invalid, and we will tune down the shadow strength.
454c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    bool hasValidUmbra = true;
455512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // We need the minimal of RaitoVI to decrease the spot shadow strength accordingly.
456512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    float minRaitoVI = FLT_MAX;
457c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
458c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    for (int i = 0; i < polyLength; i++) {
459c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Generate all the penumbra's vertices only using the (outline vertex + normal * radius)
460c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // There is no guarantee that the penumbra is still convex, but for
461c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // each outline vertex, it will connect to all its corresponding penumbra vertices as
462c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // triangle fans. And for neighber penumbra vertex, it will be a trapezoid.
463c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //
464c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Penumbra Vertices marked as Pi
465c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Outline Vertices marked as Vi
466c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //                                            (P3)
467c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //          (P2)                               |     ' (P4)
468c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //   (P1)'   |                                 |   '
469c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //         ' |                                 | '
470c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // (P0)  ------------------------------------------------(P5)
471c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           | (V0)                            |(V1)
472c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
473c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
474c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
475c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
476c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
477c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
478c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
479c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
480c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //       (V3)-----------------------------------(V2)
481c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        int preNormalIndex = (i + polyLength - 1) % polyLength;
482c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
483512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        const Vector2& previousNormal = outlineData[preNormalIndex].normal;
484512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        const Vector2& currentNormal = outlineData[i].normal;
485512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
486512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        // Depending on how roundness we want for each corner, we can subdivide
487c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // further here and/or introduce some heuristic to decide how much the
488c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // subdivision should be.
489512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        int currentExtraSliceNumber = ShadowTessellator::getExtraVertexNumber(
490512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui                previousNormal, currentNormal, SPOT_CORNER_RADIANS_DIVISOR);
491c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
492512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        int currentCornerSliceNumber = 1 + currentExtraSliceNumber;
493512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        totalExtraCornerSliceNumber += currentExtraSliceNumber;
494512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#if DEBUG_SHADOW
495512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        ALOGD("currentExtraSliceNumber should be %d", currentExtraSliceNumber);
496512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        ALOGD("currentCornerSliceNumber should be %d", currentCornerSliceNumber);
497512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        ALOGD("totalCornerSliceNumber is %d", totalExtraCornerSliceNumber);
498512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#endif
499512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        if (CC_UNLIKELY(totalExtraCornerSliceNumber > SPOT_MAX_EXTRA_CORNER_VERTEX_NUMBER)) {
500512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            currentCornerSliceNumber = 1;
501512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
502512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        for (int k = 0; k <= currentCornerSliceNumber; k++) {
503512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            Vector2 avgNormal =
504512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui                    (previousNormal * (currentCornerSliceNumber - k) + currentNormal * k) /
505512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui                    currentCornerSliceNumber;
506512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            avgNormal.normalize();
507512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            penumbra[penumbraIndex++] = outlineData[i].position +
508512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui                    avgNormal * outlineData[i].radius;
509512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
510c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
511c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
512c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Compute the umbra by the intersection from the outline's centroid!
513c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //
514c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //       (V) ------------------------------------
515c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |          '                       |
516c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |         '                        |
517c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |       ' (I)                      |
518c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |    '                             |
519c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           | '             (C)                |
520c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                  |
521c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                  |
522c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                  |
523c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                  |
524c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           ------------------------------------
525c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //
526c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Connect a line b/t the outline vertex (V) and the centroid (C), it will
527c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // intersect with the outline vertex's circle at point (I).
528c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Now, ratioVI = VI / VC, ratioIC = IC / VC
529c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Then the intersetion point can be computed as Ixy = Vxy * ratioIC + Cxy * ratioVI;
530c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //
531512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        // When all of the outline circles cover the the outline centroid, (like I is
532c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // on the other side of C), there is no real umbra any more, so we just fake
533c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // a small area around the centroid as the umbra, and tune down the spot
534c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // shadow's umbra strength to simulate the effect the whole shadow will
535c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // become lighter in this case.
536c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // The ratio can be simulated by using the inverse of maximum of ratioVI for
537c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // all (V).
538512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        float distOutline = (outlineData[i].position - outlineCentroid).length();
5393bd3fa1f1d437e22aee35381a559dcee15a437ddztenghui        if (CC_UNLIKELY(distOutline == 0)) {
540c50a03d78aaedd0003377e98710e7038bda330e9ztenghui            // If the outline has 0 area, then there is no spot shadow anyway.
541c50a03d78aaedd0003377e98710e7038bda330e9ztenghui            ALOGW("Outline has 0 area, no spot shadow!");
542c50a03d78aaedd0003377e98710e7038bda330e9ztenghui            return;
543c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        }
544512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
545512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        float ratioVI = outlineData[i].radius / distOutline;
5469db58c031f8ffa102a6d585cb585bed3bdb911a9Chris Craik        minRaitoVI = std::min(minRaitoVI, ratioVI);
547512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        if (ratioVI >= (1 - FAKE_UMBRA_SIZE_RATIO)) {
548512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            ratioVI = (1 - FAKE_UMBRA_SIZE_RATIO);
549c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        }
550c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // When we know we don't have valid umbra, don't bother to compute the
551c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // values below. But we can't skip the loop yet since we want to know the
552c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // maximum ratio.
553512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        float ratioIC = 1 - ratioVI;
554512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        umbra[i] = outlineData[i].position * ratioIC + outlineCentroid * ratioVI;
555c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    }
556c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
557512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    hasValidUmbra = (minRaitoVI <= 1.0);
558c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    float shadowStrengthScale = 1.0;
559c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    if (!hasValidUmbra) {
560512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#if DEBUG_SHADOW
561c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        ALOGW("The object is too close to the light or too small, no real umbra!");
562512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#endif
563c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        for (int i = 0; i < polyLength; i++) {
564c50a03d78aaedd0003377e98710e7038bda330e9ztenghui            umbra[i] = outlineData[i].position * FAKE_UMBRA_SIZE_RATIO +
565512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui                    outlineCentroid * (1 - FAKE_UMBRA_SIZE_RATIO);
566c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        }
567512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        shadowStrengthScale = 1.0 / minRaitoVI;
568c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    }
569c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
570512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int penumbraLength = penumbraIndex;
571512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int umbraLength = polyLength;
572512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
573c50a03d78aaedd0003377e98710e7038bda330e9ztenghui#if DEBUG_SHADOW
574512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    ALOGD("penumbraLength is %d , allocatedPenumbraLength %d", penumbraLength, allocatedPenumbraLength);
575c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    dumpPolygon(poly, polyLength, "input poly");
576c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    dumpPolygon(penumbra, penumbraLength, "penumbra");
577512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    dumpPolygon(umbra, umbraLength, "umbra");
578c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    ALOGD("hasValidUmbra is %d and shadowStrengthScale is %f", hasValidUmbra, shadowStrengthScale);
579c50a03d78aaedd0003377e98710e7038bda330e9ztenghui#endif
580c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
581512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // The penumbra and umbra needs to be in convex shape to keep consistency
582512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // and quality.
583512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // Since we are still shooting rays to penumbra, it needs to be convex.
584512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // Umbra can be represented as a fan from the centroid, but visually umbra
585512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // looks nicer when it is convex.
586512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    Vector2 finalUmbra[umbraLength];
587512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    Vector2 finalPenumbra[penumbraLength];
588512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int finalUmbraLength = hull(umbra, umbraLength, finalUmbra);
589512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int finalPenumbraLength = hull(penumbra, penumbraLength, finalPenumbra);
590512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
591512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    generateTriangleStrip(isCasterOpaque, shadowStrengthScale, finalPenumbra,
592512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            finalPenumbraLength, finalUmbra, finalUmbraLength, poly, polyLength,
593512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            shadowTriangleStrip, outlineCentroid);
594512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
595c50a03d78aaedd0003377e98710e7038bda330e9ztenghui}
596c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
5977b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
598512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * This is only for experimental purpose.
599512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * After intersections are calculated, we could smooth the polygon if needed.
600512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * So far, we don't think it is more appealing yet.
6017b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
602512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @param level The level of smoothness.
603512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @param rays The total number of rays.
604512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @param rayDist (In and Out) The distance for each ray.
605512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui *
606512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui */
607512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghuivoid SpotShadow::smoothPolygon(int level, int rays, float* rayDist) {
608512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    for (int k = 0; k < level; k++) {
609512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        for (int i = 0; i < rays; i++) {
610512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            float p1 = rayDist[(rays - 1 + i) % rays];
611512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            float p2 = rayDist[i];
612512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            float p3 = rayDist[(i + 1) % rays];
613512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            rayDist[i] = (p1 + p2 * 2 + p3) / 4;
614512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
615512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
616512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
617512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
618d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// Index pair is meant for storing the tessellation information for the penumbra
619d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// area. One index must come from exterior tangent of the circles, the other one
620d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// must come from the interior tangent of the circles.
621d2dcd6fded3a036f334a88bf9593398833f2919aztenghuistruct IndexPair {
622d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int outerIndex;
623d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int innerIndex;
624d2dcd6fded3a036f334a88bf9593398833f2919aztenghui};
6257b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
626d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// For one penumbra vertex, find the cloest umbra vertex and return its index.
627d2dcd6fded3a036f334a88bf9593398833f2919aztenghuiinline int getClosestUmbraIndex(const Vector2& pivot, const Vector2* polygon, int polygonLength) {
628d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    float minLengthSquared = FLT_MAX;
629d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int resultIndex = -1;
630d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    bool hasDecreased = false;
631d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Starting with some negative offset, assuming both umbra and penumbra are starting
632d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // at the same angle, this can help to find the result faster.
633d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Normally, loop 3 times, we can find the closest point.
634d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int offset = polygonLength - 2;
635d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < polygonLength; i++) {
636d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        int currentIndex = (i + offset) % polygonLength;
637d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        float currentLengthSquared = (pivot - polygon[currentIndex]).lengthSquared();
638d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        if (currentLengthSquared < minLengthSquared) {
639d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            if (minLengthSquared != FLT_MAX) {
640d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                hasDecreased = true;
641d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            }
642d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            minLengthSquared = currentLengthSquared;
643d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            resultIndex = currentIndex;
644d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        } else if (currentLengthSquared > minLengthSquared && hasDecreased) {
645d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // Early break b/c we have found the closet one and now the length
646d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // is increasing again.
647d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            break;
648512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
649512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
650d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    if(resultIndex == -1) {
651d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        ALOGE("resultIndex is -1, the polygon must be invalid!");
652d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        resultIndex = 0;
653d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    }
654d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    return resultIndex;
655512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
6567b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
6573932063bc75dc1e4efc2c428ca208d2e2290164dztenghui// Allow some epsilon here since the later ray intersection did allow for some small
6583932063bc75dc1e4efc2c428ca208d2e2290164dztenghui// floating point error, when the intersection point is slightly outside the segment.
659d2dcd6fded3a036f334a88bf9593398833f2919aztenghuiinline bool sameDirections(bool isPositiveCross, float a, float b) {
660d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    if (isPositiveCross) {
6613932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        return a >= -EPSILON && b >= -EPSILON;
662d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    } else {
6633932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        return a <= EPSILON && b <= EPSILON;
664512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
665512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
666512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
667d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// Find the right polygon edge to shoot the ray at.
668d2dcd6fded3a036f334a88bf9593398833f2919aztenghuiinline int findPolyIndex(bool isPositiveCross, int startPolyIndex, const Vector2& umbraDir,
669d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        const Vector2* polyToCentroid, int polyLength) {
670d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Make sure we loop with a bound.
671d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < polyLength; i++) {
672d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        int currentIndex = (i + startPolyIndex) % polyLength;
673d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        const Vector2& currentToCentroid = polyToCentroid[currentIndex];
674d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        const Vector2& nextToCentroid = polyToCentroid[(currentIndex + 1) % polyLength];
675d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
676d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        float currentCrossUmbra = currentToCentroid.cross(umbraDir);
677d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        float umbraCrossNext = umbraDir.cross(nextToCentroid);
678d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        if (sameDirections(isPositiveCross, currentCrossUmbra, umbraCrossNext)) {
679512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#if DEBUG_SHADOW
680d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            ALOGD("findPolyIndex loop %d times , index %d", i, currentIndex );
681512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#endif
682d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            return currentIndex;
683512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
684512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
685d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    LOG_ALWAYS_FATAL("Can't find the right polygon's edge from startPolyIndex %d", startPolyIndex);
686d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    return -1;
687512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
688512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
689d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// Generate the index pair for penumbra / umbra vertices, and more penumbra vertices
690d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// if needed.
691d2dcd6fded3a036f334a88bf9593398833f2919aztenghuiinline void genNewPenumbraAndPairWithUmbra(const Vector2* penumbra, int penumbraLength,
692d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        const Vector2* umbra, int umbraLength, Vector2* newPenumbra, int& newPenumbraIndex,
693d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        IndexPair* verticesPair, int& verticesPairIndex) {
694d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // In order to keep everything in just one loop, we need to pre-compute the
695d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // closest umbra vertex for the last penumbra vertex.
696d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int previousClosestUmbraIndex = getClosestUmbraIndex(penumbra[penumbraLength - 1],
697d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            umbra, umbraLength);
698d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < penumbraLength; i++) {
699d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        const Vector2& currentPenumbraVertex = penumbra[i];
700d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // For current penumbra vertex, starting from previousClosestUmbraIndex,
701d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // then check the next one until the distance increase.
702d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // The last one before the increase is the umbra vertex we need to pair with.
7033932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        float currentLengthSquared =
7043932063bc75dc1e4efc2c428ca208d2e2290164dztenghui                (currentPenumbraVertex - umbra[previousClosestUmbraIndex]).lengthSquared();
7053932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        int currentClosestUmbraIndex = previousClosestUmbraIndex;
706d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        int indexDelta = 0;
707d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        for (int j = 1; j < umbraLength; j++) {
708d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            int newUmbraIndex = (previousClosestUmbraIndex + j) % umbraLength;
709d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            float newLengthSquared = (currentPenumbraVertex - umbra[newUmbraIndex]).lengthSquared();
710d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            if (newLengthSquared > currentLengthSquared) {
7113932063bc75dc1e4efc2c428ca208d2e2290164dztenghui                // currentClosestUmbraIndex is the umbra vertex's index which has
7123932063bc75dc1e4efc2c428ca208d2e2290164dztenghui                // currently found smallest distance, so we can simply break here.
713512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui                break;
714512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            } else {
715d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                currentLengthSquared = newLengthSquared;
716d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                indexDelta++;
7173932063bc75dc1e4efc2c428ca208d2e2290164dztenghui                currentClosestUmbraIndex = newUmbraIndex;
71850ecf849cb7ccc3482517b74d2214b347927791eztenghui            }
71950ecf849cb7ccc3482517b74d2214b347927791eztenghui        }
720d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
721d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        if (indexDelta > 1) {
722d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // For those umbra don't have  penumbra, generate new penumbra vertices by interpolation.
723d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            //
724d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // Assuming Pi for penumbra vertices, and Ui for umbra vertices.
725d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // In the case like below P1 paired with U1 and P2 paired with  U5.
726d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // U2 to U4 are unpaired umbra vertices.
727d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            //
728d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // P1                                        P2
729d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // |                                          |
730d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // U1     U2                   U3     U4     U5
731d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            //
732d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // We will need to generate 3 more penumbra vertices P1.1, P1.2, P1.3
733d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // to pair with U2 to U4.
734d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            //
735d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // P1     P1.1                P1.2   P1.3    P2
736d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // |       |                   |      |      |
737d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // U1     U2                   U3     U4     U5
738d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            //
739d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // That distance ratio b/t Ui to U1 and Ui to U5 decides its paired penumbra
740d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // vertex's location.
741d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            int newPenumbraNumber = indexDelta - 1;
742d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
743a1f563134065abb360096cc06f6bfe4a8cca7a48Keith Mok            float accumulatedDeltaLength[indexDelta];
744d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            float totalDeltaLength = 0;
745d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
746d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // To save time, cache the previous umbra vertex info outside the loop
747d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // and update each loop.
748d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            Vector2 previousClosestUmbra = umbra[previousClosestUmbraIndex];
749d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            Vector2 skippedUmbra;
750d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // Use umbra data to precompute the length b/t unpaired umbra vertices,
751d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // and its ratio against the total length.
752d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            for (int k = 0; k < indexDelta; k++) {
753d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                int skippedUmbraIndex = (previousClosestUmbraIndex + k + 1) % umbraLength;
754d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                skippedUmbra = umbra[skippedUmbraIndex];
755d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                float currentDeltaLength = (skippedUmbra - previousClosestUmbra).length();
756d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
757d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                totalDeltaLength += currentDeltaLength;
758d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                accumulatedDeltaLength[k] = totalDeltaLength;
759d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
760d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                previousClosestUmbra = skippedUmbra;
761512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            }
762512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
763d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            const Vector2& previousPenumbra = penumbra[(i + penumbraLength - 1) % penumbraLength];
764d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // Then for each unpaired umbra vertex, create a new penumbra by the ratio,
765d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // and pair them togehter.
766d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            for (int k = 0; k < newPenumbraNumber; k++) {
767d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                float weightForCurrentPenumbra = 1.0f;
768d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                if (totalDeltaLength != 0.0f) {
769d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                    weightForCurrentPenumbra = accumulatedDeltaLength[k] / totalDeltaLength;
770d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                }
771d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                float weightForPreviousPenumbra = 1.0f - weightForCurrentPenumbra;
772512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
773d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                Vector2 interpolatedPenumbra = currentPenumbraVertex * weightForCurrentPenumbra +
774d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                    previousPenumbra * weightForPreviousPenumbra;
775512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
776d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                int skippedUmbraIndex = (previousClosestUmbraIndex + k + 1) % umbraLength;
777edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe                verticesPair[verticesPairIndex].outerIndex = newPenumbraIndex;
778edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe                verticesPair[verticesPairIndex].innerIndex = skippedUmbraIndex;
779edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe                verticesPairIndex++;
780d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                newPenumbra[newPenumbraIndex++] = interpolatedPenumbra;
781512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            }
782512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
783edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe        verticesPair[verticesPairIndex].outerIndex = newPenumbraIndex;
784edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe        verticesPair[verticesPairIndex].innerIndex = currentClosestUmbraIndex;
785edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe        verticesPairIndex++;
786d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        newPenumbra[newPenumbraIndex++] = currentPenumbraVertex;
7877b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
788d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        previousClosestUmbraIndex = currentClosestUmbraIndex;
789512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
790512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
791512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
792d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// Precompute all the polygon's vector, return true if the reference cross product is positive.
793d2dcd6fded3a036f334a88bf9593398833f2919aztenghuiinline bool genPolyToCentroid(const Vector2* poly2d, int polyLength,
794d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        const Vector2& centroid, Vector2* polyToCentroid) {
795d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int j = 0; j < polyLength; j++) {
796d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        polyToCentroid[j] = poly2d[j] - centroid;
7973932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        // Normalize these vectors such that we can use epsilon comparison after
7983932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        // computing their cross products with another normalized vector.
7993932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        polyToCentroid[j].normalize();
8007b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
801d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    float refCrossProduct = 0;
802d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int j = 0; j < polyLength; j++) {
803d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        refCrossProduct = polyToCentroid[j].cross(polyToCentroid[(j + 1) % polyLength]);
804d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        if (refCrossProduct != 0) {
805d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            break;
806d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        }
807512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
808d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
809d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    return refCrossProduct > 0;
8107b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
8117b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
812d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// For one umbra vertex, shoot an ray from centroid to it.
813d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// If the ray hit the polygon first, then return the intersection point as the
814d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// closer vertex.
815d2dcd6fded3a036f334a88bf9593398833f2919aztenghuiinline Vector2 getCloserVertex(const Vector2& umbraVertex, const Vector2& centroid,
816d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        const Vector2* poly2d, int polyLength, const Vector2* polyToCentroid,
817d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        bool isPositiveCross, int& previousPolyIndex) {
818d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    Vector2 umbraToCentroid = umbraVertex - centroid;
819d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    float distanceToUmbra = umbraToCentroid.length();
820d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    umbraToCentroid = umbraToCentroid / distanceToUmbra;
821d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
822d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // previousPolyIndex is updated for each item such that we can minimize the
823d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // looping inside findPolyIndex();
824d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    previousPolyIndex = findPolyIndex(isPositiveCross, previousPolyIndex,
825d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            umbraToCentroid, polyToCentroid, polyLength);
826d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
827d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    float dx = umbraToCentroid.x;
828d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    float dy = umbraToCentroid.y;
829d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    float distanceToIntersectPoly = rayIntersectPoints(centroid, dx, dy,
830d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            poly2d[previousPolyIndex], poly2d[(previousPolyIndex + 1) % polyLength]);
831d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    if (distanceToIntersectPoly < 0) {
832d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        distanceToIntersectPoly = 0;
833512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
834f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
835d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Pick the closer one as the occluded area vertex.
836d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    Vector2 closerVertex;
837d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    if (distanceToIntersectPoly < distanceToUmbra) {
838d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        closerVertex.x = centroid.x + dx * distanceToIntersectPoly;
839d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        closerVertex.y = centroid.y + dy * distanceToIntersectPoly;
840d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    } else {
841d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        closerVertex = umbraVertex;
842512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
843512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
844d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    return closerVertex;
845512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
846512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
847512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui/**
848512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * Generate a triangle strip given two convex polygon
849512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui**/
85064bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampevoid SpotShadow::generateTriangleStrip(bool isCasterOpaque, float shadowStrengthScale,
851512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        Vector2* penumbra, int penumbraLength, Vector2* umbra, int umbraLength,
852512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        const Vector3* poly, int polyLength, VertexBuffer& shadowTriangleStrip,
853512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        const Vector2& centroid) {
854512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    bool hasOccludedUmbraArea = false;
855512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    Vector2 poly2d[polyLength];
856f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
857512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    if (isCasterOpaque) {
858512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        for (int i = 0; i < polyLength; i++) {
859512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            poly2d[i].x = poly[i].x;
860512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            poly2d[i].y = poly[i].y;
861512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
862512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        // Make sure the centroid is inside the umbra, otherwise, fall back to the
863512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        // approach as if there is no occluded umbra area.
864512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        if (testPointInsidePolygon(centroid, poly2d, polyLength)) {
865512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            hasOccludedUmbraArea = true;
866512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
867512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
868512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
869d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // For each penumbra vertex, find its corresponding closest umbra vertex index.
870d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //
871d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Penumbra Vertices marked as Pi
872d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Umbra Vertices marked as Ui
873d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //                                            (P3)
874d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //          (P2)                               |     ' (P4)
875d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //   (P1)'   |                                 |   '
876d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //         ' |                                 | '
877d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // (P0)  ------------------------------------------------(P5)
878d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           | (U0)                            |(U1)
879d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
880d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |(U2)     (P5.1)
881d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
882d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
883d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
884d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
885d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
886d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
887d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //       (U4)-----------------------------------(U3)      (P6)
888d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //
889d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // At least, like P0, P1, P2, they will find the matching umbra as U0.
890d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // If we jump over some umbra vertex without matching penumbra vertex, then
891d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // we will generate some new penumbra vertex by interpolation. Like P6 is
892d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // matching U3, but U2 is not matched with any penumbra vertex.
893d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // So interpolate P5.1 out and match U2.
894d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // In this way, every umbra vertex will have a matching penumbra vertex.
895d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //
896d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // The total pair number can be as high as umbraLength + penumbraLength.
897d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    const int maxNewPenumbraLength = umbraLength + penumbraLength;
898d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    IndexPair verticesPair[maxNewPenumbraLength];
899d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int verticesPairIndex = 0;
900d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
901d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Cache all the existing penumbra vertices and newly interpolated vertices into a
902d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // a new array.
903d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    Vector2 newPenumbra[maxNewPenumbraLength];
904d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int newPenumbraIndex = 0;
905d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
906d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // For each penumbra vertex, find its closet umbra vertex by comparing the
907d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // neighbor umbra vertices.
908d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    genNewPenumbraAndPairWithUmbra(penumbra, penumbraLength, umbra, umbraLength, newPenumbra,
909d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            newPenumbraIndex, verticesPair, verticesPairIndex);
910d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    ShadowTessellator::checkOverflow(verticesPairIndex, maxNewPenumbraLength, "Spot pair");
911d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    ShadowTessellator::checkOverflow(newPenumbraIndex, maxNewPenumbraLength, "Spot new penumbra");
912d2dcd6fded3a036f334a88bf9593398833f2919aztenghui#if DEBUG_SHADOW
913d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < umbraLength; i++) {
914d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        ALOGD("umbra i %d,  [%f, %f]", i, umbra[i].x, umbra[i].y);
915d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    }
916d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < newPenumbraIndex; i++) {
917d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        ALOGD("new penumbra i %d,  [%f, %f]", i, newPenumbra[i].x, newPenumbra[i].y);
918d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    }
919d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < verticesPairIndex; i++) {
920d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        ALOGD("index i %d,  [%d, %d]", i, verticesPair[i].outerIndex, verticesPair[i].innerIndex);
921512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
922d2dcd6fded3a036f334a88bf9593398833f2919aztenghui#endif
923512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
924d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // For the size of vertex buffer, we need 3 rings, one has newPenumbraSize,
925d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // one has umbraLength, the last one has at most umbraLength.
926d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //
927d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // For the size of index buffer, the umbra area needs (2 * umbraLength + 2).
928d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // The penumbra one can vary a bit, but it is bounded by (2 * verticesPairIndex + 2).
929d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // And 2 more for jumping between penumbra to umbra.
930d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    const int newPenumbraLength = newPenumbraIndex;
931d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    const int totalVertexCount = newPenumbraLength + umbraLength * 2;
932d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    const int totalIndexCount = 2 * umbraLength + 2 * verticesPairIndex + 6;
933512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    AlphaVertex* shadowVertices =
934512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            shadowTriangleStrip.alloc<AlphaVertex>(totalVertexCount);
935512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    uint16_t* indexBuffer =
936512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            shadowTriangleStrip.allocIndices<uint16_t>(totalIndexCount);
937512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int vertexBufferIndex = 0;
938d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int indexBufferIndex = 0;
939512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
940d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Fill the IB and VB for the penumbra area.
941d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < newPenumbraLength; i++) {
942d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        AlphaVertex::set(&shadowVertices[vertexBufferIndex++], newPenumbra[i].x,
943138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik                newPenumbra[i].y, PENUMBRA_ALPHA);
944d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    }
9459c555566bfef718464546dcab3640f64d2fdc55dTeng-Hui Zhu    // Since the umbra can be a faked one when the occluder is too high, the umbra should be lighter
9469c555566bfef718464546dcab3640f64d2fdc55dTeng-Hui Zhu    // in this case.
9479c555566bfef718464546dcab3640f64d2fdc55dTeng-Hui Zhu    float scaledUmbraAlpha = UMBRA_ALPHA * shadowStrengthScale;
9489c555566bfef718464546dcab3640f64d2fdc55dTeng-Hui Zhu
949d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < umbraLength; i++) {
950d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        AlphaVertex::set(&shadowVertices[vertexBufferIndex++], umbra[i].x, umbra[i].y,
9519c555566bfef718464546dcab3640f64d2fdc55dTeng-Hui Zhu                scaledUmbraAlpha);
952512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
953512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
954d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < verticesPairIndex; i++) {
955d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        indexBuffer[indexBufferIndex++] = verticesPair[i].outerIndex;
956d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // All umbra index need to be offseted by newPenumbraSize.
957d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        indexBuffer[indexBufferIndex++] = verticesPair[i].innerIndex + newPenumbraLength;
958d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    }
959d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBuffer[indexBufferIndex++] = verticesPair[0].outerIndex;
960d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBuffer[indexBufferIndex++] = verticesPair[0].innerIndex + newPenumbraLength;
961d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
962d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Now fill the IB and VB for the umbra area.
963d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // First duplicated the index from previous strip and the first one for the
964d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // degenerated triangles.
965d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBuffer[indexBufferIndex] = indexBuffer[indexBufferIndex - 1];
966d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBufferIndex++;
967d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBuffer[indexBufferIndex++] = newPenumbraLength + 0;
968d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Save the first VB index for umbra area in order to close the loop.
969d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int savedStartIndex = vertexBufferIndex;
970512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
971512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    if (hasOccludedUmbraArea) {
972d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // Precompute all the polygon's vector, and the reference cross product,
973d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // in order to find the right polygon edge for the ray to intersect.
974d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        Vector2 polyToCentroid[polyLength];
975d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        bool isPositiveCross = genPolyToCentroid(poly2d, polyLength, centroid, polyToCentroid);
976d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
977d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // Because both the umbra and polygon are going in the same direction,
978d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // we can save the previous polygon index to make sure we have less polygon
979d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // vertex to compute for each ray.
980d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        int previousPolyIndex = 0;
981d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        for (int i = 0; i < umbraLength; i++) {
982d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // Shoot a ray from centroid to each umbra vertices and pick the one with
983d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // shorter distance to the centroid, b/t the umbra vertex or the intersection point.
984d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            Vector2 closerVertex = getCloserVertex(umbra[i], centroid, poly2d, polyLength,
985d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                    polyToCentroid, isPositiveCross, previousPolyIndex);
986d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
987d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // We already stored the umbra vertices, just need to add the occlued umbra's ones.
988d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            indexBuffer[indexBufferIndex++] = newPenumbraLength + i;
989d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            indexBuffer[indexBufferIndex++] = vertexBufferIndex;
990d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            AlphaVertex::set(&shadowVertices[vertexBufferIndex++],
9919c555566bfef718464546dcab3640f64d2fdc55dTeng-Hui Zhu                    closerVertex.x, closerVertex.y, scaledUmbraAlpha);
992512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
993512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    } else {
994d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // If there is no occluded umbra at all, then draw the triangle fan
995d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // starting from the centroid to all umbra vertices.
996512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        int lastCentroidIndex = vertexBufferIndex;
997512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        AlphaVertex::set(&shadowVertices[vertexBufferIndex++], centroid.x,
9989c555566bfef718464546dcab3640f64d2fdc55dTeng-Hui Zhu                centroid.y, scaledUmbraAlpha);
999d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        for (int i = 0; i < umbraLength; i++) {
1000d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            indexBuffer[indexBufferIndex++] = newPenumbraLength + i;
1001512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            indexBuffer[indexBufferIndex++] = lastCentroidIndex;
1002512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
1003512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
1004d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Closing the umbra area triangle's loop here.
1005d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBuffer[indexBufferIndex++] = newPenumbraLength;
1006d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBuffer[indexBufferIndex++] = savedStartIndex;
1007512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
1008512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // At the end, update the real index and vertex buffer size.
1009512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    shadowTriangleStrip.updateVertexCount(vertexBufferIndex);
1010512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    shadowTriangleStrip.updateIndexCount(indexBufferIndex);
1011512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    ShadowTessellator::checkOverflow(vertexBufferIndex, totalVertexCount, "Spot Vertex Buffer");
1012512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    ShadowTessellator::checkOverflow(indexBufferIndex, totalIndexCount, "Spot Index Buffer");
1013512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
1014117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    shadowTriangleStrip.setMeshFeatureFlags(VertexBuffer::kAlpha | VertexBuffer::kIndices);
1015512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    shadowTriangleStrip.computeBounds<AlphaVertex>();
1016512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
1017512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
1018512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#if DEBUG_SHADOW
1019512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
1020512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#define TEST_POINT_NUMBER 128
1021f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui/**
1022f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * Calculate the bounds for generating random test points.
1023f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui */
1024f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghuivoid SpotShadow::updateBound(const Vector2 inVector, Vector2& lowerBound,
1025512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        Vector2& upperBound) {
1026f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    if (inVector.x < lowerBound.x) {
1027f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        lowerBound.x = inVector.x;
1028f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1029f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1030f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    if (inVector.y < lowerBound.y) {
1031f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        lowerBound.y = inVector.y;
1032f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1033f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1034f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    if (inVector.x > upperBound.x) {
1035f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        upperBound.x = inVector.x;
1036f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1037f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1038f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    if (inVector.y > upperBound.y) {
1039f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        upperBound.y = inVector.y;
1040f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1041f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui}
1042f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1043f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui/**
1044f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * For debug purpose, when things go wrong, dump the whole polygon data.
1045f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui */
1046c50a03d78aaedd0003377e98710e7038bda330e9ztenghuivoid SpotShadow::dumpPolygon(const Vector2* poly, int polyLength, const char* polyName) {
1047c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    for (int i = 0; i < polyLength; i++) {
1048c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        ALOGD("polygon %s i %d x %f y %f", polyName, i, poly[i].x, poly[i].y);
1049c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    }
1050c50a03d78aaedd0003377e98710e7038bda330e9ztenghui}
1051c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
1052c50a03d78aaedd0003377e98710e7038bda330e9ztenghui/**
1053c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * For debug purpose, when things go wrong, dump the whole polygon data.
1054c50a03d78aaedd0003377e98710e7038bda330e9ztenghui */
1055c50a03d78aaedd0003377e98710e7038bda330e9ztenghuivoid SpotShadow::dumpPolygon(const Vector3* poly, int polyLength, const char* polyName) {
1056f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    for (int i = 0; i < polyLength; i++) {
10578d0ec389531d071529fb0a800f10733b057205d9Teng-Hui Zhu        ALOGD("polygon %s i %d x %f y %f z %f", polyName, i, poly[i].x, poly[i].y, poly[i].z);
1058f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1059f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui}
1060f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1061f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui/**
1062f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * Test whether the polygon is convex.
1063f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui */
1064f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghuibool SpotShadow::testConvex(const Vector2* polygon, int polygonLength,
1065f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        const char* name) {
1066f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    bool isConvex = true;
1067f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    for (int i = 0; i < polygonLength; i++) {
1068f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        Vector2 start = polygon[i];
1069f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        Vector2 middle = polygon[(i + 1) % polygonLength];
1070f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        Vector2 end = polygon[(i + 2) % polygonLength];
1071f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
10729122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float delta = (float(middle.x) - start.x) * (float(end.y) - start.y) -
10739122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui                (float(middle.y) - start.y) * (float(end.x) - start.x);
1074f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        bool isCCWOrCoLinear = (delta >= EPSILON);
1075f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1076f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        if (isCCWOrCoLinear) {
107750ecf849cb7ccc3482517b74d2214b347927791eztenghui            ALOGW("(Error Type 2): polygon (%s) is not a convex b/c start (x %f, y %f),"
1078f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                    "middle (x %f, y %f) and end (x %f, y %f) , delta is %f !!!",
1079f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                    name, start.x, start.y, middle.x, middle.y, end.x, end.y, delta);
1080f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            isConvex = false;
1081f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            break;
1082f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        }
1083f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1084f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    return isConvex;
1085f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui}
1086f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1087f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui/**
1088f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * Test whether or not the polygon (intersection) is within the 2 input polygons.
1089f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * Using Marte Carlo method, we generate a random point, and if it is inside the
1090f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * intersection, then it must be inside both source polygons.
1091f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui */
1092f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghuivoid SpotShadow::testIntersection(const Vector2* poly1, int poly1Length,
1093f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        const Vector2* poly2, int poly2Length,
1094f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        const Vector2* intersection, int intersectionLength) {
1095f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    // Find the min and max of x and y.
1096c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    Vector2 lowerBound = {FLT_MAX, FLT_MAX};
1097c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    Vector2 upperBound = {-FLT_MAX, -FLT_MAX};
1098f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    for (int i = 0; i < poly1Length; i++) {
1099f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        updateBound(poly1[i], lowerBound, upperBound);
1100f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1101f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    for (int i = 0; i < poly2Length; i++) {
1102f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        updateBound(poly2[i], lowerBound, upperBound);
1103f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1104f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1105f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    bool dumpPoly = false;
1106f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    for (int k = 0; k < TEST_POINT_NUMBER; k++) {
1107f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        // Generate a random point between minX, minY and maxX, maxY.
11089122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float randomX = rand() / float(RAND_MAX);
11099122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float randomY = rand() / float(RAND_MAX);
1110f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1111f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        Vector2 testPoint;
1112f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        testPoint.x = lowerBound.x + randomX * (upperBound.x - lowerBound.x);
1113f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        testPoint.y = lowerBound.y + randomY * (upperBound.y - lowerBound.y);
1114f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1115f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        // If the random point is in both poly 1 and 2, then it must be intersection.
1116f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        if (testPointInsidePolygon(testPoint, intersection, intersectionLength)) {
1117f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            if (!testPointInsidePolygon(testPoint, poly1, poly1Length)) {
1118f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                dumpPoly = true;
111950ecf849cb7ccc3482517b74d2214b347927791eztenghui                ALOGW("(Error Type 1): one point (%f, %f) in the intersection is"
1120512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui                        " not in the poly1",
1121f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                        testPoint.x, testPoint.y);
1122f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            }
1123f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1124f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            if (!testPointInsidePolygon(testPoint, poly2, poly2Length)) {
1125f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                dumpPoly = true;
112650ecf849cb7ccc3482517b74d2214b347927791eztenghui                ALOGW("(Error Type 1): one point (%f, %f) in the intersection is"
1127512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui                        " not in the poly2",
1128f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                        testPoint.x, testPoint.y);
1129f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            }
1130f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        }
1131f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1132f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1133f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    if (dumpPoly) {
1134f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        dumpPolygon(intersection, intersectionLength, "intersection");
1135f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        for (int i = 1; i < intersectionLength; i++) {
1136f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            Vector2 delta = intersection[i] - intersection[i - 1];
1137f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            ALOGD("Intersetion i, %d Vs i-1 is delta %f", i, delta.lengthSquared());
1138f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        }
1139f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1140f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        dumpPolygon(poly1, poly1Length, "poly 1");
1141f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        dumpPolygon(poly2, poly2Length, "poly 2");
1142f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1143f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui}
1144f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui#endif
1145f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
11467b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}; // namespace uirenderer
11477b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}; // namespace android
1148