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" |
| 18 | |
| 19 | #include <math.h> |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include "AmbientShadow.h" |
| 23 | #include "ShadowTessellator.h" |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 24 | #include "SpotShadow.h" |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 29 | template<typename T> |
| 30 | static inline T max(T a, T b) { |
| 31 | return a > b ? a : b; |
| 32 | } |
| 33 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 34 | // TODO: Support path as the input of the polygon instead of the rect's width |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 35 | // and height. And the z values need to be computed according to the |
| 36 | // transformation for each vertex. |
| 37 | /** |
| 38 | * Generate the polygon for the caster. |
| 39 | * |
| 40 | * @param width the width of the caster |
| 41 | * @param height the height of the caster |
| 42 | * @param casterTransform transformation info of the caster |
| 43 | * @param polygon return the caster's polygon |
| 44 | * |
| 45 | */ |
| 46 | void ShadowTessellator::generateCasterPolygon(float width, float height, |
| 47 | const mat4& casterTransform, int vertexCount, Vector3* polygon) { |
Chris Craik | 7b3dfa4 | 2014-01-16 16:21:10 -0800 | [diff] [blame] | 48 | Rect blockRect(0, 0, width, height); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 49 | polygon[0].x = blockRect.left; |
| 50 | polygon[0].y = blockRect.top; |
Chris Craik | 7b3dfa4 | 2014-01-16 16:21:10 -0800 | [diff] [blame] | 51 | polygon[0].z = 0; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 52 | polygon[1].x = blockRect.right; |
| 53 | polygon[1].y = blockRect.top; |
Chris Craik | 7b3dfa4 | 2014-01-16 16:21:10 -0800 | [diff] [blame] | 54 | polygon[1].z = 0; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 55 | polygon[2].x = blockRect.right; |
| 56 | polygon[2].y = blockRect.bottom; |
Chris Craik | 7b3dfa4 | 2014-01-16 16:21:10 -0800 | [diff] [blame] | 57 | polygon[2].z = 0; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 58 | polygon[3].x = blockRect.left; |
| 59 | polygon[3].y = blockRect.bottom; |
Chris Craik | 7b3dfa4 | 2014-01-16 16:21:10 -0800 | [diff] [blame] | 60 | polygon[3].z = 0; |
| 61 | casterTransform.mapPoint3d(polygon[0]); |
| 62 | casterTransform.mapPoint3d(polygon[1]); |
| 63 | casterTransform.mapPoint3d(polygon[2]); |
| 64 | casterTransform.mapPoint3d(polygon[3]); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void ShadowTessellator::tessellateAmbientShadow(float width, float height, |
| 68 | const mat4& casterTransform, VertexBuffer& shadowVertexBuffer) { |
| 69 | |
| 70 | const int vertexCount = 4; |
| 71 | Vector3 polygon[vertexCount]; |
| 72 | generateCasterPolygon(width, height, casterTransform, vertexCount, polygon); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 73 | |
| 74 | // A bunch of parameters to tweak the shadow. |
| 75 | // TODO: Allow some of these changable by debug settings or APIs. |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 76 | const int rays = 128; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 77 | const int layers = 2; |
| 78 | const float strength = 0.5; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 79 | const float heightFactor = 128; |
| 80 | const float geomFactor = 64; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 81 | |
| 82 | AmbientShadow::createAmbientShadow(polygon, vertexCount, rays, layers, strength, |
| 83 | heightFactor, geomFactor, shadowVertexBuffer); |
| 84 | |
| 85 | } |
| 86 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 87 | void ShadowTessellator::tessellateSpotShadow(float width, float height, |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame^] | 88 | const Vector3& lightPosScale, const mat4& receiverTransform, |
| 89 | int screenWidth, int screenHeight, const mat4& casterTransform, |
| 90 | VertexBuffer& shadowVertexBuffer) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 91 | const int vertexCount = 4; |
| 92 | Vector3 polygon[vertexCount]; |
| 93 | generateCasterPolygon(width, height, casterTransform, vertexCount, polygon); |
| 94 | |
| 95 | // A bunch of parameters to tweak the shadow. |
| 96 | // TODO: Allow some of these changable by debug settings or APIs. |
| 97 | const int rays = 256; |
| 98 | const int layers = 2; |
| 99 | const float strength = 0.5; |
| 100 | int maximal = max(screenWidth, screenHeight); |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame^] | 101 | Vector3 lightCenter(screenWidth * lightPosScale.x, screenHeight * lightPosScale.y, |
| 102 | maximal * lightPosScale.z); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 103 | #if DEBUG_SHADOW |
| 104 | ALOGD("light center %f %f %f", lightCenter.x, lightCenter.y, lightCenter.z); |
| 105 | #endif |
Chris Craik | 3197cde | 2014-01-16 14:03:39 -0800 | [diff] [blame] | 106 | |
| 107 | // light position (because it's in local space) needs to compensate for receiver transform |
| 108 | // TODO: should apply to light orientation, not just position |
| 109 | Matrix4 reverseReceiverTransform; |
| 110 | reverseReceiverTransform.loadInverse(receiverTransform); |
| 111 | reverseReceiverTransform.mapPoint3d(lightCenter); |
| 112 | |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame^] | 113 | const float lightSize = maximal / 4; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 114 | const int lightVertexCount = 16; |
| 115 | |
| 116 | SpotShadow::createSpotShadow(polygon, vertexCount, lightCenter, lightSize, |
| 117 | lightVertexCount, rays, layers, strength, shadowVertexBuffer); |
| 118 | |
| 119 | } |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 120 | }; // namespace uirenderer |
| 121 | }; // namespace android |