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 | */ |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 47 | VertexBufferMode AmbientShadow::createAmbientShadow(bool isCasterOpaque, |
| 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 | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 51 | VertexBufferMode mode = kVertexBufferMode_OnePolyRingShadow; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 52 | // Validate the inputs. |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 53 | if (vertexCount < 3 || heightFactor <= 0 || rays <= 0 |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 54 | || geomFactor <= 0) { |
| 55 | #if DEBUG_SHADOW |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 56 | ALOGW("Invalid input for createAmbientShadow(), early return!"); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 57 | #endif |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 58 | return mode; // vertex buffer is empty, so any mode doesn't matter. |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 59 | } |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 60 | |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 61 | Vector<Vector2> dir; // TODO: use C++11 unique_ptr |
| 62 | dir.setCapacity(rays); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 63 | float rayDist[rays]; |
| 64 | float rayHeight[rays]; |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 65 | calculateRayDirections(rays, dir.editArray()); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 66 | |
| 67 | // Calculate the length and height of the points along the edge. |
| 68 | // |
| 69 | // The math here is: |
| 70 | // Intersect each ray (starting from the centroid) with the polygon. |
| 71 | for (int i = 0; i < rays; i++) { |
| 72 | int edgeIndex; |
| 73 | float edgeFraction; |
| 74 | float rayDistance; |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 75 | calculateIntersection(vertices, vertexCount, centroid3d, dir[i], edgeIndex, |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 76 | edgeFraction, rayDistance); |
| 77 | rayDist[i] = rayDistance; |
| 78 | if (edgeIndex < 0 || edgeIndex >= vertexCount) { |
| 79 | #if DEBUG_SHADOW |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 80 | ALOGW("Invalid edgeIndex!"); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 81 | #endif |
| 82 | edgeIndex = 0; |
| 83 | } |
| 84 | float h1 = vertices[edgeIndex].z; |
| 85 | float h2 = vertices[((edgeIndex + 1) % vertexCount)].z; |
| 86 | rayHeight[i] = h1 + edgeFraction * (h2 - h1); |
| 87 | } |
| 88 | |
| 89 | // The output buffer length basically is roughly rays * layers, but since we |
| 90 | // need triangle strips, so we need to duplicate vertices to accomplish that. |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 91 | AlphaVertex* shadowVertices = |
| 92 | shadowVertexBuffer.alloc<AlphaVertex>(SHADOW_VERTEX_COUNT); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 93 | |
| 94 | // Calculate the vertex of the shadows. |
| 95 | // |
| 96 | // The math here is: |
| 97 | // Along the edges of the polygon, for each intersection point P (generated above), |
| 98 | // calculate the normal N, which should be perpendicular to the edge of the |
| 99 | // polygon (represented by the neighbor intersection points) . |
| 100 | // Shadow's vertices will be generated as : P + N * scale. |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 101 | const Vector2 centroid2d = Vector2(centroid3d.x, centroid3d.y); |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 102 | for (int rayIndex = 0; rayIndex < rays; rayIndex++) { |
| 103 | Vector2 normal(1.0f, 0.0f); |
| 104 | calculateNormal(rays, rayIndex, dir.array(), rayDist, normal); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 105 | |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 106 | // The vertex should be start from rayDist[i] then scale the |
| 107 | // normalizeNormal! |
| 108 | Vector2 intersection = dir[rayIndex] * rayDist[rayIndex] + |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 109 | centroid2d; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 110 | |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 111 | // outer ring of points, expanded based upon height of each ray intersection |
| 112 | float expansionDist = rayHeight[rayIndex] * heightFactor * |
| 113 | geomFactor; |
| 114 | AlphaVertex::set(&shadowVertices[rayIndex], |
| 115 | intersection.x + normal.x * expansionDist, |
| 116 | intersection.y + normal.y * expansionDist, |
| 117 | 0.0f); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 118 | |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 119 | // inner ring of points |
| 120 | float opacity = 1.0 / (1 + rayHeight[rayIndex] * heightFactor); |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 121 | AlphaVertex::set(&shadowVertices[rays + rayIndex], |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 122 | intersection.x, |
| 123 | intersection.y, |
| 124 | opacity); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 125 | } |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 126 | |
| 127 | // If caster isn't opaque, we need to to fill the umbra by storing the umbra's |
| 128 | // centroid in the innermost ring of vertices. |
| 129 | if (!isCasterOpaque) { |
| 130 | mode = kVertexBufferMode_TwoPolyRingShadow; |
| 131 | float centroidAlpha = 1.0 / (1 + centroid3d.z * heightFactor); |
| 132 | AlphaVertex centroidXYA; |
| 133 | AlphaVertex::set(¢roidXYA, centroid2d.x, centroid2d.y, centroidAlpha); |
| 134 | for (int rayIndex = 0; rayIndex < rays; rayIndex++) { |
| 135 | shadowVertices[2 * rays + rayIndex] = centroidXYA; |
| 136 | } |
| 137 | } |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 138 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 139 | #if DEBUG_SHADOW |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 140 | for (int i = 0; i < SHADOW_VERTEX_COUNT; i++) { |
| 141 | ALOGD("ambient shadow value: i %d, (x:%f, y:%f, a:%f)", i, shadowVertices[i].x, |
| 142 | shadowVertices[i].y, shadowVertices[i].alpha); |
| 143 | } |
| 144 | #endif |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 145 | return mode; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Generate an array of rays' direction vectors. |
| 150 | * |
| 151 | * @param rays The number of rays shooting out from the centroid. |
| 152 | * @param dir Return the array of ray vectors. |
| 153 | */ |
| 154 | void AmbientShadow::calculateRayDirections(int rays, Vector2* dir) { |
| 155 | float deltaAngle = 2 * M_PI / rays; |
| 156 | |
| 157 | for (int i = 0; i < rays; i++) { |
| 158 | dir[i].x = sinf(deltaAngle * i); |
| 159 | dir[i].y = cosf(deltaAngle * i); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Calculate the intersection of a ray hitting the polygon. |
| 165 | * |
| 166 | * @param vertices The shadow caster's polygon, which is represented in a |
| 167 | * Vector3 array. |
| 168 | * @param vertexCount The length of caster's polygon in terms of number of vertices. |
| 169 | * @param start The starting point of the ray. |
| 170 | * @param dir The direction vector of the ray. |
| 171 | * |
| 172 | * @param outEdgeIndex Return the index of the segment (or index of the starting |
| 173 | * vertex) that ray intersect with. |
| 174 | * @param outEdgeFraction Return the fraction offset from the segment starting |
| 175 | * index. |
| 176 | * @param outRayDist Return the ray distance from centroid to the intersection. |
| 177 | */ |
| 178 | void AmbientShadow::calculateIntersection(const Vector3* vertices, int vertexCount, |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 179 | const Vector3& start, const Vector2& dir, int& outEdgeIndex, |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 180 | float& outEdgeFraction, float& outRayDist) { |
| 181 | float startX = start.x; |
| 182 | float startY = start.y; |
| 183 | float dirX = dir.x; |
| 184 | float dirY = dir.y; |
| 185 | // Start the search from the last edge from poly[len-1] to poly[0]. |
| 186 | int p1 = vertexCount - 1; |
| 187 | |
| 188 | for (int p2 = 0; p2 < vertexCount; p2++) { |
| 189 | float p1x = vertices[p1].x; |
| 190 | float p1y = vertices[p1].y; |
| 191 | float p2x = vertices[p2].x; |
| 192 | float p2y = vertices[p2].y; |
| 193 | |
| 194 | // The math here is derived from: |
| 195 | // f(t, v) = p1x * (1 - t) + p2x * t - (startX + dirX * v) = 0; |
| 196 | // g(t, v) = p1y * (1 - t) + p2y * t - (startY + dirY * v) = 0; |
| 197 | float div = (dirX * (p1y - p2y) + dirY * p2x - dirY * p1x); |
| 198 | if (div != 0) { |
| 199 | float t = (dirX * (p1y - startY) + dirY * startX - dirY * p1x) / (div); |
| 200 | if (t > 0 && t <= 1) { |
| 201 | float t2 = (p1x * (startY - p2y) |
| 202 | + p2x * (p1y - startY) |
| 203 | + startX * (p2y - p1y)) / div; |
| 204 | if (t2 > 0) { |
| 205 | outEdgeIndex = p1; |
| 206 | outRayDist = t2; |
| 207 | outEdgeFraction = t; |
| 208 | return; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | p1 = p2; |
| 213 | } |
| 214 | return; |
| 215 | }; |
| 216 | |
| 217 | /** |
| 218 | * Calculate the normal at the intersection point between a ray and the polygon. |
| 219 | * |
| 220 | * @param rays The total number of rays. |
| 221 | * @param currentRayIndex The index of the ray which the normal is based on. |
| 222 | * @param dir The array of the all the rays directions. |
| 223 | * @param rayDist The pre-computed ray distances array. |
| 224 | * |
| 225 | * @param normal Return the normal. |
| 226 | */ |
| 227 | void AmbientShadow::calculateNormal(int rays, int currentRayIndex, |
| 228 | const Vector2* dir, const float* rayDist, Vector2& normal) { |
| 229 | int preIndex = (currentRayIndex - 1 + rays) % rays; |
| 230 | int postIndex = (currentRayIndex + 1) % rays; |
| 231 | Vector2 p1 = dir[preIndex] * rayDist[preIndex]; |
| 232 | Vector2 p2 = dir[postIndex] * rayDist[postIndex]; |
| 233 | |
| 234 | // Now the V (deltaX, deltaY) is the vector going CW around the poly. |
| 235 | Vector2 delta = p2 - p1; |
| 236 | if (delta.length() != 0) { |
| 237 | delta.normalize(); |
| 238 | // Calculate the normal , which is CCW 90 rotate to the V. |
| 239 | // 90 degrees CCW about z-axis: (x, y, z) -> (-y, x, z) |
| 240 | normal.x = -delta.y; |
| 241 | normal.y = delta.x; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | }; // namespace uirenderer |
| 246 | }; // namespace android |