ShadowTessellator.cpp revision 15a07a21eb33e8ca1c7444944fe0541a53380c0c
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"
24#include "SpotShadow.h"
25
26namespace android {
27namespace uirenderer {
28
29template<typename T>
30static inline T max(T a, T b) {
31    return a > b ? a : b;
32}
33
34void ShadowTessellator::tessellateAmbientShadow(const Vector3* casterPolygon, int casterVertexCount,
35        VertexBuffer& shadowVertexBuffer) {
36    // A bunch of parameters to tweak the shadow.
37    // TODO: Allow some of these changable by debug settings or APIs.
38    const int rays = 128;
39    const int layers = 2;
40    const float strength = 0.5;
41    const float heightFactor = 128;
42    const float geomFactor = 64;
43
44    AmbientShadow::createAmbientShadow(casterPolygon, casterVertexCount, rays, layers, strength,
45            heightFactor, geomFactor, shadowVertexBuffer);
46
47}
48
49void ShadowTessellator::tessellateSpotShadow(const Vector3* casterPolygon, int casterVertexCount,
50        const Vector3& lightPosScale, const mat4& receiverTransform,
51        int screenWidth, int screenHeight, VertexBuffer& shadowVertexBuffer) {
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);
58    Vector3 lightCenter(screenWidth * lightPosScale.x, screenHeight * lightPosScale.y,
59            maximal * lightPosScale.z);
60#if DEBUG_SHADOW
61    ALOGD("light center %f %f %f", lightCenter.x, lightCenter.y, lightCenter.z);
62#endif
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
70    const float lightSize = maximal / 4;
71    const int lightVertexCount = 16;
72
73    SpotShadow::createSpotShadow(casterPolygon, casterVertexCount, lightCenter, lightSize,
74            lightVertexCount, rays, layers, strength, shadowVertexBuffer);
75
76}
77}; // namespace uirenderer
78}; // namespace android
79