blob: b6271f63a6068f4506feea1f010f2d1d78dd6fe2 [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
ztenghui55bfb4e2013-12-03 10:38:55 -080017#include <math.h>
18#include <utils/Log.h>
Chris Craik87f9df82014-03-07 14:34:42 -080019#include <utils/Trace.h>
Chris Craik2507c342015-05-04 14:36:49 -070020#include <utils/Vector.h>
Lazar Trsic12407522015-06-23 11:46:49 +020021#include <utils/MathUtils.h>
ztenghui55bfb4e2013-12-03 10:38:55 -080022
23#include "AmbientShadow.h"
Chris Craik2507c342015-05-04 14:36:49 -070024#include "Properties.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080025#include "ShadowTessellator.h"
ztenghui7b4516e2014-01-07 10:42:55 -080026#include "SpotShadow.h"
Chris Craik2507c342015-05-04 14:36:49 -070027#include "Vector.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080028
29namespace android {
30namespace uirenderer {
31
Chris Craik05f3d6e2014-06-02 16:27:04 -070032void ShadowTessellator::tessellateAmbientShadow(bool isCasterOpaque,
ztenghui50ecf842014-03-11 16:52:30 -070033 const Vector3* casterPolygon, int casterVertexCount,
ztenghuiaf6f7ed2014-03-18 17:25:49 -070034 const Vector3& centroid3d, const Rect& casterBounds,
35 const Rect& localClip, float maxZ, VertexBuffer& shadowVertexBuffer) {
Chris Craik87f9df82014-03-07 14:34:42 -080036 ATRACE_CALL();
37
ztenghui55bfb4e2013-12-03 10:38:55 -080038 // A bunch of parameters to tweak the shadow.
39 // TODO: Allow some of these changable by debug settings or APIs.
ztenghui8def74d2014-10-01 16:10:16 -070040 float heightFactor = 1.0f / 128;
ztenghui7b4516e2014-01-07 10:42:55 -080041 const float geomFactor = 64;
ztenghui55bfb4e2013-12-03 10:38:55 -080042
Chris Craik2507c342015-05-04 14:36:49 -070043 if (CC_UNLIKELY(Properties::overrideAmbientRatio > 0.0f)) {
44 heightFactor *= Properties::overrideAmbientRatio;
Chris Craikf5be3ca2014-04-30 18:20:03 -070045 }
46
ztenghuiaf6f7ed2014-03-18 17:25:49 -070047 Rect ambientShadowBounds(casterBounds);
48 ambientShadowBounds.outset(maxZ * geomFactor * heightFactor);
49
50 if (!localClip.intersects(ambientShadowBounds)) {
51#if DEBUG_SHADOW
52 ALOGD("Ambient shadow is out of clip rect!");
53#endif
Chris Craik05f3d6e2014-06-02 16:27:04 -070054 return;
ztenghuiaf6f7ed2014-03-18 17:25:49 -070055 }
56
Chris Craik05f3d6e2014-06-02 16:27:04 -070057 AmbientShadow::createAmbientShadow(isCasterOpaque, casterPolygon,
ztenghui50ecf842014-03-11 16:52:30 -070058 casterVertexCount, centroid3d, heightFactor, geomFactor,
59 shadowVertexBuffer);
ztenghui55bfb4e2013-12-03 10:38:55 -080060}
61
Chris Craik05f3d6e2014-06-02 16:27:04 -070062void ShadowTessellator::tessellateSpotShadow(bool isCasterOpaque,
ztenghuic50a03d2014-08-21 13:47:54 -070063 const Vector3* casterPolygon, int casterVertexCount, const Vector3& casterCentroid,
Chris Craik797b95b22014-05-20 18:10:25 -070064 const mat4& receiverTransform, const Vector3& lightCenter, int lightRadius,
65 const Rect& casterBounds, const Rect& localClip, VertexBuffer& shadowVertexBuffer) {
Chris Craik87f9df82014-03-07 14:34:42 -080066 ATRACE_CALL();
67
Chris Craik797b95b22014-05-20 18:10:25 -070068 Vector3 adjustedLightCenter(lightCenter);
Chris Craik2507c342015-05-04 14:36:49 -070069 if (CC_UNLIKELY(Properties::overrideLightPosY > 0)) {
70 adjustedLightCenter.y = - Properties::overrideLightPosY; // negated since this shifts up
Chris Craikf5be3ca2014-04-30 18:20:03 -070071 }
Chris Craik2507c342015-05-04 14:36:49 -070072 if (CC_UNLIKELY(Properties::overrideLightPosZ > 0)) {
73 adjustedLightCenter.z = Properties::overrideLightPosZ;
Chris Craikf5be3ca2014-04-30 18:20:03 -070074 }
75
ztenghui7b4516e2014-01-07 10:42:55 -080076#if DEBUG_SHADOW
Chris Craik797b95b22014-05-20 18:10:25 -070077 ALOGD("light center %f %f %f",
78 adjustedLightCenter.x, adjustedLightCenter.y, adjustedLightCenter.z);
ztenghui7b4516e2014-01-07 10:42:55 -080079#endif
Chris Craik3197cde2014-01-16 14:03:39 -080080
81 // light position (because it's in local space) needs to compensate for receiver transform
82 // TODO: should apply to light orientation, not just position
83 Matrix4 reverseReceiverTransform;
84 reverseReceiverTransform.loadInverse(receiverTransform);
Chris Craik797b95b22014-05-20 18:10:25 -070085 reverseReceiverTransform.mapPoint3d(adjustedLightCenter);
Chris Craik3197cde2014-01-16 14:03:39 -080086
Chris Craik2507c342015-05-04 14:36:49 -070087 if (CC_UNLIKELY(Properties::overrideLightRadius > 0)) {
88 lightRadius = Properties::overrideLightRadius;
Chris Craikf5be3ca2014-04-30 18:20:03 -070089 }
90
ztenghuiaf6f7ed2014-03-18 17:25:49 -070091 // Now light and caster are both in local space, we will check whether
92 // the shadow is within the clip area.
Chris Craik797b95b22014-05-20 18:10:25 -070093 Rect lightRect = Rect(adjustedLightCenter.x - lightRadius, adjustedLightCenter.y - lightRadius,
94 adjustedLightCenter.x + lightRadius, adjustedLightCenter.y + lightRadius);
ztenghuiaf6f7ed2014-03-18 17:25:49 -070095 lightRect.unionWith(localClip);
96 if (!lightRect.intersects(casterBounds)) {
97#if DEBUG_SHADOW
98 ALOGD("Spot shadow is out of clip rect!");
99#endif
Chris Craik05f3d6e2014-06-02 16:27:04 -0700100 return;
ztenghuiaf6f7ed2014-03-18 17:25:49 -0700101 }
102
ztenghuic50a03d2014-08-21 13:47:54 -0700103 SpotShadow::createSpotShadow(isCasterOpaque, adjustedLightCenter, lightRadius,
104 casterPolygon, casterVertexCount, casterCentroid, shadowVertexBuffer);
105
ztenghui50ecf842014-03-11 16:52:30 -0700106#if DEBUG_SHADOW
107 if(shadowVertexBuffer.getVertexCount() <= 0) {
108 ALOGD("Spot shadow generation failed %d", shadowVertexBuffer.getVertexCount());
109 }
110#endif
ztenghui7b4516e2014-01-07 10:42:55 -0800111}
ztenghui63d41ab2014-02-14 13:13:41 -0800112
ztenghui63d41ab2014-02-14 13:13:41 -0800113/**
114 * Calculate the centroid of a 2d polygon.
115 *
116 * @param poly The polygon, which is represented in a Vector2 array.
117 * @param polyLength The length of the polygon in terms of number of vertices.
118 * @return the centroid of the polygon.
119 */
120Vector2 ShadowTessellator::centroid2d(const Vector2* poly, int polyLength) {
121 double sumx = 0;
122 double sumy = 0;
123 int p1 = polyLength - 1;
124 double area = 0;
125 for (int p2 = 0; p2 < polyLength; p2++) {
126 double x1 = poly[p1].x;
127 double y1 = poly[p1].y;
128 double x2 = poly[p2].x;
129 double y2 = poly[p2].y;
130 double a = (x1 * y2 - x2 * y1);
131 sumx += (x1 + x2) * a;
132 sumy += (y1 + y2) * a;
133 area += a;
134 p1 = p2;
135 }
136
137 Vector2 centroid = poly[0];
138 if (area != 0) {
John Reck1aa5d2d2014-07-24 13:38:28 -0700139 centroid = (Vector2){static_cast<float>(sumx / (3 * area)),
140 static_cast<float>(sumy / (3 * area))};
ztenghui63d41ab2014-02-14 13:13:41 -0800141 } else {
ztenghui50ecf842014-03-11 16:52:30 -0700142 ALOGW("Area is 0 while computing centroid!");
ztenghui63d41ab2014-02-14 13:13:41 -0800143 }
144 return centroid;
145}
146
ztenghuic50a03d2014-08-21 13:47:54 -0700147// Make sure p1 -> p2 is going CW around the poly.
148Vector2 ShadowTessellator::calculateNormal(const Vector2& p1, const Vector2& p2) {
149 Vector2 result = p2 - p1;
150 if (result.x != 0 || result.y != 0) {
151 result.normalize();
152 // Calculate the normal , which is CCW 90 rotate to the delta.
153 float tempy = result.y;
154 result.y = result.x;
155 result.x = -tempy;
156 }
157 return result;
158}
ztenghui2e023f32014-04-28 16:43:13 -0700159
ztenghui512e6432014-09-10 13:08:20 -0700160int ShadowTessellator::getExtraVertexNumber(const Vector2& vector1,
161 const Vector2& vector2, float divisor) {
162 // When there is no distance difference, there is no need for extra vertices.
163 if (vector1.lengthSquared() == 0 || vector2.lengthSquared() == 0) {
164 return 0;
165 }
166 // The formula is :
167 // extraNumber = floor(acos(dot(n1, n2)) / (M_PI / EXTRA_VERTEX_PER_PI))
168 // The value ranges for each step are:
169 // dot( ) --- [-1, 1]
170 // acos( ) --- [0, M_PI]
171 // floor(...) --- [0, EXTRA_VERTEX_PER_PI]
172 float dotProduct = vector1.dot(vector2);
Lazar Trsic12407522015-06-23 11:46:49 +0200173 // make sure that dotProduct value is in acsof input range [-1, 1]
174 dotProduct = MathUtils::clamp(dotProduct, -1.0f, 1.0f);
ztenghui512e6432014-09-10 13:08:20 -0700175 // TODO: Use look up table for the dotProduct to extraVerticesNumber
176 // computation, if needed.
177 float angle = acosf(dotProduct);
178 return (int) floor(angle / divisor);
179}
180
181void ShadowTessellator::checkOverflow(int used, int total, const char* bufferName) {
182 LOG_ALWAYS_FATAL_IF(used > total, "Error: %s overflow!!! used %d, total %d",
183 bufferName, used, total);
184}
185
ztenghui55bfb4e2013-12-03 10:38:55 -0800186}; // namespace uirenderer
187}; // namespace android