blob: 6ec42c26932914af328ef5ec79c983c04c56caee [file] [log] [blame]
ztenghui55bfb4e2013-12-03 10:38:55 -08001/*
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
ztenghuid5e8ade2014-08-13 15:48:02 -070019/**
20 * Extra vertices for the corner for smoother corner.
21 * Only for outer vertices.
22 * Note that we use such extra memory to avoid an extra loop.
23 */
24// For half circle, we could add EXTRA_VERTEX_PER_PI vertices.
25// Set to 1 if we don't want to have any.
26#define EXTRA_CORNER_VERTEX_PER_PI 12
27
28// For the whole polygon, the sum of all the deltas b/t normals is 2 * M_PI,
29// therefore, the maximum number of extra vertices will be twice bigger.
30#define MAX_EXTRA_CORNER_VERTEX_NUMBER (2 * EXTRA_CORNER_VERTEX_PER_PI)
31
32// For each RADIANS_DIVISOR, we would allocate one more vertex b/t the normals.
33#define CORNER_RADIANS_DIVISOR (M_PI / EXTRA_CORNER_VERTEX_PER_PI)
34
35/**
36 * Extra vertices for the Edge for interpolation artifacts.
37 * Same value for both inner and outer vertices.
38 */
39#define EXTRA_EDGE_VERTEX_PER_PI 50
40
41#define MAX_EXTRA_EDGE_VERTEX_NUMBER (2 * EXTRA_EDGE_VERTEX_PER_PI)
42
43#define EDGE_RADIANS_DIVISOR (M_PI / EXTRA_EDGE_VERTEX_PER_PI)
44
45/**
46 * Other constants:
47 */
48// For the edge of the penumbra, the opacity is 0.
49#define OUTER_OPACITY (0.0f)
50
51// Once the alpha difference is greater than this threshold, we will allocate extra
52// edge vertices.
53// If this is set to negative value, then all the edge will be tessellated.
54#define ALPHA_THRESHOLD (0.1f / 255.0f)
55
ztenghui55bfb4e2013-12-03 10:38:55 -080056#include <math.h>
57#include <utils/Log.h>
Chris Craik564acf72014-01-02 16:46:18 -080058#include <utils/Vector.h>
ztenghui55bfb4e2013-12-03 10:38:55 -080059
60#include "AmbientShadow.h"
ztenghui63d41ab2014-02-14 13:13:41 -080061#include "ShadowTessellator.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080062#include "Vertex.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040063#include "VertexBuffer.h"
ztenghuid5e8ade2014-08-13 15:48:02 -070064#include "utils/MathUtils.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080065
66namespace android {
67namespace uirenderer {
68
69/**
ztenghuid5e8ade2014-08-13 15:48:02 -070070 * Local utility functions.
71 */
72inline Vector2 getNormalFromVertices(const Vector3* vertices, int current, int next) {
73 // Convert from Vector3 to Vector2 first.
74 Vector2 currentVertex = { vertices[current].x, vertices[current].y };
75 Vector2 nextVertex = { vertices[next].x, vertices[next].y };
76
77 return ShadowTessellator::calculateNormal(currentVertex, nextVertex);
78}
79
80// The input z value will be converted to be non-negative inside.
81// The output must be ranged from 0 to 1.
82inline float getAlphaFromFactoredZ(float factoredZ) {
83 return 1.0 / (1 + MathUtils::max(factoredZ, 0.0f));
84}
85
86inline float getTransformedAlphaFromAlpha(float alpha) {
87 return acosf(1.0f - 2.0f * alpha);
88}
89
90// The output is ranged from 0 to M_PI.
91inline float getTransformedAlphaFromFactoredZ(float factoredZ) {
92 return getTransformedAlphaFromAlpha(getAlphaFromFactoredZ(factoredZ));
93}
94
ztenghuid5e8ade2014-08-13 15:48:02 -070095inline int getEdgeExtraAndUpdateSpike(Vector2* currentSpike,
96 const Vector3& secondVertex, const Vector3& centroid) {
97 Vector2 secondSpike = {secondVertex.x - centroid.x, secondVertex.y - centroid.y};
98 secondSpike.normalize();
99
ztenghui512e6432014-09-10 13:08:20 -0700100 int result = ShadowTessellator::getExtraVertexNumber(secondSpike, *currentSpike,
101 EDGE_RADIANS_DIVISOR);
ztenghuid5e8ade2014-08-13 15:48:02 -0700102 *currentSpike = secondSpike;
103 return result;
104}
105
106// Given the caster's vertex count, compute all the buffers size depending on
107// whether or not the caster is opaque.
108inline void computeBufferSize(int* totalVertexCount, int* totalIndexCount,
109 int* totalUmbraCount, int casterVertexCount, bool isCasterOpaque) {
110 // Compute the size of the vertex buffer.
111 int outerVertexCount = casterVertexCount * 2 + MAX_EXTRA_CORNER_VERTEX_NUMBER +
112 MAX_EXTRA_EDGE_VERTEX_NUMBER;
113 int innerVertexCount = casterVertexCount + MAX_EXTRA_EDGE_VERTEX_NUMBER;
114 *totalVertexCount = outerVertexCount + innerVertexCount;
115
116 // Compute the size of the index buffer.
117 *totalIndexCount = 2 * outerVertexCount + 2;
118
119 // Compute the size of the umber buffer.
120 // For translucent object, keep track of the umbra(inner) vertex in order to draw
121 // inside. We only need to store the index information.
122 *totalUmbraCount = 0;
123 if (!isCasterOpaque) {
124 // Add the centroid if occluder is translucent.
Andreas Gampeedaecc12014-11-10 20:54:07 -0800125 (*totalVertexCount)++;
ztenghuid5e8ade2014-08-13 15:48:02 -0700126 *totalIndexCount += 2 * innerVertexCount + 1;
127 *totalUmbraCount = innerVertexCount;
128 }
129}
130
131inline bool needsExtraForEdge(float firstAlpha, float secondAlpha) {
Bernhard Rosenkränzerc44958c72014-11-17 21:48:46 +0100132 return fabsf(firstAlpha - secondAlpha) > ALPHA_THRESHOLD;
ztenghuid5e8ade2014-08-13 15:48:02 -0700133}
134
135/**
ztenghui55bfb4e2013-12-03 10:38:55 -0800136 * Calculate the shadows as a triangle strips while alpha value as the
137 * shadow values.
138 *
ztenghui50ecf842014-03-11 16:52:30 -0700139 * @param isCasterOpaque Whether the caster is opaque.
ztenghui55bfb4e2013-12-03 10:38:55 -0800140 * @param vertices The shadow caster's polygon, which is represented in a Vector3
141 * array.
142 * @param vertexCount The length of caster's polygon in terms of number of
143 * vertices.
ztenghui63d41ab2014-02-14 13:13:41 -0800144 * @param centroid3d The centroid of the shadow caster.
ztenghui55bfb4e2013-12-03 10:38:55 -0800145 * @param heightFactor The factor showing the higher the object, the lighter the
146 * shadow.
147 * @param geomFactor The factor scaling the geometry expansion along the normal.
148 *
149 * @param shadowVertexBuffer Return an floating point array of (x, y, a)
150 * triangle strips mode.
ztenghuid5e8ade2014-08-13 15:48:02 -0700151 *
152 * An simple illustration:
153 * For now let's mark the outer vertex as Pi, the inner as Vi, the centroid as C.
154 *
155 * First project the occluder to the Z=0 surface.
156 * Then we got all the inner vertices. And we compute the normal for each edge.
157 * According to the normal, we generate outer vertices. E.g: We generate P1 / P4
158 * as extra corner vertices to make the corner looks round and smoother.
159 *
160 * Due to the fact that the alpha is not linear interpolated along the inner
161 * edge, when the alpha is different, we may add extra vertices such as P2.1, P2.2,
162 * V0.1, V0.2 to avoid the visual artifacts.
163 *
164 * (P3)
165 * (P2) (P2.1) (P2.2) | ' (P4)
166 * (P1)' | | | | '
167 * ' | | | | '
168 * (P0) ------------------------------------------------(P5)
169 * | (V0) (V0.1) (V0.2) |(V1)
170 * | |
171 * | |
172 * | (C) |
173 * | |
174 * | |
175 * | |
176 * | |
177 * (V3)-----------------------------------(V2)
ztenghui55bfb4e2013-12-03 10:38:55 -0800178 */
Chris Craik05f3d6e2014-06-02 16:27:04 -0700179void AmbientShadow::createAmbientShadow(bool isCasterOpaque,
ztenghuid5e8ade2014-08-13 15:48:02 -0700180 const Vector3* casterVertices, int casterVertexCount, const Vector3& centroid3d,
ztenghui50ecf842014-03-11 16:52:30 -0700181 float heightFactor, float geomFactor, VertexBuffer& shadowVertexBuffer) {
ztenghuid5e8ade2014-08-13 15:48:02 -0700182 shadowVertexBuffer.setMode(VertexBuffer::kIndices);
ztenghui55bfb4e2013-12-03 10:38:55 -0800183
ztenghuid5e8ade2014-08-13 15:48:02 -0700184 // In order to computer the outer vertices in one loop, we need pre-compute
185 // the normal by the vertex (n - 1) to vertex 0, and the spike and alpha value
186 // for vertex 0.
187 Vector2 previousNormal = getNormalFromVertices(casterVertices,
188 casterVertexCount - 1 , 0);
189 Vector2 currentSpike = {casterVertices[0].x - centroid3d.x,
190 casterVertices[0].y - centroid3d.y};
191 currentSpike.normalize();
192 float currentAlpha = getAlphaFromFactoredZ(casterVertices[0].z * heightFactor);
ztenghui55bfb4e2013-12-03 10:38:55 -0800193
ztenghuid5e8ade2014-08-13 15:48:02 -0700194 // Preparing all the output data.
195 int totalVertexCount, totalIndexCount, totalUmbraCount;
196 computeBufferSize(&totalVertexCount, &totalIndexCount, &totalUmbraCount,
197 casterVertexCount, isCasterOpaque);
ztenghui50ecf842014-03-11 16:52:30 -0700198 AlphaVertex* shadowVertices =
ztenghuid5e8ade2014-08-13 15:48:02 -0700199 shadowVertexBuffer.alloc<AlphaVertex>(totalVertexCount);
200 int vertexBufferIndex = 0;
201 uint16_t* indexBuffer = shadowVertexBuffer.allocIndices<uint16_t>(totalIndexCount);
202 int indexBufferIndex = 0;
203 uint16_t umbraVertices[totalUmbraCount];
204 int umbraIndex = 0;
ztenghui55bfb4e2013-12-03 10:38:55 -0800205
ztenghuid5e8ade2014-08-13 15:48:02 -0700206 for (int i = 0; i < casterVertexCount; i++) {
207 // Corner: first figure out the extra vertices we need for the corner.
208 const Vector3& innerVertex = casterVertices[i];
209 Vector2 currentNormal = getNormalFromVertices(casterVertices, i,
210 (i + 1) % casterVertexCount);
ztenghui55bfb4e2013-12-03 10:38:55 -0800211
ztenghui512e6432014-09-10 13:08:20 -0700212 int extraVerticesNumber = ShadowTessellator::getExtraVertexNumber(currentNormal,
213 previousNormal, CORNER_RADIANS_DIVISOR);
ztenghui55bfb4e2013-12-03 10:38:55 -0800214
ztenghuid5e8ade2014-08-13 15:48:02 -0700215 float expansionDist = innerVertex.z * heightFactor * geomFactor;
216 const int cornerSlicesNumber = extraVerticesNumber + 1; // Minimal as 1.
ztenghui55bfb4e2013-12-03 10:38:55 -0800217#if DEBUG_SHADOW
ztenghuid5e8ade2014-08-13 15:48:02 -0700218 ALOGD("cornerSlicesNumber is %d", cornerSlicesNumber);
ztenghui63d41ab2014-02-14 13:13:41 -0800219#endif
ztenghui55bfb4e2013-12-03 10:38:55 -0800220
ztenghuid5e8ade2014-08-13 15:48:02 -0700221 // Corner: fill the corner Vertex Buffer(VB) and Index Buffer(IB).
222 // We fill the inner vertex first, such that we can fill the index buffer
223 // inside the loop.
224 int currentInnerVertexIndex = vertexBufferIndex;
225 if (!isCasterOpaque) {
226 umbraVertices[umbraIndex++] = vertexBufferIndex;
ztenghui7940dc52014-04-22 11:21:49 -0700227 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700228 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], casterVertices[i].x,
229 casterVertices[i].y,
230 getTransformedAlphaFromAlpha(currentAlpha));
ztenghui7940dc52014-04-22 11:21:49 -0700231
ztenghuid5e8ade2014-08-13 15:48:02 -0700232 const Vector3& innerStart = casterVertices[i];
ztenghui7940dc52014-04-22 11:21:49 -0700233
ztenghuid5e8ade2014-08-13 15:48:02 -0700234 // outerStart is the first outer vertex for this inner vertex.
235 // outerLast is the last outer vertex for this inner vertex.
236 Vector2 outerStart = {0, 0};
237 Vector2 outerLast = {0, 0};
238 // This will create vertices from [0, cornerSlicesNumber] inclusively,
239 // which means minimally 2 vertices even without the extra ones.
240 for (int j = 0; j <= cornerSlicesNumber; j++) {
241 Vector2 averageNormal =
242 previousNormal * (cornerSlicesNumber - j) + currentNormal * j;
243 averageNormal /= cornerSlicesNumber;
244 averageNormal.normalize();
245 Vector2 outerVertex;
246 outerVertex.x = innerVertex.x + averageNormal.x * expansionDist;
247 outerVertex.y = innerVertex.y + averageNormal.y * expansionDist;
ztenghui7940dc52014-04-22 11:21:49 -0700248
ztenghuid5e8ade2014-08-13 15:48:02 -0700249 indexBuffer[indexBufferIndex++] = vertexBufferIndex;
250 indexBuffer[indexBufferIndex++] = currentInnerVertexIndex;
251 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], outerVertex.x,
252 outerVertex.y, OUTER_OPACITY);
ztenghui7940dc52014-04-22 11:21:49 -0700253
ztenghuid5e8ade2014-08-13 15:48:02 -0700254 if (j == 0) {
255 outerStart = outerVertex;
256 } else if (j == cornerSlicesNumber) {
257 outerLast = outerVertex;
ztenghui7940dc52014-04-22 11:21:49 -0700258 }
259 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700260 previousNormal = currentNormal;
ztenghui55bfb4e2013-12-03 10:38:55 -0800261
ztenghuid5e8ade2014-08-13 15:48:02 -0700262 // Edge: first figure out the extra vertices needed for the edge.
263 const Vector3& innerNext = casterVertices[(i + 1) % casterVertexCount];
264 float nextAlpha = getAlphaFromFactoredZ(innerNext.z * heightFactor);
265 if (needsExtraForEdge(currentAlpha, nextAlpha)) {
266 // TODO: See if we can / should cache this outer vertex across the loop.
267 Vector2 outerNext;
268 float expansionDist = innerNext.z * heightFactor * geomFactor;
269 outerNext.x = innerNext.x + currentNormal.x * expansionDist;
270 outerNext.y = innerNext.y + currentNormal.y * expansionDist;
271
272 // Compute the angle and see how many extra points we need.
273 int extraVerticesNumber = getEdgeExtraAndUpdateSpike(&currentSpike,
274 innerNext, centroid3d);
ztenghui7940dc52014-04-22 11:21:49 -0700275#if DEBUG_SHADOW
ztenghuid5e8ade2014-08-13 15:48:02 -0700276 ALOGD("extraVerticesNumber %d for edge %d", extraVerticesNumber, i);
ztenghui7940dc52014-04-22 11:21:49 -0700277#endif
ztenghuid5e8ade2014-08-13 15:48:02 -0700278 // Edge: fill the edge's VB and IB.
279 // This will create vertices pair from [1, extraVerticesNumber - 1].
280 // If there is no extra vertices created here, the edge will be drawn
281 // as just 2 triangles.
282 for (int k = 1; k < extraVerticesNumber; k++) {
283 int startWeight = extraVerticesNumber - k;
284 Vector2 currentOuter =
285 (outerLast * startWeight + outerNext * k) / extraVerticesNumber;
286 indexBuffer[indexBufferIndex++] = vertexBufferIndex;
287 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], currentOuter.x,
288 currentOuter.y, OUTER_OPACITY);
ztenghui55bfb4e2013-12-03 10:38:55 -0800289
ztenghuid5e8ade2014-08-13 15:48:02 -0700290 if (!isCasterOpaque) {
291 umbraVertices[umbraIndex++] = vertexBufferIndex;
ztenghui55bfb4e2013-12-03 10:38:55 -0800292 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700293 Vector3 currentInner =
294 (innerStart * startWeight + innerNext * k) / extraVerticesNumber;
295 indexBuffer[indexBufferIndex++] = vertexBufferIndex;
296 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], currentInner.x,
297 currentInner.y,
298 getTransformedAlphaFromFactoredZ(currentInner.z * heightFactor));
ztenghui55bfb4e2013-12-03 10:38:55 -0800299 }
300 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700301 currentAlpha = nextAlpha;
ztenghui55bfb4e2013-12-03 10:38:55 -0800302 }
ztenghui55bfb4e2013-12-03 10:38:55 -0800303
ztenghuid5e8ade2014-08-13 15:48:02 -0700304 indexBuffer[indexBufferIndex++] = 1;
305 indexBuffer[indexBufferIndex++] = 0;
ztenghui55bfb4e2013-12-03 10:38:55 -0800306
ztenghuid5e8ade2014-08-13 15:48:02 -0700307 if (!isCasterOpaque) {
308 // Add the centroid as the last one in the vertex buffer.
309 float centroidOpacity =
310 getTransformedAlphaFromFactoredZ(centroid3d.z * heightFactor);
311 int centroidIndex = vertexBufferIndex;
312 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], centroid3d.x,
313 centroid3d.y, centroidOpacity);
314
315 for (int i = 0; i < umbraIndex; i++) {
316 // Note that umbraVertices[0] is always 0.
317 // So the start and the end of the umbra are using the "0".
318 // And penumbra ended with 0, so a degenerated triangle is formed b/t
319 // the umbra and penumbra.
320 indexBuffer[indexBufferIndex++] = umbraVertices[i];
321 indexBuffer[indexBufferIndex++] = centroidIndex;
322 }
323 indexBuffer[indexBufferIndex++] = 0;
ztenghui55bfb4e2013-12-03 10:38:55 -0800324 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700325
326 // At the end, update the real index and vertex buffer size.
327 shadowVertexBuffer.updateVertexCount(vertexBufferIndex);
328 shadowVertexBuffer.updateIndexCount(indexBufferIndex);
Chris Craik4ac36f82014-12-09 16:54:03 -0800329 shadowVertexBuffer.computeBounds<AlphaVertex>();
ztenghuid5e8ade2014-08-13 15:48:02 -0700330
ztenghuid2dcd6f2014-10-29 16:04:29 -0700331 ShadowTessellator::checkOverflow(vertexBufferIndex, totalVertexCount, "Ambient Vertex Buffer");
332 ShadowTessellator::checkOverflow(indexBufferIndex, totalIndexCount, "Ambient Index Buffer");
333 ShadowTessellator::checkOverflow(umbraIndex, totalUmbraCount, "Ambient Umbra Buffer");
ztenghuid5e8ade2014-08-13 15:48:02 -0700334
335#if DEBUG_SHADOW
336 for (int i = 0; i < vertexBufferIndex; i++) {
337 ALOGD("vertexBuffer i %d, (%f, %f %f)", i, shadowVertices[i].x, shadowVertices[i].y,
338 shadowVertices[i].alpha);
339 }
340 for (int i = 0; i < indexBufferIndex; i++) {
341 ALOGD("indexBuffer i %d, indexBuffer[i] %d", i, indexBuffer[i]);
342 }
343#endif
ztenghui55bfb4e2013-12-03 10:38:55 -0800344}
345
346}; // namespace uirenderer
347}; // namespace android