Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * OpenOffice.org - a multi-platform office productivity suite
5 : : *
6 : : * The Contents of this file are made available subject to
7 : : * the terms of GNU Lesser General Public License Version 2.1.
8 : : *
9 : : *
10 : : * GNU Lesser General Public License Version 2.1
11 : : * =============================================
12 : : * Copyright 2005 by Sun Microsystems, Inc.
13 : : * 901 San Antonio Road, Palo Alto, CA 94303, USA
14 : : *
15 : : * This library is free software; you can redistribute it and/or
16 : : * modify it under the terms of the GNU Lesser General Public
17 : : * License version 2.1, as published by the Free Software Foundation.
18 : : *
19 : : * This library is distributed in the hope that it will be useful,
20 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 : : * Lesser General Public License for more details.
23 : : *
24 : : * You should have received a copy of the GNU Lesser General Public
25 : : * License along with this library; if not, write to the Free Software
26 : : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 : : * MA 02111-1307 USA
28 : : *
29 : : ************************************************************************/
30 : :
31 : : #include <drawinglayer/attribute/sdrlightingattribute3d.hxx>
32 : : #include <basegfx/color/bcolor.hxx>
33 : : #include <basegfx/vector/b3dvector.hxx>
34 : : #include <drawinglayer/attribute/sdrlightattribute3d.hxx>
35 : :
36 : : //////////////////////////////////////////////////////////////////////////////
37 : :
38 : : namespace drawinglayer
39 : : {
40 : : namespace attribute
41 : : {
42 : 9 : class ImpSdrLightingAttribute
43 : : {
44 : : public:
45 : : // refcounter
46 : : sal_uInt32 mnRefCount;
47 : :
48 : : // 3D light attribute definitions
49 : : basegfx::BColor maAmbientLight;
50 : : ::std::vector< Sdr3DLightAttribute > maLightVector;
51 : :
52 : 11 : ImpSdrLightingAttribute(
53 : : const basegfx::BColor& rAmbientLight,
54 : : const ::std::vector< Sdr3DLightAttribute >& rLightVector)
55 : : : mnRefCount(0),
56 : : maAmbientLight(rAmbientLight),
57 [ + - ]: 11 : maLightVector(rLightVector)
58 : : {
59 : 11 : }
60 : :
61 : : // data read access
62 : 749 : const basegfx::BColor& getAmbientLight() const { return maAmbientLight; }
63 : 1528 : const ::std::vector< Sdr3DLightAttribute >& getLightVector() const { return maLightVector; }
64 : :
65 : 0 : bool operator==(const ImpSdrLightingAttribute& rCandidate) const
66 : : {
67 : 0 : return (getAmbientLight() == rCandidate.getAmbientLight()
68 [ # # ][ # # ]: 0 : && getLightVector() == rCandidate.getLightVector());
69 : : }
70 : :
71 : 3918 : static ImpSdrLightingAttribute* get_global_default()
72 : : {
73 : : static ImpSdrLightingAttribute* pDefault = 0;
74 : :
75 [ + + ]: 3918 : if(!pDefault)
76 : : {
77 : : pDefault = new ImpSdrLightingAttribute(
78 : : basegfx::BColor(),
79 [ + - ][ + - ]: 2 : std::vector< Sdr3DLightAttribute >());
[ + - ]
80 : :
81 : : // never delete; start with RefCount 1, not 0
82 : 2 : pDefault->mnRefCount++;
83 : : }
84 : :
85 : 3918 : return pDefault;
86 : : }
87 : : };
88 : :
89 : 9 : SdrLightingAttribute::SdrLightingAttribute(
90 : : const basegfx::BColor& rAmbientLight,
91 : : const ::std::vector< Sdr3DLightAttribute >& rLightVector)
92 : : : mpSdrLightingAttribute(new ImpSdrLightingAttribute(
93 [ + - ]: 9 : rAmbientLight, rLightVector))
94 : : {
95 : 9 : }
96 : :
97 : 3903 : SdrLightingAttribute::SdrLightingAttribute()
98 : 3903 : : mpSdrLightingAttribute(ImpSdrLightingAttribute::get_global_default())
99 : : {
100 : 3903 : mpSdrLightingAttribute->mnRefCount++;
101 : 3903 : }
102 : :
103 : 15 : SdrLightingAttribute::SdrLightingAttribute(const SdrLightingAttribute& rCandidate)
104 : 15 : : mpSdrLightingAttribute(rCandidate.mpSdrLightingAttribute)
105 : : {
106 : 15 : mpSdrLightingAttribute->mnRefCount++;
107 : 15 : }
108 : :
109 : 3927 : SdrLightingAttribute::~SdrLightingAttribute()
110 : : {
111 [ + + ]: 3927 : if(mpSdrLightingAttribute->mnRefCount)
112 : : {
113 : 3918 : mpSdrLightingAttribute->mnRefCount--;
114 : : }
115 : : else
116 : : {
117 [ + - ]: 9 : delete mpSdrLightingAttribute;
118 : : }
119 : 3927 : }
120 : :
121 : 15 : bool SdrLightingAttribute::isDefault() const
122 : : {
123 : 15 : return mpSdrLightingAttribute == ImpSdrLightingAttribute::get_global_default();
124 : : }
125 : :
126 : 3176 : SdrLightingAttribute& SdrLightingAttribute::operator=(const SdrLightingAttribute& rCandidate)
127 : : {
128 [ + + ]: 3176 : if(rCandidate.mpSdrLightingAttribute != mpSdrLightingAttribute)
129 : : {
130 [ + - ]: 9 : if(mpSdrLightingAttribute->mnRefCount)
131 : : {
132 : 9 : mpSdrLightingAttribute->mnRefCount--;
133 : : }
134 : : else
135 : : {
136 [ # # ]: 0 : delete mpSdrLightingAttribute;
137 : : }
138 : :
139 : 9 : mpSdrLightingAttribute = rCandidate.mpSdrLightingAttribute;
140 : 9 : mpSdrLightingAttribute->mnRefCount++;
141 : : }
142 : :
143 : 3176 : return *this;
144 : : }
145 : :
146 : 0 : bool SdrLightingAttribute::operator==(const SdrLightingAttribute& rCandidate) const
147 : : {
148 [ # # ]: 0 : if(rCandidate.mpSdrLightingAttribute == mpSdrLightingAttribute)
149 : : {
150 : 0 : return true;
151 : : }
152 : :
153 [ # # ]: 0 : if(rCandidate.isDefault() != isDefault())
154 : : {
155 : 0 : return false;
156 : : }
157 : :
158 : 0 : return (*rCandidate.mpSdrLightingAttribute == *mpSdrLightingAttribute);
159 : : }
160 : :
161 : 30 : const ::std::vector< Sdr3DLightAttribute >& SdrLightingAttribute::getLightVector() const
162 : : {
163 : 30 : return mpSdrLightingAttribute->getLightVector();
164 : : }
165 : :
166 : : // color model solver
167 : 749 : basegfx::BColor SdrLightingAttribute::solveColorModel(
168 : : const basegfx::B3DVector& rNormalInEyeCoordinates,
169 : : const basegfx::BColor& rColor, const basegfx::BColor& rSpecular,
170 : : const basegfx::BColor& rEmission, sal_uInt16 nSpecularIntensity) const
171 : : {
172 : : // initialize with emissive color
173 : 749 : basegfx::BColor aRetval(rEmission);
174 : :
175 : : // take care of global ambient light
176 : 749 : aRetval += mpSdrLightingAttribute->getAmbientLight() * rColor;
177 : :
178 : : // prepare light access. Is there a light?
179 : 749 : const sal_uInt32 nLightCount(mpSdrLightingAttribute->getLightVector().size());
180 : :
181 [ + - ][ + - ]: 749 : if(nLightCount && !rNormalInEyeCoordinates.equalZero())
[ + - ][ + - ]
182 : : {
183 : : // prepare normal
184 : 749 : basegfx::B3DVector aEyeNormal(rNormalInEyeCoordinates);
185 [ + - ]: 749 : aEyeNormal.normalize();
186 : :
187 [ + + ]: 1498 : for(sal_uInt32 a(0L); a < nLightCount; a++)
188 : : {
189 [ + - ]: 749 : const Sdr3DLightAttribute& rLight(mpSdrLightingAttribute->getLightVector()[a]);
190 [ + - ]: 749 : const double fCosFac(rLight.getDirection().scalar(aEyeNormal));
191 : :
192 [ + + ]: 749 : if(basegfx::fTools::more(fCosFac, 0.0))
193 : : {
194 [ + - ]: 614 : aRetval += ((rLight.getColor() * rColor) * fCosFac);
195 : :
196 [ - + ][ + - ]: 614 : if(rLight.getSpecular())
197 : : {
198 : : // expand by (0.0, 0.0, 1.0) in Z
199 [ # # ][ # # ]: 0 : basegfx::B3DVector aSpecularNormal(rLight.getDirection().getX(), rLight.getDirection().getY(), rLight.getDirection().getZ() + 1.0);
[ # # ]
200 [ # # ]: 0 : aSpecularNormal.normalize();
201 : 0 : double fCosFac2(aSpecularNormal.scalar(aEyeNormal));
202 : :
203 [ # # ]: 0 : if(basegfx::fTools::more(fCosFac2, 0.0))
204 : : {
205 : 0 : fCosFac2 = pow(fCosFac2, (double)nSpecularIntensity);
206 : 0 : aRetval += (rSpecular * fCosFac2);
207 : 0 : }
208 : : }
209 : : }
210 : 749 : }
211 : : }
212 : :
213 : : // clamp to color space before usage
214 [ + - ]: 749 : aRetval.clamp();
215 : :
216 : 749 : return aRetval;
217 : : }
218 : : } // end of namespace attribute
219 : : } // end of namespace drawinglayer
220 : :
221 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|