xref: /trunk/main/drawinglayer/source/attribute/sdrlightingattribute3d.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*4bfbcde8SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*4bfbcde8SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*4bfbcde8SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*4bfbcde8SAndrew Rist  * distributed with this work for additional information
6*4bfbcde8SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*4bfbcde8SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*4bfbcde8SAndrew Rist  * "License"); you may not use this file except in compliance
9*4bfbcde8SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*4bfbcde8SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*4bfbcde8SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*4bfbcde8SAndrew Rist  * software distributed under the License is distributed on an
15*4bfbcde8SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*4bfbcde8SAndrew Rist  * KIND, either express or implied.  See the License for the
17*4bfbcde8SAndrew Rist  * specific language governing permissions and limitations
18*4bfbcde8SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*4bfbcde8SAndrew Rist  *************************************************************/
21cdf0e10cSrcweir 
22cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
23cdf0e10cSrcweir #include "precompiled_drawinglayer.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <drawinglayer/attribute/sdrlightingattribute3d.hxx>
26cdf0e10cSrcweir #include <basegfx/color/bcolor.hxx>
27cdf0e10cSrcweir #include <basegfx/vector/b3dvector.hxx>
28cdf0e10cSrcweir #include <drawinglayer/attribute/sdrlightattribute3d.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace drawinglayer
33cdf0e10cSrcweir {
34cdf0e10cSrcweir     namespace attribute
35cdf0e10cSrcweir     {
36cdf0e10cSrcweir         class ImpSdrLightingAttribute
37cdf0e10cSrcweir         {
38cdf0e10cSrcweir         public:
39cdf0e10cSrcweir             // refcounter
40cdf0e10cSrcweir             sal_uInt32                              mnRefCount;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir             // 3D light attribute definitions
43cdf0e10cSrcweir             basegfx::BColor                         maAmbientLight;
44cdf0e10cSrcweir             ::std::vector< Sdr3DLightAttribute >    maLightVector;
45cdf0e10cSrcweir 
ImpSdrLightingAttribute(const basegfx::BColor & rAmbientLight,const::std::vector<Sdr3DLightAttribute> & rLightVector)46cdf0e10cSrcweir             ImpSdrLightingAttribute(
47cdf0e10cSrcweir                 const basegfx::BColor& rAmbientLight,
48cdf0e10cSrcweir                 const ::std::vector< Sdr3DLightAttribute >& rLightVector)
49cdf0e10cSrcweir             :   mnRefCount(0),
50cdf0e10cSrcweir                 maAmbientLight(rAmbientLight),
51cdf0e10cSrcweir                 maLightVector(rLightVector)
52cdf0e10cSrcweir             {
53cdf0e10cSrcweir             }
54cdf0e10cSrcweir 
55cdf0e10cSrcweir             // data read access
getAmbientLight() const56cdf0e10cSrcweir             const basegfx::BColor& getAmbientLight() const { return maAmbientLight; }
getLightVector() const57cdf0e10cSrcweir             const ::std::vector< Sdr3DLightAttribute >& getLightVector() const { return maLightVector; }
58cdf0e10cSrcweir 
operator ==(const ImpSdrLightingAttribute & rCandidate) const59cdf0e10cSrcweir             bool operator==(const ImpSdrLightingAttribute& rCandidate) const
60cdf0e10cSrcweir             {
61cdf0e10cSrcweir                 return (getAmbientLight() == rCandidate.getAmbientLight()
62cdf0e10cSrcweir                     && getLightVector() == rCandidate.getLightVector());
63cdf0e10cSrcweir             }
64cdf0e10cSrcweir 
get_global_default()65cdf0e10cSrcweir             static ImpSdrLightingAttribute* get_global_default()
66cdf0e10cSrcweir             {
67cdf0e10cSrcweir                 static ImpSdrLightingAttribute* pDefault = 0;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir                 if(!pDefault)
70cdf0e10cSrcweir                 {
71cdf0e10cSrcweir                     pDefault = new ImpSdrLightingAttribute(
72cdf0e10cSrcweir                         basegfx::BColor(),
73cdf0e10cSrcweir                         std::vector< Sdr3DLightAttribute >());
74cdf0e10cSrcweir 
75cdf0e10cSrcweir                     // never delete; start with RefCount 1, not 0
76cdf0e10cSrcweir                     pDefault->mnRefCount++;
77cdf0e10cSrcweir                 }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir                 return pDefault;
80cdf0e10cSrcweir             }
81cdf0e10cSrcweir         };
82cdf0e10cSrcweir 
SdrLightingAttribute(const basegfx::BColor & rAmbientLight,const::std::vector<Sdr3DLightAttribute> & rLightVector)83cdf0e10cSrcweir         SdrLightingAttribute::SdrLightingAttribute(
84cdf0e10cSrcweir             const basegfx::BColor& rAmbientLight,
85cdf0e10cSrcweir             const ::std::vector< Sdr3DLightAttribute >& rLightVector)
86cdf0e10cSrcweir         :   mpSdrLightingAttribute(new ImpSdrLightingAttribute(
87cdf0e10cSrcweir                 rAmbientLight, rLightVector))
88cdf0e10cSrcweir         {
89cdf0e10cSrcweir         }
90cdf0e10cSrcweir 
SdrLightingAttribute()91cdf0e10cSrcweir         SdrLightingAttribute::SdrLightingAttribute()
92cdf0e10cSrcweir         :   mpSdrLightingAttribute(ImpSdrLightingAttribute::get_global_default())
93cdf0e10cSrcweir         {
94cdf0e10cSrcweir             mpSdrLightingAttribute->mnRefCount++;
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir 
SdrLightingAttribute(const SdrLightingAttribute & rCandidate)97cdf0e10cSrcweir         SdrLightingAttribute::SdrLightingAttribute(const SdrLightingAttribute& rCandidate)
98cdf0e10cSrcweir         :   mpSdrLightingAttribute(rCandidate.mpSdrLightingAttribute)
99cdf0e10cSrcweir         {
100cdf0e10cSrcweir             mpSdrLightingAttribute->mnRefCount++;
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir 
~SdrLightingAttribute()103cdf0e10cSrcweir         SdrLightingAttribute::~SdrLightingAttribute()
104cdf0e10cSrcweir         {
105cdf0e10cSrcweir             if(mpSdrLightingAttribute->mnRefCount)
106cdf0e10cSrcweir             {
107cdf0e10cSrcweir                 mpSdrLightingAttribute->mnRefCount--;
108cdf0e10cSrcweir             }
109cdf0e10cSrcweir             else
110cdf0e10cSrcweir             {
111cdf0e10cSrcweir                 delete mpSdrLightingAttribute;
112cdf0e10cSrcweir             }
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir 
isDefault() const115cdf0e10cSrcweir         bool SdrLightingAttribute::isDefault() const
116cdf0e10cSrcweir         {
117cdf0e10cSrcweir             return mpSdrLightingAttribute == ImpSdrLightingAttribute::get_global_default();
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir 
operator =(const SdrLightingAttribute & rCandidate)120cdf0e10cSrcweir         SdrLightingAttribute& SdrLightingAttribute::operator=(const SdrLightingAttribute& rCandidate)
121cdf0e10cSrcweir         {
122cdf0e10cSrcweir             if(rCandidate.mpSdrLightingAttribute != mpSdrLightingAttribute)
123cdf0e10cSrcweir             {
124cdf0e10cSrcweir                 if(mpSdrLightingAttribute->mnRefCount)
125cdf0e10cSrcweir                 {
126cdf0e10cSrcweir                     mpSdrLightingAttribute->mnRefCount--;
127cdf0e10cSrcweir                 }
128cdf0e10cSrcweir                 else
129cdf0e10cSrcweir                 {
130cdf0e10cSrcweir                     delete mpSdrLightingAttribute;
131cdf0e10cSrcweir                 }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir                 mpSdrLightingAttribute = rCandidate.mpSdrLightingAttribute;
134cdf0e10cSrcweir                 mpSdrLightingAttribute->mnRefCount++;
135cdf0e10cSrcweir             }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir             return *this;
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir 
operator ==(const SdrLightingAttribute & rCandidate) const140cdf0e10cSrcweir         bool SdrLightingAttribute::operator==(const SdrLightingAttribute& rCandidate) const
141cdf0e10cSrcweir         {
142cdf0e10cSrcweir             if(rCandidate.mpSdrLightingAttribute == mpSdrLightingAttribute)
143cdf0e10cSrcweir             {
144cdf0e10cSrcweir                 return true;
145cdf0e10cSrcweir             }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir             if(rCandidate.isDefault() != isDefault())
148cdf0e10cSrcweir             {
149cdf0e10cSrcweir                 return false;
150cdf0e10cSrcweir             }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir             return (*rCandidate.mpSdrLightingAttribute == *mpSdrLightingAttribute);
153cdf0e10cSrcweir         }
154cdf0e10cSrcweir 
getAmbientLight() const155cdf0e10cSrcweir         const basegfx::BColor& SdrLightingAttribute::getAmbientLight() const
156cdf0e10cSrcweir         {
157cdf0e10cSrcweir             return mpSdrLightingAttribute->getAmbientLight();
158cdf0e10cSrcweir         }
159cdf0e10cSrcweir 
getLightVector() const160cdf0e10cSrcweir         const ::std::vector< Sdr3DLightAttribute >& SdrLightingAttribute::getLightVector() const
161cdf0e10cSrcweir         {
162cdf0e10cSrcweir             return mpSdrLightingAttribute->getLightVector();
163cdf0e10cSrcweir         }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         // color model solver
solveColorModel(const basegfx::B3DVector & rNormalInEyeCoordinates,const basegfx::BColor & rColor,const basegfx::BColor & rSpecular,const basegfx::BColor & rEmission,sal_uInt16 nSpecularIntensity) const166cdf0e10cSrcweir         basegfx::BColor SdrLightingAttribute::solveColorModel(
167cdf0e10cSrcweir             const basegfx::B3DVector& rNormalInEyeCoordinates,
168cdf0e10cSrcweir             const basegfx::BColor& rColor, const basegfx::BColor& rSpecular,
169cdf0e10cSrcweir             const basegfx::BColor& rEmission, sal_uInt16 nSpecularIntensity) const
170cdf0e10cSrcweir         {
171cdf0e10cSrcweir             // initialize with emissive color
172cdf0e10cSrcweir             basegfx::BColor aRetval(rEmission);
173cdf0e10cSrcweir 
174cdf0e10cSrcweir             // take care of global ambient light
175cdf0e10cSrcweir             aRetval += mpSdrLightingAttribute->getAmbientLight() * rColor;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir             // prepare light access. Is there a light?
178cdf0e10cSrcweir             const sal_uInt32 nLightCount(mpSdrLightingAttribute->getLightVector().size());
179cdf0e10cSrcweir 
180cdf0e10cSrcweir             if(nLightCount && !rNormalInEyeCoordinates.equalZero())
181cdf0e10cSrcweir             {
182cdf0e10cSrcweir                 // prepare normal
183cdf0e10cSrcweir                 basegfx::B3DVector aEyeNormal(rNormalInEyeCoordinates);
184cdf0e10cSrcweir                 aEyeNormal.normalize();
185cdf0e10cSrcweir 
186cdf0e10cSrcweir                 for(sal_uInt32 a(0L); a < nLightCount; a++)
187cdf0e10cSrcweir                 {
188cdf0e10cSrcweir                     const Sdr3DLightAttribute& rLight(mpSdrLightingAttribute->getLightVector()[a]);
189cdf0e10cSrcweir                     const double fCosFac(rLight.getDirection().scalar(aEyeNormal));
190cdf0e10cSrcweir 
191cdf0e10cSrcweir                     if(basegfx::fTools::more(fCosFac, 0.0))
192cdf0e10cSrcweir                     {
193cdf0e10cSrcweir                         aRetval += ((rLight.getColor() * rColor) * fCosFac);
194cdf0e10cSrcweir 
195cdf0e10cSrcweir                         if(rLight.getSpecular())
196cdf0e10cSrcweir                         {
197cdf0e10cSrcweir                             // expand by (0.0, 0.0, 1.0) in Z
198cdf0e10cSrcweir                             basegfx::B3DVector aSpecularNormal(rLight.getDirection().getX(), rLight.getDirection().getY(), rLight.getDirection().getZ() + 1.0);
199cdf0e10cSrcweir                             aSpecularNormal.normalize();
200cdf0e10cSrcweir                             double fCosFac2(aSpecularNormal.scalar(aEyeNormal));
201cdf0e10cSrcweir 
202cdf0e10cSrcweir                             if(basegfx::fTools::more(fCosFac2, 0.0))
203cdf0e10cSrcweir                             {
204cdf0e10cSrcweir                                 fCosFac2 = pow(fCosFac2, (double)nSpecularIntensity);
205cdf0e10cSrcweir                                 aRetval += (rSpecular * fCosFac2);
206cdf0e10cSrcweir                             }
207cdf0e10cSrcweir                         }
208cdf0e10cSrcweir                     }
209cdf0e10cSrcweir                 }
210cdf0e10cSrcweir             }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir             // clamp to color space before usage
213cdf0e10cSrcweir             aRetval.clamp();
214cdf0e10cSrcweir 
215cdf0e10cSrcweir             return aRetval;
216cdf0e10cSrcweir         }
217cdf0e10cSrcweir     } // end of namespace attribute
218cdf0e10cSrcweir } // end of namespace drawinglayer
219cdf0e10cSrcweir 
220cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
221cdf0e10cSrcweir // eof
222