ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
| 19 | #include <math.h> |
| 20 | #include <utils/Log.h> |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 21 | #include <utils/Vector.h> |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 22 | |
| 23 | #include "AmbientShadow.h" |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 24 | #include "ShadowTessellator.h" |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 25 | #include "Vertex.h" |
| 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | /** |
| 31 | * Calculate the shadows as a triangle strips while alpha value as the |
| 32 | * shadow values. |
| 33 | * |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 34 | * @param isCasterOpaque Whether the caster is opaque. |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 35 | * @param vertices The shadow caster's polygon, which is represented in a Vector3 |
| 36 | * array. |
| 37 | * @param vertexCount The length of caster's polygon in terms of number of |
| 38 | * vertices. |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 39 | * @param centroid3d The centroid of the shadow caster. |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 40 | * @param heightFactor The factor showing the higher the object, the lighter the |
| 41 | * shadow. |
| 42 | * @param geomFactor The factor scaling the geometry expansion along the normal. |
| 43 | * |
| 44 | * @param shadowVertexBuffer Return an floating point array of (x, y, a) |
| 45 | * triangle strips mode. |
| 46 | */ |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 47 | void AmbientShadow::createAmbientShadow(bool isCasterOpaque, |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 48 | const Vector3* vertices, int vertexCount, const Vector3& centroid3d, |
| 49 | float heightFactor, float geomFactor, VertexBuffer& shadowVertexBuffer) { |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 50 | const int rays = SHADOW_RAY_COUNT; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 51 | // Validate the inputs. |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 52 | if (vertexCount < 3 || heightFactor <= 0 || rays <= 0 |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 53 | || geomFactor <= 0) { |
| 54 | #if DEBUG_SHADOW |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 55 | ALOGW("Invalid input for createAmbientShadow(), early return!"); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 56 | #endif |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 57 | return; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 58 | } |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 59 | |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 60 | Vector<Vector2> dir; // TODO: use C++11 unique_ptr |
| 61 | dir.setCapacity(rays); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 62 | float rayDist[rays]; |
| 63 | float rayHeight[rays]; |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 64 | calculateRayDirections(rays, vertices, vertexCount, centroid3d, dir.editArray()); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 65 | |
| 66 | // Calculate the length and height of the points along the edge. |
| 67 | // |
| 68 | // The math here is: |
| 69 | // Intersect each ray (starting from the centroid) with the polygon. |
| 70 | for (int i = 0; i < rays; i++) { |
| 71 | int edgeIndex; |
| 72 | float edgeFraction; |
| 73 | float rayDistance; |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 74 | calculateIntersection(vertices, vertexCount, centroid3d, dir[i], edgeIndex, |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 75 | edgeFraction, rayDistance); |
| 76 | rayDist[i] = rayDistance; |
| 77 | if (edgeIndex < 0 || edgeIndex >= vertexCount) { |
| 78 | #if DEBUG_SHADOW |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 79 | ALOGW("Invalid edgeIndex!"); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 80 | #endif |
| 81 | edgeIndex = 0; |
| 82 | } |
| 83 | float h1 = vertices[edgeIndex].z; |
| 84 | float h2 = vertices[((edgeIndex + 1) % vertexCount)].z; |
| 85 | rayHeight[i] = h1 + edgeFraction * (h2 - h1); |
| 86 | } |
| 87 | |
| 88 | // The output buffer length basically is roughly rays * layers, but since we |
| 89 | // need triangle strips, so we need to duplicate vertices to accomplish that. |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 90 | AlphaVertex* shadowVertices = |
| 91 | shadowVertexBuffer.alloc<AlphaVertex>(SHADOW_VERTEX_COUNT); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 92 | |
| 93 | // Calculate the vertex of the shadows. |
| 94 | // |
| 95 | // The math here is: |
| 96 | // Along the edges of the polygon, for each intersection point P (generated above), |
| 97 | // calculate the normal N, which should be perpendicular to the edge of the |
| 98 | // polygon (represented by the neighbor intersection points) . |
| 99 | // Shadow's vertices will be generated as : P + N * scale. |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 100 | const Vector2 centroid2d = {centroid3d.x, centroid3d.y}; |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 101 | for (int rayIndex = 0; rayIndex < rays; rayIndex++) { |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 102 | Vector2 normal = {1.0f, 0.0f}; |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 103 | calculateNormal(rays, rayIndex, dir.array(), rayDist, normal); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 104 | |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 105 | // The vertex should be start from rayDist[i] then scale the |
| 106 | // normalizeNormal! |
| 107 | Vector2 intersection = dir[rayIndex] * rayDist[rayIndex] + |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 108 | centroid2d; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 109 | |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 110 | // outer ring of points, expanded based upon height of each ray intersection |
| 111 | float expansionDist = rayHeight[rayIndex] * heightFactor * |
| 112 | geomFactor; |
| 113 | AlphaVertex::set(&shadowVertices[rayIndex], |
| 114 | intersection.x + normal.x * expansionDist, |
| 115 | intersection.y + normal.y * expansionDist, |
| 116 | 0.0f); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 117 | |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 118 | // inner ring of points |
| 119 | float opacity = 1.0 / (1 + rayHeight[rayIndex] * heightFactor); |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 120 | // NOTE: Shadow alpha values are transformed when stored in alphavertices, |
| 121 | // so that they can be consumed directly by gFS_Main_ApplyVertexAlphaShadowInterp |
| 122 | float transformedOpacity = acos(1.0f - 2.0f * opacity); |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 123 | AlphaVertex::set(&shadowVertices[rays + rayIndex], |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 124 | intersection.x, |
| 125 | intersection.y, |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 126 | transformedOpacity); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 127 | } |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 128 | |
Chris Craik | 9a89bc6 | 2014-07-23 17:21:25 -0700 | [diff] [blame] | 129 | if (isCasterOpaque) { |
| 130 | // skip inner ring, calc bounds over filled portion of buffer |
| 131 | shadowVertexBuffer.computeBounds<AlphaVertex>(2 * rays); |
| 132 | shadowVertexBuffer.setMode(VertexBuffer::kOnePolyRingShadow); |
| 133 | } else { |
| 134 | // If caster isn't opaque, we need to to fill the umbra by storing the umbra's |
| 135 | // centroid in the innermost ring of vertices. |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 136 | float centroidAlpha = 1.0 / (1 + centroid3d.z * heightFactor); |
| 137 | AlphaVertex centroidXYA; |
| 138 | AlphaVertex::set(¢roidXYA, centroid2d.x, centroid2d.y, centroidAlpha); |
| 139 | for (int rayIndex = 0; rayIndex < rays; rayIndex++) { |
| 140 | shadowVertices[2 * rays + rayIndex] = centroidXYA; |
| 141 | } |
Chris Craik | 9a89bc6 | 2014-07-23 17:21:25 -0700 | [diff] [blame] | 142 | // calc bounds over entire buffer |
| 143 | shadowVertexBuffer.computeBounds<AlphaVertex>(); |
| 144 | shadowVertexBuffer.setMode(VertexBuffer::kTwoPolyRingShadow); |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 145 | } |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 146 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 147 | #if DEBUG_SHADOW |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 148 | for (int i = 0; i < SHADOW_VERTEX_COUNT; i++) { |
| 149 | ALOGD("ambient shadow value: i %d, (x:%f, y:%f, a:%f)", i, shadowVertices[i].x, |
| 150 | shadowVertices[i].y, shadowVertices[i].alpha); |
| 151 | } |
| 152 | #endif |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Generate an array of rays' direction vectors. |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 157 | * To make sure the vertices generated are clockwise, the directions are from PI |
| 158 | * to -PI. |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 159 | * |
| 160 | * @param rays The number of rays shooting out from the centroid. |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 161 | * @param vertices Vertices of the polygon. |
| 162 | * @param vertexCount The number of vertices. |
| 163 | * @param centroid3d The centroid of the polygon. |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 164 | * @param dir Return the array of ray vectors. |
| 165 | */ |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 166 | void AmbientShadow::calculateRayDirections(const int rays, const Vector3* vertices, |
| 167 | const int vertexCount, const Vector3& centroid3d, Vector2* dir) { |
| 168 | // If we don't have enough rays, then fall back to the uniform distribution. |
| 169 | if (vertexCount * 2 > rays) { |
| 170 | float deltaAngle = 2 * M_PI / rays; |
| 171 | for (int i = 0; i < rays; i++) { |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 172 | dir[i].x = cosf(M_PI - deltaAngle * i); |
| 173 | dir[i].y = sinf(M_PI - deltaAngle * i); |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 174 | } |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | // If we have enough rays, then we assign each vertices a ray, and distribute |
| 179 | // the rest uniformly. |
| 180 | float rayThetas[rays]; |
| 181 | |
| 182 | const int uniformRayCount = rays - vertexCount; |
| 183 | const float deltaAngle = 2 * M_PI / uniformRayCount; |
| 184 | |
| 185 | // We have to generate all the vertices' theta anyway and we also need to |
| 186 | // find the minimal, so let's precompute it first. |
| 187 | // Since the incoming polygon is clockwise, we can find the dip to identify |
| 188 | // the minimal theta. |
| 189 | float polyThetas[vertexCount]; |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 190 | int maxPolyThetaIndex = 0; |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 191 | for (int i = 0; i < vertexCount; i++) { |
| 192 | polyThetas[i] = atan2(vertices[i].y - centroid3d.y, |
| 193 | vertices[i].x - centroid3d.x); |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 194 | if (i > 0 && polyThetas[i] > polyThetas[i - 1]) { |
| 195 | maxPolyThetaIndex = i; |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 199 | // Both poly's thetas and uniform thetas are in decrease order(clockwise) |
| 200 | // from PI to -PI. |
| 201 | int polyThetaIndex = maxPolyThetaIndex; |
| 202 | float polyTheta = polyThetas[maxPolyThetaIndex]; |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 203 | int uniformThetaIndex = 0; |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 204 | float uniformTheta = M_PI; |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 205 | for (int i = 0; i < rays; i++) { |
| 206 | // Compare both thetas and pick the smaller one and move on. |
| 207 | bool hasThetaCollision = abs(polyTheta - uniformTheta) < MINIMAL_DELTA_THETA; |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 208 | if (polyTheta > uniformTheta || hasThetaCollision) { |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 209 | if (hasThetaCollision) { |
| 210 | // Shift the uniformTheta to middle way between current polyTheta |
| 211 | // and next uniform theta. The next uniform theta can wrap around |
| 212 | // to exactly PI safely here. |
| 213 | // Note that neither polyTheta nor uniformTheta can be FLT_MAX |
| 214 | // due to the hasThetaCollision is true. |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 215 | uniformTheta = (polyTheta + M_PI - deltaAngle * (uniformThetaIndex + 1)) / 2; |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 216 | #if DEBUG_SHADOW |
| 217 | ALOGD("Shifted uniformTheta to %f", uniformTheta); |
| 218 | #endif |
| 219 | } |
| 220 | rayThetas[i] = polyTheta; |
| 221 | polyThetaIndex = (polyThetaIndex + 1) % vertexCount; |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 222 | if (polyThetaIndex != maxPolyThetaIndex) { |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 223 | polyTheta = polyThetas[polyThetaIndex]; |
| 224 | } else { |
| 225 | // out of poly points. |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 226 | polyTheta = - FLT_MAX; |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 227 | } |
| 228 | } else { |
| 229 | rayThetas[i] = uniformTheta; |
| 230 | uniformThetaIndex++; |
| 231 | if (uniformThetaIndex < uniformRayCount) { |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 232 | uniformTheta = M_PI - deltaAngle * uniformThetaIndex; |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 233 | } else { |
| 234 | // out of uniform points. |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 235 | uniformTheta = - FLT_MAX; |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 239 | |
| 240 | for (int i = 0; i < rays; i++) { |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 241 | #if DEBUG_SHADOW |
| 242 | ALOGD("No. %d : %f", i, rayThetas[i] * 180 / M_PI); |
| 243 | #endif |
| 244 | // TODO: Fix the intersection precision problem and remvoe the delta added |
| 245 | // here. |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 246 | dir[i].x = cosf(rayThetas[i] + MINIMAL_DELTA_THETA); |
| 247 | dir[i].y = sinf(rayThetas[i] + MINIMAL_DELTA_THETA); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Calculate the intersection of a ray hitting the polygon. |
| 253 | * |
| 254 | * @param vertices The shadow caster's polygon, which is represented in a |
| 255 | * Vector3 array. |
| 256 | * @param vertexCount The length of caster's polygon in terms of number of vertices. |
| 257 | * @param start The starting point of the ray. |
| 258 | * @param dir The direction vector of the ray. |
| 259 | * |
| 260 | * @param outEdgeIndex Return the index of the segment (or index of the starting |
| 261 | * vertex) that ray intersect with. |
| 262 | * @param outEdgeFraction Return the fraction offset from the segment starting |
| 263 | * index. |
| 264 | * @param outRayDist Return the ray distance from centroid to the intersection. |
| 265 | */ |
| 266 | void AmbientShadow::calculateIntersection(const Vector3* vertices, int vertexCount, |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 267 | const Vector3& start, const Vector2& dir, int& outEdgeIndex, |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 268 | float& outEdgeFraction, float& outRayDist) { |
| 269 | float startX = start.x; |
| 270 | float startY = start.y; |
| 271 | float dirX = dir.x; |
| 272 | float dirY = dir.y; |
| 273 | // Start the search from the last edge from poly[len-1] to poly[0]. |
| 274 | int p1 = vertexCount - 1; |
| 275 | |
| 276 | for (int p2 = 0; p2 < vertexCount; p2++) { |
| 277 | float p1x = vertices[p1].x; |
| 278 | float p1y = vertices[p1].y; |
| 279 | float p2x = vertices[p2].x; |
| 280 | float p2y = vertices[p2].y; |
| 281 | |
| 282 | // The math here is derived from: |
| 283 | // f(t, v) = p1x * (1 - t) + p2x * t - (startX + dirX * v) = 0; |
| 284 | // g(t, v) = p1y * (1 - t) + p2y * t - (startY + dirY * v) = 0; |
| 285 | float div = (dirX * (p1y - p2y) + dirY * p2x - dirY * p1x); |
| 286 | if (div != 0) { |
| 287 | float t = (dirX * (p1y - startY) + dirY * startX - dirY * p1x) / (div); |
| 288 | if (t > 0 && t <= 1) { |
| 289 | float t2 = (p1x * (startY - p2y) |
| 290 | + p2x * (p1y - startY) |
| 291 | + startX * (p2y - p1y)) / div; |
| 292 | if (t2 > 0) { |
| 293 | outEdgeIndex = p1; |
| 294 | outRayDist = t2; |
| 295 | outEdgeFraction = t; |
| 296 | return; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | p1 = p2; |
| 301 | } |
| 302 | return; |
| 303 | }; |
| 304 | |
| 305 | /** |
| 306 | * Calculate the normal at the intersection point between a ray and the polygon. |
| 307 | * |
| 308 | * @param rays The total number of rays. |
| 309 | * @param currentRayIndex The index of the ray which the normal is based on. |
| 310 | * @param dir The array of the all the rays directions. |
| 311 | * @param rayDist The pre-computed ray distances array. |
| 312 | * |
| 313 | * @param normal Return the normal. |
| 314 | */ |
| 315 | void AmbientShadow::calculateNormal(int rays, int currentRayIndex, |
| 316 | const Vector2* dir, const float* rayDist, Vector2& normal) { |
| 317 | int preIndex = (currentRayIndex - 1 + rays) % rays; |
| 318 | int postIndex = (currentRayIndex + 1) % rays; |
| 319 | Vector2 p1 = dir[preIndex] * rayDist[preIndex]; |
| 320 | Vector2 p2 = dir[postIndex] * rayDist[postIndex]; |
| 321 | |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 322 | // Now the rays are going CW around the poly. |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 323 | Vector2 delta = p2 - p1; |
| 324 | if (delta.length() != 0) { |
| 325 | delta.normalize(); |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 326 | // Calculate the normal , which is CCW 90 rotate to the delta. |
| 327 | normal.x = - delta.y; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 328 | normal.y = delta.x; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | }; // namespace uirenderer |
| 333 | }; // namespace android |