AmbientShadow.cpp revision 7940dc57e3ffcf9c4a33375215f3a42250fa896c
155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui/*
255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * Copyright (C) 2013 The Android Open Source Project
355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * Licensed under the Apache License, Version 2.0 (the "License");
555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * you may not use this file except in compliance with the License.
655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * You may obtain a copy of the License at
755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *      http://www.apache.org/licenses/LICENSE-2.0
955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
1055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * Unless required by applicable law or agreed to in writing, software
1155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * distributed under the License is distributed on an "AS IS" BASIS,
1255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * See the License for the specific language governing permissions and
1455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * limitations under the License.
1555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui */
1655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
1755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#define LOG_TAG "OpenGLRenderer"
1855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
1955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#include <math.h>
2055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#include <utils/Log.h>
21564acf7c9bff822f608cda0d5df0a64a9f9aaefdChris Craik#include <utils/Vector.h>
2255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
2355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#include "AmbientShadow.h"
2463d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui#include "ShadowTessellator.h"
2555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#include "Vertex.h"
2655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
2755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghuinamespace android {
2855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghuinamespace uirenderer {
2955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
3055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui/**
3155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * Calculate the shadows as a triangle strips while alpha value as the
3255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * shadow values.
3355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
3450ecf849cb7ccc3482517b74d2214b347927791eztenghui * @param isCasterOpaque Whether the caster is opaque.
3555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param vertices The shadow caster's polygon, which is represented in a Vector3
3655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *                  array.
3755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param vertexCount The length of caster's polygon in terms of number of
3855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *                    vertices.
3963d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui * @param centroid3d The centroid of the shadow caster.
4055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param heightFactor The factor showing the higher the object, the lighter the
4155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *                     shadow.
4255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param geomFactor The factor scaling the geometry expansion along the normal.
4355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
4455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param shadowVertexBuffer Return an floating point array of (x, y, a)
4555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *               triangle strips mode.
4655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui */
4750ecf849cb7ccc3482517b74d2214b347927791eztenghuiVertexBufferMode AmbientShadow::createAmbientShadow(bool isCasterOpaque,
4850ecf849cb7ccc3482517b74d2214b347927791eztenghui        const Vector3* vertices, int vertexCount, const Vector3& centroid3d,
4950ecf849cb7ccc3482517b74d2214b347927791eztenghui        float heightFactor, float geomFactor, VertexBuffer& shadowVertexBuffer) {
5063d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui    const int rays = SHADOW_RAY_COUNT;
5150ecf849cb7ccc3482517b74d2214b347927791eztenghui    VertexBufferMode mode = kVertexBufferMode_OnePolyRingShadow;
5255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // Validate the inputs.
53726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    if (vertexCount < 3 || heightFactor <= 0 || rays <= 0
5455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        || geomFactor <= 0) {
5555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#if DEBUG_SHADOW
5650ecf849cb7ccc3482517b74d2214b347927791eztenghui        ALOGW("Invalid input for createAmbientShadow(), early return!");
5755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#endif
5850ecf849cb7ccc3482517b74d2214b347927791eztenghui        return mode; // vertex buffer is empty, so any mode doesn't matter.
5955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    }
6055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
61564acf7c9bff822f608cda0d5df0a64a9f9aaefdChris Craik    Vector<Vector2> dir; // TODO: use C++11 unique_ptr
62564acf7c9bff822f608cda0d5df0a64a9f9aaefdChris Craik    dir.setCapacity(rays);
6355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    float rayDist[rays];
6455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    float rayHeight[rays];
657940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    calculateRayDirections(rays, vertices, vertexCount, centroid3d, dir.editArray());
6655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
6755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // Calculate the length and height of the points along the edge.
6855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    //
6955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // The math here is:
7055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // Intersect each ray (starting from the centroid) with the polygon.
7155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    for (int i = 0; i < rays; i++) {
7255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        int edgeIndex;
7355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float edgeFraction;
7455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float rayDistance;
7563d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui        calculateIntersection(vertices, vertexCount, centroid3d, dir[i], edgeIndex,
7655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                edgeFraction, rayDistance);
7755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        rayDist[i] = rayDistance;
7855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        if (edgeIndex < 0 || edgeIndex >= vertexCount) {
7955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#if DEBUG_SHADOW
8050ecf849cb7ccc3482517b74d2214b347927791eztenghui            ALOGW("Invalid edgeIndex!");
8155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#endif
8255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui            edgeIndex = 0;
8355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        }
8455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float h1 = vertices[edgeIndex].z;
8555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float h2 = vertices[((edgeIndex + 1) % vertexCount)].z;
8655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        rayHeight[i] = h1 + edgeFraction * (h2 - h1);
8755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    }
8855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
8955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // The output buffer length basically is roughly rays * layers, but since we
9055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // need triangle strips, so we need to duplicate vertices to accomplish that.
9150ecf849cb7ccc3482517b74d2214b347927791eztenghui    AlphaVertex* shadowVertices =
9250ecf849cb7ccc3482517b74d2214b347927791eztenghui            shadowVertexBuffer.alloc<AlphaVertex>(SHADOW_VERTEX_COUNT);
9355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
9455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // Calculate the vertex of the shadows.
9555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    //
9655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // The math here is:
9755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // Along the edges of the polygon, for each intersection point P (generated above),
9855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // calculate the normal N, which should be perpendicular to the edge of the
9955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // polygon (represented by the neighbor intersection points) .
10055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // Shadow's vertices will be generated as : P + N * scale.
10150ecf849cb7ccc3482517b74d2214b347927791eztenghui    const Vector2 centroid2d = Vector2(centroid3d.x, centroid3d.y);
102726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik    for (int rayIndex = 0; rayIndex < rays; rayIndex++) {
103726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        Vector2 normal(1.0f, 0.0f);
104726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        calculateNormal(rays, rayIndex, dir.array(), rayDist, normal);
10555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
106726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        // The vertex should be start from rayDist[i] then scale the
107726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        // normalizeNormal!
108726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        Vector2 intersection = dir[rayIndex] * rayDist[rayIndex] +
10950ecf849cb7ccc3482517b74d2214b347927791eztenghui                centroid2d;
11055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
111726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        // outer ring of points, expanded based upon height of each ray intersection
112726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        float expansionDist = rayHeight[rayIndex] * heightFactor *
113726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik                geomFactor;
114726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        AlphaVertex::set(&shadowVertices[rayIndex],
115726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik                intersection.x + normal.x * expansionDist,
116726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik                intersection.y + normal.y * expansionDist,
117726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik                0.0f);
11855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
119726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        // inner ring of points
120726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik        float opacity = 1.0 / (1 + rayHeight[rayIndex] * heightFactor);
12150ecf849cb7ccc3482517b74d2214b347927791eztenghui        AlphaVertex::set(&shadowVertices[rays + rayIndex],
122726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik                intersection.x,
123726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik                intersection.y,
124726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik                opacity);
12555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    }
12655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
12750ecf849cb7ccc3482517b74d2214b347927791eztenghui    // If caster isn't opaque, we need to to fill the umbra by storing the umbra's
12850ecf849cb7ccc3482517b74d2214b347927791eztenghui    // centroid in the innermost ring of vertices.
12950ecf849cb7ccc3482517b74d2214b347927791eztenghui    if (!isCasterOpaque) {
13050ecf849cb7ccc3482517b74d2214b347927791eztenghui        mode = kVertexBufferMode_TwoPolyRingShadow;
13150ecf849cb7ccc3482517b74d2214b347927791eztenghui        float centroidAlpha = 1.0 / (1 + centroid3d.z * heightFactor);
13250ecf849cb7ccc3482517b74d2214b347927791eztenghui        AlphaVertex centroidXYA;
13350ecf849cb7ccc3482517b74d2214b347927791eztenghui        AlphaVertex::set(&centroidXYA, centroid2d.x, centroid2d.y, centroidAlpha);
13450ecf849cb7ccc3482517b74d2214b347927791eztenghui        for (int rayIndex = 0; rayIndex < rays; rayIndex++) {
13550ecf849cb7ccc3482517b74d2214b347927791eztenghui            shadowVertices[2 * rays + rayIndex] = centroidXYA;
13650ecf849cb7ccc3482517b74d2214b347927791eztenghui        }
13755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    }
13850ecf849cb7ccc3482517b74d2214b347927791eztenghui
13950ecf849cb7ccc3482517b74d2214b347927791eztenghui#if DEBUG_SHADOW
14063d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui    for (int i = 0; i < SHADOW_VERTEX_COUNT; i++) {
14163d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui        ALOGD("ambient shadow value: i %d, (x:%f, y:%f, a:%f)", i, shadowVertices[i].x,
14263d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui                shadowVertices[i].y, shadowVertices[i].alpha);
14355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    }
14455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#endif
14550ecf849cb7ccc3482517b74d2214b347927791eztenghui    return mode;
14655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui}
14755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
14855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui/**
14955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * Generate an array of rays' direction vectors.
15055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
15155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param rays The number of rays shooting out from the centroid.
1527940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui * @param vertices Vertices of the polygon.
1537940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui * @param vertexCount The number of vertices.
1547940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui * @param centroid3d The centroid of the polygon.
15555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param dir Return the array of ray vectors.
15655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui */
1577940dc57e3ffcf9c4a33375215f3a42250fa896cztenghuivoid AmbientShadow::calculateRayDirections(const int rays, const Vector3* vertices,
1587940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        const int vertexCount, const Vector3& centroid3d, Vector2* dir) {
1597940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    // If we don't have enough rays, then fall back to the uniform distribution.
1607940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    if (vertexCount * 2 > rays) {
1617940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        float deltaAngle = 2 * M_PI / rays;
1627940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        for (int i = 0; i < rays; i++) {
1637940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            dir[i].x = sinf(deltaAngle * i);
1647940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            dir[i].y = cosf(deltaAngle * i);
1657940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        }
1667940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        return;
1677940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    }
16855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
1697940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    // If we have enough rays, then we assign each vertices a ray, and distribute
1707940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    // the rest uniformly.
1717940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    float rayThetas[rays];
1727940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui
1737940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    const int uniformRayCount = rays - vertexCount;
1747940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    const float deltaAngle = 2 * M_PI / uniformRayCount;
1757940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui
1767940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    // We have to generate all the vertices' theta anyway and we also need to
1777940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    // find the minimal, so let's precompute it first.
1787940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    // Since the incoming polygon is clockwise, we can find the dip to identify
1797940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    // the minimal theta.
1807940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    float polyThetas[vertexCount];
1817940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    int minimalPolyThetaIndex = 0;
1827940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    for (int i = 0; i < vertexCount; i++) {
1837940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        polyThetas[i] = atan2(vertices[i].y - centroid3d.y,
1847940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                vertices[i].x - centroid3d.x);
1857940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        if (i > 0 && polyThetas[i] < polyThetas[i - 1]) {
1867940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            minimalPolyThetaIndex = i;
1877940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        }
1887940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    }
1897940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui
1907940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    int polyThetaIndex = minimalPolyThetaIndex;
1917940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    float polyTheta = polyThetas[minimalPolyThetaIndex];
1927940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    int uniformThetaIndex = 0;
1937940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    float uniformTheta = - M_PI;
19455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    for (int i = 0; i < rays; i++) {
1957940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        // Compare both thetas and pick the smaller one and move on.
1967940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        bool hasThetaCollision = abs(polyTheta - uniformTheta) < MINIMAL_DELTA_THETA;
1977940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        if (polyTheta < uniformTheta || hasThetaCollision) {
1987940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            if (hasThetaCollision) {
1997940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                // Shift the uniformTheta to middle way between current polyTheta
2007940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                // and next uniform theta. The next uniform theta can wrap around
2017940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                // to exactly PI safely here.
2027940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                // Note that neither polyTheta nor uniformTheta can be FLT_MAX
2037940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                // due to the hasThetaCollision is true.
2047940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                uniformTheta = (polyTheta +  deltaAngle * (uniformThetaIndex + 1) - M_PI) / 2;
2057940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui#if DEBUG_SHADOW
2067940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                ALOGD("Shifted uniformTheta to %f", uniformTheta);
2077940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui#endif
2087940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            }
2097940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            rayThetas[i] = polyTheta;
2107940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            polyThetaIndex = (polyThetaIndex + 1) % vertexCount;
2117940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            if (polyThetaIndex != minimalPolyThetaIndex) {
2127940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                polyTheta = polyThetas[polyThetaIndex];
2137940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            } else {
2147940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                // out of poly points.
2157940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                polyTheta = FLT_MAX;
2167940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            }
2177940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        } else {
2187940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            rayThetas[i] = uniformTheta;
2197940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            uniformThetaIndex++;
2207940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            if (uniformThetaIndex < uniformRayCount) {
2217940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                uniformTheta = deltaAngle * uniformThetaIndex - M_PI;
2227940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            } else {
2237940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                // out of uniform points.
2247940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui                uniformTheta = FLT_MAX;
2257940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui            }
2267940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        }
2277940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    }
2287940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui
2297940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui    for (int i = 0; i < rays; i++) {
2307940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui#if DEBUG_SHADOW
2317940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        ALOGD("No. %d : %f", i, rayThetas[i] * 180 / M_PI);
2327940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui#endif
2337940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        // TODO: Fix the intersection precision problem and remvoe the delta added
2347940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        // here.
2357940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        dir[i].x = sinf(rayThetas[i] + MINIMAL_DELTA_THETA);
2367940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui        dir[i].y = cosf(rayThetas[i] + MINIMAL_DELTA_THETA);
23755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    }
23855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui}
23955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
24055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui/**
24155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * Calculate the intersection of a ray hitting the polygon.
24255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
24355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param vertices The shadow caster's polygon, which is represented in a
24455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *                 Vector3 array.
24555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param vertexCount The length of caster's polygon in terms of number of vertices.
24655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param start The starting point of the ray.
24755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param dir The direction vector of the ray.
24855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
24955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param outEdgeIndex Return the index of the segment (or index of the starting
25055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *                     vertex) that ray intersect with.
25155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param outEdgeFraction Return the fraction offset from the segment starting
25255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *                        index.
25355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param outRayDist Return the ray distance from centroid to the intersection.
25455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui */
25555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghuivoid AmbientShadow::calculateIntersection(const Vector3* vertices, int vertexCount,
25663d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui        const Vector3& start, const Vector2& dir, int& outEdgeIndex,
25755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float& outEdgeFraction, float& outRayDist) {
25855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    float startX = start.x;
25955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    float startY = start.y;
26055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    float dirX = dir.x;
26155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    float dirY = dir.y;
26255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // Start the search from the last edge from poly[len-1] to poly[0].
26355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    int p1 = vertexCount - 1;
26455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
26555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    for (int p2 = 0; p2 < vertexCount; p2++) {
26655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float p1x = vertices[p1].x;
26755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float p1y = vertices[p1].y;
26855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float p2x = vertices[p2].x;
26955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float p2y = vertices[p2].y;
27055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
27155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        // The math here is derived from:
27255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        // f(t, v) = p1x * (1 - t) + p2x * t - (startX + dirX * v) = 0;
27355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        // g(t, v) = p1y * (1 - t) + p2y * t - (startY + dirY * v) = 0;
27455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float div = (dirX * (p1y - p2y) + dirY * p2x - dirY * p1x);
27555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        if (div != 0) {
27655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui            float t = (dirX * (p1y - startY) + dirY * startX - dirY * p1x) / (div);
27755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui            if (t > 0 && t <= 1) {
27855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                float t2 = (p1x * (startY - p2y)
27955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                            + p2x * (p1y - startY)
28055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                            + startX * (p2y - p1y)) / div;
28155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                if (t2 > 0) {
28255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                    outEdgeIndex = p1;
28355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                    outRayDist = t2;
28455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                    outEdgeFraction = t;
28555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                    return;
28655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                }
28755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui            }
28855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        }
28955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        p1 = p2;
29055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    }
29155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    return;
29255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui};
29355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
29455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui/**
29555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * Calculate the normal at the intersection point between a ray and the polygon.
29655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
29755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param rays The total number of rays.
29855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param currentRayIndex The index of the ray which the normal is based on.
29955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param dir The array of the all the rays directions.
30055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param rayDist The pre-computed ray distances array.
30155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
30255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param normal Return the normal.
30355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui */
30455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghuivoid AmbientShadow::calculateNormal(int rays, int currentRayIndex,
30555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        const Vector2* dir, const float* rayDist, Vector2& normal) {
30655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    int preIndex = (currentRayIndex - 1 + rays) % rays;
30755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    int postIndex = (currentRayIndex + 1) % rays;
30855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    Vector2 p1 = dir[preIndex] * rayDist[preIndex];
30955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    Vector2 p2 = dir[postIndex] * rayDist[postIndex];
31055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
31155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // Now the V (deltaX, deltaY) is the vector going CW around the poly.
31255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    Vector2 delta = p2 - p1;
31355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    if (delta.length() != 0) {
31455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        delta.normalize();
31555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        // Calculate the normal , which is CCW 90 rotate to the V.
31655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        // 90 degrees CCW about z-axis: (x, y, z) -> (-y, x, z)
31755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        normal.x = -delta.y;
31855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        normal.y = delta.x;
31955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    }
32055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui}
32155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
32255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui}; // namespace uirenderer
32355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui}; // namespace android
324