1 /*
2 * Copyright (C) 2008 Alex Mathews <possessedpenguinbob@gmail.com>
3 * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
4 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
5 * Copyright (C) 2005 Eric Seidel <eric@webkit.org>
6 * Copyright (C) 2010 Zoltan Herczeg <zherczeg@webkit.org>
7 * Copyright (C) 2011 University of Szeged
8 * Copyright (C) 2011 Renata Hodovan <reni@webkit.org>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "config.h"
33 #include "platform/graphics/filters/SpotLightSource.h"
34
35 #include "platform/text/TextStream.h"
36
37 namespace blink {
38
39 // spot-light edge darkening depends on an absolute treshold
40 // according to the SVG 1.1 SE light regression tests
41 static const float antiAliasTreshold = 0.016f;
42
initPaintingData(PaintingData & paintingData) const43 void SpotLightSource::initPaintingData(PaintingData& paintingData) const
44 {
45 paintingData.privateColorVector = paintingData.colorVector;
46 paintingData.directionVector.setX(m_direction.x() - m_position.x());
47 paintingData.directionVector.setY(m_direction.y() - m_position.y());
48 paintingData.directionVector.setZ(m_direction.z() - m_position.z());
49 paintingData.directionVector.normalize();
50
51 if (!m_limitingConeAngle) {
52 paintingData.coneCutOffLimit = 0.0f;
53 paintingData.coneFullLight = -antiAliasTreshold;
54 } else {
55 float limitingConeAngle = m_limitingConeAngle;
56 if (limitingConeAngle < 0.0f)
57 limitingConeAngle = -limitingConeAngle;
58 if (limitingConeAngle > 90.0f)
59 limitingConeAngle = 90.0f;
60 paintingData.coneCutOffLimit = cosf(deg2rad(180.0f - limitingConeAngle));
61 paintingData.coneFullLight = paintingData.coneCutOffLimit - antiAliasTreshold;
62 }
63 }
64
updatePaintingData(PaintingData & paintingData,int x,int y,float z) const65 void SpotLightSource::updatePaintingData(PaintingData& paintingData, int x, int y, float z) const
66 {
67 paintingData.lightVector.setX(m_position.x() - x);
68 paintingData.lightVector.setY(m_position.y() - y);
69 paintingData.lightVector.setZ(m_position.z() - z);
70 paintingData.lightVectorLength = paintingData.lightVector.length();
71
72 float cosineOfAngle = (paintingData.lightVector * paintingData.directionVector) / paintingData.lightVectorLength;
73 if (cosineOfAngle > paintingData.coneCutOffLimit) {
74 // No light is produced, scanlines are not updated
75 paintingData.colorVector.setX(0.0f);
76 paintingData.colorVector.setY(0.0f);
77 paintingData.colorVector.setZ(0.0f);
78 return;
79 }
80
81 // Set the color of the pixel
82 float lightStrength;
83 if (1.0f == m_specularExponent) {
84 lightStrength = -cosineOfAngle; // -cosineOfAngle ^ 1 == -cosineOfAngle
85 } else {
86 lightStrength = powf(-cosineOfAngle, m_specularExponent);
87 }
88
89 if (cosineOfAngle > paintingData.coneFullLight)
90 lightStrength *= (paintingData.coneCutOffLimit - cosineOfAngle) / (paintingData.coneCutOffLimit - paintingData.coneFullLight);
91
92 if (lightStrength > 1.0f)
93 lightStrength = 1.0f;
94
95 paintingData.colorVector.setX(paintingData.privateColorVector.x() * lightStrength);
96 paintingData.colorVector.setY(paintingData.privateColorVector.y() * lightStrength);
97 paintingData.colorVector.setZ(paintingData.privateColorVector.z() * lightStrength);
98 }
99
setPosition(const FloatPoint3D & position)100 bool SpotLightSource::setPosition(const FloatPoint3D& position)
101 {
102 if (m_position == position)
103 return false;
104 m_position = position;
105 return true;
106 }
107
setPointsAt(const FloatPoint3D & direction)108 bool SpotLightSource::setPointsAt(const FloatPoint3D& direction)
109 {
110 if (m_direction == direction)
111 return false;
112 m_direction = direction;
113 return true;
114 }
115
setSpecularExponent(float specularExponent)116 bool SpotLightSource::setSpecularExponent(float specularExponent)
117 {
118 specularExponent = std::min(std::max(specularExponent, 1.0f), 128.0f);
119 if (m_specularExponent == specularExponent)
120 return false;
121 m_specularExponent = specularExponent;
122 return true;
123 }
124
setLimitingConeAngle(float limitingConeAngle)125 bool SpotLightSource::setLimitingConeAngle(float limitingConeAngle)
126 {
127 if (m_limitingConeAngle == limitingConeAngle)
128 return false;
129 m_limitingConeAngle = limitingConeAngle;
130 return true;
131 }
132
operator <<(TextStream & ts,const FloatPoint3D & p)133 static TextStream& operator<<(TextStream& ts, const FloatPoint3D& p)
134 {
135 ts << "x=" << p.x() << " y=" << p.y() << " z=" << p.z();
136 return ts;
137 }
138
externalRepresentation(TextStream & ts) const139 TextStream& SpotLightSource::externalRepresentation(TextStream& ts) const
140 {
141 ts << "[type=SPOT-LIGHT] ";
142 ts << "[position=\"" << position() << "\"]";
143 ts << "[direction=\"" << direction() << "\"]";
144 ts << "[specularExponent=\"" << specularExponent() << "\"]";
145 ts << "[limitingConeAngle=\"" << limitingConeAngle() << "\"]";
146 return ts;
147 }
148
149 }; // namespace blink
150