1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <drawinglayer/attribute/sdrlightingattribute3d.hxx>
21 #include <basegfx/color/bcolor.hxx>
22 #include <basegfx/vector/b3dvector.hxx>
23 #include <drawinglayer/attribute/sdrlightattribute3d.hxx>
24 #include <rtl/instance.hxx>
28 namespace drawinglayer
32 class ImpSdrLightingAttribute
35 // 3D light attribute definitions
36 basegfx::BColor maAmbientLight
;
37 ::std::vector
< Sdr3DLightAttribute
> maLightVector
;
39 ImpSdrLightingAttribute(
40 const basegfx::BColor
& rAmbientLight
,
41 const ::std::vector
< Sdr3DLightAttribute
>& rLightVector
)
42 : maAmbientLight(rAmbientLight
),
43 maLightVector(rLightVector
)
47 ImpSdrLightingAttribute()
48 : maAmbientLight(basegfx::BColor()),
49 maLightVector(std::vector
< Sdr3DLightAttribute
>())
54 const basegfx::BColor
& getAmbientLight() const { return maAmbientLight
; }
55 const ::std::vector
< Sdr3DLightAttribute
>& getLightVector() const { return maLightVector
; }
57 bool operator==(const ImpSdrLightingAttribute
& rCandidate
) const
59 return (getAmbientLight() == rCandidate
.getAmbientLight()
60 && getLightVector() == rCandidate
.getLightVector());
66 struct theGlobalDefault
:
67 public rtl::Static
< SdrLightingAttribute::ImplType
, theGlobalDefault
> {};
70 SdrLightingAttribute::SdrLightingAttribute(
71 const basegfx::BColor
& rAmbientLight
,
72 const ::std::vector
< Sdr3DLightAttribute
>& rLightVector
)
73 : mpSdrLightingAttribute(ImpSdrLightingAttribute(
74 rAmbientLight
, rLightVector
))
78 SdrLightingAttribute::SdrLightingAttribute()
79 : mpSdrLightingAttribute(theGlobalDefault::get())
83 SdrLightingAttribute::SdrLightingAttribute(const SdrLightingAttribute
& rCandidate
)
84 : mpSdrLightingAttribute(rCandidate
.mpSdrLightingAttribute
)
88 SdrLightingAttribute::~SdrLightingAttribute()
92 bool SdrLightingAttribute::isDefault() const
94 return mpSdrLightingAttribute
.same_object(theGlobalDefault::get());
97 SdrLightingAttribute
& SdrLightingAttribute::operator=(const SdrLightingAttribute
& rCandidate
)
99 mpSdrLightingAttribute
= rCandidate
.mpSdrLightingAttribute
;
103 bool SdrLightingAttribute::operator==(const SdrLightingAttribute
& rCandidate
) const
105 // tdf#87509 default attr is always != non-default attr, even with same values
106 if(rCandidate
.isDefault() != isDefault())
109 return rCandidate
.mpSdrLightingAttribute
== mpSdrLightingAttribute
;
112 const ::std::vector
< Sdr3DLightAttribute
>& SdrLightingAttribute::getLightVector() const
114 return mpSdrLightingAttribute
->getLightVector();
117 // color model solver
118 basegfx::BColor
SdrLightingAttribute::solveColorModel(
119 const basegfx::B3DVector
& rNormalInEyeCoordinates
,
120 const basegfx::BColor
& rColor
, const basegfx::BColor
& rSpecular
,
121 const basegfx::BColor
& rEmission
, sal_uInt16 nSpecularIntensity
) const
123 // initialize with emissive color
124 basegfx::BColor
aRetval(rEmission
);
126 // take care of global ambient light
127 aRetval
+= mpSdrLightingAttribute
->getAmbientLight() * rColor
;
129 // prepare light access. Is there a light?
130 const sal_uInt32
nLightCount(mpSdrLightingAttribute
->getLightVector().size());
132 if(nLightCount
&& !rNormalInEyeCoordinates
.equalZero())
135 basegfx::B3DVector
aEyeNormal(rNormalInEyeCoordinates
);
136 aEyeNormal
.normalize();
138 for(sal_uInt32
a(0L); a
< nLightCount
; a
++)
140 const Sdr3DLightAttribute
& rLight(mpSdrLightingAttribute
->getLightVector()[a
]);
141 const double fCosFac(rLight
.getDirection().scalar(aEyeNormal
));
143 if(basegfx::fTools::more(fCosFac
, 0.0))
145 aRetval
+= ((rLight
.getColor() * rColor
) * fCosFac
);
147 if(rLight
.getSpecular())
149 // expand by (0.0, 0.0, 1.0) in Z
150 basegfx::B3DVector
aSpecularNormal(rLight
.getDirection().getX(), rLight
.getDirection().getY(), rLight
.getDirection().getZ() + 1.0);
151 aSpecularNormal
.normalize();
152 double fCosFac2(aSpecularNormal
.scalar(aEyeNormal
));
154 if(basegfx::fTools::more(fCosFac2
, 0.0))
156 fCosFac2
= pow(fCosFac2
, (double)nSpecularIntensity
);
157 aRetval
+= (rSpecular
* fCosFac2
);
164 // clamp to color space before usage
169 } // end of namespace attribute
170 } // end of namespace drawinglayer
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */