ShadowTessellator.cpp revision 8191effc83b3ec4cfc8647a6eb16ee641fa85a2f
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 Rect blockRect(0, 0, width, height); 49 polygon[0].x = blockRect.left; 50 polygon[0].y = blockRect.top; 51 polygon[0].z = 0; 52 polygon[1].x = blockRect.right; 53 polygon[1].y = blockRect.top; 54 polygon[1].z = 0; 55 polygon[2].x = blockRect.right; 56 polygon[2].y = blockRect.bottom; 57 polygon[2].z = 0; 58 polygon[3].x = blockRect.left; 59 polygon[3].y = blockRect.bottom; 60 polygon[3].z = 0; 61 casterTransform.mapPoint3d(polygon[0]); 62 casterTransform.mapPoint3d(polygon[1]); 63 casterTransform.mapPoint3d(polygon[2]); 64 casterTransform.mapPoint3d(polygon[3]); 65} 66 67void ShadowTessellator::tessellateAmbientShadow(float width, float height, 68 const mat4& casterTransform, VertexBuffer& shadowVertexBuffer) { 69 70 const int vertexCount = 4; 71 Vector3 polygon[vertexCount]; 72 generateCasterPolygon(width, height, casterTransform, vertexCount, polygon); 73 74 // A bunch of parameters to tweak the shadow. 75 // TODO: Allow some of these changable by debug settings or APIs. 76 const int rays = 128; 77 const int layers = 2; 78 const float strength = 0.5; 79 const float heightFactor = 128; 80 const float geomFactor = 64; 81 82 AmbientShadow::createAmbientShadow(polygon, vertexCount, rays, layers, strength, 83 heightFactor, geomFactor, shadowVertexBuffer); 84 85} 86 87void ShadowTessellator::tessellateSpotShadow(float width, float height, 88 const mat4& receiverTransform, int screenWidth, int screenHeight, 89 const mat4& casterTransform, VertexBuffer& shadowVertexBuffer) { 90 const int vertexCount = 4; 91 Vector3 polygon[vertexCount]; 92 generateCasterPolygon(width, height, casterTransform, vertexCount, polygon); 93 94 // A bunch of parameters to tweak the shadow. 95 // TODO: Allow some of these changable by debug settings or APIs. 96 const int rays = 256; 97 const int layers = 2; 98 const float strength = 0.5; 99 int maximal = max(screenWidth, screenHeight); 100 Vector3 lightCenter(screenWidth / 2, 0, maximal); 101#if DEBUG_SHADOW 102 ALOGD("light center %f %f %f", lightCenter.x, lightCenter.y, lightCenter.z); 103#endif 104 105 // light position (because it's in local space) needs to compensate for receiver transform 106 // TODO: should apply to light orientation, not just position 107 Matrix4 reverseReceiverTransform; 108 reverseReceiverTransform.loadInverse(receiverTransform); 109 reverseReceiverTransform.mapPoint3d(lightCenter); 110 111 const float lightSize = maximal / 8; 112 const int lightVertexCount = 16; 113 114 SpotShadow::createSpotShadow(polygon, vertexCount, lightCenter, lightSize, 115 lightVertexCount, rays, layers, strength, shadowVertexBuffer); 116 117} 118}; // namespace uirenderer 119}; // namespace android 120