1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * $RCSfile: sdrattribute3d.cxx,v $ 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * $Revision: 1.5 $ 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * last change: $Author: aw $ $Date: 2008-05-27 14:11:19 $ 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * The Contents of this file are made available subject to 12*cdf0e10cSrcweir * the terms of GNU Lesser General Public License Version 2.1. 13*cdf0e10cSrcweir * 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * GNU Lesser General Public License Version 2.1 16*cdf0e10cSrcweir * ============================================= 17*cdf0e10cSrcweir * Copyright 2005 by Sun Microsystems, Inc. 18*cdf0e10cSrcweir * 901 San Antonio Road, Palo Alto, CA 94303, USA 19*cdf0e10cSrcweir * 20*cdf0e10cSrcweir * This library is free software; you can redistribute it and/or 21*cdf0e10cSrcweir * modify it under the terms of the GNU Lesser General Public 22*cdf0e10cSrcweir * License version 2.1, as published by the Free Software Foundation. 23*cdf0e10cSrcweir * 24*cdf0e10cSrcweir * This library is distributed in the hope that it will be useful, 25*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 26*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 27*cdf0e10cSrcweir * Lesser General Public License for more details. 28*cdf0e10cSrcweir * 29*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public 30*cdf0e10cSrcweir * License along with this library; if not, write to the Free Software 31*cdf0e10cSrcweir * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 32*cdf0e10cSrcweir * MA 02111-1307 USA 33*cdf0e10cSrcweir * 34*cdf0e10cSrcweir ************************************************************************/ 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 37*cdf0e10cSrcweir #include "precompiled_drawinglayer.hxx" 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir #include <drawinglayer/attribute/sdrlightingattribute3d.hxx> 40*cdf0e10cSrcweir #include <basegfx/color/bcolor.hxx> 41*cdf0e10cSrcweir #include <basegfx/vector/b3dvector.hxx> 42*cdf0e10cSrcweir #include <drawinglayer/attribute/sdrlightattribute3d.hxx> 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////// 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir namespace drawinglayer 47*cdf0e10cSrcweir { 48*cdf0e10cSrcweir namespace attribute 49*cdf0e10cSrcweir { 50*cdf0e10cSrcweir class ImpSdrLightingAttribute 51*cdf0e10cSrcweir { 52*cdf0e10cSrcweir public: 53*cdf0e10cSrcweir // refcounter 54*cdf0e10cSrcweir sal_uInt32 mnRefCount; 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir // 3D light attribute definitions 57*cdf0e10cSrcweir basegfx::BColor maAmbientLight; 58*cdf0e10cSrcweir ::std::vector< Sdr3DLightAttribute > maLightVector; 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir ImpSdrLightingAttribute( 61*cdf0e10cSrcweir const basegfx::BColor& rAmbientLight, 62*cdf0e10cSrcweir const ::std::vector< Sdr3DLightAttribute >& rLightVector) 63*cdf0e10cSrcweir : mnRefCount(0), 64*cdf0e10cSrcweir maAmbientLight(rAmbientLight), 65*cdf0e10cSrcweir maLightVector(rLightVector) 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir } 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir // data read access 70*cdf0e10cSrcweir const basegfx::BColor& getAmbientLight() const { return maAmbientLight; } 71*cdf0e10cSrcweir const ::std::vector< Sdr3DLightAttribute >& getLightVector() const { return maLightVector; } 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir bool operator==(const ImpSdrLightingAttribute& rCandidate) const 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir return (getAmbientLight() == rCandidate.getAmbientLight() 76*cdf0e10cSrcweir && getLightVector() == rCandidate.getLightVector()); 77*cdf0e10cSrcweir } 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir static ImpSdrLightingAttribute* get_global_default() 80*cdf0e10cSrcweir { 81*cdf0e10cSrcweir static ImpSdrLightingAttribute* pDefault = 0; 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir if(!pDefault) 84*cdf0e10cSrcweir { 85*cdf0e10cSrcweir pDefault = new ImpSdrLightingAttribute( 86*cdf0e10cSrcweir basegfx::BColor(), 87*cdf0e10cSrcweir std::vector< Sdr3DLightAttribute >()); 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir // never delete; start with RefCount 1, not 0 90*cdf0e10cSrcweir pDefault->mnRefCount++; 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir return pDefault; 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir }; 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir SdrLightingAttribute::SdrLightingAttribute( 98*cdf0e10cSrcweir const basegfx::BColor& rAmbientLight, 99*cdf0e10cSrcweir const ::std::vector< Sdr3DLightAttribute >& rLightVector) 100*cdf0e10cSrcweir : mpSdrLightingAttribute(new ImpSdrLightingAttribute( 101*cdf0e10cSrcweir rAmbientLight, rLightVector)) 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir SdrLightingAttribute::SdrLightingAttribute() 106*cdf0e10cSrcweir : mpSdrLightingAttribute(ImpSdrLightingAttribute::get_global_default()) 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir mpSdrLightingAttribute->mnRefCount++; 109*cdf0e10cSrcweir } 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir SdrLightingAttribute::SdrLightingAttribute(const SdrLightingAttribute& rCandidate) 112*cdf0e10cSrcweir : mpSdrLightingAttribute(rCandidate.mpSdrLightingAttribute) 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir mpSdrLightingAttribute->mnRefCount++; 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir SdrLightingAttribute::~SdrLightingAttribute() 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir if(mpSdrLightingAttribute->mnRefCount) 120*cdf0e10cSrcweir { 121*cdf0e10cSrcweir mpSdrLightingAttribute->mnRefCount--; 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir else 124*cdf0e10cSrcweir { 125*cdf0e10cSrcweir delete mpSdrLightingAttribute; 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir bool SdrLightingAttribute::isDefault() const 130*cdf0e10cSrcweir { 131*cdf0e10cSrcweir return mpSdrLightingAttribute == ImpSdrLightingAttribute::get_global_default(); 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir SdrLightingAttribute& SdrLightingAttribute::operator=(const SdrLightingAttribute& rCandidate) 135*cdf0e10cSrcweir { 136*cdf0e10cSrcweir if(rCandidate.mpSdrLightingAttribute != mpSdrLightingAttribute) 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir if(mpSdrLightingAttribute->mnRefCount) 139*cdf0e10cSrcweir { 140*cdf0e10cSrcweir mpSdrLightingAttribute->mnRefCount--; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir else 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir delete mpSdrLightingAttribute; 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir mpSdrLightingAttribute = rCandidate.mpSdrLightingAttribute; 148*cdf0e10cSrcweir mpSdrLightingAttribute->mnRefCount++; 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir return *this; 152*cdf0e10cSrcweir } 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir bool SdrLightingAttribute::operator==(const SdrLightingAttribute& rCandidate) const 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir if(rCandidate.mpSdrLightingAttribute == mpSdrLightingAttribute) 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir return true; 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir if(rCandidate.isDefault() != isDefault()) 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir return false; 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir return (*rCandidate.mpSdrLightingAttribute == *mpSdrLightingAttribute); 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir const basegfx::BColor& SdrLightingAttribute::getAmbientLight() const 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir return mpSdrLightingAttribute->getAmbientLight(); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir const ::std::vector< Sdr3DLightAttribute >& SdrLightingAttribute::getLightVector() const 175*cdf0e10cSrcweir { 176*cdf0e10cSrcweir return mpSdrLightingAttribute->getLightVector(); 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir // color model solver 180*cdf0e10cSrcweir basegfx::BColor SdrLightingAttribute::solveColorModel( 181*cdf0e10cSrcweir const basegfx::B3DVector& rNormalInEyeCoordinates, 182*cdf0e10cSrcweir const basegfx::BColor& rColor, const basegfx::BColor& rSpecular, 183*cdf0e10cSrcweir const basegfx::BColor& rEmission, sal_uInt16 nSpecularIntensity) const 184*cdf0e10cSrcweir { 185*cdf0e10cSrcweir // initialize with emissive color 186*cdf0e10cSrcweir basegfx::BColor aRetval(rEmission); 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir // take care of global ambient light 189*cdf0e10cSrcweir aRetval += mpSdrLightingAttribute->getAmbientLight() * rColor; 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir // prepare light access. Is there a light? 192*cdf0e10cSrcweir const sal_uInt32 nLightCount(mpSdrLightingAttribute->getLightVector().size()); 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir if(nLightCount && !rNormalInEyeCoordinates.equalZero()) 195*cdf0e10cSrcweir { 196*cdf0e10cSrcweir // prepare normal 197*cdf0e10cSrcweir basegfx::B3DVector aEyeNormal(rNormalInEyeCoordinates); 198*cdf0e10cSrcweir aEyeNormal.normalize(); 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir for(sal_uInt32 a(0L); a < nLightCount; a++) 201*cdf0e10cSrcweir { 202*cdf0e10cSrcweir const Sdr3DLightAttribute& rLight(mpSdrLightingAttribute->getLightVector()[a]); 203*cdf0e10cSrcweir const double fCosFac(rLight.getDirection().scalar(aEyeNormal)); 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir if(basegfx::fTools::more(fCosFac, 0.0)) 206*cdf0e10cSrcweir { 207*cdf0e10cSrcweir aRetval += ((rLight.getColor() * rColor) * fCosFac); 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir if(rLight.getSpecular()) 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir // expand by (0.0, 0.0, 1.0) in Z 212*cdf0e10cSrcweir basegfx::B3DVector aSpecularNormal(rLight.getDirection().getX(), rLight.getDirection().getY(), rLight.getDirection().getZ() + 1.0); 213*cdf0e10cSrcweir aSpecularNormal.normalize(); 214*cdf0e10cSrcweir double fCosFac2(aSpecularNormal.scalar(aEyeNormal)); 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir if(basegfx::fTools::more(fCosFac2, 0.0)) 217*cdf0e10cSrcweir { 218*cdf0e10cSrcweir fCosFac2 = pow(fCosFac2, (double)nSpecularIntensity); 219*cdf0e10cSrcweir aRetval += (rSpecular * fCosFac2); 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir } 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir // clamp to color space before usage 227*cdf0e10cSrcweir aRetval.clamp(); 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir return aRetval; 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir } // end of namespace attribute 232*cdf0e10cSrcweir } // end of namespace drawinglayer 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////// 235*cdf0e10cSrcweir // eof 236