AmbientShadow.cpp revision 50ecf849cb7ccc3482517b74d2214b347927791e
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];
65564acf7c9bff822f608cda0d5df0a64a9f9aaefdChris Craik    calculateRayDirections(rays, 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.
15255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param dir Return the array of ray vectors.
15355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui */
15455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghuivoid AmbientShadow::calculateRayDirections(int rays, Vector2* dir) {
15555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    float deltaAngle = 2 * M_PI / rays;
15655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
15755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    for (int i = 0; i < rays; i++) {
15855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        dir[i].x = sinf(deltaAngle * i);
15955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        dir[i].y = cosf(deltaAngle * i);
16055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    }
16155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui}
16255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
16355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui/**
16455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * Calculate the intersection of a ray hitting the polygon.
16555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
16655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param vertices The shadow caster's polygon, which is represented in a
16755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *                 Vector3 array.
16855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param vertexCount The length of caster's polygon in terms of number of vertices.
16955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param start The starting point of the ray.
17055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param dir The direction vector of the ray.
17155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
17255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param outEdgeIndex Return the index of the segment (or index of the starting
17355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *                     vertex) that ray intersect with.
17455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param outEdgeFraction Return the fraction offset from the segment starting
17555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *                        index.
17655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param outRayDist Return the ray distance from centroid to the intersection.
17755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui */
17855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghuivoid AmbientShadow::calculateIntersection(const Vector3* vertices, int vertexCount,
17963d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui        const Vector3& start, const Vector2& dir, int& outEdgeIndex,
18055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float& outEdgeFraction, float& outRayDist) {
18155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    float startX = start.x;
18255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    float startY = start.y;
18355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    float dirX = dir.x;
18455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    float dirY = dir.y;
18555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // Start the search from the last edge from poly[len-1] to poly[0].
18655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    int p1 = vertexCount - 1;
18755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
18855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    for (int p2 = 0; p2 < vertexCount; p2++) {
18955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float p1x = vertices[p1].x;
19055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float p1y = vertices[p1].y;
19155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float p2x = vertices[p2].x;
19255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float p2y = vertices[p2].y;
19355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
19455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        // The math here is derived from:
19555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        // f(t, v) = p1x * (1 - t) + p2x * t - (startX + dirX * v) = 0;
19655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        // g(t, v) = p1y * (1 - t) + p2y * t - (startY + dirY * v) = 0;
19755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        float div = (dirX * (p1y - p2y) + dirY * p2x - dirY * p1x);
19855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        if (div != 0) {
19955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui            float t = (dirX * (p1y - startY) + dirY * startX - dirY * p1x) / (div);
20055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui            if (t > 0 && t <= 1) {
20155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                float t2 = (p1x * (startY - p2y)
20255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                            + p2x * (p1y - startY)
20355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                            + startX * (p2y - p1y)) / div;
20455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                if (t2 > 0) {
20555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                    outEdgeIndex = p1;
20655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                    outRayDist = t2;
20755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                    outEdgeFraction = t;
20855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                    return;
20955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui                }
21055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui            }
21155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        }
21255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        p1 = p2;
21355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    }
21455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    return;
21555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui};
21655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
21755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui/**
21855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * Calculate the normal at the intersection point between a ray and the polygon.
21955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
22055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param rays The total number of rays.
22155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param currentRayIndex The index of the ray which the normal is based on.
22255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param dir The array of the all the rays directions.
22355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param rayDist The pre-computed ray distances array.
22455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
22555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * @param normal Return the normal.
22655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui */
22755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghuivoid AmbientShadow::calculateNormal(int rays, int currentRayIndex,
22855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        const Vector2* dir, const float* rayDist, Vector2& normal) {
22955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    int preIndex = (currentRayIndex - 1 + rays) % rays;
23055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    int postIndex = (currentRayIndex + 1) % rays;
23155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    Vector2 p1 = dir[preIndex] * rayDist[preIndex];
23255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    Vector2 p2 = dir[postIndex] * rayDist[postIndex];
23355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
23455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    // Now the V (deltaX, deltaY) is the vector going CW around the poly.
23555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    Vector2 delta = p2 - p1;
23655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    if (delta.length() != 0) {
23755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        delta.normalize();
23855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        // Calculate the normal , which is CCW 90 rotate to the V.
23955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        // 90 degrees CCW about z-axis: (x, y, z) -> (-y, x, z)
24055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        normal.x = -delta.y;
24155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui        normal.y = delta.x;
24255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui    }
24355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui}
24455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
24555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui}; // namespace uirenderer
24655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui}; // namespace android
247