This source file includes following definitions.
- initPaintingData
- updatePaintingData
- setX
- setY
- setZ
- setPointsAtX
- setPointsAtY
- setPointsAtZ
- setSpecularExponent
- setLimitingConeAngle
- externalRepresentation
#include "config.h"
#include "platform/graphics/filters/SpotLightSource.h"
#include "platform/text/TextStream.h"
namespace WebCore {
static const float antiAliasTreshold = 0.016f;
void SpotLightSource::initPaintingData(PaintingData& paintingData) const
{
paintingData.privateColorVector = paintingData.colorVector;
paintingData.directionVector.setX(m_direction.x() - m_position.x());
paintingData.directionVector.setY(m_direction.y() - m_position.y());
paintingData.directionVector.setZ(m_direction.z() - m_position.z());
paintingData.directionVector.normalize();
if (!m_limitingConeAngle) {
paintingData.coneCutOffLimit = 0.0f;
paintingData.coneFullLight = -antiAliasTreshold;
} else {
float limitingConeAngle = m_limitingConeAngle;
if (limitingConeAngle < 0.0f)
limitingConeAngle = -limitingConeAngle;
if (limitingConeAngle > 90.0f)
limitingConeAngle = 90.0f;
paintingData.coneCutOffLimit = cosf(deg2rad(180.0f - limitingConeAngle));
paintingData.coneFullLight = paintingData.coneCutOffLimit - antiAliasTreshold;
}
}
void SpotLightSource::updatePaintingData(PaintingData& paintingData, int x, int y, float z) const
{
paintingData.lightVector.setX(m_position.x() - x);
paintingData.lightVector.setY(m_position.y() - y);
paintingData.lightVector.setZ(m_position.z() - z);
paintingData.lightVectorLength = paintingData.lightVector.length();
float cosineOfAngle = (paintingData.lightVector * paintingData.directionVector) / paintingData.lightVectorLength;
if (cosineOfAngle > paintingData.coneCutOffLimit) {
paintingData.colorVector.setX(0.0f);
paintingData.colorVector.setY(0.0f);
paintingData.colorVector.setZ(0.0f);
return;
}
float lightStrength;
if (1.0f == m_specularExponent) {
lightStrength = -cosineOfAngle;
} else {
lightStrength = powf(-cosineOfAngle, m_specularExponent);
}
if (cosineOfAngle > paintingData.coneFullLight)
lightStrength *= (paintingData.coneCutOffLimit - cosineOfAngle) / (paintingData.coneCutOffLimit - paintingData.coneFullLight);
if (lightStrength > 1.0f)
lightStrength = 1.0f;
paintingData.colorVector.setX(paintingData.privateColorVector.x() * lightStrength);
paintingData.colorVector.setY(paintingData.privateColorVector.y() * lightStrength);
paintingData.colorVector.setZ(paintingData.privateColorVector.z() * lightStrength);
}
bool SpotLightSource::setX(float x)
{
if (m_position.x() == x)
return false;
m_position.setX(x);
return true;
}
bool SpotLightSource::setY(float y)
{
if (m_position.y() == y)
return false;
m_position.setY(y);
return true;
}
bool SpotLightSource::setZ(float z)
{
if (m_position.z() == z)
return false;
m_position.setZ(z);
return true;
}
bool SpotLightSource::setPointsAtX(float pointsAtX)
{
if (m_direction.x() == pointsAtX)
return false;
m_direction.setX(pointsAtX);
return true;
}
bool SpotLightSource::setPointsAtY(float pointsAtY)
{
if (m_direction.y() == pointsAtY)
return false;
m_direction.setY(pointsAtY);
return true;
}
bool SpotLightSource::setPointsAtZ(float pointsAtZ)
{
if (m_direction.z() == pointsAtZ)
return false;
m_direction.setZ(pointsAtZ);
return true;
}
bool SpotLightSource::setSpecularExponent(float specularExponent)
{
specularExponent = std::min(std::max(specularExponent, 1.0f), 128.0f);
if (m_specularExponent == specularExponent)
return false;
m_specularExponent = specularExponent;
return true;
}
bool SpotLightSource::setLimitingConeAngle(float limitingConeAngle)
{
if (m_limitingConeAngle == limitingConeAngle)
return false;
m_limitingConeAngle = limitingConeAngle;
return true;
}
static TextStream& operator<<(TextStream& ts, const FloatPoint3D& p)
{
ts << "x=" << p.x() << " y=" << p.y() << " z=" << p.z();
return ts;
}
TextStream& SpotLightSource::externalRepresentation(TextStream& ts) const
{
ts << "[type=SPOT-LIGHT] ";
ts << "[position=\"" << position() << "\"]";
ts << "[direction=\"" << direction() << "\"]";
ts << "[specularExponent=\"" << specularExponent() << "\"]";
ts << "[limitingConeAngle=\"" << limitingConeAngle() << "\"]";
return ts;
}
};