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.
401bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck#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 <math.h>
569db58c031f8ffa102a6d585cb585bed3bdb911a9Chris Craik#include <stdlib.h>
579db58c031f8ffa102a6d585cb585bed3bdb911a9Chris Craik#include <utils/Log.h>
581bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck#include <algorithm>
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 */
1181bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reckstatic float rayIntersectPoints(const Vector2& rayOrigin, float dx, float dy, const Vector2& p1,
1191bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                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);
1251bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck    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) +
1351bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                      rayOrigin.x * (p2.y - p1.y)) /
1361bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                     divisor;
137726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik
1381bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck    return distance;  // may be negative in error cases
1397b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
1407b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1417b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
1427b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Sort points by their X coordinates
1437b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
1447b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param points the points as a Vector2 array.
1457b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param pointsLength the number of vertices of the polygon.
1467b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
1477b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuivoid SpotShadow::xsort(Vector2* points, int pointsLength) {
1481bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck    auto cmp = [](const Vector2& a, const Vector2& b) -> bool { return a.x < b.x; };
1491e4209e3871493ccfb09e43634d4082f49c227beJohn Reck    std::sort(points, points + pointsLength, cmp);
1507b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
1517b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1527b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
1537b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * compute the convex hull of a collection of Points
1547b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
1557b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param points the points as a Vector2 array.
1567b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param pointsLength the number of vertices of the polygon.
1577b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param retPoly pre allocated array of floats to put the vertices
1587b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @return the number of points in the polygon 0 if no intersection
1597b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
1607b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuiint SpotShadow::hull(Vector2* points, int pointsLength, Vector2* retPoly) {
1617b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    xsort(points, pointsLength);
1627b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    int n = pointsLength;
1637b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    Vector2 lUpper[n];
1647b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    lUpper[0] = points[0];
1657b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    lUpper[1] = points[1];
1667b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1677b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    int lUpperSize = 2;
1687b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1697b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = 2; i < n; i++) {
1707b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        lUpper[lUpperSize] = points[i];
1717b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        lUpperSize++;
1727b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1731bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck        while (lUpperSize > 2 &&
1741bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck               !ccw(lUpper[lUpperSize - 3].x, lUpper[lUpperSize - 3].y, lUpper[lUpperSize - 2].x,
1751bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                    lUpper[lUpperSize - 2].y, lUpper[lUpperSize - 1].x, lUpper[lUpperSize - 1].y)) {
1767b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            // Remove the middle point of the three last
1777b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            lUpper[lUpperSize - 2].x = lUpper[lUpperSize - 1].x;
1787b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            lUpper[lUpperSize - 2].y = lUpper[lUpperSize - 1].y;
1797b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            lUpperSize--;
1807b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        }
1817b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
1827b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1837b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    Vector2 lLower[n];
1847b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    lLower[0] = points[n - 1];
1857b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    lLower[1] = points[n - 2];
1867b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1877b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    int lLowerSize = 2;
1887b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1897b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = n - 3; i >= 0; i--) {
1907b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        lLower[lLowerSize] = points[i];
1917b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        lLowerSize++;
1927b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
1931bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck        while (lLowerSize > 2 &&
1941bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck               !ccw(lLower[lLowerSize - 3].x, lLower[lLowerSize - 3].y, lLower[lLowerSize - 2].x,
1951bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                    lLower[lLowerSize - 2].y, lLower[lLowerSize - 1].x, lLower[lLowerSize - 1].y)) {
1967b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            // Remove the middle point of the three last
1977b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            lLower[lLowerSize - 2] = lLower[lLowerSize - 1];
1987b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            lLowerSize--;
1997b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        }
2007b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
2017b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
202726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    // output points in CW ordering
203726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    const int total = lUpperSize + lLowerSize - 2;
204726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    int outIndex = total - 1;
2057b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = 0; i < lUpperSize; i++) {
206726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        retPoly[outIndex] = lUpper[i];
207726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        outIndex--;
2087b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
2097b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2107b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = 1; i < lLowerSize - 1; i++) {
211726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        retPoly[outIndex] = lLower[i];
212726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        outIndex--;
2137b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
2147b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    // TODO: Add test harness which verify that all the points are inside the hull.
215726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    return total;
2167b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
2177b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2187b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
219f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * Test whether the 3 points form a counter clockwise turn.
2207b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
2217b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @return true if a right hand turn
2227b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
2231bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reckbool SpotShadow::ccw(float ax, float ay, float bx, float by, float cx, float cy) {
2247b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    return (bx - ax) * (cy - ay) - (by - ay) * (cx - ax) > EPSILON;
2257b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
2267b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2277b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
2287b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Sort points about a center point
2297b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
2307b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param poly The in and out polyogon as a Vector2 array.
2317b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param polyLength The number of vertices of the polygon.
2327b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param center the center ctr[0] = x , ctr[1] = y to sort around.
2337b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
2347b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuivoid SpotShadow::sort(Vector2* poly, int polyLength, const Vector2& center) {
2357b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    quicksortCirc(poly, 0, polyLength - 1, center);
2367b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
2377b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2387b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
2397b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Swap points pointed to by i and j
2407b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
2417b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuivoid SpotShadow::swap(Vector2* points, int i, int j) {
2427b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    Vector2 temp = points[i];
2437b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    points[i] = points[j];
2447b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    points[j] = temp;
2457b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
2467b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2477b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
2487b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * quick sort implementation about the center.
2497b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
2501bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reckvoid SpotShadow::quicksortCirc(Vector2* points, int low, int high, const Vector2& center) {
2517b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    int i = low, j = high;
2527b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    int p = low + (high - low) / 2;
2537b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    float pivot = angle(points[p], center);
2547b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    while (i <= j) {
255726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        while (angle(points[i], center) > pivot) {
2567b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            i++;
2577b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        }
258726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        while (angle(points[j], center) < pivot) {
2597b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            j--;
2607b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        }
2617b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2627b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        if (i <= j) {
2637b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            swap(points, i, j);
2647b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            i++;
2657b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            j--;
2667b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        }
2677b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
2687b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    if (low < j) quicksortCirc(points, low, j, center);
2697b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    if (i < high) quicksortCirc(points, i, high, center);
2707b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
2717b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2727b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
2737b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Test whether a point is inside the polygon.
2747b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
2757b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param testPoint the point to test
2767b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param poly the polygon
2777b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @return true if the testPoint is inside the poly.
2787b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
2791bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reckbool SpotShadow::testPointInsidePolygon(const Vector2 testPoint, const Vector2* poly, int len) {
2807b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    bool c = false;
2819122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui    float testx = testPoint.x;
2829122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui    float testy = testPoint.y;
2837b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = 0, j = len - 1; i < len; j = i++) {
2849122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float startX = poly[j].x;
2859122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float startY = poly[j].y;
2869122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float endX = poly[i].x;
2879122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float endY = poly[i].y;
2887b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2891bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck        if (((endY > testy) != (startY > testy)) &&
2901bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck            (testx < (startX - endX) * (testy - endY) / (startY - endY) + endX)) {
2917b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui            c = !c;
2927b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        }
2937b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
2947b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    return c;
2957b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
2967b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
2977b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
2987b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Reverse the polygon
2997b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
3007b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param polygon the polygon as a Vector2 array
3017b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param len the number of points of the polygon
3027b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
3037b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghuivoid SpotShadow::reverse(Vector2* polygon, int len) {
3047b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    int n = len / 2;
3057b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = 0; i < n; i++) {
3067b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        Vector2 tmp = polygon[i];
3077b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        int k = len - 1 - i;
3087b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        polygon[i] = polygon[k];
3097b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        polygon[k] = tmp;
3107b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
3117b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
3127b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
3137b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
3147b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * Compute a horizontal circular polygon about point (x , y , height) of radius
3157b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * (size)
3167b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
3177b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param points number of the points of the output polygon.
3187b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param lightCenter the center of the light.
3197b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param size the light size.
3207b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui * @param ret result polygon.
3217b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
3221bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reckvoid SpotShadow::computeLightPolygon(int points, const Vector3& lightCenter, float size,
3231bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                     Vector3* ret) {
3247b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    // TODO: Caching all the sin / cos values and store them in a look up table.
3257b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    for (int i = 0; i < points; i++) {
3269122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float angle = 2 * i * M_PI / points;
327726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        ret[i].x = cosf(angle) * size + lightCenter.x;
328726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        ret[i].y = sinf(angle) * size + lightCenter.y;
3297b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui        ret[i].z = lightCenter.z;
3307b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
3317b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
3327b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
3337b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
334512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * From light center, project one vertex to the z=0 surface and get the outline.
3357b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
336512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @param outline The result which is the outline position.
337512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @param lightCenter The center of light.
338512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @param polyVertex The input polygon's vertex.
339512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui *
340512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @return float The ratio of (polygon.z / light.z - polygon.z)
3417b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui */
3421bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reckfloat SpotShadow::projectCasterToOutline(Vector2& outline, const Vector3& lightCenter,
3431bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                         const Vector3& polyVertex) {
344c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    float lightToPolyZ = lightCenter.z - polyVertex.z;
345c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    float ratioZ = CASTER_Z_CAP_RATIO;
346c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    if (lightToPolyZ != 0) {
347c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // If any caster's vertex is almost above the light, we just keep it as 95%
348c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // of the height of the light.
3493bd3fa1f1d437e22aee35381a559dcee15a437ddztenghui        ratioZ = MathUtils::clamp(polyVertex.z / lightToPolyZ, 0.0f, CASTER_Z_CAP_RATIO);
350c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    }
351c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
352c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    outline.x = polyVertex.x - ratioZ * (lightCenter.x - polyVertex.x);
353c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    outline.y = polyVertex.y - ratioZ * (lightCenter.y - polyVertex.y);
354c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    return ratioZ;
355c50a03d78aaedd0003377e98710e7038bda330e9ztenghui}
356c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
357c50a03d78aaedd0003377e98710e7038bda330e9ztenghui/**
358c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * Generate the shadow spot light of shape lightPoly and a object poly
359c50a03d78aaedd0003377e98710e7038bda330e9ztenghui *
360c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * @param isCasterOpaque whether the caster is opaque
361c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * @param lightCenter the center of the light
362c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * @param lightSize the radius of the light
363c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * @param poly x,y,z vertexes of a convex polygon that occludes the light source
364c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * @param polyLength number of vertexes of the occluding polygon
365c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * @param shadowTriangleStrip return an (x,y,alpha) triangle strip representing the shadow. Return
366c50a03d78aaedd0003377e98710e7038bda330e9ztenghui *                            empty strip if error.
367c50a03d78aaedd0003377e98710e7038bda330e9ztenghui */
3681bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reckvoid SpotShadow::createSpotShadow(bool isCasterOpaque, const Vector3& lightCenter, float lightSize,
3691bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                  const Vector3* poly, int polyLength, const Vector3& polyCentroid,
3701bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                  VertexBuffer& shadowTriangleStrip) {
3713bd3fa1f1d437e22aee35381a559dcee15a437ddztenghui    if (CC_UNLIKELY(lightCenter.z <= 0)) {
3723bd3fa1f1d437e22aee35381a559dcee15a437ddztenghui        ALOGW("Relative Light Z is not positive. No spot shadow!");
3733bd3fa1f1d437e22aee35381a559dcee15a437ddztenghui        return;
3743bd3fa1f1d437e22aee35381a559dcee15a437ddztenghui    }
375512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    if (CC_UNLIKELY(polyLength < 3)) {
376512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#if DEBUG_SHADOW
377512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        ALOGW("Invalid polygon length. No spot shadow!");
378512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#endif
379512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        return;
380512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
381c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    OutlineData outlineData[polyLength];
382c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    Vector2 outlineCentroid;
383c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // Calculate the projected outline for each polygon's vertices from the light center.
384c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //
385c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //                       O     Light
386c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //                      /
387c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //                    /
388c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //                   .     Polygon vertex
389c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //                 /
390c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //               /
391c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //              O     Outline vertices
392c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    //
393c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // Ratio = (Poly - Outline) / (Light - Poly)
394c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // Outline.x = Poly.x - Ratio * (Light.x - Poly.x)
395c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // Outline's radius / Light's radius = Ratio
396c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
397c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // Compute the last outline vertex to make sure we can get the normal and outline
398c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // in one single loop.
3991bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck    projectCasterToOutline(outlineData[polyLength - 1].position, lightCenter, poly[polyLength - 1]);
400c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
401c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    // Take the outline's polygon, calculate the normal for each outline edge.
402c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    int currentNormalIndex = polyLength - 1;
403c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    int nextNormalIndex = 0;
404c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
405c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    for (int i = 0; i < polyLength; i++) {
4061bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck        float ratioZ = projectCasterToOutline(outlineData[i].position, lightCenter, poly[i]);
407c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        outlineData[i].radius = ratioZ * lightSize;
408c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
409c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        outlineData[currentNormalIndex].normal = ShadowTessellator::calculateNormal(
4101bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                outlineData[currentNormalIndex].position, outlineData[nextNormalIndex].position);
411c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        currentNormalIndex = (currentNormalIndex + 1) % polyLength;
412c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        nextNormalIndex++;
413c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    }
414c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
415c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    projectCasterToOutline(outlineCentroid, lightCenter, polyCentroid);
416c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
417c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    int penumbraIndex = 0;
418512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // Then each polygon's vertex produce at minmal 2 penumbra vertices.
419512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // Since the size can be dynamic here, we keep track of the size and update
420512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // the real size at the end.
421512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int allocatedPenumbraLength = 2 * polyLength + SPOT_MAX_EXTRA_CORNER_VERTEX_NUMBER;
422512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    Vector2 penumbra[allocatedPenumbraLength];
423512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int totalExtraCornerSliceNumber = 0;
424c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
425c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    Vector2 umbra[polyLength];
426c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
427512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // When centroid is covered by all circles from outline, then we consider
428512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // the umbra is invalid, and we will tune down the shadow strength.
429c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    bool hasValidUmbra = true;
430512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // We need the minimal of RaitoVI to decrease the spot shadow strength accordingly.
431512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    float minRaitoVI = FLT_MAX;
432c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
433c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    for (int i = 0; i < polyLength; i++) {
434c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Generate all the penumbra's vertices only using the (outline vertex + normal * radius)
435c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // There is no guarantee that the penumbra is still convex, but for
436c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // each outline vertex, it will connect to all its corresponding penumbra vertices as
437c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // triangle fans. And for neighber penumbra vertex, it will be a trapezoid.
438c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //
439c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Penumbra Vertices marked as Pi
440c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Outline Vertices marked as Vi
441c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //                                            (P3)
442c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //          (P2)                               |     ' (P4)
443c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //   (P1)'   |                                 |   '
444c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //         ' |                                 | '
445c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // (P0)  ------------------------------------------------(P5)
446c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           | (V0)                            |(V1)
447c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
448c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
449c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
450c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
451c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
452c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
453c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
454c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                 |
455c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //       (V3)-----------------------------------(V2)
456c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        int preNormalIndex = (i + polyLength - 1) % polyLength;
457c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
458512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        const Vector2& previousNormal = outlineData[preNormalIndex].normal;
459512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        const Vector2& currentNormal = outlineData[i].normal;
460512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
461512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        // Depending on how roundness we want for each corner, we can subdivide
462c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // further here and/or introduce some heuristic to decide how much the
463c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // subdivision should be.
464512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        int currentExtraSliceNumber = ShadowTessellator::getExtraVertexNumber(
465512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui                previousNormal, currentNormal, SPOT_CORNER_RADIANS_DIVISOR);
466c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
467512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        int currentCornerSliceNumber = 1 + currentExtraSliceNumber;
468512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        totalExtraCornerSliceNumber += currentExtraSliceNumber;
469512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#if DEBUG_SHADOW
470512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        ALOGD("currentExtraSliceNumber should be %d", currentExtraSliceNumber);
471512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        ALOGD("currentCornerSliceNumber should be %d", currentCornerSliceNumber);
472512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        ALOGD("totalCornerSliceNumber is %d", totalExtraCornerSliceNumber);
473512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#endif
474512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        if (CC_UNLIKELY(totalExtraCornerSliceNumber > SPOT_MAX_EXTRA_CORNER_VERTEX_NUMBER)) {
475512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            currentCornerSliceNumber = 1;
476512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
477512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        for (int k = 0; k <= currentCornerSliceNumber; k++) {
478512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            Vector2 avgNormal =
479512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui                    (previousNormal * (currentCornerSliceNumber - k) + currentNormal * k) /
480512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui                    currentCornerSliceNumber;
481512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            avgNormal.normalize();
4821bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck            penumbra[penumbraIndex++] = outlineData[i].position + avgNormal * outlineData[i].radius;
483512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
484c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
485c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Compute the umbra by the intersection from the outline's centroid!
486c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //
487c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //       (V) ------------------------------------
488c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |          '                       |
489c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |         '                        |
490c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |       ' (I)                      |
491c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |    '                             |
492c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           | '             (C)                |
493c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                  |
494c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                  |
495c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                  |
496c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           |                                  |
497c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //           ------------------------------------
498c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //
499c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Connect a line b/t the outline vertex (V) and the centroid (C), it will
500c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // intersect with the outline vertex's circle at point (I).
501c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Now, ratioVI = VI / VC, ratioIC = IC / VC
502c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // Then the intersetion point can be computed as Ixy = Vxy * ratioIC + Cxy * ratioVI;
503c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        //
504512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        // When all of the outline circles cover the the outline centroid, (like I is
505c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // on the other side of C), there is no real umbra any more, so we just fake
506c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // a small area around the centroid as the umbra, and tune down the spot
507c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // shadow's umbra strength to simulate the effect the whole shadow will
508c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // become lighter in this case.
509c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // The ratio can be simulated by using the inverse of maximum of ratioVI for
510c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // all (V).
511512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        float distOutline = (outlineData[i].position - outlineCentroid).length();
5123bd3fa1f1d437e22aee35381a559dcee15a437ddztenghui        if (CC_UNLIKELY(distOutline == 0)) {
513c50a03d78aaedd0003377e98710e7038bda330e9ztenghui            // If the outline has 0 area, then there is no spot shadow anyway.
514c50a03d78aaedd0003377e98710e7038bda330e9ztenghui            ALOGW("Outline has 0 area, no spot shadow!");
515c50a03d78aaedd0003377e98710e7038bda330e9ztenghui            return;
516c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        }
517512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
518512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        float ratioVI = outlineData[i].radius / distOutline;
5199db58c031f8ffa102a6d585cb585bed3bdb911a9Chris Craik        minRaitoVI = std::min(minRaitoVI, ratioVI);
520512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        if (ratioVI >= (1 - FAKE_UMBRA_SIZE_RATIO)) {
521512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            ratioVI = (1 - FAKE_UMBRA_SIZE_RATIO);
522c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        }
523c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // When we know we don't have valid umbra, don't bother to compute the
524c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // values below. But we can't skip the loop yet since we want to know the
525c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        // maximum ratio.
526512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        float ratioIC = 1 - ratioVI;
527512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        umbra[i] = outlineData[i].position * ratioIC + outlineCentroid * ratioVI;
528c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    }
529c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
530512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    hasValidUmbra = (minRaitoVI <= 1.0);
531c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    float shadowStrengthScale = 1.0;
532c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    if (!hasValidUmbra) {
533512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#if DEBUG_SHADOW
534c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        ALOGW("The object is too close to the light or too small, no real umbra!");
535512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#endif
536c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        for (int i = 0; i < polyLength; i++) {
537c50a03d78aaedd0003377e98710e7038bda330e9ztenghui            umbra[i] = outlineData[i].position * FAKE_UMBRA_SIZE_RATIO +
5381bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                       outlineCentroid * (1 - FAKE_UMBRA_SIZE_RATIO);
539c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        }
540512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        shadowStrengthScale = 1.0 / minRaitoVI;
541c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    }
542c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
543512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int penumbraLength = penumbraIndex;
544512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int umbraLength = polyLength;
545512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
546c50a03d78aaedd0003377e98710e7038bda330e9ztenghui#if DEBUG_SHADOW
5471bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck    ALOGD("penumbraLength is %d , allocatedPenumbraLength %d", penumbraLength,
5481bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck          allocatedPenumbraLength);
549c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    dumpPolygon(poly, polyLength, "input poly");
550c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    dumpPolygon(penumbra, penumbraLength, "penumbra");
551512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    dumpPolygon(umbra, umbraLength, "umbra");
552c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    ALOGD("hasValidUmbra is %d and shadowStrengthScale is %f", hasValidUmbra, shadowStrengthScale);
553c50a03d78aaedd0003377e98710e7038bda330e9ztenghui#endif
554c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
555512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // The penumbra and umbra needs to be in convex shape to keep consistency
556512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // and quality.
557512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // Since we are still shooting rays to penumbra, it needs to be convex.
558512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // Umbra can be represented as a fan from the centroid, but visually umbra
559512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // looks nicer when it is convex.
560512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    Vector2 finalUmbra[umbraLength];
561512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    Vector2 finalPenumbra[penumbraLength];
562512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int finalUmbraLength = hull(umbra, umbraLength, finalUmbra);
563512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int finalPenumbraLength = hull(penumbra, penumbraLength, finalPenumbra);
564512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
5651bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck    generateTriangleStrip(isCasterOpaque, shadowStrengthScale, finalPenumbra, finalPenumbraLength,
5661bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                          finalUmbra, finalUmbraLength, poly, polyLength, shadowTriangleStrip,
5671bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                          outlineCentroid);
568c50a03d78aaedd0003377e98710e7038bda330e9ztenghui}
569c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
5707b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui/**
571512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * This is only for experimental purpose.
572512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * After intersections are calculated, we could smooth the polygon if needed.
573512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * So far, we don't think it is more appealing yet.
5747b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui *
575512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @param level The level of smoothness.
576512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @param rays The total number of rays.
577512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * @param rayDist (In and Out) The distance for each ray.
578512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui *
579512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui */
580512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghuivoid SpotShadow::smoothPolygon(int level, int rays, float* rayDist) {
581512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    for (int k = 0; k < level; k++) {
582512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        for (int i = 0; i < rays; i++) {
583512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            float p1 = rayDist[(rays - 1 + i) % rays];
584512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            float p2 = rayDist[i];
585512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            float p3 = rayDist[(i + 1) % rays];
586512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            rayDist[i] = (p1 + p2 * 2 + p3) / 4;
587512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
588512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
589512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
590512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
591d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// Index pair is meant for storing the tessellation information for the penumbra
592d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// area. One index must come from exterior tangent of the circles, the other one
593d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// must come from the interior tangent of the circles.
594d2dcd6fded3a036f334a88bf9593398833f2919aztenghuistruct IndexPair {
595d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int outerIndex;
596d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int innerIndex;
597d2dcd6fded3a036f334a88bf9593398833f2919aztenghui};
5987b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
599d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// For one penumbra vertex, find the cloest umbra vertex and return its index.
600d2dcd6fded3a036f334a88bf9593398833f2919aztenghuiinline int getClosestUmbraIndex(const Vector2& pivot, const Vector2* polygon, int polygonLength) {
601d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    float minLengthSquared = FLT_MAX;
602d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int resultIndex = -1;
603d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    bool hasDecreased = false;
604d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Starting with some negative offset, assuming both umbra and penumbra are starting
605d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // at the same angle, this can help to find the result faster.
606d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Normally, loop 3 times, we can find the closest point.
607d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int offset = polygonLength - 2;
608d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < polygonLength; i++) {
609d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        int currentIndex = (i + offset) % polygonLength;
610d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        float currentLengthSquared = (pivot - polygon[currentIndex]).lengthSquared();
611d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        if (currentLengthSquared < minLengthSquared) {
612d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            if (minLengthSquared != FLT_MAX) {
613d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                hasDecreased = true;
614d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            }
615d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            minLengthSquared = currentLengthSquared;
616d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            resultIndex = currentIndex;
617d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        } else if (currentLengthSquared > minLengthSquared && hasDecreased) {
618d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // Early break b/c we have found the closet one and now the length
619d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // is increasing again.
620d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            break;
621512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
622512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
6231bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck    if (resultIndex == -1) {
624d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        ALOGE("resultIndex is -1, the polygon must be invalid!");
625d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        resultIndex = 0;
626d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    }
627d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    return resultIndex;
628512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
6297b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
6303932063bc75dc1e4efc2c428ca208d2e2290164dztenghui// Allow some epsilon here since the later ray intersection did allow for some small
6313932063bc75dc1e4efc2c428ca208d2e2290164dztenghui// floating point error, when the intersection point is slightly outside the segment.
632d2dcd6fded3a036f334a88bf9593398833f2919aztenghuiinline bool sameDirections(bool isPositiveCross, float a, float b) {
633d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    if (isPositiveCross) {
6343932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        return a >= -EPSILON && b >= -EPSILON;
635d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    } else {
6363932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        return a <= EPSILON && b <= EPSILON;
637512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
638512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
639512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
640d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// Find the right polygon edge to shoot the ray at.
641d2dcd6fded3a036f334a88bf9593398833f2919aztenghuiinline int findPolyIndex(bool isPositiveCross, int startPolyIndex, const Vector2& umbraDir,
6421bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                         const Vector2* polyToCentroid, int polyLength) {
643d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Make sure we loop with a bound.
644d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < polyLength; i++) {
645d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        int currentIndex = (i + startPolyIndex) % polyLength;
646d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        const Vector2& currentToCentroid = polyToCentroid[currentIndex];
647d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        const Vector2& nextToCentroid = polyToCentroid[(currentIndex + 1) % polyLength];
648d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
649d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        float currentCrossUmbra = currentToCentroid.cross(umbraDir);
650d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        float umbraCrossNext = umbraDir.cross(nextToCentroid);
651d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        if (sameDirections(isPositiveCross, currentCrossUmbra, umbraCrossNext)) {
652512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#if DEBUG_SHADOW
6531bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck            ALOGD("findPolyIndex loop %d times , index %d", i, currentIndex);
654512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#endif
655d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            return currentIndex;
656512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
657512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
658d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    LOG_ALWAYS_FATAL("Can't find the right polygon's edge from startPolyIndex %d", startPolyIndex);
659d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    return -1;
660512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
661512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
662d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// Generate the index pair for penumbra / umbra vertices, and more penumbra vertices
663d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// if needed.
664d2dcd6fded3a036f334a88bf9593398833f2919aztenghuiinline void genNewPenumbraAndPairWithUmbra(const Vector2* penumbra, int penumbraLength,
6651bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                           const Vector2* umbra, int umbraLength,
6661bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                           Vector2* newPenumbra, int& newPenumbraIndex,
6671bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                           IndexPair* verticesPair, int& verticesPairIndex) {
668d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // In order to keep everything in just one loop, we need to pre-compute the
669d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // closest umbra vertex for the last penumbra vertex.
6701bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck    int previousClosestUmbraIndex =
6711bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck            getClosestUmbraIndex(penumbra[penumbraLength - 1], umbra, umbraLength);
672d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < penumbraLength; i++) {
673d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        const Vector2& currentPenumbraVertex = penumbra[i];
674d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // For current penumbra vertex, starting from previousClosestUmbraIndex,
675d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // then check the next one until the distance increase.
676d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // The last one before the increase is the umbra vertex we need to pair with.
6773932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        float currentLengthSquared =
6783932063bc75dc1e4efc2c428ca208d2e2290164dztenghui                (currentPenumbraVertex - umbra[previousClosestUmbraIndex]).lengthSquared();
6793932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        int currentClosestUmbraIndex = previousClosestUmbraIndex;
680d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        int indexDelta = 0;
681d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        for (int j = 1; j < umbraLength; j++) {
682d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            int newUmbraIndex = (previousClosestUmbraIndex + j) % umbraLength;
683d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            float newLengthSquared = (currentPenumbraVertex - umbra[newUmbraIndex]).lengthSquared();
684d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            if (newLengthSquared > currentLengthSquared) {
6853932063bc75dc1e4efc2c428ca208d2e2290164dztenghui                // currentClosestUmbraIndex is the umbra vertex's index which has
6863932063bc75dc1e4efc2c428ca208d2e2290164dztenghui                // currently found smallest distance, so we can simply break here.
687512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui                break;
688512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            } else {
689d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                currentLengthSquared = newLengthSquared;
690d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                indexDelta++;
6913932063bc75dc1e4efc2c428ca208d2e2290164dztenghui                currentClosestUmbraIndex = newUmbraIndex;
69250ecf849cb7ccc3482517b74d2214b347927791eztenghui            }
69350ecf849cb7ccc3482517b74d2214b347927791eztenghui        }
694d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
695d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        if (indexDelta > 1) {
6961bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck            // For those umbra don't have  penumbra, generate new penumbra vertices by
6971bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck            // interpolation.
698d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            //
699d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // Assuming Pi for penumbra vertices, and Ui for umbra vertices.
700d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // In the case like below P1 paired with U1 and P2 paired with  U5.
701d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // U2 to U4 are unpaired umbra vertices.
702d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            //
703d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // P1                                        P2
704d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // |                                          |
705d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // U1     U2                   U3     U4     U5
706d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            //
707d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // We will need to generate 3 more penumbra vertices P1.1, P1.2, P1.3
708d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // to pair with U2 to U4.
709d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            //
710d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // P1     P1.1                P1.2   P1.3    P2
711d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // |       |                   |      |      |
712d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // U1     U2                   U3     U4     U5
713d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            //
714d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // That distance ratio b/t Ui to U1 and Ui to U5 decides its paired penumbra
715d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // vertex's location.
716d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            int newPenumbraNumber = indexDelta - 1;
717d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
718a1f563134065abb360096cc06f6bfe4a8cca7a48Keith Mok            float accumulatedDeltaLength[indexDelta];
719d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            float totalDeltaLength = 0;
720d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
721d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // To save time, cache the previous umbra vertex info outside the loop
722d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // and update each loop.
723d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            Vector2 previousClosestUmbra = umbra[previousClosestUmbraIndex];
724d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            Vector2 skippedUmbra;
725d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // Use umbra data to precompute the length b/t unpaired umbra vertices,
726d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // and its ratio against the total length.
727d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            for (int k = 0; k < indexDelta; k++) {
728d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                int skippedUmbraIndex = (previousClosestUmbraIndex + k + 1) % umbraLength;
729d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                skippedUmbra = umbra[skippedUmbraIndex];
730d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                float currentDeltaLength = (skippedUmbra - previousClosestUmbra).length();
731d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
732d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                totalDeltaLength += currentDeltaLength;
733d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                accumulatedDeltaLength[k] = totalDeltaLength;
734d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
735d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                previousClosestUmbra = skippedUmbra;
736512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            }
737512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
738d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            const Vector2& previousPenumbra = penumbra[(i + penumbraLength - 1) % penumbraLength];
739d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // Then for each unpaired umbra vertex, create a new penumbra by the ratio,
740d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // and pair them togehter.
741d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            for (int k = 0; k < newPenumbraNumber; k++) {
742d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                float weightForCurrentPenumbra = 1.0f;
743d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                if (totalDeltaLength != 0.0f) {
744d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                    weightForCurrentPenumbra = accumulatedDeltaLength[k] / totalDeltaLength;
745d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                }
746d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                float weightForPreviousPenumbra = 1.0f - weightForCurrentPenumbra;
747512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
748d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                Vector2 interpolatedPenumbra = currentPenumbraVertex * weightForCurrentPenumbra +
7491bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                               previousPenumbra * weightForPreviousPenumbra;
750512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
751d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                int skippedUmbraIndex = (previousClosestUmbraIndex + k + 1) % umbraLength;
752edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe                verticesPair[verticesPairIndex].outerIndex = newPenumbraIndex;
753edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe                verticesPair[verticesPairIndex].innerIndex = skippedUmbraIndex;
754edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe                verticesPairIndex++;
755d2dcd6fded3a036f334a88bf9593398833f2919aztenghui                newPenumbra[newPenumbraIndex++] = interpolatedPenumbra;
756512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            }
757512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
758edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe        verticesPair[verticesPairIndex].outerIndex = newPenumbraIndex;
759edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe        verticesPair[verticesPairIndex].innerIndex = currentClosestUmbraIndex;
760edaecc1db0584fa017822dfc2da0c968b53967e6Andreas Gampe        verticesPairIndex++;
761d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        newPenumbra[newPenumbraIndex++] = currentPenumbraVertex;
7627b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
763d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        previousClosestUmbraIndex = currentClosestUmbraIndex;
764512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
765512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
766512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
767d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// Precompute all the polygon's vector, return true if the reference cross product is positive.
7681bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reckinline bool genPolyToCentroid(const Vector2* poly2d, int polyLength, const Vector2& centroid,
7691bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                              Vector2* polyToCentroid) {
770d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int j = 0; j < polyLength; j++) {
771d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        polyToCentroid[j] = poly2d[j] - centroid;
7723932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        // Normalize these vectors such that we can use epsilon comparison after
7733932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        // computing their cross products with another normalized vector.
7743932063bc75dc1e4efc2c428ca208d2e2290164dztenghui        polyToCentroid[j].normalize();
7757b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui    }
776d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    float refCrossProduct = 0;
777d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int j = 0; j < polyLength; j++) {
778d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        refCrossProduct = polyToCentroid[j].cross(polyToCentroid[(j + 1) % polyLength]);
779d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        if (refCrossProduct != 0) {
780d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            break;
781d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        }
782512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
783d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
784d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    return refCrossProduct > 0;
7857b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui}
7867b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
787d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// For one umbra vertex, shoot an ray from centroid to it.
788d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// If the ray hit the polygon first, then return the intersection point as the
789d2dcd6fded3a036f334a88bf9593398833f2919aztenghui// closer vertex.
790d2dcd6fded3a036f334a88bf9593398833f2919aztenghuiinline Vector2 getCloserVertex(const Vector2& umbraVertex, const Vector2& centroid,
7911bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                               const Vector2* poly2d, int polyLength, const Vector2* polyToCentroid,
7921bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                               bool isPositiveCross, int& previousPolyIndex) {
793d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    Vector2 umbraToCentroid = umbraVertex - centroid;
794d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    float distanceToUmbra = umbraToCentroid.length();
795d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    umbraToCentroid = umbraToCentroid / distanceToUmbra;
796d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
797d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // previousPolyIndex is updated for each item such that we can minimize the
798d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // looping inside findPolyIndex();
7991bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck    previousPolyIndex = findPolyIndex(isPositiveCross, previousPolyIndex, umbraToCentroid,
8001bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                      polyToCentroid, polyLength);
801d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
802d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    float dx = umbraToCentroid.x;
803d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    float dy = umbraToCentroid.y;
8041bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck    float distanceToIntersectPoly =
8051bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck            rayIntersectPoints(centroid, dx, dy, poly2d[previousPolyIndex],
8061bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                               poly2d[(previousPolyIndex + 1) % polyLength]);
807d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    if (distanceToIntersectPoly < 0) {
808d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        distanceToIntersectPoly = 0;
809512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
810f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
811d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Pick the closer one as the occluded area vertex.
812d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    Vector2 closerVertex;
813d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    if (distanceToIntersectPoly < distanceToUmbra) {
814d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        closerVertex.x = centroid.x + dx * distanceToIntersectPoly;
815d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        closerVertex.y = centroid.y + dy * distanceToIntersectPoly;
816d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    } else {
817d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        closerVertex = umbraVertex;
818512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
819512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
820d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    return closerVertex;
821512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
822512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
823512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui/**
824512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui * Generate a triangle strip given two convex polygon
825512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui**/
82664bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampevoid SpotShadow::generateTriangleStrip(bool isCasterOpaque, float shadowStrengthScale,
8271bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                       Vector2* penumbra, int penumbraLength, Vector2* umbra,
8281bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                       int umbraLength, const Vector3* poly, int polyLength,
8291bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                       VertexBuffer& shadowTriangleStrip, const Vector2& centroid) {
830512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    bool hasOccludedUmbraArea = false;
831512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    Vector2 poly2d[polyLength];
832f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
833512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    if (isCasterOpaque) {
834512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        for (int i = 0; i < polyLength; i++) {
835512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            poly2d[i].x = poly[i].x;
836512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            poly2d[i].y = poly[i].y;
837512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
838512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        // Make sure the centroid is inside the umbra, otherwise, fall back to the
839512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        // approach as if there is no occluded umbra area.
840512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        if (testPointInsidePolygon(centroid, poly2d, polyLength)) {
841512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            hasOccludedUmbraArea = true;
842512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
843512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
844512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
845d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // For each penumbra vertex, find its corresponding closest umbra vertex index.
846d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //
847d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Penumbra Vertices marked as Pi
848d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Umbra Vertices marked as Ui
849d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //                                            (P3)
850d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //          (P2)                               |     ' (P4)
851d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //   (P1)'   |                                 |   '
852d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //         ' |                                 | '
853d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // (P0)  ------------------------------------------------(P5)
854d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           | (U0)                            |(U1)
855d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
856d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |(U2)     (P5.1)
857d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
858d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
859d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
860d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
861d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
862d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //           |                                 |
863d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //       (U4)-----------------------------------(U3)      (P6)
864d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //
865d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // At least, like P0, P1, P2, they will find the matching umbra as U0.
866d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // If we jump over some umbra vertex without matching penumbra vertex, then
867d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // we will generate some new penumbra vertex by interpolation. Like P6 is
868d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // matching U3, but U2 is not matched with any penumbra vertex.
869d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // So interpolate P5.1 out and match U2.
870d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // In this way, every umbra vertex will have a matching penumbra vertex.
871d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //
872d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // The total pair number can be as high as umbraLength + penumbraLength.
873d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    const int maxNewPenumbraLength = umbraLength + penumbraLength;
874d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    IndexPair verticesPair[maxNewPenumbraLength];
875d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int verticesPairIndex = 0;
876d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
877d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Cache all the existing penumbra vertices and newly interpolated vertices into a
878d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // a new array.
879d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    Vector2 newPenumbra[maxNewPenumbraLength];
880d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int newPenumbraIndex = 0;
881d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
882d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // For each penumbra vertex, find its closet umbra vertex by comparing the
883d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // neighbor umbra vertices.
884d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    genNewPenumbraAndPairWithUmbra(penumbra, penumbraLength, umbra, umbraLength, newPenumbra,
8851bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                   newPenumbraIndex, verticesPair, verticesPairIndex);
886d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    ShadowTessellator::checkOverflow(verticesPairIndex, maxNewPenumbraLength, "Spot pair");
887d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    ShadowTessellator::checkOverflow(newPenumbraIndex, maxNewPenumbraLength, "Spot new penumbra");
888d2dcd6fded3a036f334a88bf9593398833f2919aztenghui#if DEBUG_SHADOW
889d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < umbraLength; i++) {
890d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        ALOGD("umbra i %d,  [%f, %f]", i, umbra[i].x, umbra[i].y);
891d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    }
892d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < newPenumbraIndex; i++) {
893d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        ALOGD("new penumbra i %d,  [%f, %f]", i, newPenumbra[i].x, newPenumbra[i].y);
894d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    }
895d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < verticesPairIndex; i++) {
896d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        ALOGD("index i %d,  [%d, %d]", i, verticesPair[i].outerIndex, verticesPair[i].innerIndex);
897512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
898d2dcd6fded3a036f334a88bf9593398833f2919aztenghui#endif
899512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
900d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // For the size of vertex buffer, we need 3 rings, one has newPenumbraSize,
901d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // one has umbraLength, the last one has at most umbraLength.
902d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    //
903d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // For the size of index buffer, the umbra area needs (2 * umbraLength + 2).
904d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // The penumbra one can vary a bit, but it is bounded by (2 * verticesPairIndex + 2).
905d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // And 2 more for jumping between penumbra to umbra.
906d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    const int newPenumbraLength = newPenumbraIndex;
907d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    const int totalVertexCount = newPenumbraLength + umbraLength * 2;
908d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    const int totalIndexCount = 2 * umbraLength + 2 * verticesPairIndex + 6;
9091bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck    AlphaVertex* shadowVertices = shadowTriangleStrip.alloc<AlphaVertex>(totalVertexCount);
9101bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck    uint16_t* indexBuffer = shadowTriangleStrip.allocIndices<uint16_t>(totalIndexCount);
911512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    int vertexBufferIndex = 0;
912d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int indexBufferIndex = 0;
913512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
914d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Fill the IB and VB for the penumbra area.
915d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < newPenumbraLength; i++) {
9161bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck        AlphaVertex::set(&shadowVertices[vertexBufferIndex++], newPenumbra[i].x, newPenumbra[i].y,
9171bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                         PENUMBRA_ALPHA);
918d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    }
9199c555566bfef718464546dcab3640f64d2fdc55dTeng-Hui Zhu    // Since the umbra can be a faked one when the occluder is too high, the umbra should be lighter
9209c555566bfef718464546dcab3640f64d2fdc55dTeng-Hui Zhu    // in this case.
9219c555566bfef718464546dcab3640f64d2fdc55dTeng-Hui Zhu    float scaledUmbraAlpha = UMBRA_ALPHA * shadowStrengthScale;
9229c555566bfef718464546dcab3640f64d2fdc55dTeng-Hui Zhu
923d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < umbraLength; i++) {
924d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        AlphaVertex::set(&shadowVertices[vertexBufferIndex++], umbra[i].x, umbra[i].y,
9251bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                         scaledUmbraAlpha);
926512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
927512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
928d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    for (int i = 0; i < verticesPairIndex; i++) {
929d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        indexBuffer[indexBufferIndex++] = verticesPair[i].outerIndex;
930d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // All umbra index need to be offseted by newPenumbraSize.
931d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        indexBuffer[indexBufferIndex++] = verticesPair[i].innerIndex + newPenumbraLength;
932d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    }
933d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBuffer[indexBufferIndex++] = verticesPair[0].outerIndex;
934d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBuffer[indexBufferIndex++] = verticesPair[0].innerIndex + newPenumbraLength;
935d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
936d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Now fill the IB and VB for the umbra area.
937d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // First duplicated the index from previous strip and the first one for the
938d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // degenerated triangles.
939d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBuffer[indexBufferIndex] = indexBuffer[indexBufferIndex - 1];
940d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBufferIndex++;
941d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBuffer[indexBufferIndex++] = newPenumbraLength + 0;
942d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Save the first VB index for umbra area in order to close the loop.
943d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    int savedStartIndex = vertexBufferIndex;
944512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
945512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    if (hasOccludedUmbraArea) {
946d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // Precompute all the polygon's vector, and the reference cross product,
947d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // in order to find the right polygon edge for the ray to intersect.
948d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        Vector2 polyToCentroid[polyLength];
949d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        bool isPositiveCross = genPolyToCentroid(poly2d, polyLength, centroid, polyToCentroid);
950d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
951d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // Because both the umbra and polygon are going in the same direction,
952d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // we can save the previous polygon index to make sure we have less polygon
953d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // vertex to compute for each ray.
954d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        int previousPolyIndex = 0;
955d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        for (int i = 0; i < umbraLength; i++) {
956d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // Shoot a ray from centroid to each umbra vertices and pick the one with
957d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // shorter distance to the centroid, b/t the umbra vertex or the intersection point.
9581bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck            Vector2 closerVertex =
9591bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                    getCloserVertex(umbra[i], centroid, poly2d, polyLength, polyToCentroid,
9601bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                    isPositiveCross, previousPolyIndex);
961d2dcd6fded3a036f334a88bf9593398833f2919aztenghui
962d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            // We already stored the umbra vertices, just need to add the occlued umbra's ones.
963d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            indexBuffer[indexBufferIndex++] = newPenumbraLength + i;
964d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            indexBuffer[indexBufferIndex++] = vertexBufferIndex;
9651bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck            AlphaVertex::set(&shadowVertices[vertexBufferIndex++], closerVertex.x, closerVertex.y,
9661bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                             scaledUmbraAlpha);
967512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
968512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    } else {
969d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // If there is no occluded umbra at all, then draw the triangle fan
970d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        // starting from the centroid to all umbra vertices.
971512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        int lastCentroidIndex = vertexBufferIndex;
9721bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck        AlphaVertex::set(&shadowVertices[vertexBufferIndex++], centroid.x, centroid.y,
9731bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                         scaledUmbraAlpha);
974d2dcd6fded3a036f334a88bf9593398833f2919aztenghui        for (int i = 0; i < umbraLength; i++) {
975d2dcd6fded3a036f334a88bf9593398833f2919aztenghui            indexBuffer[indexBufferIndex++] = newPenumbraLength + i;
976512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            indexBuffer[indexBufferIndex++] = lastCentroidIndex;
977512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui        }
978512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    }
979d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    // Closing the umbra area triangle's loop here.
980d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBuffer[indexBufferIndex++] = newPenumbraLength;
981d2dcd6fded3a036f334a88bf9593398833f2919aztenghui    indexBuffer[indexBufferIndex++] = savedStartIndex;
982512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
983512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    // At the end, update the real index and vertex buffer size.
984512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    shadowTriangleStrip.updateVertexCount(vertexBufferIndex);
985512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    shadowTriangleStrip.updateIndexCount(indexBufferIndex);
986512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    ShadowTessellator::checkOverflow(vertexBufferIndex, totalVertexCount, "Spot Vertex Buffer");
987512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    ShadowTessellator::checkOverflow(indexBufferIndex, totalIndexCount, "Spot Index Buffer");
988512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
989117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craik    shadowTriangleStrip.setMeshFeatureFlags(VertexBuffer::kAlpha | VertexBuffer::kIndices);
990512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    shadowTriangleStrip.computeBounds<AlphaVertex>();
991512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui}
992512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
993512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#if DEBUG_SHADOW
994512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
995512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui#define TEST_POINT_NUMBER 128
996f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui/**
997f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * Calculate the bounds for generating random test points.
998f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui */
9991bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reckvoid SpotShadow::updateBound(const Vector2 inVector, Vector2& lowerBound, Vector2& upperBound) {
1000f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    if (inVector.x < lowerBound.x) {
1001f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        lowerBound.x = inVector.x;
1002f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1003f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1004f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    if (inVector.y < lowerBound.y) {
1005f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        lowerBound.y = inVector.y;
1006f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1007f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1008f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    if (inVector.x > upperBound.x) {
1009f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        upperBound.x = inVector.x;
1010f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1011f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1012f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    if (inVector.y > upperBound.y) {
1013f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        upperBound.y = inVector.y;
1014f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1015f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui}
1016f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1017f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui/**
1018f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * For debug purpose, when things go wrong, dump the whole polygon data.
1019f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui */
1020c50a03d78aaedd0003377e98710e7038bda330e9ztenghuivoid SpotShadow::dumpPolygon(const Vector2* poly, int polyLength, const char* polyName) {
1021c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    for (int i = 0; i < polyLength; i++) {
1022c50a03d78aaedd0003377e98710e7038bda330e9ztenghui        ALOGD("polygon %s i %d x %f y %f", polyName, i, poly[i].x, poly[i].y);
1023c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    }
1024c50a03d78aaedd0003377e98710e7038bda330e9ztenghui}
1025c50a03d78aaedd0003377e98710e7038bda330e9ztenghui
1026c50a03d78aaedd0003377e98710e7038bda330e9ztenghui/**
1027c50a03d78aaedd0003377e98710e7038bda330e9ztenghui * For debug purpose, when things go wrong, dump the whole polygon data.
1028c50a03d78aaedd0003377e98710e7038bda330e9ztenghui */
1029c50a03d78aaedd0003377e98710e7038bda330e9ztenghuivoid SpotShadow::dumpPolygon(const Vector3* poly, int polyLength, const char* polyName) {
1030f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    for (int i = 0; i < polyLength; i++) {
10318d0ec389531d071529fb0a800f10733b057205d9Teng-Hui Zhu        ALOGD("polygon %s i %d x %f y %f z %f", polyName, i, poly[i].x, poly[i].y, poly[i].z);
1032f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1033f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui}
1034f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1035f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui/**
1036f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * Test whether the polygon is convex.
1037f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui */
10381bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reckbool SpotShadow::testConvex(const Vector2* polygon, int polygonLength, const char* name) {
1039f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    bool isConvex = true;
1040f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    for (int i = 0; i < polygonLength; i++) {
1041f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        Vector2 start = polygon[i];
1042f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        Vector2 middle = polygon[(i + 1) % polygonLength];
1043f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        Vector2 end = polygon[(i + 2) % polygonLength];
1044f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
10459122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float delta = (float(middle.x) - start.x) * (float(end.y) - start.y) -
10461bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                      (float(middle.y) - start.y) * (float(end.x) - start.x);
1047f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        bool isCCWOrCoLinear = (delta >= EPSILON);
1048f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1049f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        if (isCCWOrCoLinear) {
105050ecf849cb7ccc3482517b74d2214b347927791eztenghui            ALOGW("(Error Type 2): polygon (%s) is not a convex b/c start (x %f, y %f),"
10511bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                  "middle (x %f, y %f) and end (x %f, y %f) , delta is %f !!!",
10521bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                  name, start.x, start.y, middle.x, middle.y, end.x, end.y, delta);
1053f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            isConvex = false;
1054f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            break;
1055f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        }
1056f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1057f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    return isConvex;
1058f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui}
1059f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1060f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui/**
1061f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * Test whether or not the polygon (intersection) is within the 2 input polygons.
1062f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * Using Marte Carlo method, we generate a random point, and if it is inside the
1063f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui * intersection, then it must be inside both source polygons.
1064f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui */
10651bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reckvoid SpotShadow::testIntersection(const Vector2* poly1, int poly1Length, const Vector2* poly2,
10661bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                  int poly2Length, const Vector2* intersection,
10671bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                  int intersectionLength) {
1068f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    // Find the min and max of x and y.
1069c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    Vector2 lowerBound = {FLT_MAX, FLT_MAX};
1070c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    Vector2 upperBound = {-FLT_MAX, -FLT_MAX};
1071f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    for (int i = 0; i < poly1Length; i++) {
1072f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        updateBound(poly1[i], lowerBound, upperBound);
1073f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1074f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    for (int i = 0; i < poly2Length; i++) {
1075f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        updateBound(poly2[i], lowerBound, upperBound);
1076f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1077f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1078f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    bool dumpPoly = false;
1079f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    for (int k = 0; k < TEST_POINT_NUMBER; k++) {
1080f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        // Generate a random point between minX, minY and maxX, maxY.
10819122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float randomX = rand() / float(RAND_MAX);
10829122b1b168d2a74d51517ed7282f4d6a8adea367ztenghui        float randomY = rand() / float(RAND_MAX);
1083f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1084f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        Vector2 testPoint;
1085f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        testPoint.x = lowerBound.x + randomX * (upperBound.x - lowerBound.x);
1086f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        testPoint.y = lowerBound.y + randomY * (upperBound.y - lowerBound.y);
1087f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1088f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        // If the random point is in both poly 1 and 2, then it must be intersection.
1089f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        if (testPointInsidePolygon(testPoint, intersection, intersectionLength)) {
1090f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            if (!testPointInsidePolygon(testPoint, poly1, poly1Length)) {
1091f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                dumpPoly = true;
109250ecf849cb7ccc3482517b74d2214b347927791eztenghui                ALOGW("(Error Type 1): one point (%f, %f) in the intersection is"
10931bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                      " not in the poly1",
10941bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                      testPoint.x, testPoint.y);
1095f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            }
1096f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1097f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            if (!testPointInsidePolygon(testPoint, poly2, poly2Length)) {
1098f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui                dumpPoly = true;
109950ecf849cb7ccc3482517b74d2214b347927791eztenghui                ALOGW("(Error Type 1): one point (%f, %f) in the intersection is"
11001bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                      " not in the poly2",
11011bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                      testPoint.x, testPoint.y);
1102f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            }
1103f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        }
1104f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1105f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1106f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    if (dumpPoly) {
1107f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        dumpPolygon(intersection, intersectionLength, "intersection");
1108f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        for (int i = 1; i < intersectionLength; i++) {
1109f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            Vector2 delta = intersection[i] - intersection[i - 1];
1110f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui            ALOGD("Intersetion i, %d Vs i-1 is delta %f", i, delta.lengthSquared());
1111f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        }
1112f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
1113f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        dumpPolygon(poly1, poly1Length, "poly 1");
1114f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui        dumpPolygon(poly2, poly2Length, "poly 2");
1115f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui    }
1116f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui}
1117f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui#endif
1118f5ca8b4cb178008472e67fa0ae6a3e3fa75d7952ztenghui
11191bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck};  // namespace uirenderer
11201bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck};  // namespace android
1121