• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <utils/Vector.h>
24 #include <utils/MathUtils.h>
25 
26 #include "AmbientShadow.h"
27 #include "Properties.h"
28 #include "ShadowTessellator.h"
29 #include "SpotShadow.h"
30 #include "Vector.h"
31 
32 namespace android {
33 namespace uirenderer {
34 
tessellateAmbientShadow(bool isCasterOpaque,const Vector3 * casterPolygon,int casterVertexCount,const Vector3 & centroid3d,const Rect & casterBounds,const Rect & localClip,float maxZ,VertexBuffer & shadowVertexBuffer)35 void ShadowTessellator::tessellateAmbientShadow(bool isCasterOpaque,
36         const Vector3* casterPolygon, int casterVertexCount,
37         const Vector3& centroid3d, const Rect& casterBounds,
38         const Rect& localClip, float maxZ, VertexBuffer& shadowVertexBuffer) {
39     ATRACE_CALL();
40 
41     // A bunch of parameters to tweak the shadow.
42     // TODO: Allow some of these changable by debug settings or APIs.
43     float heightFactor = 1.0f / 128;
44     const float geomFactor = 64;
45 
46     if (CC_UNLIKELY(Properties::overrideAmbientRatio > 0.0f)) {
47         heightFactor *= Properties::overrideAmbientRatio;
48     }
49 
50     Rect ambientShadowBounds(casterBounds);
51     ambientShadowBounds.outset(maxZ * geomFactor * heightFactor);
52 
53     if (!localClip.intersects(ambientShadowBounds)) {
54 #if DEBUG_SHADOW
55         ALOGD("Ambient shadow is out of clip rect!");
56 #endif
57         return;
58     }
59 
60     AmbientShadow::createAmbientShadow(isCasterOpaque, casterPolygon,
61             casterVertexCount, centroid3d, heightFactor, geomFactor,
62             shadowVertexBuffer);
63 }
64 
tessellateSpotShadow(bool isCasterOpaque,const Vector3 * casterPolygon,int casterVertexCount,const Vector3 & casterCentroid,const mat4 & receiverTransform,const Vector3 & lightCenter,int lightRadius,const Rect & casterBounds,const Rect & localClip,VertexBuffer & shadowVertexBuffer)65 void ShadowTessellator::tessellateSpotShadow(bool isCasterOpaque,
66         const Vector3* casterPolygon, int casterVertexCount, const Vector3& casterCentroid,
67         const mat4& receiverTransform, const Vector3& lightCenter, int lightRadius,
68         const Rect& casterBounds, const Rect& localClip, VertexBuffer& shadowVertexBuffer) {
69     ATRACE_CALL();
70 
71     Vector3 adjustedLightCenter(lightCenter);
72     if (CC_UNLIKELY(Properties::overrideLightPosY > 0)) {
73         adjustedLightCenter.y = - Properties::overrideLightPosY; // negated since this shifts up
74     }
75     if (CC_UNLIKELY(Properties::overrideLightPosZ > 0)) {
76         adjustedLightCenter.z = Properties::overrideLightPosZ;
77     }
78 
79 #if DEBUG_SHADOW
80     ALOGD("light center %f %f %f %d",
81             adjustedLightCenter.x, adjustedLightCenter.y, adjustedLightCenter.z, lightRadius);
82 #endif
83     if (isnan(adjustedLightCenter.x)
84             || isnan(adjustedLightCenter.y)
85             || isnan(adjustedLightCenter.z)) {
86         return;
87     }
88 
89     // light position (because it's in local space) needs to compensate for receiver transform
90     // TODO: should apply to light orientation, not just position
91     Matrix4 reverseReceiverTransform;
92     reverseReceiverTransform.loadInverse(receiverTransform);
93     reverseReceiverTransform.mapPoint3d(adjustedLightCenter);
94 
95     if (CC_UNLIKELY(Properties::overrideLightRadius > 0)) {
96         lightRadius = Properties::overrideLightRadius;
97     }
98 
99     // Now light and caster are both in local space, we will check whether
100     // the shadow is within the clip area.
101     Rect lightRect = Rect(adjustedLightCenter.x - lightRadius, adjustedLightCenter.y - lightRadius,
102             adjustedLightCenter.x + lightRadius, adjustedLightCenter.y + lightRadius);
103     lightRect.unionWith(localClip);
104     if (!lightRect.intersects(casterBounds)) {
105 #if DEBUG_SHADOW
106         ALOGD("Spot shadow is out of clip rect!");
107 #endif
108         return;
109     }
110 
111     SpotShadow::createSpotShadow(isCasterOpaque, adjustedLightCenter, lightRadius,
112             casterPolygon, casterVertexCount, casterCentroid, shadowVertexBuffer);
113 
114 #if DEBUG_SHADOW
115      if(shadowVertexBuffer.getVertexCount() <= 0) {
116         ALOGD("Spot shadow generation failed %d", shadowVertexBuffer.getVertexCount());
117      }
118 #endif
119 }
120 
121 /**
122  * Calculate the centroid of a 2d polygon.
123  *
124  * @param poly The polygon, which is represented in a Vector2 array.
125  * @param polyLength The length of the polygon in terms of number of vertices.
126  * @return the centroid of the polygon.
127  */
centroid2d(const Vector2 * poly,int polyLength)128 Vector2 ShadowTessellator::centroid2d(const Vector2* poly, int polyLength) {
129     double sumx = 0;
130     double sumy = 0;
131     int p1 = polyLength - 1;
132     double area = 0;
133     for (int p2 = 0; p2 < polyLength; p2++) {
134         double x1 = poly[p1].x;
135         double y1 = poly[p1].y;
136         double x2 = poly[p2].x;
137         double y2 = poly[p2].y;
138         double a = (x1 * y2 - x2 * y1);
139         sumx += (x1 + x2) * a;
140         sumy += (y1 + y2) * a;
141         area += a;
142         p1 = p2;
143     }
144 
145     Vector2 centroid = poly[0];
146     if (area != 0) {
147         centroid = (Vector2){static_cast<float>(sumx / (3 * area)),
148             static_cast<float>(sumy / (3 * area))};
149     } else {
150         ALOGW("Area is 0 while computing centroid!");
151     }
152     return centroid;
153 }
154 
155 // Make sure p1 -> p2 is going CW around the poly.
calculateNormal(const Vector2 & p1,const Vector2 & p2)156 Vector2 ShadowTessellator::calculateNormal(const Vector2& p1, const Vector2& p2) {
157     Vector2 result = p2 - p1;
158     if (result.x != 0 || result.y != 0) {
159         result.normalize();
160         // Calculate the normal , which is CCW 90 rotate to the delta.
161         float tempy = result.y;
162         result.y = result.x;
163         result.x = -tempy;
164     }
165     return result;
166 }
167 
getExtraVertexNumber(const Vector2 & vector1,const Vector2 & vector2,float divisor)168 int ShadowTessellator::getExtraVertexNumber(const Vector2& vector1,
169         const Vector2& vector2, float divisor) {
170     // When there is no distance difference, there is no need for extra vertices.
171     if (vector1.lengthSquared() == 0 || vector2.lengthSquared() == 0) {
172         return 0;
173     }
174     // The formula is :
175     // extraNumber = floor(acos(dot(n1, n2)) / (M_PI / EXTRA_VERTEX_PER_PI))
176     // The value ranges for each step are:
177     // dot( ) --- [-1, 1]
178     // acos( )     --- [0, M_PI]
179     // floor(...)  --- [0, EXTRA_VERTEX_PER_PI]
180     float dotProduct = vector1.dot(vector2);
181     // make sure that dotProduct value is in acsof input range [-1, 1]
182     dotProduct = MathUtils::clamp(dotProduct, -1.0f, 1.0f);
183     // TODO: Use look up table for the dotProduct to extraVerticesNumber
184     // computation, if needed.
185     float angle = acosf(dotProduct);
186     return (int) floor(angle / divisor);
187 }
188 
checkOverflow(int used,int total,const char * bufferName)189 void ShadowTessellator::checkOverflow(int used, int total, const char* bufferName) {
190     LOG_ALWAYS_FATAL_IF(used > total, "Error: %s overflow!!! used %d, total %d",
191             bufferName, used, total);
192 }
193 
194 }; // namespace uirenderer
195 }; // namespace android
196