2013-12-03 10:38:55 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "OpenGLRenderer"
|
2014-03-07 14:34:42 -08:00
|
|
|
#define ATRACE_TAG ATRACE_TAG_VIEW
|
2013-12-03 10:38:55 -08:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <utils/Log.h>
|
2014-03-07 14:34:42 -08:00
|
|
|
#include <utils/Trace.h>
|
2013-12-03 10:38:55 -08:00
|
|
|
|
|
|
|
#include "AmbientShadow.h"
|
|
|
|
#include "ShadowTessellator.h"
|
2014-01-07 10:42:55 -08:00
|
|
|
#include "SpotShadow.h"
|
2013-12-03 10:38:55 -08:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
2014-01-07 10:42:55 -08:00
|
|
|
template<typename T>
|
|
|
|
static inline T max(T a, T b) {
|
|
|
|
return a > b ? a : b;
|
|
|
|
}
|
|
|
|
|
2014-03-11 16:52:30 -07:00
|
|
|
VertexBufferMode ShadowTessellator::tessellateAmbientShadow(bool isCasterOpaque,
|
|
|
|
const Vector3* casterPolygon, int casterVertexCount,
|
|
|
|
const Vector3& centroid3d, VertexBuffer& shadowVertexBuffer) {
|
2014-03-07 14:34:42 -08:00
|
|
|
ATRACE_CALL();
|
|
|
|
|
2013-12-03 10:38:55 -08:00
|
|
|
// A bunch of parameters to tweak the shadow.
|
|
|
|
// TODO: Allow some of these changable by debug settings or APIs.
|
2014-03-07 18:27:49 -08:00
|
|
|
const float heightFactor = 1.0f / 128;
|
2014-01-07 10:42:55 -08:00
|
|
|
const float geomFactor = 64;
|
2013-12-03 10:38:55 -08:00
|
|
|
|
2014-03-11 16:52:30 -07:00
|
|
|
return AmbientShadow::createAmbientShadow(isCasterOpaque, casterPolygon,
|
|
|
|
casterVertexCount, centroid3d, heightFactor, geomFactor,
|
|
|
|
shadowVertexBuffer);
|
2013-12-03 10:38:55 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-11 16:52:30 -07:00
|
|
|
VertexBufferMode ShadowTessellator::tessellateSpotShadow(bool isCasterOpaque,
|
|
|
|
const Vector3* casterPolygon, int casterVertexCount,
|
2014-01-17 10:34:10 -08:00
|
|
|
const Vector3& lightPosScale, const mat4& receiverTransform,
|
2014-01-26 13:43:53 -08:00
|
|
|
int screenWidth, int screenHeight, VertexBuffer& shadowVertexBuffer) {
|
2014-03-07 14:34:42 -08:00
|
|
|
ATRACE_CALL();
|
|
|
|
|
2014-01-07 10:42:55 -08:00
|
|
|
// A bunch of parameters to tweak the shadow.
|
|
|
|
// TODO: Allow some of these changable by debug settings or APIs.
|
|
|
|
int maximal = max(screenWidth, screenHeight);
|
2014-01-17 10:34:10 -08:00
|
|
|
Vector3 lightCenter(screenWidth * lightPosScale.x, screenHeight * lightPosScale.y,
|
|
|
|
maximal * lightPosScale.z);
|
2014-01-07 10:42:55 -08:00
|
|
|
#if DEBUG_SHADOW
|
|
|
|
ALOGD("light center %f %f %f", lightCenter.x, lightCenter.y, lightCenter.z);
|
|
|
|
#endif
|
2014-01-16 14:03:39 -08:00
|
|
|
|
|
|
|
// light position (because it's in local space) needs to compensate for receiver transform
|
|
|
|
// TODO: should apply to light orientation, not just position
|
|
|
|
Matrix4 reverseReceiverTransform;
|
|
|
|
reverseReceiverTransform.loadInverse(receiverTransform);
|
|
|
|
reverseReceiverTransform.mapPoint3d(lightCenter);
|
|
|
|
|
2014-01-17 10:34:10 -08:00
|
|
|
const float lightSize = maximal / 4;
|
2014-03-07 18:27:49 -08:00
|
|
|
const int lightVertexCount = 8;
|
2014-01-07 10:42:55 -08:00
|
|
|
|
2014-03-11 16:52:30 -07:00
|
|
|
VertexBufferMode mode = SpotShadow::createSpotShadow(isCasterOpaque,
|
|
|
|
casterPolygon, casterVertexCount, lightCenter, lightSize,
|
|
|
|
lightVertexCount, shadowVertexBuffer);
|
2014-01-07 10:42:55 -08:00
|
|
|
|
2014-03-11 16:52:30 -07:00
|
|
|
#if DEBUG_SHADOW
|
|
|
|
if(shadowVertexBuffer.getVertexCount() <= 0) {
|
|
|
|
ALOGD("Spot shadow generation failed %d", shadowVertexBuffer.getVertexCount());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return mode;
|
2014-01-07 10:42:55 -08:00
|
|
|
}
|
2014-02-14 13:13:41 -08:00
|
|
|
|
|
|
|
void ShadowTessellator::generateShadowIndices(uint16_t* shadowIndices) {
|
|
|
|
int currentIndex = 0;
|
|
|
|
const int rays = SHADOW_RAY_COUNT;
|
|
|
|
// For the penumbra area.
|
2014-03-11 16:52:30 -07:00
|
|
|
for (int layer = 0; layer < 2; layer ++) {
|
|
|
|
int baseIndex = layer * rays;
|
|
|
|
for (int i = 0; i < rays; i++) {
|
|
|
|
shadowIndices[currentIndex++] = i + baseIndex;
|
|
|
|
shadowIndices[currentIndex++] = rays + i + baseIndex;
|
|
|
|
}
|
|
|
|
// To close the loop, back to the ray 0.
|
|
|
|
shadowIndices[currentIndex++] = 0 + baseIndex;
|
|
|
|
// Note this is the same as the first index of next layer loop.
|
|
|
|
shadowIndices[currentIndex++] = rays + baseIndex;
|
2014-02-14 13:13:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_SHADOW
|
2014-03-11 16:52:30 -07:00
|
|
|
if (currentIndex != MAX_SHADOW_INDEX_COUNT) {
|
|
|
|
ALOGW("vertex index count is wrong. current %d, expected %d",
|
|
|
|
currentIndex, MAX_SHADOW_INDEX_COUNT);
|
2014-02-14 13:13:41 -08:00
|
|
|
}
|
2014-03-11 16:52:30 -07:00
|
|
|
for (int i = 0; i < MAX_SHADOW_INDEX_COUNT; i++) {
|
2014-02-14 13:13:41 -08:00
|
|
|
ALOGD("vertex index is (%d, %d)", i, shadowIndices[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the centroid of a 2d polygon.
|
|
|
|
*
|
|
|
|
* @param poly The polygon, which is represented in a Vector2 array.
|
|
|
|
* @param polyLength The length of the polygon in terms of number of vertices.
|
|
|
|
* @return the centroid of the polygon.
|
|
|
|
*/
|
|
|
|
Vector2 ShadowTessellator::centroid2d(const Vector2* poly, int polyLength) {
|
|
|
|
double sumx = 0;
|
|
|
|
double sumy = 0;
|
|
|
|
int p1 = polyLength - 1;
|
|
|
|
double area = 0;
|
|
|
|
for (int p2 = 0; p2 < polyLength; p2++) {
|
|
|
|
double x1 = poly[p1].x;
|
|
|
|
double y1 = poly[p1].y;
|
|
|
|
double x2 = poly[p2].x;
|
|
|
|
double y2 = poly[p2].y;
|
|
|
|
double a = (x1 * y2 - x2 * y1);
|
|
|
|
sumx += (x1 + x2) * a;
|
|
|
|
sumy += (y1 + y2) * a;
|
|
|
|
area += a;
|
|
|
|
p1 = p2;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 centroid = poly[0];
|
|
|
|
if (area != 0) {
|
|
|
|
centroid = Vector2(sumx / (3 * area), sumy / (3 * area));
|
|
|
|
} else {
|
2014-03-11 16:52:30 -07:00
|
|
|
ALOGW("Area is 0 while computing centroid!");
|
2014-02-14 13:13:41 -08:00
|
|
|
}
|
|
|
|
return centroid;
|
|
|
|
}
|
|
|
|
|
2013-12-03 10:38:55 -08:00
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|