Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
25 :
26 :
27 :
28 : namespace drawinglayer
29 : {
30 : namespace attribute
31 : {
32 62 : class ImpSdrLightingAttribute
33 : {
34 : public:
35 : // 3D light attribute definitions
36 : basegfx::BColor maAmbientLight;
37 : ::std::vector< Sdr3DLightAttribute > maLightVector;
38 :
39 19 : ImpSdrLightingAttribute(
40 : const basegfx::BColor& rAmbientLight,
41 : const ::std::vector< Sdr3DLightAttribute >& rLightVector)
42 : : maAmbientLight(rAmbientLight),
43 19 : maLightVector(rLightVector)
44 : {
45 19 : }
46 :
47 5 : ImpSdrLightingAttribute()
48 : : maAmbientLight(basegfx::BColor()),
49 5 : maLightVector(std::vector< Sdr3DLightAttribute >())
50 : {
51 5 : }
52 :
53 : // data read access
54 18906 : const basegfx::BColor& getAmbientLight() const { return maAmbientLight; }
55 37727 : const ::std::vector< Sdr3DLightAttribute >& getLightVector() const { return maLightVector; }
56 :
57 6 : bool operator==(const ImpSdrLightingAttribute& rCandidate) const
58 : {
59 6 : return (getAmbientLight() == rCandidate.getAmbientLight()
60 6 : && getLightVector() == rCandidate.getLightVector());
61 : }
62 : };
63 :
64 : namespace
65 : {
66 : struct theGlobalDefault :
67 : public rtl::Static< SdrLightingAttribute::ImplType, theGlobalDefault > {};
68 : }
69 :
70 19 : SdrLightingAttribute::SdrLightingAttribute(
71 : const basegfx::BColor& rAmbientLight,
72 : const ::std::vector< Sdr3DLightAttribute >& rLightVector)
73 : : mpSdrLightingAttribute(ImpSdrLightingAttribute(
74 19 : rAmbientLight, rLightVector))
75 : {
76 19 : }
77 :
78 8142 : SdrLightingAttribute::SdrLightingAttribute()
79 8142 : : mpSdrLightingAttribute(theGlobalDefault::get())
80 : {
81 8142 : }
82 :
83 24 : SdrLightingAttribute::SdrLightingAttribute(const SdrLightingAttribute& rCandidate)
84 24 : : mpSdrLightingAttribute(rCandidate.mpSdrLightingAttribute)
85 : {
86 24 : }
87 :
88 8185 : SdrLightingAttribute::~SdrLightingAttribute()
89 : {
90 8185 : }
91 :
92 82 : bool SdrLightingAttribute::isDefault() const
93 : {
94 82 : return mpSdrLightingAttribute.same_object(theGlobalDefault::get());
95 : }
96 :
97 6888 : SdrLightingAttribute& SdrLightingAttribute::operator=(const SdrLightingAttribute& rCandidate)
98 : {
99 6888 : mpSdrLightingAttribute = rCandidate.mpSdrLightingAttribute;
100 6888 : return *this;
101 : }
102 :
103 29 : bool SdrLightingAttribute::operator==(const SdrLightingAttribute& rCandidate) const
104 : {
105 : // tdf#87509 default attr is always != non-default attr, even with same values
106 29 : if(rCandidate.isDefault() != isDefault())
107 0 : return false;
108 :
109 29 : return rCandidate.mpSdrLightingAttribute == mpSdrLightingAttribute;
110 : }
111 :
112 30 : const ::std::vector< Sdr3DLightAttribute >& SdrLightingAttribute::getLightVector() const
113 : {
114 30 : return mpSdrLightingAttribute->getLightVector();
115 : }
116 :
117 : // color model solver
118 18894 : 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
122 : {
123 : // initialize with emissive color
124 18894 : basegfx::BColor aRetval(rEmission);
125 :
126 : // take care of global ambient light
127 18894 : aRetval += mpSdrLightingAttribute->getAmbientLight() * rColor;
128 :
129 : // prepare light access. Is there a light?
130 18894 : const sal_uInt32 nLightCount(mpSdrLightingAttribute->getLightVector().size());
131 :
132 18894 : if(nLightCount && !rNormalInEyeCoordinates.equalZero())
133 : {
134 : // prepare normal
135 18791 : basegfx::B3DVector aEyeNormal(rNormalInEyeCoordinates);
136 18791 : aEyeNormal.normalize();
137 :
138 37582 : for(sal_uInt32 a(0L); a < nLightCount; a++)
139 : {
140 18791 : const Sdr3DLightAttribute& rLight(mpSdrLightingAttribute->getLightVector()[a]);
141 18791 : const double fCosFac(rLight.getDirection().scalar(aEyeNormal));
142 :
143 18791 : if(basegfx::fTools::more(fCosFac, 0.0))
144 : {
145 16013 : aRetval += ((rLight.getColor() * rColor) * fCosFac);
146 :
147 16013 : if(rLight.getSpecular())
148 : {
149 : // expand by (0.0, 0.0, 1.0) in Z
150 0 : basegfx::B3DVector aSpecularNormal(rLight.getDirection().getX(), rLight.getDirection().getY(), rLight.getDirection().getZ() + 1.0);
151 0 : aSpecularNormal.normalize();
152 0 : double fCosFac2(aSpecularNormal.scalar(aEyeNormal));
153 :
154 0 : if(basegfx::fTools::more(fCosFac2, 0.0))
155 : {
156 0 : fCosFac2 = pow(fCosFac2, (double)nSpecularIntensity);
157 0 : aRetval += (rSpecular * fCosFac2);
158 0 : }
159 : }
160 : }
161 18791 : }
162 : }
163 :
164 : // clamp to color space before usage
165 18894 : aRetval.clamp();
166 :
167 18894 : return aRetval;
168 : }
169 : } // end of namespace attribute
170 : } // end of namespace drawinglayer
171 :
172 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|