ShadowTessellator.cpp revision 3197cded4e265bc99dc82d695bbb7163fe134ed4
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
34// TODO: Support path as the input of the polygon instead of the rect's width
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 */
46void ShadowTessellator::generateCasterPolygon(float width, float height,
47        const mat4& casterTransform, int vertexCount, Vector3* polygon) {
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.
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;
70}
71
72void 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);
78
79    // A bunch of parameters to tweak the shadow.
80    // TODO: Allow some of these changable by debug settings or APIs.
81    const int rays = 128;
82    const int layers = 2;
83    const float strength = 0.5;
84    const float heightFactor = 128;
85    const float geomFactor = 64;
86
87    AmbientShadow::createAmbientShadow(polygon, vertexCount, rays, layers, strength,
88            heightFactor, geomFactor, shadowVertexBuffer);
89
90}
91
92void ShadowTessellator::tessellateSpotShadow(float width, float height,
93        const mat4& receiverTransform, 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
110    // light position (because it's in local space) needs to compensate for receiver transform
111    // TODO: should apply to light orientation, not just position
112    Matrix4 reverseReceiverTransform;
113    reverseReceiverTransform.loadInverse(receiverTransform);
114    reverseReceiverTransform.mapPoint3d(lightCenter);
115
116    const float lightSize = maximal / 8;
117    const int lightVertexCount = 16;
118
119    SpotShadow::createSpotShadow(polygon, vertexCount, lightCenter, lightSize,
120            lightVertexCount, rays, layers, strength, shadowVertexBuffer);
121
122}
123}; // namespace uirenderer
124}; // namespace android
125