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) { |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 48 | |
| 49 | Vector3 pivot(width / 2, height / 2, 0.0f); |
| 50 | casterTransform.mapPoint3d(pivot); |
| 51 | |
| 52 | // TODO: The zScaleFactor need to be mapped to the screen. |
| 53 | float zScaleFactor = 0.5; |
| 54 | Rect blockRect(pivot.x - width * zScaleFactor, pivot.y - height * zScaleFactor, |
| 55 | pivot.x + width * zScaleFactor, pivot.y + height * zScaleFactor); |
| 56 | |
| 57 | // Generate the caster's polygon from the rect. |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 58 | polygon[0].x = blockRect.left; |
| 59 | polygon[0].y = blockRect.top; |
| 60 | polygon[0].z = pivot.z; |
| 61 | polygon[1].x = blockRect.right; |
| 62 | polygon[1].y = blockRect.top; |
| 63 | polygon[1].z = pivot.z; |
| 64 | polygon[2].x = blockRect.right; |
| 65 | polygon[2].y = blockRect.bottom; |
| 66 | polygon[2].z = pivot.z; |
| 67 | polygon[3].x = blockRect.left; |
| 68 | polygon[3].y = blockRect.bottom; |
| 69 | polygon[3].z = pivot.z; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void ShadowTessellator::tessellateAmbientShadow(float width, float height, |
| 73 | const mat4& casterTransform, VertexBuffer& shadowVertexBuffer) { |
| 74 | |
| 75 | const int vertexCount = 4; |
| 76 | Vector3 polygon[vertexCount]; |
| 77 | generateCasterPolygon(width, height, casterTransform, vertexCount, polygon); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 78 | |
| 79 | // A bunch of parameters to tweak the shadow. |
| 80 | // TODO: Allow some of these changable by debug settings or APIs. |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 81 | const int rays = 128; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 82 | const int layers = 2; |
| 83 | const float strength = 0.5; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 84 | const float heightFactor = 128; |
| 85 | const float geomFactor = 64; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 86 | |
| 87 | AmbientShadow::createAmbientShadow(polygon, vertexCount, rays, layers, strength, |
| 88 | heightFactor, geomFactor, shadowVertexBuffer); |
| 89 | |
| 90 | } |
| 91 | |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 92 | void ShadowTessellator::tessellateSpotShadow(float width, float height, |
| 93 | int screenWidth, int screenHeight, |
| 94 | const mat4& casterTransform, VertexBuffer& shadowVertexBuffer) { |
| 95 | const int vertexCount = 4; |
| 96 | Vector3 polygon[vertexCount]; |
| 97 | generateCasterPolygon(width, height, casterTransform, vertexCount, polygon); |
| 98 | |
| 99 | // A bunch of parameters to tweak the shadow. |
| 100 | // TODO: Allow some of these changable by debug settings or APIs. |
| 101 | const int rays = 256; |
| 102 | const int layers = 2; |
| 103 | const float strength = 0.5; |
| 104 | int maximal = max(screenWidth, screenHeight); |
| 105 | Vector3 lightCenter(screenWidth / 2, 0, maximal); |
| 106 | #if DEBUG_SHADOW |
| 107 | ALOGD("light center %f %f %f", lightCenter.x, lightCenter.y, lightCenter.z); |
| 108 | #endif |
| 109 | const float lightSize = maximal / 8; |
| 110 | const int lightVertexCount = 16; |
| 111 | |
| 112 | SpotShadow::createSpotShadow(polygon, vertexCount, lightCenter, lightSize, |
| 113 | lightVertexCount, rays, layers, strength, shadowVertexBuffer); |
| 114 | |
| 115 | } |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 116 | }; // namespace uirenderer |
| 117 | }; // namespace android |