ShadowTessellator.cpp revision 117bdbcfa3e8306dad21e7e01fa71b00cdfa7265
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 32void ShadowTessellator::tessellateAmbientShadow(bool isCasterOpaque, 33 const Vector3* casterPolygon, int casterVertexCount, 34 const Vector3& centroid3d, const Rect& casterBounds, 35 const Rect& localClip, float maxZ, VertexBuffer& shadowVertexBuffer) { 36 ATRACE_CALL(); 37 38 // A bunch of parameters to tweak the shadow. 39 // TODO: Allow some of these changable by debug settings or APIs. 40 float heightFactor = 1.0f / 128; 41 const float geomFactor = 64; 42 43 Caches& caches = Caches::getInstance(); 44 if (CC_UNLIKELY(caches.propertyAmbientRatio > 0.0f)) { 45 heightFactor *= caches.propertyAmbientRatio; 46 } 47 48 Rect ambientShadowBounds(casterBounds); 49 ambientShadowBounds.outset(maxZ * geomFactor * heightFactor); 50 51 if (!localClip.intersects(ambientShadowBounds)) { 52#if DEBUG_SHADOW 53 ALOGD("Ambient shadow is out of clip rect!"); 54#endif 55 return; 56 } 57 58 AmbientShadow::createAmbientShadow(isCasterOpaque, casterPolygon, 59 casterVertexCount, centroid3d, heightFactor, geomFactor, 60 shadowVertexBuffer); 61} 62 63void ShadowTessellator::tessellateSpotShadow(bool isCasterOpaque, 64 const Vector3* casterPolygon, int casterVertexCount, const Vector3& casterCentroid, 65 const mat4& receiverTransform, const Vector3& lightCenter, int lightRadius, 66 const Rect& casterBounds, const Rect& localClip, VertexBuffer& shadowVertexBuffer) { 67 ATRACE_CALL(); 68 69 Caches& caches = Caches::getInstance(); 70 71 Vector3 adjustedLightCenter(lightCenter); 72 if (CC_UNLIKELY(caches.propertyLightPosY > 0)) { 73 adjustedLightCenter.y = - caches.propertyLightPosY; // negated since this shifts up 74 } 75 if (CC_UNLIKELY(caches.propertyLightPosZ > 0)) { 76 adjustedLightCenter.z = caches.propertyLightPosZ; 77 } 78 79#if DEBUG_SHADOW 80 ALOGD("light center %f %f %f", 81 adjustedLightCenter.x, adjustedLightCenter.y, adjustedLightCenter.z); 82#endif 83 84 // light position (because it's in local space) needs to compensate for receiver transform 85 // TODO: should apply to light orientation, not just position 86 Matrix4 reverseReceiverTransform; 87 reverseReceiverTransform.loadInverse(receiverTransform); 88 reverseReceiverTransform.mapPoint3d(adjustedLightCenter); 89 90 if (CC_UNLIKELY(caches.propertyLightDiameter > 0)) { 91 lightRadius = caches.propertyLightDiameter; 92 } 93 94 // Now light and caster are both in local space, we will check whether 95 // the shadow is within the clip area. 96 Rect lightRect = Rect(adjustedLightCenter.x - lightRadius, adjustedLightCenter.y - lightRadius, 97 adjustedLightCenter.x + lightRadius, adjustedLightCenter.y + lightRadius); 98 lightRect.unionWith(localClip); 99 if (!lightRect.intersects(casterBounds)) { 100#if DEBUG_SHADOW 101 ALOGD("Spot shadow is out of clip rect!"); 102#endif 103 return; 104 } 105 106 SpotShadow::createSpotShadow(isCasterOpaque, adjustedLightCenter, lightRadius, 107 casterPolygon, casterVertexCount, casterCentroid, shadowVertexBuffer); 108 109#if DEBUG_SHADOW 110 if(shadowVertexBuffer.getVertexCount() <= 0) { 111 ALOGD("Spot shadow generation failed %d", shadowVertexBuffer.getVertexCount()); 112 } 113#endif 114} 115 116/** 117 * Calculate the centroid of a 2d polygon. 118 * 119 * @param poly The polygon, which is represented in a Vector2 array. 120 * @param polyLength The length of the polygon in terms of number of vertices. 121 * @return the centroid of the polygon. 122 */ 123Vector2 ShadowTessellator::centroid2d(const Vector2* poly, int polyLength) { 124 double sumx = 0; 125 double sumy = 0; 126 int p1 = polyLength - 1; 127 double area = 0; 128 for (int p2 = 0; p2 < polyLength; p2++) { 129 double x1 = poly[p1].x; 130 double y1 = poly[p1].y; 131 double x2 = poly[p2].x; 132 double y2 = poly[p2].y; 133 double a = (x1 * y2 - x2 * y1); 134 sumx += (x1 + x2) * a; 135 sumy += (y1 + y2) * a; 136 area += a; 137 p1 = p2; 138 } 139 140 Vector2 centroid = poly[0]; 141 if (area != 0) { 142 centroid = (Vector2){static_cast<float>(sumx / (3 * area)), 143 static_cast<float>(sumy / (3 * area))}; 144 } else { 145 ALOGW("Area is 0 while computing centroid!"); 146 } 147 return centroid; 148} 149 150// Make sure p1 -> p2 is going CW around the poly. 151Vector2 ShadowTessellator::calculateNormal(const Vector2& p1, const Vector2& p2) { 152 Vector2 result = p2 - p1; 153 if (result.x != 0 || result.y != 0) { 154 result.normalize(); 155 // Calculate the normal , which is CCW 90 rotate to the delta. 156 float tempy = result.y; 157 result.y = result.x; 158 result.x = -tempy; 159 } 160 return result; 161} 162/** 163 * Test whether the polygon is order in clockwise. 164 * 165 * @param polygon the polygon as a Vector2 array 166 * @param len the number of points of the polygon 167 */ 168bool ShadowTessellator::isClockwise(const Vector2* polygon, int len) { 169 if (len < 2 || polygon == nullptr) { 170 return true; 171 } 172 double sum = 0; 173 double p1x = polygon[len - 1].x; 174 double p1y = polygon[len - 1].y; 175 for (int i = 0; i < len; i++) { 176 177 double p2x = polygon[i].x; 178 double p2y = polygon[i].y; 179 sum += p1x * p2y - p2x * p1y; 180 p1x = p2x; 181 p1y = p2y; 182 } 183 return sum < 0; 184} 185 186bool ShadowTessellator::isClockwisePath(const SkPath& path) { 187 SkPath::Iter iter(path, false); 188 SkPoint pts[4]; 189 SkPath::Verb v; 190 191 Vector<Vector2> arrayForDirection; 192 while (SkPath::kDone_Verb != (v = iter.next(pts))) { 193 switch (v) { 194 case SkPath::kMove_Verb: 195 arrayForDirection.add((Vector2){pts[0].x(), pts[0].y()}); 196 break; 197 case SkPath::kLine_Verb: 198 arrayForDirection.add((Vector2){pts[1].x(), pts[1].y()}); 199 break; 200 case SkPath::kQuad_Verb: 201 arrayForDirection.add((Vector2){pts[1].x(), pts[1].y()}); 202 arrayForDirection.add((Vector2){pts[2].x(), pts[2].y()}); 203 break; 204 case SkPath::kCubic_Verb: 205 arrayForDirection.add((Vector2){pts[1].x(), pts[1].y()}); 206 arrayForDirection.add((Vector2){pts[2].x(), pts[2].y()}); 207 arrayForDirection.add((Vector2){pts[3].x(), pts[3].y()}); 208 break; 209 default: 210 break; 211 } 212 } 213 214 return isClockwise(arrayForDirection.array(), arrayForDirection.size()); 215} 216 217void ShadowTessellator::reverseVertexArray(Vertex* polygon, int len) { 218 int n = len / 2; 219 for (int i = 0; i < n; i++) { 220 Vertex tmp = polygon[i]; 221 int k = len - 1 - i; 222 polygon[i] = polygon[k]; 223 polygon[k] = tmp; 224 } 225} 226 227int ShadowTessellator::getExtraVertexNumber(const Vector2& vector1, 228 const Vector2& vector2, float divisor) { 229 // When there is no distance difference, there is no need for extra vertices. 230 if (vector1.lengthSquared() == 0 || vector2.lengthSquared() == 0) { 231 return 0; 232 } 233 // The formula is : 234 // extraNumber = floor(acos(dot(n1, n2)) / (M_PI / EXTRA_VERTEX_PER_PI)) 235 // The value ranges for each step are: 236 // dot( ) --- [-1, 1] 237 // acos( ) --- [0, M_PI] 238 // floor(...) --- [0, EXTRA_VERTEX_PER_PI] 239 float dotProduct = vector1.dot(vector2); 240 // TODO: Use look up table for the dotProduct to extraVerticesNumber 241 // computation, if needed. 242 float angle = acosf(dotProduct); 243 return (int) floor(angle / divisor); 244} 245 246void ShadowTessellator::checkOverflow(int used, int total, const char* bufferName) { 247 LOG_ALWAYS_FATAL_IF(used > total, "Error: %s overflow!!! used %d, total %d", 248 bufferName, used, total); 249} 250 251}; // namespace uirenderer 252}; // namespace android 253