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" |
| 24 | #include "Vertex.h" |
| 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
| 29 | /** |
| 30 | * Calculate the shadows as a triangle strips while alpha value as the |
| 31 | * shadow values. |
| 32 | * |
| 33 | * @param vertices The shadow caster's polygon, which is represented in a Vector3 |
| 34 | * array. |
| 35 | * @param vertexCount The length of caster's polygon in terms of number of |
| 36 | * vertices. |
| 37 | * @param rays The number of rays shooting out from the centroid. |
| 38 | * @param layers The number of rings outside the polygon. |
| 39 | * @param strength The darkness of the shadow, the higher, the darker. |
| 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 | */ |
| 47 | void AmbientShadow::createAmbientShadow(const Vector3* vertices, int vertexCount, |
| 48 | int rays, int layers, float strength, float heightFactor, float geomFactor, |
| 49 | VertexBuffer& shadowVertexBuffer) { |
| 50 | |
| 51 | // Validate the inputs. |
| 52 | if (strength <= 0 || heightFactor <= 0 || layers <= 0 || rays <= 0 |
| 53 | || geomFactor <= 0) { |
| 54 | #if DEBUG_SHADOW |
| 55 | ALOGE("Invalid input for createAmbientShadow(), early return!"); |
| 56 | #endif |
| 57 | return; |
| 58 | } |
| 59 | int rings = layers + 1; |
| 60 | int size = rays * rings; |
| 61 | Vector2 centroid; |
| 62 | calculatePolygonCentroid(vertices, vertexCount, centroid); |
| 63 | |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 64 | Vector<Vector2> dir; // TODO: use C++11 unique_ptr |
| 65 | dir.setCapacity(rays); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 66 | float rayDist[rays]; |
| 67 | float rayHeight[rays]; |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 68 | calculateRayDirections(rays, dir.editArray()); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 69 | |
| 70 | // Calculate the length and height of the points along the edge. |
| 71 | // |
| 72 | // The math here is: |
| 73 | // Intersect each ray (starting from the centroid) with the polygon. |
| 74 | for (int i = 0; i < rays; i++) { |
| 75 | int edgeIndex; |
| 76 | float edgeFraction; |
| 77 | float rayDistance; |
| 78 | calculateIntersection(vertices, vertexCount, centroid, dir[i], edgeIndex, |
| 79 | edgeFraction, rayDistance); |
| 80 | rayDist[i] = rayDistance; |
| 81 | if (edgeIndex < 0 || edgeIndex >= vertexCount) { |
| 82 | #if DEBUG_SHADOW |
| 83 | ALOGE("Invalid edgeIndex!"); |
| 84 | #endif |
| 85 | edgeIndex = 0; |
| 86 | } |
| 87 | float h1 = vertices[edgeIndex].z; |
| 88 | float h2 = vertices[((edgeIndex + 1) % vertexCount)].z; |
| 89 | rayHeight[i] = h1 + edgeFraction * (h2 - h1); |
| 90 | } |
| 91 | |
| 92 | // The output buffer length basically is roughly rays * layers, but since we |
| 93 | // need triangle strips, so we need to duplicate vertices to accomplish that. |
| 94 | const int shadowVertexCount = (2 + rays + ((layers) * 2 * (rays + 1))); |
| 95 | AlphaVertex* shadowVertices = shadowVertexBuffer.alloc<AlphaVertex>(shadowVertexCount); |
| 96 | |
| 97 | // Calculate the vertex of the shadows. |
| 98 | // |
| 99 | // The math here is: |
| 100 | // Along the edges of the polygon, for each intersection point P (generated above), |
| 101 | // calculate the normal N, which should be perpendicular to the edge of the |
| 102 | // polygon (represented by the neighbor intersection points) . |
| 103 | // Shadow's vertices will be generated as : P + N * scale. |
| 104 | int currentIndex = 0; |
| 105 | for (int r = 0; r < layers; r++) { |
| 106 | int firstInLayer = currentIndex; |
| 107 | for (int i = 0; i < rays; i++) { |
| 108 | |
| 109 | Vector2 normal(1.0f, 0.0f); |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 110 | calculateNormal(rays, i, dir.array(), rayDist, normal); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 111 | |
| 112 | float opacity = strength * (0.5f) / (1 + rayHeight[i] / heightFactor); |
| 113 | |
| 114 | // The vertex should be start from rayDist[i] then scale the |
| 115 | // normalizeNormal! |
| 116 | Vector2 intersection = dir[i] * rayDist[i] + centroid; |
| 117 | |
| 118 | // Use 2 rings' vertices to complete one layer's strip |
| 119 | for (int j = r; j < (r + 2); j++) { |
| 120 | float jf = j / (float)(rings - 1); |
| 121 | |
| 122 | float expansionDist = rayHeight[i] / heightFactor * geomFactor * jf; |
| 123 | AlphaVertex::set(&shadowVertices[currentIndex], |
| 124 | intersection.x + normal.x * expansionDist, |
| 125 | intersection.y + normal.y * expansionDist, |
| 126 | (1 - jf) * opacity); |
| 127 | currentIndex++; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // From one layer to the next, we need to duplicate the vertex to |
| 132 | // continue as a single strip. |
| 133 | shadowVertices[currentIndex] = shadowVertices[firstInLayer]; |
| 134 | currentIndex++; |
| 135 | shadowVertices[currentIndex] = shadowVertices[firstInLayer + 1]; |
| 136 | currentIndex++; |
| 137 | } |
| 138 | |
| 139 | // After all rings are done, we need to jump into the polygon. |
| 140 | // In order to keep everything in a strip, we need to duplicate the last one |
| 141 | // of the rings and the first one inside the polygon. |
| 142 | int lastInRings = currentIndex - 1; |
| 143 | shadowVertices[currentIndex] = shadowVertices[lastInRings]; |
| 144 | currentIndex++; |
| 145 | |
| 146 | // We skip one and fill it back after we finish the internal triangles. |
| 147 | currentIndex++; |
| 148 | int firstInternal = currentIndex; |
| 149 | |
| 150 | // Combine the internal area of the polygon into a triangle strip, too. |
| 151 | // The basic idea is zig zag between the intersection points. |
| 152 | // 0 -> (n - 1) -> 1 -> (n - 2) ... |
| 153 | for (int k = 0; k < rays; k++) { |
| 154 | int i = k / 2; |
| 155 | if ((k & 1) == 1) { // traverse the inside in a zig zag pattern for strips |
| 156 | i = rays - i - 1; |
| 157 | } |
| 158 | float cast = rayDist[i] * (1 + rayHeight[i] / heightFactor); |
| 159 | float opacity = strength * (0.5f) / (1 + rayHeight[i] / heightFactor); |
| 160 | float t = rayDist[i]; |
| 161 | |
| 162 | AlphaVertex::set(&shadowVertices[currentIndex], dir[i].x * t + centroid.x, |
| 163 | dir[i].y * t + centroid.y, opacity); |
| 164 | currentIndex++; |
| 165 | } |
| 166 | |
| 167 | currentIndex = firstInternal - 1; |
| 168 | shadowVertices[currentIndex] = shadowVertices[firstInternal]; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Calculate the centroid of a given polygon. |
| 173 | * |
| 174 | * @param vertices The shadow caster's polygon, which is represented in a |
| 175 | * straight Vector3 array. |
| 176 | * @param vertexCount The length of caster's polygon in terms of number of vertices. |
| 177 | * |
| 178 | * @param centroid Return the centroid of the polygon. |
| 179 | */ |
| 180 | void AmbientShadow::calculatePolygonCentroid(const Vector3* vertices, int vertexCount, |
| 181 | Vector2& centroid) { |
| 182 | float sumx = 0; |
| 183 | float sumy = 0; |
| 184 | int p1 = vertexCount - 1; |
| 185 | float area = 0; |
| 186 | for (int p2 = 0; p2 < vertexCount; p2++) { |
| 187 | float x1 = vertices[p1].x; |
| 188 | float y1 = vertices[p1].y; |
| 189 | float x2 = vertices[p2].x; |
| 190 | float y2 = vertices[p2].y; |
| 191 | float a = (x1 * y2 - x2 * y1); |
| 192 | sumx += (x1 + x2) * a; |
| 193 | sumy += (y1 + y2) * a; |
| 194 | area += a; |
| 195 | p1 = p2; |
| 196 | } |
| 197 | |
| 198 | if (area == 0) { |
| 199 | #if DEBUG_SHADOW |
| 200 | ALOGE("Area is 0!"); |
| 201 | #endif |
| 202 | centroid.x = vertices[0].x; |
| 203 | centroid.y = vertices[0].y; |
| 204 | } else { |
| 205 | centroid.x = sumx / (3 * area); |
| 206 | centroid.y = sumy / (3 * area); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Generate an array of rays' direction vectors. |
| 212 | * |
| 213 | * @param rays The number of rays shooting out from the centroid. |
| 214 | * @param dir Return the array of ray vectors. |
| 215 | */ |
| 216 | void AmbientShadow::calculateRayDirections(int rays, Vector2* dir) { |
| 217 | float deltaAngle = 2 * M_PI / rays; |
| 218 | |
| 219 | for (int i = 0; i < rays; i++) { |
| 220 | dir[i].x = sinf(deltaAngle * i); |
| 221 | dir[i].y = cosf(deltaAngle * i); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Calculate the intersection of a ray hitting the polygon. |
| 227 | * |
| 228 | * @param vertices The shadow caster's polygon, which is represented in a |
| 229 | * Vector3 array. |
| 230 | * @param vertexCount The length of caster's polygon in terms of number of vertices. |
| 231 | * @param start The starting point of the ray. |
| 232 | * @param dir The direction vector of the ray. |
| 233 | * |
| 234 | * @param outEdgeIndex Return the index of the segment (or index of the starting |
| 235 | * vertex) that ray intersect with. |
| 236 | * @param outEdgeFraction Return the fraction offset from the segment starting |
| 237 | * index. |
| 238 | * @param outRayDist Return the ray distance from centroid to the intersection. |
| 239 | */ |
| 240 | void AmbientShadow::calculateIntersection(const Vector3* vertices, int vertexCount, |
| 241 | const Vector2& start, const Vector2& dir, int& outEdgeIndex, |
| 242 | float& outEdgeFraction, float& outRayDist) { |
| 243 | float startX = start.x; |
| 244 | float startY = start.y; |
| 245 | float dirX = dir.x; |
| 246 | float dirY = dir.y; |
| 247 | // Start the search from the last edge from poly[len-1] to poly[0]. |
| 248 | int p1 = vertexCount - 1; |
| 249 | |
| 250 | for (int p2 = 0; p2 < vertexCount; p2++) { |
| 251 | float p1x = vertices[p1].x; |
| 252 | float p1y = vertices[p1].y; |
| 253 | float p2x = vertices[p2].x; |
| 254 | float p2y = vertices[p2].y; |
| 255 | |
| 256 | // The math here is derived from: |
| 257 | // f(t, v) = p1x * (1 - t) + p2x * t - (startX + dirX * v) = 0; |
| 258 | // g(t, v) = p1y * (1 - t) + p2y * t - (startY + dirY * v) = 0; |
| 259 | float div = (dirX * (p1y - p2y) + dirY * p2x - dirY * p1x); |
| 260 | if (div != 0) { |
| 261 | float t = (dirX * (p1y - startY) + dirY * startX - dirY * p1x) / (div); |
| 262 | if (t > 0 && t <= 1) { |
| 263 | float t2 = (p1x * (startY - p2y) |
| 264 | + p2x * (p1y - startY) |
| 265 | + startX * (p2y - p1y)) / div; |
| 266 | if (t2 > 0) { |
| 267 | outEdgeIndex = p1; |
| 268 | outRayDist = t2; |
| 269 | outEdgeFraction = t; |
| 270 | return; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | p1 = p2; |
| 275 | } |
| 276 | return; |
| 277 | }; |
| 278 | |
| 279 | /** |
| 280 | * Calculate the normal at the intersection point between a ray and the polygon. |
| 281 | * |
| 282 | * @param rays The total number of rays. |
| 283 | * @param currentRayIndex The index of the ray which the normal is based on. |
| 284 | * @param dir The array of the all the rays directions. |
| 285 | * @param rayDist The pre-computed ray distances array. |
| 286 | * |
| 287 | * @param normal Return the normal. |
| 288 | */ |
| 289 | void AmbientShadow::calculateNormal(int rays, int currentRayIndex, |
| 290 | const Vector2* dir, const float* rayDist, Vector2& normal) { |
| 291 | int preIndex = (currentRayIndex - 1 + rays) % rays; |
| 292 | int postIndex = (currentRayIndex + 1) % rays; |
| 293 | Vector2 p1 = dir[preIndex] * rayDist[preIndex]; |
| 294 | Vector2 p2 = dir[postIndex] * rayDist[postIndex]; |
| 295 | |
| 296 | // Now the V (deltaX, deltaY) is the vector going CW around the poly. |
| 297 | Vector2 delta = p2 - p1; |
| 298 | if (delta.length() != 0) { |
| 299 | delta.normalize(); |
| 300 | // Calculate the normal , which is CCW 90 rotate to the V. |
| 301 | // 90 degrees CCW about z-axis: (x, y, z) -> (-y, x, z) |
| 302 | normal.x = -delta.y; |
| 303 | normal.y = delta.x; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | }; // namespace uirenderer |
| 308 | }; // namespace android |