ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 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" |
Chris Craik | 87f9df8 | 2014-03-07 14:34:42 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_VIEW |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 19 | |
| 20 | #include <math.h> |
| 21 | #include <utils/Log.h> |
Chris Craik | 87f9df8 | 2014-03-07 14:34:42 -0800 | [diff] [blame] | 22 | #include <utils/Trace.h> |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 23 | |
| 24 | #include "AmbientShadow.h" |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame^] | 25 | #include "Caches.h" |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 26 | #include "ShadowTessellator.h" |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 27 | #include "SpotShadow.h" |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 32 | template<typename T> |
| 33 | static inline T max(T a, T b) { |
| 34 | return a > b ? a : b; |
| 35 | } |
| 36 | |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 37 | VertexBufferMode ShadowTessellator::tessellateAmbientShadow(bool isCasterOpaque, |
| 38 | const Vector3* casterPolygon, int casterVertexCount, |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 39 | const Vector3& centroid3d, const Rect& casterBounds, |
| 40 | const Rect& localClip, float maxZ, VertexBuffer& shadowVertexBuffer) { |
Chris Craik | 87f9df8 | 2014-03-07 14:34:42 -0800 | [diff] [blame] | 41 | ATRACE_CALL(); |
| 42 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 43 | // A bunch of parameters to tweak the shadow. |
| 44 | // TODO: Allow some of these changable by debug settings or APIs. |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame^] | 45 | float heightFactor = 1.0f / 128; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 46 | const float geomFactor = 64; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 47 | |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame^] | 48 | Caches& caches = Caches::getInstance(); |
| 49 | if (CC_UNLIKELY(caches.propertyAmbientRatio > 0.0f)) { |
| 50 | heightFactor *= caches.propertyAmbientRatio; |
| 51 | } |
| 52 | |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 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 | |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 63 | return AmbientShadow::createAmbientShadow(isCasterOpaque, casterPolygon, |
| 64 | casterVertexCount, centroid3d, heightFactor, geomFactor, |
| 65 | shadowVertexBuffer); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 66 | |
| 67 | } |
| 68 | |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 69 | VertexBufferMode ShadowTessellator::tessellateSpotShadow(bool isCasterOpaque, |
| 70 | const Vector3* casterPolygon, int casterVertexCount, |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame^] | 71 | const mat4& receiverTransform, |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 72 | int screenWidth, int screenHeight, const Rect& casterBounds, |
| 73 | const Rect& localClip, VertexBuffer& shadowVertexBuffer) { |
Chris Craik | 87f9df8 | 2014-03-07 14:34:42 -0800 | [diff] [blame] | 74 | ATRACE_CALL(); |
| 75 | |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame^] | 76 | Caches& caches = Caches::getInstance(); |
| 77 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 78 | // A bunch of parameters to tweak the shadow. |
| 79 | // TODO: Allow some of these changable by debug settings or APIs. |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 80 | int maximal = max(screenWidth, screenHeight); |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame^] | 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 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 91 | #if DEBUG_SHADOW |
| 92 | ALOGD("light center %f %f %f", lightCenter.x, lightCenter.y, lightCenter.z); |
| 93 | #endif |
Chris Craik | 3197cde | 2014-01-16 14:03:39 -0800 | [diff] [blame] | 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 | |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame^] | 101 | float lightSize = maximal / 4; |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 102 | const int lightVertexCount = 8; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 103 | |
Chris Craik | f5be3ca | 2014-04-30 18:20:03 -0700 | [diff] [blame^] | 104 | if (CC_UNLIKELY(caches.propertyLightDiameter > 0)) { |
| 105 | lightSize = caches.propertyLightDiameter; |
| 106 | } |
| 107 | |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 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 | |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 120 | VertexBufferMode mode = SpotShadow::createSpotShadow(isCasterOpaque, |
| 121 | casterPolygon, casterVertexCount, lightCenter, lightSize, |
| 122 | lightVertexCount, shadowVertexBuffer); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 123 | |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 124 | #if DEBUG_SHADOW |
| 125 | if(shadowVertexBuffer.getVertexCount() <= 0) { |
| 126 | ALOGD("Spot shadow generation failed %d", shadowVertexBuffer.getVertexCount()); |
| 127 | } |
| 128 | #endif |
| 129 | return mode; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 130 | } |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 131 | |
| 132 | void ShadowTessellator::generateShadowIndices(uint16_t* shadowIndices) { |
| 133 | int currentIndex = 0; |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 134 | const int rays = SHADOW_RAY_COUNT; |
| 135 | // For the penumbra area. |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 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; |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 146 | } |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 147 | |
| 148 | #if DEBUG_SHADOW |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 149 | if (currentIndex != MAX_SHADOW_INDEX_COUNT) { |
| 150 | ALOGW("vertex index count is wrong. current %d, expected %d", |
| 151 | currentIndex, MAX_SHADOW_INDEX_COUNT); |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 152 | } |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 153 | for (int i = 0; i < MAX_SHADOW_INDEX_COUNT; i++) { |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 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 | */ |
| 166 | Vector2 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 { |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 187 | ALOGW("Area is 0 while computing centroid!"); |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 188 | } |
| 189 | return centroid; |
| 190 | } |
| 191 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 192 | }; // namespace uirenderer |
| 193 | }; // namespace android |