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>
26 namespace drawinglayer::attribute
28 class ImpSdrLightingAttribute
31 // 3D light attribute definitions
32 basegfx::BColor maAmbientLight
;
33 std::vector
< Sdr3DLightAttribute
> maLightVector
;
35 ImpSdrLightingAttribute(
36 const basegfx::BColor
& rAmbientLight
,
37 std::vector
< Sdr3DLightAttribute
>&& rLightVector
)
38 : maAmbientLight(rAmbientLight
),
39 maLightVector(std::move(rLightVector
))
43 ImpSdrLightingAttribute()
48 const basegfx::BColor
& getAmbientLight() const { return maAmbientLight
; }
49 const std::vector
< Sdr3DLightAttribute
>& getLightVector() const { return maLightVector
; }
51 bool operator==(const ImpSdrLightingAttribute
& rCandidate
) const
53 return (getAmbientLight() == rCandidate
.getAmbientLight()
54 && getLightVector() == rCandidate
.getLightVector());
60 SdrLightingAttribute::ImplType
& theGlobalDefault()
62 static SdrLightingAttribute::ImplType SINGLETON
;
67 SdrLightingAttribute::SdrLightingAttribute(
68 const basegfx::BColor
& rAmbientLight
,
69 std::vector
< Sdr3DLightAttribute
>&& rLightVector
)
70 : mpSdrLightingAttribute(ImpSdrLightingAttribute(
71 rAmbientLight
, std::move(rLightVector
)))
75 SdrLightingAttribute::SdrLightingAttribute()
76 : mpSdrLightingAttribute(theGlobalDefault())
80 SdrLightingAttribute::SdrLightingAttribute(const SdrLightingAttribute
&) = default;
82 SdrLightingAttribute::SdrLightingAttribute(SdrLightingAttribute
&&) = default;
84 SdrLightingAttribute::~SdrLightingAttribute() = default;
87 bool SdrLightingAttribute::isDefault() const
89 return mpSdrLightingAttribute
.same_object(theGlobalDefault());
92 SdrLightingAttribute
& SdrLightingAttribute::operator=(const SdrLightingAttribute
&) = default;
94 SdrLightingAttribute
& SdrLightingAttribute::operator=(SdrLightingAttribute
&&) = default;
96 bool SdrLightingAttribute::operator==(const SdrLightingAttribute
& rCandidate
) const
98 // tdf#87509 default attr is always != non-default attr, even with same values
99 if(rCandidate
.isDefault() != isDefault())
102 return rCandidate
.mpSdrLightingAttribute
== mpSdrLightingAttribute
;
105 const std::vector
< Sdr3DLightAttribute
>& SdrLightingAttribute::getLightVector() const
107 return mpSdrLightingAttribute
->getLightVector();
110 const basegfx::BColor
& SdrLightingAttribute::getAmbientLightColor() const
112 return mpSdrLightingAttribute
->maAmbientLight
;
115 // color model solver
116 basegfx::BColor
SdrLightingAttribute::solveColorModel(
117 const basegfx::B3DVector
& rNormalInEyeCoordinates
,
118 const basegfx::BColor
& rColor
, const basegfx::BColor
& rSpecular
,
119 const basegfx::BColor
& rEmission
, sal_uInt16 nSpecularIntensity
) const
121 // initialize with emissive color
122 basegfx::BColor
aRetval(rEmission
);
124 // take care of global ambient light
125 aRetval
+= mpSdrLightingAttribute
->getAmbientLight() * rColor
;
127 const std::vector
<Sdr3DLightAttribute
>& rLightVector
= mpSdrLightingAttribute
->getLightVector();
129 // prepare light access. Is there a light?
130 const sal_uInt32
nLightCount(rLightVector
.size());
132 if(nLightCount
&& !rNormalInEyeCoordinates
.equalZero())
135 basegfx::B3DVector
aEyeNormal(rNormalInEyeCoordinates
);
136 aEyeNormal
.normalize();
138 for(sal_uInt32
a(0); a
< nLightCount
; a
++)
140 const Sdr3DLightAttribute
& rLight(rLightVector
[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
, static_cast<double>(nSpecularIntensity
));
157 aRetval
+= rSpecular
* fCosFac2
;
164 // clamp to color space before usage
170 } // end of namespace
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */