blob: 7700ea0c6ad5852172399ea9f790e59a43554499 [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
17#define LOG_TAG "OpenGLRenderer"
18
19#include <math.h>
20#include <utils/Log.h>
21
22#include "AmbientShadow.h"
23#include "ShadowTessellator.h"
ztenghui7b4516e2014-01-07 10:42:55 -080024#include "SpotShadow.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080025
26namespace android {
27namespace uirenderer {
28
ztenghui7b4516e2014-01-07 10:42:55 -080029template<typename T>
30static inline T max(T a, T b) {
31 return a > b ? a : b;
32}
33
Chris Craik15a07a22014-01-26 13:43:53 -080034void ShadowTessellator::tessellateAmbientShadow(const Vector3* casterPolygon, int casterVertexCount,
35 VertexBuffer& shadowVertexBuffer) {
ztenghui55bfb4e2013-12-03 10:38:55 -080036 // A bunch of parameters to tweak the shadow.
37 // TODO: Allow some of these changable by debug settings or APIs.
ztenghui7b4516e2014-01-07 10:42:55 -080038 const int rays = 128;
ztenghui55bfb4e2013-12-03 10:38:55 -080039 const int layers = 2;
40 const float strength = 0.5;
ztenghui7b4516e2014-01-07 10:42:55 -080041 const float heightFactor = 128;
42 const float geomFactor = 64;
ztenghui55bfb4e2013-12-03 10:38:55 -080043
Chris Craik15a07a22014-01-26 13:43:53 -080044 AmbientShadow::createAmbientShadow(casterPolygon, casterVertexCount, rays, layers, strength,
ztenghui55bfb4e2013-12-03 10:38:55 -080045 heightFactor, geomFactor, shadowVertexBuffer);
46
47}
48
Chris Craik15a07a22014-01-26 13:43:53 -080049void ShadowTessellator::tessellateSpotShadow(const Vector3* casterPolygon, int casterVertexCount,
ztenghuicc3c2562014-01-17 10:34:10 -080050 const Vector3& lightPosScale, const mat4& receiverTransform,
Chris Craik15a07a22014-01-26 13:43:53 -080051 int screenWidth, int screenHeight, VertexBuffer& shadowVertexBuffer) {
ztenghui7b4516e2014-01-07 10:42:55 -080052 // 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);
ztenghuicc3c2562014-01-17 10:34:10 -080058 Vector3 lightCenter(screenWidth * lightPosScale.x, screenHeight * lightPosScale.y,
59 maximal * lightPosScale.z);
ztenghui7b4516e2014-01-07 10:42:55 -080060#if DEBUG_SHADOW
61 ALOGD("light center %f %f %f", lightCenter.x, lightCenter.y, lightCenter.z);
62#endif
Chris Craik3197cde2014-01-16 14:03:39 -080063
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
ztenghuicc3c2562014-01-17 10:34:10 -080070 const float lightSize = maximal / 4;
ztenghui7b4516e2014-01-07 10:42:55 -080071 const int lightVertexCount = 16;
72
Chris Craik15a07a22014-01-26 13:43:53 -080073 SpotShadow::createSpotShadow(casterPolygon, casterVertexCount, lightCenter, lightSize,
ztenghui7b4516e2014-01-07 10:42:55 -080074 lightVertexCount, rays, layers, strength, shadowVertexBuffer);
75
76}
ztenghui55bfb4e2013-12-03 10:38:55 -080077}; // namespace uirenderer
78}; // namespace android