blob: 6385ef79d0ea1f836216148c2a77bf1797176b7c [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
ztenghui55bfb4e2013-12-03 10:38:55 -080034// TODO: Support path as the input of the polygon instead of the rect's width
ztenghui7b4516e2014-01-07 10:42:55 -080035// 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) {
ztenghui55bfb4e2013-12-03 10:38:55 -080048
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.
ztenghui55bfb4e2013-12-03 10:38:55 -080058 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;
ztenghui7b4516e2014-01-07 10:42:55 -080070}
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);
ztenghui55bfb4e2013-12-03 10:38:55 -080078
79 // A bunch of parameters to tweak the shadow.
80 // TODO: Allow some of these changable by debug settings or APIs.
ztenghui7b4516e2014-01-07 10:42:55 -080081 const int rays = 128;
ztenghui55bfb4e2013-12-03 10:38:55 -080082 const int layers = 2;
83 const float strength = 0.5;
ztenghui7b4516e2014-01-07 10:42:55 -080084 const float heightFactor = 128;
85 const float geomFactor = 64;
ztenghui55bfb4e2013-12-03 10:38:55 -080086
87 AmbientShadow::createAmbientShadow(polygon, vertexCount, rays, layers, strength,
88 heightFactor, geomFactor, shadowVertexBuffer);
89
90}
91
ztenghui7b4516e2014-01-07 10:42:55 -080092void 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}
ztenghui55bfb4e2013-12-03 10:38:55 -0800116}; // namespace uirenderer
117}; // namespace android