ShadowTessellator.cpp revision f5be3ca5cc5b3a10747b577f60059a99862bb9a8
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#define ATRACE_TAG ATRACE_TAG_VIEW
19
20#include <math.h>
21#include <utils/Log.h>
22#include <utils/Trace.h>
23
24#include "AmbientShadow.h"
25#include "Caches.h"
26#include "ShadowTessellator.h"
27#include "SpotShadow.h"
28
29namespace android {
30namespace uirenderer {
31
32template<typename T>
33static inline T max(T a, T b) {
34    return a > b ? a : b;
35}
36
37VertexBufferMode ShadowTessellator::tessellateAmbientShadow(bool isCasterOpaque,
38        const Vector3* casterPolygon, int casterVertexCount,
39        const Vector3& centroid3d, const Rect& casterBounds,
40        const Rect& localClip, float maxZ, VertexBuffer& shadowVertexBuffer) {
41    ATRACE_CALL();
42
43    // A bunch of parameters to tweak the shadow.
44    // TODO: Allow some of these changable by debug settings or APIs.
45    float heightFactor = 1.0f / 128;
46    const float geomFactor = 64;
47
48    Caches& caches = Caches::getInstance();
49    if (CC_UNLIKELY(caches.propertyAmbientRatio > 0.0f)) {
50        heightFactor *= caches.propertyAmbientRatio;
51    }
52
53    Rect ambientShadowBounds(casterBounds);
54    ambientShadowBounds.outset(maxZ * geomFactor * heightFactor);
55
56    if (!localClip.intersects(ambientShadowBounds)) {
57#if DEBUG_SHADOW
58        ALOGD("Ambient shadow is out of clip rect!");
59#endif
60        return kVertexBufferMode_OnePolyRingShadow;
61    }
62
63    return AmbientShadow::createAmbientShadow(isCasterOpaque, casterPolygon,
64            casterVertexCount, centroid3d, heightFactor, geomFactor,
65            shadowVertexBuffer);
66
67}
68
69VertexBufferMode ShadowTessellator::tessellateSpotShadow(bool isCasterOpaque,
70        const Vector3* casterPolygon, int casterVertexCount,
71        const mat4& receiverTransform,
72        int screenWidth, int screenHeight, const Rect& casterBounds,
73        const Rect& localClip, VertexBuffer& shadowVertexBuffer) {
74    ATRACE_CALL();
75
76    Caches& caches = Caches::getInstance();
77
78    // A bunch of parameters to tweak the shadow.
79    // TODO: Allow some of these changable by debug settings or APIs.
80    int maximal = max(screenWidth, screenHeight);
81    Vector3 lightCenter(screenWidth * 0.5f, 0, maximal);
82
83    if (CC_UNLIKELY(caches.propertyLightPosY > 0)) {
84        lightCenter.y = - caches.propertyLightPosY; // negated since this shifts up
85    }
86    if (CC_UNLIKELY(caches.propertyLightPosZ > 0)) {
87        lightCenter.z = caches.propertyLightPosZ;
88    }
89
90
91#if DEBUG_SHADOW
92    ALOGD("light center %f %f %f", lightCenter.x, lightCenter.y, lightCenter.z);
93#endif
94
95    // light position (because it's in local space) needs to compensate for receiver transform
96    // TODO: should apply to light orientation, not just position
97    Matrix4 reverseReceiverTransform;
98    reverseReceiverTransform.loadInverse(receiverTransform);
99    reverseReceiverTransform.mapPoint3d(lightCenter);
100
101    float lightSize = maximal / 4;
102    const int lightVertexCount = 8;
103
104    if (CC_UNLIKELY(caches.propertyLightDiameter > 0)) {
105        lightSize = caches.propertyLightDiameter;
106    }
107
108    // Now light and caster are both in local space, we will check whether
109    // the shadow is within the clip area.
110    Rect lightRect = Rect(lightCenter.x - lightSize, lightCenter.y - lightSize,
111            lightCenter.x + lightSize, lightCenter.y + lightSize);
112    lightRect.unionWith(localClip);
113    if (!lightRect.intersects(casterBounds)) {
114#if DEBUG_SHADOW
115        ALOGD("Spot shadow is out of clip rect!");
116#endif
117        return kVertexBufferMode_OnePolyRingShadow;
118    }
119
120    VertexBufferMode mode = SpotShadow::createSpotShadow(isCasterOpaque,
121            casterPolygon, casterVertexCount, lightCenter, lightSize,
122            lightVertexCount, shadowVertexBuffer);
123
124#if DEBUG_SHADOW
125     if(shadowVertexBuffer.getVertexCount() <= 0) {
126        ALOGD("Spot shadow generation failed %d", shadowVertexBuffer.getVertexCount());
127     }
128#endif
129     return mode;
130}
131
132void ShadowTessellator::generateShadowIndices(uint16_t* shadowIndices) {
133    int currentIndex = 0;
134    const int rays = SHADOW_RAY_COUNT;
135    // For the penumbra area.
136    for (int layer = 0; layer < 2; layer ++) {
137        int baseIndex = layer * rays;
138        for (int i = 0; i < rays; i++) {
139            shadowIndices[currentIndex++] = i + baseIndex;
140            shadowIndices[currentIndex++] = rays + i + baseIndex;
141        }
142        // To close the loop, back to the ray 0.
143        shadowIndices[currentIndex++] = 0 + baseIndex;
144         // Note this is the same as the first index of next layer loop.
145        shadowIndices[currentIndex++] = rays + baseIndex;
146    }
147
148#if DEBUG_SHADOW
149    if (currentIndex != MAX_SHADOW_INDEX_COUNT) {
150        ALOGW("vertex index count is wrong. current %d, expected %d",
151                currentIndex, MAX_SHADOW_INDEX_COUNT);
152    }
153    for (int i = 0; i < MAX_SHADOW_INDEX_COUNT; i++) {
154        ALOGD("vertex index is (%d, %d)", i, shadowIndices[i]);
155    }
156#endif
157}
158
159/**
160 * Calculate the centroid of a 2d polygon.
161 *
162 * @param poly The polygon, which is represented in a Vector2 array.
163 * @param polyLength The length of the polygon in terms of number of vertices.
164 * @return the centroid of the polygon.
165 */
166Vector2 ShadowTessellator::centroid2d(const Vector2* poly, int polyLength) {
167    double sumx = 0;
168    double sumy = 0;
169    int p1 = polyLength - 1;
170    double area = 0;
171    for (int p2 = 0; p2 < polyLength; p2++) {
172        double x1 = poly[p1].x;
173        double y1 = poly[p1].y;
174        double x2 = poly[p2].x;
175        double y2 = poly[p2].y;
176        double a = (x1 * y2 - x2 * y1);
177        sumx += (x1 + x2) * a;
178        sumy += (y1 + y2) * a;
179        area += a;
180        p1 = p2;
181    }
182
183    Vector2 centroid = poly[0];
184    if (area != 0) {
185        centroid = Vector2(sumx / (3 * area), sumy / (3 * area));
186    } else {
187        ALOGW("Area is 0 while computing centroid!");
188    }
189    return centroid;
190}
191
192}; // namespace uirenderer
193}; // namespace android
194