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 | |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 34 | void ShadowTessellator::tessellateAmbientShadow(const Vector3* casterPolygon, int casterVertexCount, |
| 35 | VertexBuffer& shadowVertexBuffer) { |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 36 | // A bunch of parameters to tweak the shadow. |
| 37 | // TODO: Allow some of these changable by debug settings or APIs. |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 38 | const int rays = 128; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 39 | const int layers = 2; |
| 40 | const float strength = 0.5; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 41 | const float heightFactor = 128; |
| 42 | const float geomFactor = 64; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 43 | |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 44 | AmbientShadow::createAmbientShadow(casterPolygon, casterVertexCount, rays, layers, strength, |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 45 | heightFactor, geomFactor, shadowVertexBuffer); |
| 46 | |
| 47 | } |
| 48 | |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 49 | void ShadowTessellator::tessellateSpotShadow(const Vector3* casterPolygon, int casterVertexCount, |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame] | 50 | const Vector3& lightPosScale, const mat4& receiverTransform, |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 51 | int screenWidth, int screenHeight, VertexBuffer& shadowVertexBuffer) { |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 52 | // A bunch of parameters to tweak the shadow. |
| 53 | // TODO: Allow some of these changable by debug settings or APIs. |
| 54 | const int rays = 256; |
| 55 | const int layers = 2; |
| 56 | const float strength = 0.5; |
| 57 | int maximal = max(screenWidth, screenHeight); |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame] | 58 | Vector3 lightCenter(screenWidth * lightPosScale.x, screenHeight * lightPosScale.y, |
| 59 | maximal * lightPosScale.z); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 60 | #if DEBUG_SHADOW |
| 61 | ALOGD("light center %f %f %f", lightCenter.x, lightCenter.y, lightCenter.z); |
| 62 | #endif |
Chris Craik | 3197cde | 2014-01-16 14:03:39 -0800 | [diff] [blame] | 63 | |
| 64 | // light position (because it's in local space) needs to compensate for receiver transform |
| 65 | // TODO: should apply to light orientation, not just position |
| 66 | Matrix4 reverseReceiverTransform; |
| 67 | reverseReceiverTransform.loadInverse(receiverTransform); |
| 68 | reverseReceiverTransform.mapPoint3d(lightCenter); |
| 69 | |
ztenghui | cc3c256 | 2014-01-17 10:34:10 -0800 | [diff] [blame] | 70 | const float lightSize = maximal / 4; |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 71 | const int lightVertexCount = 16; |
| 72 | |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 73 | SpotShadow::createSpotShadow(casterPolygon, casterVertexCount, lightCenter, lightSize, |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 74 | lightVertexCount, rays, layers, strength, shadowVertexBuffer); |
| 75 | |
| 76 | } |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 77 | }; // namespace uirenderer |
| 78 | }; // namespace android |