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 :
25 : //////////////////////////////////////////////////////////////////////////////
26 :
27 : namespace drawinglayer
28 : {
29 : namespace attribute
30 : {
31 0 : class ImpSdrLightingAttribute
32 : {
33 : public:
34 : // refcounter
35 : sal_uInt32 mnRefCount;
36 :
37 : // 3D light attribute definitions
38 : basegfx::BColor maAmbientLight;
39 : ::std::vector< Sdr3DLightAttribute > maLightVector;
40 :
41 0 : ImpSdrLightingAttribute(
42 : const basegfx::BColor& rAmbientLight,
43 : const ::std::vector< Sdr3DLightAttribute >& rLightVector)
44 : : mnRefCount(0),
45 : maAmbientLight(rAmbientLight),
46 0 : maLightVector(rLightVector)
47 : {
48 0 : }
49 :
50 : // data read access
51 0 : const basegfx::BColor& getAmbientLight() const { return maAmbientLight; }
52 0 : const ::std::vector< Sdr3DLightAttribute >& getLightVector() const { return maLightVector; }
53 :
54 0 : bool operator==(const ImpSdrLightingAttribute& rCandidate) const
55 : {
56 0 : return (getAmbientLight() == rCandidate.getAmbientLight()
57 0 : && getLightVector() == rCandidate.getLightVector());
58 : }
59 :
60 0 : static ImpSdrLightingAttribute* get_global_default()
61 : {
62 : static ImpSdrLightingAttribute* pDefault = 0;
63 :
64 0 : if(!pDefault)
65 : {
66 : pDefault = new ImpSdrLightingAttribute(
67 : basegfx::BColor(),
68 0 : std::vector< Sdr3DLightAttribute >());
69 :
70 : // never delete; start with RefCount 1, not 0
71 0 : pDefault->mnRefCount++;
72 : }
73 :
74 0 : return pDefault;
75 : }
76 : };
77 :
78 0 : SdrLightingAttribute::SdrLightingAttribute(
79 : const basegfx::BColor& rAmbientLight,
80 : const ::std::vector< Sdr3DLightAttribute >& rLightVector)
81 : : mpSdrLightingAttribute(new ImpSdrLightingAttribute(
82 0 : rAmbientLight, rLightVector))
83 : {
84 0 : }
85 :
86 0 : SdrLightingAttribute::SdrLightingAttribute()
87 0 : : mpSdrLightingAttribute(ImpSdrLightingAttribute::get_global_default())
88 : {
89 0 : mpSdrLightingAttribute->mnRefCount++;
90 0 : }
91 :
92 0 : SdrLightingAttribute::SdrLightingAttribute(const SdrLightingAttribute& rCandidate)
93 0 : : mpSdrLightingAttribute(rCandidate.mpSdrLightingAttribute)
94 : {
95 0 : mpSdrLightingAttribute->mnRefCount++;
96 0 : }
97 :
98 0 : SdrLightingAttribute::~SdrLightingAttribute()
99 : {
100 0 : if(mpSdrLightingAttribute->mnRefCount)
101 : {
102 0 : mpSdrLightingAttribute->mnRefCount--;
103 : }
104 : else
105 : {
106 0 : delete mpSdrLightingAttribute;
107 : }
108 0 : }
109 :
110 0 : bool SdrLightingAttribute::isDefault() const
111 : {
112 0 : return mpSdrLightingAttribute == ImpSdrLightingAttribute::get_global_default();
113 : }
114 :
115 0 : SdrLightingAttribute& SdrLightingAttribute::operator=(const SdrLightingAttribute& rCandidate)
116 : {
117 0 : if(rCandidate.mpSdrLightingAttribute != mpSdrLightingAttribute)
118 : {
119 0 : if(mpSdrLightingAttribute->mnRefCount)
120 : {
121 0 : mpSdrLightingAttribute->mnRefCount--;
122 : }
123 : else
124 : {
125 0 : delete mpSdrLightingAttribute;
126 : }
127 :
128 0 : mpSdrLightingAttribute = rCandidate.mpSdrLightingAttribute;
129 0 : mpSdrLightingAttribute->mnRefCount++;
130 : }
131 :
132 0 : return *this;
133 : }
134 :
135 0 : bool SdrLightingAttribute::operator==(const SdrLightingAttribute& rCandidate) const
136 : {
137 0 : if(rCandidate.mpSdrLightingAttribute == mpSdrLightingAttribute)
138 : {
139 0 : return true;
140 : }
141 :
142 0 : if(rCandidate.isDefault() != isDefault())
143 : {
144 0 : return false;
145 : }
146 :
147 0 : return (*rCandidate.mpSdrLightingAttribute == *mpSdrLightingAttribute);
148 : }
149 :
150 0 : const ::std::vector< Sdr3DLightAttribute >& SdrLightingAttribute::getLightVector() const
151 : {
152 0 : return mpSdrLightingAttribute->getLightVector();
153 : }
154 :
155 : // color model solver
156 0 : basegfx::BColor SdrLightingAttribute::solveColorModel(
157 : const basegfx::B3DVector& rNormalInEyeCoordinates,
158 : const basegfx::BColor& rColor, const basegfx::BColor& rSpecular,
159 : const basegfx::BColor& rEmission, sal_uInt16 nSpecularIntensity) const
160 : {
161 : // initialize with emissive color
162 0 : basegfx::BColor aRetval(rEmission);
163 :
164 : // take care of global ambient light
165 0 : aRetval += mpSdrLightingAttribute->getAmbientLight() * rColor;
166 :
167 : // prepare light access. Is there a light?
168 0 : const sal_uInt32 nLightCount(mpSdrLightingAttribute->getLightVector().size());
169 :
170 0 : if(nLightCount && !rNormalInEyeCoordinates.equalZero())
171 : {
172 : // prepare normal
173 0 : basegfx::B3DVector aEyeNormal(rNormalInEyeCoordinates);
174 0 : aEyeNormal.normalize();
175 :
176 0 : for(sal_uInt32 a(0L); a < nLightCount; a++)
177 : {
178 0 : const Sdr3DLightAttribute& rLight(mpSdrLightingAttribute->getLightVector()[a]);
179 0 : const double fCosFac(rLight.getDirection().scalar(aEyeNormal));
180 :
181 0 : if(basegfx::fTools::more(fCosFac, 0.0))
182 : {
183 0 : aRetval += ((rLight.getColor() * rColor) * fCosFac);
184 :
185 0 : if(rLight.getSpecular())
186 : {
187 : // expand by (0.0, 0.0, 1.0) in Z
188 0 : basegfx::B3DVector aSpecularNormal(rLight.getDirection().getX(), rLight.getDirection().getY(), rLight.getDirection().getZ() + 1.0);
189 0 : aSpecularNormal.normalize();
190 0 : double fCosFac2(aSpecularNormal.scalar(aEyeNormal));
191 :
192 0 : if(basegfx::fTools::more(fCosFac2, 0.0))
193 : {
194 0 : fCosFac2 = pow(fCosFac2, (double)nSpecularIntensity);
195 0 : aRetval += (rSpecular * fCosFac2);
196 0 : }
197 : }
198 : }
199 0 : }
200 : }
201 :
202 : // clamp to color space before usage
203 0 : aRetval.clamp();
204 :
205 0 : return aRetval;
206 : }
207 : } // end of namespace attribute
208 : } // end of namespace drawinglayer
209 :
210 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|