tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / svx / source / customshapes / EnhancedCustomShape3d.cxx
blobcd2d1c751ebab4b8d4a464c7fecae6f2f00c7012
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "EnhancedCustomShape3d.hxx"
21 #include <o3tl/unit_conversion.hxx>
22 #include <svx/deflt3d.hxx>
23 #include <svx/svdmodel.hxx>
24 #include <tools/poly.hxx>
25 #include <svx/svditer.hxx>
26 #include <svx/svdobj.hxx>
27 #include <svx/svdoashp.hxx>
28 #include <svl/itemset.hxx>
29 #include <svl/whiter.hxx>
30 #include <svx/xfillit0.hxx>
31 #include <svx/xlineit0.hxx>
32 #include <svx/xsflclit.hxx>
33 #include <svx/xbtmpit.hxx>
34 #include <svx/xflclit.hxx>
35 #include <svx/svdopath.hxx>
36 #include <svx/svddef.hxx>
37 #include <svx/svx3ditems.hxx>
38 #include <extrud3d.hxx>
39 #include <svx/xflbmtit.hxx>
40 #include <svx/xlnclit.hxx>
41 #include <svx/sdasitm.hxx>
42 #include <svx/scene3d.hxx>
43 #include <com/sun/star/drawing/Position3D.hpp>
44 #include <com/sun/star/drawing/Direction3D.hpp>
45 #include <com/sun/star/drawing/NormalsKind.hpp>
46 #include <com/sun/star/drawing/ShadeMode.hpp>
47 #include <svx/sdr/properties/properties.hxx>
48 #include <com/sun/star/drawing/EnhancedCustomShapeParameterPair.hpp>
49 #include <com/sun/star/drawing/EnhancedCustomShapeMetalType.hpp>
50 #include <com/sun/star/drawing/ProjectionMode.hpp>
51 #include <basegfx/color/bcolor.hxx>
52 #include <basegfx/polygon/b2dpolypolygontools.hxx>
53 #include <basegfx/polygon/b3dpolygon.hxx>
54 #include <basegfx/range/b2drange.hxx>
55 #include <sdr/primitive2d/sdrattributecreator.hxx>
56 #include <drawinglayer/attribute/sdrlineattribute.hxx>
57 #include <drawinglayer/attribute/sdrlinestartendattribute.hxx>
58 #include <svx/xlnwtit.hxx>
59 #include <svx/xlntrit.hxx>
60 #include <svx/xfltrit.hxx>
61 #include <comphelper/configuration.hxx>
63 using namespace com::sun::star;
64 using namespace com::sun::star::uno;
66 namespace {
68 void GetOrigin( const SdrCustomShapeGeometryItem& rItem, double& rOriginX, double& rOriginY )
70 css::drawing::EnhancedCustomShapeParameterPair aOriginParaPair;
71 const Any* pAny = rItem.GetPropertyValueByName( u"Extrusion"_ustr, u"Origin"_ustr );
72 if ( ! ( pAny && ( *pAny >>= aOriginParaPair ) && ( aOriginParaPair.First.Value >>= rOriginX ) && ( aOriginParaPair.Second.Value >>= rOriginY ) ) )
74 rOriginX = 0.50;
75 rOriginY =-0.50;
79 void GetRotateAngle( const SdrCustomShapeGeometryItem& rItem, double& rAngleX, double& rAngleY )
81 css::drawing::EnhancedCustomShapeParameterPair aRotateAngleParaPair;
82 const Any* pAny = rItem.GetPropertyValueByName( u"Extrusion"_ustr, u"RotateAngle"_ustr );
83 if ( ! ( pAny && ( *pAny >>= aRotateAngleParaPair ) && ( aRotateAngleParaPair.First.Value >>= rAngleX ) && ( aRotateAngleParaPair.Second.Value >>= rAngleY ) ) )
85 rAngleX = 0.0;
86 rAngleY = 0.0;
88 rAngleX = basegfx::deg2rad(rAngleX);
89 rAngleY = basegfx::deg2rad(rAngleY);
92 void GetSkew( const SdrCustomShapeGeometryItem& rItem, double& rSkewAmount, double& rSkewAngle )
94 css::drawing::EnhancedCustomShapeParameterPair aSkewParaPair;
95 const Any* pAny = rItem.GetPropertyValueByName( u"Extrusion"_ustr, u"Skew"_ustr );
96 if ( ! ( pAny && ( *pAny >>= aSkewParaPair ) && ( aSkewParaPair.First.Value >>= rSkewAmount ) && ( aSkewParaPair.Second.Value >>= rSkewAngle ) ) )
98 rSkewAmount = 50;
99 // ODF default is 45, but older ODF documents expect -135 as default. For intermediate
100 // solution see tdf#141301 and tdf#141127.
101 // MS Office default -135 is set in msdffimp.cxx to make import independent from setting here.
102 rSkewAngle = -135;
104 rSkewAngle = basegfx::deg2rad(rSkewAngle);
107 void GetExtrusionDepth( const SdrCustomShapeGeometryItem& rItem, const double* pMap, double& rBackwardDepth, double& rForwardDepth )
109 css::drawing::EnhancedCustomShapeParameterPair aDepthParaPair;
110 double fDepth = 0, fFraction = 0;
111 const Any* pAny = rItem.GetPropertyValueByName( u"Extrusion"_ustr, u"Depth"_ustr );
112 if ( pAny && ( *pAny >>= aDepthParaPair ) && ( aDepthParaPair.First.Value >>= fDepth ) && ( aDepthParaPair.Second.Value >>= fFraction ) )
114 rForwardDepth = fDepth * fFraction;
115 rBackwardDepth = fDepth - rForwardDepth;
117 else
119 rBackwardDepth = 1270;
120 rForwardDepth = 0;
122 if ( pMap )
124 double fMap = *pMap;
125 rBackwardDepth *= fMap;
126 rForwardDepth *= fMap;
130 double GetDouble( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, double fDefault )
132 double fRetValue = fDefault;
133 const Any* pAny = rItem.GetPropertyValueByName( u"Extrusion"_ustr, rPropertyName );
134 if ( pAny )
135 *pAny >>= fRetValue;
136 return fRetValue;
139 drawing::ShadeMode GetShadeMode( const SdrCustomShapeGeometryItem& rItem, const drawing::ShadeMode eDefault )
141 drawing::ShadeMode eRet( eDefault );
142 const Any* pAny = rItem.GetPropertyValueByName( u"Extrusion"_ustr, u"ShadeMode"_ustr );
143 if ( pAny )
145 if (!(*pAny >>= eRet))
147 sal_Int32 nEnum = 0;
148 if(*pAny >>= nEnum)
150 eRet = static_cast<drawing::ShadeMode>(nEnum);
154 return eRet;
157 bool GetBool( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, const bool bDefault )
159 bool bRetValue = bDefault;
160 const Any* pAny = rItem.GetPropertyValueByName( u"Extrusion"_ustr, rPropertyName );
161 if ( pAny )
162 *pAny >>= bRetValue;
163 return bRetValue;
166 drawing::Position3D GetPosition3D( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName,
167 const drawing::Position3D& rDefault, const double* pMap )
169 drawing::Position3D aRetValue( rDefault );
170 const Any* pAny = rItem.GetPropertyValueByName( u"Extrusion"_ustr, rPropertyName );
171 if ( pAny )
172 *pAny >>= aRetValue;
173 if ( pMap )
175 aRetValue.PositionX *= *pMap;
176 aRetValue.PositionY *= *pMap;
177 aRetValue.PositionZ *= *pMap;
179 return aRetValue;
182 drawing::Direction3D GetDirection3D( const SdrCustomShapeGeometryItem& rItem, const OUString& rPropertyName, const drawing::Direction3D& rDefault )
184 drawing::Direction3D aRetValue( rDefault );
185 const Any* pAny = rItem.GetPropertyValueByName( u"Extrusion"_ustr, rPropertyName );
186 if ( pAny )
187 *pAny >>= aRetValue;
188 return aRetValue;
191 sal_Int16 GetMetalType(const SdrCustomShapeGeometryItem& rItem, const sal_Int16 eDefault)
193 sal_Int16 aRetValue(eDefault);
194 const Any* pAny = rItem.GetPropertyValueByName(u"Extrusion"_ustr, u"MetalType"_ustr);
195 if (pAny)
196 *pAny >>= aRetValue;
197 return aRetValue;
200 // Calculates the light directions for the additional lights, which are used to emulate soft
201 // lights of MS Office. Method needs to be documented in the Wiki
202 // https://wiki.documentfoundation.org/Development/ODF_Implementer_Notes in part
203 // List_of_LibreOffice_ODF_implementation-defined_items
204 // The method expects vector rLight to be normalized and results normalized vectors.
205 void lcl_SoftLightsDirection(const basegfx::B3DVector& rLight, basegfx::B3DVector& rSoftUp,
206 basegfx::B3DVector& rSoftDown, basegfx::B3DVector& rSoftRight,
207 basegfx::B3DVector& rSoftLeft)
209 constexpr double fAngle = basegfx::deg2rad(60); // angle between regular light and soft light
211 // We first create directions around (0|0|1) and then rotate them to the light position.
212 rSoftUp = basegfx::B3DVector(0.0, sin(fAngle), cos(fAngle));
213 rSoftDown = basegfx::B3DVector(0.0, -sin(fAngle), cos(fAngle));
214 rSoftRight = basegfx::B3DVector(sin(fAngle), 0.0, cos(fAngle));
215 rSoftLeft = basegfx::B3DVector(-sin(fAngle), 0.0, cos(fAngle));
217 basegfx::B3DHomMatrix aRotateMat;
218 aRotateMat.rotate(0.0, 0.0, M_PI_4);
219 if (rLight.getX() == 0.0 && rLight.getZ() == 0.0)
221 // Special case with light from top or bottom
222 if (rLight.getY() >= 0.0)
223 aRotateMat.rotate(-M_PI_2, 0.0, 0.0);
224 else
225 aRotateMat.rotate(M_PI_2, 0.0, 0.0);
227 else
229 // Azimuth from z-axis to x-axis. (0|0|1) to (1|0|0) is 90deg.
230 double fAzimuth = atan2(rLight.getX(), rLight.getZ());
231 // Elevation from xz-plane to y-axis. (0|0|1) to (0|1|0) is 90deg.
232 double fElevation = atan2(rLight.getY(), std::hypot(rLight.getX(), rLight.getZ()));
233 aRotateMat.rotate(-fElevation, fAzimuth, 0.0);
236 rSoftUp = aRotateMat * rSoftUp;
237 rSoftDown = aRotateMat * rSoftDown;
238 rSoftRight = aRotateMat * rSoftRight;
239 rSoftLeft = aRotateMat * rSoftLeft;
243 rtl::Reference<SdrObject> EnhancedCustomShape3d::Create3DObject(
244 const SdrObject* pShape2d,
245 const SdrObjCustomShape& rSdrObjCustomShape)
247 rtl::Reference<SdrObject> pRet;
248 const SdrCustomShapeGeometryItem& rGeometryItem(rSdrObjCustomShape.GetMergedItem(SDRATTR_CUSTOMSHAPE_GEOMETRY));
249 double fMap(1.0), *pMap = nullptr;
251 if ( rSdrObjCustomShape.getSdrModelFromSdrObject().GetScaleUnit() != MapUnit::Map100thMM )
253 DBG_ASSERT( rSdrObjCustomShape.getSdrModelFromSdrObject().GetScaleUnit() == MapUnit::MapTwip, "EnhancedCustomShape3d::Current MapMode is Unsupported" );
254 // But we could use MapToO3tlUnit from <tools/UnitConversion> ... ?
255 fMap *= o3tl::convert(1.0, o3tl::Length::mm100, o3tl::Length::twip);
256 pMap = &fMap;
259 if ( GetBool( rGeometryItem, u"Extrusion"_ustr, false ) )
261 bool bIsMirroredX(rSdrObjCustomShape.IsMirroredX());
262 bool bIsMirroredY(rSdrObjCustomShape.IsMirroredY());
263 tools::Rectangle aSnapRect(rSdrObjCustomShape.GetLogicRect());
264 Degree100 nObjectRotation(rSdrObjCustomShape.GetRotateAngle());
265 if ( nObjectRotation )
267 double a = toRadians(36000_deg100 - nObjectRotation);
268 tools::Long dx = aSnapRect.Right() - aSnapRect.Left();
269 tools::Long dy = aSnapRect.Bottom()- aSnapRect.Top();
270 Point aP( aSnapRect.TopLeft() );
271 RotatePoint( aP, rSdrObjCustomShape.GetSnapRect().Center(), sin( a ), cos( a ) );
272 aSnapRect.SetLeft( aP.X() );
273 aSnapRect.SetTop( aP.Y() );
274 aSnapRect.SetRight( aSnapRect.Left() + dx );
275 aSnapRect.SetBottom( aSnapRect.Top() + dy );
277 Point aCenter( aSnapRect.Center() );
279 SfxItemSet aSet( rSdrObjCustomShape.GetMergedItemSet() );
281 // tdf#146360 If the ItemSet of the source SdrObject has a parent
282 // (which means it has a StyleSheet), we need to do some old-style
283 // 'BurnInStyleSheetAttributes' action.
284 // That means to set all Items which are set in the StyleSheet
285 // directly in the ItemSet.
286 // This is okay here since the 3D SdrObjects created are
287 // placeholders that get rendered, but never reach the
288 // surface/the user. If attributes for the source SdrObject
289 // change, these will be recreated.
290 // The problem is that while "aSet" still has a ptr to the style's
291 // ItemSet, this gets lost at the ItemSet of the SdrObject when
292 // an ItemSet gets set at the 3D SdrObject, like in diverse
293 // SetMergedItemSet calls below. This leads to fetching the wrong
294 // (default) FillBitmap in the calls p3DObj->GetMergedItem below
295 // (which is 32x32 white, that's what you see without the fix).
296 // This could also be fixed (tried it) by either
297 // - using rSdrObjCustomShape.GetMergedItem
298 // - setting the StyleSheet at 3D SdrObjects ASAP (done at caller)
299 // but both solutions contain the risk to not find all places, so
300 // it's just more safe to merge the StyleSheet attributes to the
301 // ItemSet used for the whole creation.
302 if(nullptr != aSet.GetParent())
304 SfxWhichIter aIter(aSet);
305 sal_uInt16 nWhich(aIter.FirstWhich());
306 const SfxPoolItem *pItem(nullptr);
308 while(nWhich)
310 // this may look at 1st look like doing nothing, but it converts
311 // items set in parent/style to SfxItemState::SET items in the
312 // ItemSet (see AttributeProperties::ForceStyleToHardAttributes())
313 if(SfxItemState::SET == aSet.GetItemState(nWhich, true, &pItem))
315 aSet.Put(*pItem);
318 nWhich = aIter.NextWhich();
321 aSet.SetParent(nullptr);
324 //SJ: vertical writing is not required, by removing this item no outliner is created
325 aSet.ClearItem( SDRATTR_TEXTDIRECTION );
327 // #i105323# For 3D AutoShapes, the shadow attribute has to be applied to each
328 // created visualisation helper model shape individually. The shadow itself
329 // will then be rendered from the 3D renderer correctly for the whole 3D scene
330 // (and thus behind all objects of which the visualisation may be built). So,
331 // do NOT remove it from the ItemSet here.
332 // aSet.ClearItem(SDRATTR_SHADOW);
334 std::vector< E3dCompoundObject* > aPlaceholderObjectList;
336 double fExtrusionBackward, fExtrusionForward;
337 GetExtrusionDepth( rGeometryItem, pMap, fExtrusionBackward, fExtrusionForward );
338 double fDepth = fExtrusionBackward + fExtrusionForward;
339 if ( fDepth < 1.0 )
340 fDepth = 1.0;
342 drawing::ProjectionMode eProjectionMode( drawing::ProjectionMode_PARALLEL );
343 const Any* pAny = rGeometryItem.GetPropertyValueByName( u"Extrusion"_ustr, u"ProjectionMode"_ustr );
344 if (pAny)
346 if(!(*pAny >>= eProjectionMode))
348 sal_Int32 nEnum = 0;
349 if(*pAny >>= nEnum)
351 eProjectionMode = static_cast<drawing::ProjectionMode>(nEnum);
355 // pShape2d Convert in scenes which include 3D Objects
356 E3dDefaultAttributes a3DDefaultAttr;
357 a3DDefaultAttr.SetDefaultLatheCharacterMode( true );
358 a3DDefaultAttr.SetDefaultExtrudeCharacterMode( true );
360 rtl::Reference<E3dScene> pScene = new E3dScene(rSdrObjCustomShape.getSdrModelFromSdrObject());
362 bool bSceneHasObjects ( false );
363 bool bUseTwoFillStyles( false );
365 drawing::ShadeMode eShadeMode( GetShadeMode( rGeometryItem, drawing::ShadeMode_FLAT ) );
366 bool bUseExtrusionColor = GetBool( rGeometryItem, u"Color"_ustr, false );
368 drawing::FillStyle eFillStyle( aSet.Get(XATTR_FILLSTYLE).GetValue() );
369 pScene->GetProperties().SetObjectItem( Svx3DShadeModeItem(static_cast<sal_uInt16>(eShadeMode)));
370 aSet.Put( makeSvx3DPercentDiagonalItem( 0 ) );
371 aSet.Put( Svx3DTextureModeItem( 1 ) );
372 // SPECIFIC needed for ShadeMode_SMOOTH and ShadeMode_PHONG, otherwise FLAT is faster.
373 if (eShadeMode == drawing::ShadeMode_SMOOTH || eShadeMode == drawing::ShadeMode_PHONG)
374 aSet.Put( Svx3DNormalsKindItem(static_cast<sal_uInt16>(drawing::NormalsKind_SPECIFIC)));
375 else
376 aSet.Put( Svx3DNormalsKindItem(static_cast<sal_uInt16>(drawing::NormalsKind_FLAT)));
378 if ( eShadeMode == drawing::ShadeMode_DRAFT )
380 aSet.Put( XLineStyleItem( drawing::LineStyle_SOLID ) );
381 aSet.Put( XFillStyleItem ( drawing::FillStyle_NONE ) );
382 aSet.Put( makeSvx3DDoubleSidedItem( true ) );
384 else
386 aSet.Put( XLineStyleItem( drawing::LineStyle_NONE ) );
387 if ( eFillStyle == drawing::FillStyle_NONE )
388 aSet.Put( XFillStyleItem( drawing::FillStyle_SOLID ) );
389 else if ( ( eFillStyle == drawing::FillStyle_BITMAP ) || ( eFillStyle == drawing::FillStyle_GRADIENT ) || bUseExtrusionColor )
390 bUseTwoFillStyles = true;
392 // If shapes are mirrored once (mirroring two times correct geometry again)
393 // double-sided at the object and two-sided-lighting at the scene need to be set.
395 // #i122777# Also use double sided for two fill styles since there several 3d objects get
396 // created with a depth of 0; one of them is the backside which needs double-sided to
397 // get visible
398 if(bUseTwoFillStyles || (bIsMirroredX && !bIsMirroredY) || (!bIsMirroredX && bIsMirroredY))
400 aSet.Put( makeSvx3DDoubleSidedItem( true ) );
401 pScene->GetProperties().SetObjectItem( makeSvx3DTwoSidedLightingItem( true ) );
405 tools::Rectangle aBoundRect2d;
406 basegfx::B2DPolyPolygon aTotalPolyPoly;
407 SdrObjListIter aIter( *pShape2d, SdrIterMode::DeepNoGroups );
408 const bool bMultipleSubObjects(aIter.Count() > 1);
409 const bool bFuzzing(comphelper::IsFuzzing());
411 while( aIter.IsMore() )
413 const SdrObject* pNext = aIter.Next();
414 bool bIsPlaceholderObject = (pNext->GetMergedItem( XATTR_FILLSTYLE ).GetValue() == drawing::FillStyle_NONE )
415 && (pNext->GetMergedItem( XATTR_LINESTYLE ).GetValue() == drawing::LineStyle_NONE );
416 basegfx::B2DPolyPolygon aPolyPoly;
417 SfxItemSet aLocalSet(aSet);
418 drawing::FillStyle aLocalFillStyle(eFillStyle);
420 if ( auto pPathObj = dynamic_cast<const SdrPathObj*>(pNext) )
422 const SfxItemSet& rSet = pNext->GetMergedItemSet();
423 bool bNeedToConvertToContour(false);
425 // do conversion only for single line objects; for all others a fill and a
426 // line object get created. When we have fill, we want no line. That line has
427 // always been there, but since it was never converted to contour, it kept
428 // invisible (all this 'hidden' logic should be migrated to primitives).
429 if(!bMultipleSubObjects)
431 const drawing::FillStyle eStyle(rSet.Get(XATTR_FILLSTYLE).GetValue());
433 if(drawing::FillStyle_NONE == eStyle)
435 const drawinglayer::attribute::SdrLineAttribute aLine(
436 drawinglayer::primitive2d::createNewSdrLineAttribute(rSet));
438 bNeedToConvertToContour = (0.0 < aLine.getWidth() || 0.0 != aLine.getFullDotDashLen());
440 if(!bNeedToConvertToContour && !aLine.isDefault())
442 const drawinglayer::attribute::SdrLineStartEndAttribute aLineStartEnd(
443 drawinglayer::primitive2d::createNewSdrLineStartEndAttribute(rSet, aLine.getWidth()));
445 if((aLineStartEnd.getStartWidth() && aLineStartEnd.isStartActive())
446 || (aLineStartEnd.getEndWidth() && aLineStartEnd.isEndActive()))
448 bNeedToConvertToContour = true;
454 if (bNeedToConvertToContour && !bFuzzing)
456 rtl::Reference<SdrObject> pNewObj = pNext->ConvertToContourObj(const_cast< SdrObject* >(pNext));
457 SdrPathObj* pNewPathObj = dynamic_cast< SdrPathObj* >(pNewObj.get());
459 if(pNewPathObj)
461 aPolyPoly = pNewPathObj->GetPathPoly();
463 if(aPolyPoly.isClosed())
465 // correct item properties from line to fill style
466 if(eShadeMode == drawing::ShadeMode_DRAFT)
468 // for draft, create wireframe with fixed line width
469 aLocalSet.Put(XLineStyleItem(drawing::LineStyle_SOLID));
470 aLocalSet.Put(XLineWidthItem(40));
471 aLocalFillStyle = drawing::FillStyle_NONE;
473 else
475 // switch from line to fill, copy line attr to fill attr (color, transparence)
476 aLocalSet.Put(XLineWidthItem(0));
477 aLocalSet.Put(XLineStyleItem(drawing::LineStyle_NONE));
478 aLocalSet.Put(XFillColorItem(OUString(), aLocalSet.Get(XATTR_LINECOLOR).GetColorValue()));
479 aLocalSet.Put(XFillStyleItem(drawing::FillStyle_SOLID));
480 aLocalSet.Put(XFillTransparenceItem(aLocalSet.Get(XATTR_LINETRANSPARENCE).GetValue()));
481 aLocalFillStyle = drawing::FillStyle_SOLID;
484 else
486 // correct item properties to hairlines
487 aLocalSet.Put(XLineWidthItem(0));
488 aLocalSet.Put(XLineStyleItem(drawing::LineStyle_SOLID));
492 else
494 aPolyPoly = pPathObj->GetPathPoly();
497 else
499 rtl::Reference<SdrObject> pNewObj = pNext->ConvertToPolyObj( false, false );
500 SdrPathObj* pPath = dynamic_cast<SdrPathObj*>( pNewObj.get() );
501 if ( pPath )
502 aPolyPoly = pPath->GetPathPoly();
505 if( aPolyPoly.count() )
507 if(aPolyPoly.areControlPointsUsed())
509 aPolyPoly = basegfx::utils::adaptiveSubdivideByAngle(aPolyPoly);
512 const basegfx::B2DRange aTempRange(basegfx::utils::getRange(aPolyPoly));
513 const tools::Rectangle aBoundRect(basegfx::fround<tools::Long>(aTempRange.getMinX()), basegfx::fround<tools::Long>(aTempRange.getMinY()), basegfx::fround<tools::Long>(aTempRange.getMaxX()), basegfx::fround<tools::Long>(aTempRange.getMaxY()));
514 aTotalPolyPoly.append(aPolyPoly);
515 aBoundRect2d.Union( aBoundRect );
517 // #i122777# depth 0 is okay for planes when using double-sided
518 rtl::Reference<E3dCompoundObject> p3DObj = new E3dExtrudeObj(
519 rSdrObjCustomShape.getSdrModelFromSdrObject(),
520 a3DDefaultAttr,
521 aPolyPoly,
522 bUseTwoFillStyles ? 0 : fDepth );
524 p3DObj->NbcSetLayer( pShape2d->GetLayer() );
525 p3DObj->SetMergedItemSet( aLocalSet );
527 if ( bIsPlaceholderObject )
528 aPlaceholderObjectList.push_back( p3DObj.get() );
529 else if ( bUseTwoFillStyles )
531 BitmapEx aFillBmp;
532 bool bFillBmpTile = p3DObj->GetMergedItem( XATTR_FILLBMP_TILE ).GetValue();
533 if ( bFillBmpTile )
535 const XFillBitmapItem& rBmpItm = p3DObj->GetMergedItem(XATTR_FILLBITMAP);
536 aFillBmp = rBmpItm.GetGraphicObject().GetGraphic().GetBitmapEx();
538 // #i122777# old adaptation of FillStyle bitmap size to 5-times the original size; this is not needed
539 // anymore and was used in old times to male the fill look better when converting to 3D. Removed
540 // from regular 3D objects for some time, also needs to be removed from CustomShapes
542 //Size aLogicalSize = aFillBmp.GetPrefSize();
543 //if ( aFillBmp.GetPrefMapMode() == MapUnit::MapPixel )
544 // aLogicalSize = Application::GetDefaultDevice()->PixelToLogic( aLogicalSize, MapUnit::Map100thMM );
545 //else
546 // aLogicalSize = OutputDevice::LogicToLogic( aLogicalSize, aFillBmp.GetPrefMapMode(), MapUnit::Map100thMM );
547 //aLogicalSize.Width() *= 5; ;// :-( nice scaling, look at engine3d/obj3d.cxx
548 //aLogicalSize.Height() *= 5;
549 //aFillBmp.SetPrefSize( aLogicalSize );
550 //aFillBmp.SetPrefMapMode( MapUnit::Map100thMM );
551 //p3DObj->SetMergedItem(XFillBitmapItem(String(), Graphic(aFillBmp)));
553 else
555 if ( aSnapRect != aBoundRect && aSnapRect.GetWidth() > 0 && aSnapRect.GetHeight() > 0)
557 const XFillBitmapItem& rBmpItm = p3DObj->GetMergedItem(XATTR_FILLBITMAP);
558 aFillBmp = rBmpItm.GetGraphicObject().GetGraphic().GetBitmapEx();
559 Size aBmpSize( aFillBmp.GetSizePixel() );
560 double fXScale = static_cast<double>(aBoundRect.GetWidth()) / static_cast<double>(aSnapRect.GetWidth());
561 double fYScale = static_cast<double>(aBoundRect.GetHeight()) / static_cast<double>(aSnapRect.GetHeight());
563 Point aPt( static_cast<sal_Int32>( static_cast<double>( aBoundRect.Left() - aSnapRect.Left() )* static_cast<double>(aBmpSize.Width()) / static_cast<double>(aSnapRect.GetWidth()) ),
564 static_cast<sal_Int32>( static_cast<double>( aBoundRect.Top() - aSnapRect.Top() ) * static_cast<double>(aBmpSize.Height()) / static_cast<double>(aSnapRect.GetHeight()) ) );
565 Size aSize( static_cast<sal_Int32>( aBmpSize.Width() * fXScale ),
566 static_cast<sal_Int32>( aBmpSize.Height() * fYScale ) );
567 tools::Rectangle aCropRect( aPt, aSize );
568 aFillBmp.Crop( aCropRect );
569 p3DObj->SetMergedItem(XFillBitmapItem(OUString(), Graphic(aFillBmp)));
572 pScene->InsertObject( p3DObj.get() );
573 p3DObj = new E3dExtrudeObj(
574 rSdrObjCustomShape.getSdrModelFromSdrObject(),
575 a3DDefaultAttr,
576 aPolyPoly,
577 fDepth);
578 p3DObj->NbcSetLayer( pShape2d->GetLayer() );
579 p3DObj->SetMergedItemSet( aLocalSet );
580 if ( bUseExtrusionColor )
581 p3DObj->SetMergedItem( XFillColorItem( u""_ustr, rSdrObjCustomShape.GetMergedItem( XATTR_SECONDARYFILLCOLOR ).GetColorValue() ) );
582 p3DObj->SetMergedItem( XFillStyleItem( drawing::FillStyle_SOLID ) );
583 p3DObj->SetMergedItem( Svx3DCloseFrontItem( false ) );
584 p3DObj->SetMergedItem( Svx3DCloseBackItem( false ) );
585 pScene->InsertObject( p3DObj.get() );
587 // #i122777# depth 0 is okay for planes when using double-sided
588 p3DObj = new E3dExtrudeObj(
589 rSdrObjCustomShape.getSdrModelFromSdrObject(),
590 a3DDefaultAttr,
591 std::move(aPolyPoly),
594 p3DObj->NbcSetLayer( pShape2d->GetLayer() );
595 p3DObj->SetMergedItemSet( aLocalSet );
597 basegfx::B3DHomMatrix aFrontTransform( p3DObj->GetTransform() );
598 aFrontTransform.translate( 0.0, 0.0, fDepth );
599 p3DObj->NbcSetTransform( aFrontTransform );
601 if ( ( aLocalFillStyle == drawing::FillStyle_BITMAP ) && !aFillBmp.IsEmpty() )
603 p3DObj->SetMergedItem(XFillBitmapItem(OUString(), Graphic(aFillBmp)));
606 else if ( aLocalFillStyle == drawing::FillStyle_NONE )
608 const XLineColorItem& rLineColor = p3DObj->GetMergedItem( XATTR_LINECOLOR );
609 p3DObj->SetMergedItem( XFillColorItem( u""_ustr, rLineColor.GetColorValue() ) );
610 p3DObj->SetMergedItem( makeSvx3DDoubleSidedItem( true ) );
611 p3DObj->SetMergedItem( Svx3DCloseFrontItem( false ) );
612 p3DObj->SetMergedItem( Svx3DCloseBackItem( false ) );
614 pScene->InsertObject( p3DObj.get() );
615 bSceneHasObjects = true;
619 if ( bSceneHasObjects ) // is the SdrObject properly converted
621 // then we can change the return value
622 pRet = pScene;
624 // Camera settings, Perspective ...
625 Camera3D rCamera = pScene->GetCamera();
626 pScene->NbcSetSnapRect( aSnapRect );
628 // InitScene replacement
629 double fW = aBoundRect2d.getOpenWidth();
630 double fH = aBoundRect2d.getOpenHeight();
631 rCamera.SetAutoAdjustProjection( false );
632 rCamera.SetViewWindow( -fW / 2, - fH / 2, fW, fH);
633 basegfx::B3DPoint aLookAt( 0.0, 0.0, 0.0 );
634 basegfx::B3DPoint aCamPos( 0.0, 0.0, 100.0 );
635 rCamera.SetPosAndLookAt( aCamPos, aLookAt );
636 rCamera.SetFocalLength( 1.0 );
637 ProjectionType eProjectionType( eProjectionMode == drawing::ProjectionMode_PARALLEL ? ProjectionType::Parallel : ProjectionType::Perspective );
638 rCamera.SetProjection( eProjectionType );
639 pScene->SetCamera( rCamera );
640 pScene->SetBoundAndSnapRectsDirty();
642 basegfx::B3DHomMatrix aNewTransform( pScene->GetTransform() );
643 basegfx::B2DHomMatrix aPolyPolyTransform;
644 // Apply flip and z-rotation to scene transformation (y up). At same time transform
645 // aTotalPolyPoly (y down) which will be used for 2D boundRect of shape having 2D
646 // transformations applied.
648 // API values use shape center as origin. Move scene so, that shape center is origin.
649 aNewTransform.translate( -aCenter.X(), aCenter.Y(), -fExtrusionBackward);
650 aPolyPolyTransform.translate(-aCenter.X(), -aCenter.Y());
652 double fZRotate(basegfx::deg2rad(rSdrObjCustomShape.GetObjectRotation()));
653 if ( fZRotate != 0.0 )
655 aNewTransform.rotate( 0.0, 0.0, fZRotate );
656 aPolyPolyTransform.rotate(-fZRotate);
658 if ( bIsMirroredX )
660 aNewTransform.scale( -1.0, 1, 1 );
661 aPolyPolyTransform.scale(-1.0, 1);
663 if ( bIsMirroredY )
665 aNewTransform.scale( 1, -1.0, 1 );
666 aPolyPolyTransform.scale(1, -1.0);
668 aPolyPolyTransform.translate(aCenter.X(), aCenter.Y());
669 aTotalPolyPoly.transform(aPolyPolyTransform);
671 // x- and y-rotation have an own rotation center. x- and y-value of rotation center are
672 // fractions of shape size, z-value is in Hmm in property. Shape center is (0 0 0).
673 // Values in property are in custom shape extrusion space with y-axis down.
674 double fXRotate, fYRotate;
675 GetRotateAngle( rGeometryItem, fXRotate, fYRotate );
676 drawing::Direction3D aRotationCenterDefault( 0, 0, 0 );
677 drawing::Direction3D aRotationCenter( GetDirection3D( rGeometryItem, u"RotationCenter"_ustr, aRotationCenterDefault ) );
678 aRotationCenter.DirectionX *= aSnapRect.getOpenWidth();
679 aRotationCenter.DirectionY *= aSnapRect.getOpenHeight();
680 if (pMap)
682 aRotationCenter.DirectionZ *= *pMap;
684 aNewTransform.translate( -aRotationCenter.DirectionX, aRotationCenter.DirectionY, -aRotationCenter.DirectionZ );
685 if( fYRotate != 0.0 )
686 aNewTransform.rotate( 0.0, -fYRotate, 0.0 );
687 if( fXRotate != 0.0 )
688 aNewTransform.rotate( -fXRotate, 0.0, 0.0 );
689 aNewTransform.translate(aRotationCenter.DirectionX, -aRotationCenter.DirectionY, aRotationCenter.DirectionZ);
691 // oblique parallel projection is done by shearing the object, not by moving the camera
692 if (eProjectionMode == drawing::ProjectionMode_PARALLEL)
694 double fSkew, fAlpha;
695 GetSkew( rGeometryItem, fSkew, fAlpha );
696 if ( fSkew != 0.0 )
698 double fInvTanBeta( fSkew / 100.0 );
699 if(fInvTanBeta)
701 aNewTransform.shearXY(
702 fInvTanBeta * cos(fAlpha),
703 fInvTanBeta * sin(fAlpha));
708 pScene->NbcSetTransform( aNewTransform );
710 // These values are used later again, so declare them outside the if-statement. They will
711 // contain the absolute values of ViewPoint in 3D scene coordinate system, y-axis up.
712 double fViewPointX = 0; // dummy values
713 double fViewPointY = 0;
714 double fViewPointZ = 25000;
715 if (eProjectionMode == drawing::ProjectionMode_PERSPECTIVE)
717 double fOriginX, fOriginY;
718 // Calculate BoundRect of shape, including flip and z-rotation, from aTotalPolyPoly.
719 tools::Rectangle aBoundAfter2DTransform; // aBoundAfter2DTransform has y-axis down.
720 basegfx::B2DRange aTotalPolyPolyRange(aTotalPolyPoly.getB2DRange());
721 aBoundAfter2DTransform.SetLeft(aTotalPolyPolyRange.getMinX());
722 aBoundAfter2DTransform.SetTop(aTotalPolyPolyRange.getMinY());
723 aBoundAfter2DTransform.SetRight(aTotalPolyPolyRange.getMaxX());
724 aBoundAfter2DTransform.SetBottom(aTotalPolyPolyRange.getMaxY());
726 // Property "Origin" in API is relative to bounding box of shape after 2D
727 // transformations. Range is [-0.5;0.5] with center of bounding box as 0.
728 // Resolve "Origin" fractions to length
729 GetOrigin( rGeometryItem, fOriginX, fOriginY );
730 fOriginX *= aBoundAfter2DTransform.GetWidth();
731 fOriginY *= aBoundAfter2DTransform.GetHeight();
732 // Resolve length to absolute value for 3D
733 fOriginX += aBoundAfter2DTransform.Center().X();
734 fOriginY += aBoundAfter2DTransform.Center().Y();
735 fOriginY = - fOriginY;
736 // Scene is translated so that shape center is origin of coordinate system.
737 // Translate point "Origin" too.
738 fOriginX -= aCenter.X();
739 fOriginY -= -aCenter.Y();
740 // API ViewPoint values are relative to point "Origin" and have y-axis down.
741 // ToDo: These default ViewPoint values are used as default by MS Office. But ODF
742 // default is (3500, -3500, 25000), details in tdf#146192.
743 drawing::Position3D aViewPointDefault( 3472, -3472, 25000 );
744 drawing::Position3D aViewPoint( GetPosition3D( rGeometryItem, u"ViewPoint"_ustr, aViewPointDefault, pMap ) );
745 fViewPointX = aViewPoint.PositionX + fOriginX;
746 fViewPointY = - aViewPoint.PositionY + fOriginY;
747 fViewPointZ = aViewPoint.PositionZ;
750 // now set correct camera position
751 if (eProjectionMode == drawing::ProjectionMode_PARALLEL)
753 basegfx::B3DPoint _aLookAt( 0.0, 0.0, 0.0 );
754 basegfx::B3DPoint _aNewCamPos( 0.0, 0.0, 25000.0 );
755 rCamera.SetPosAndLookAt( _aNewCamPos, _aLookAt );
756 pScene->SetCamera( rCamera );
758 else
760 basegfx::B3DPoint _aLookAt(fViewPointX, fViewPointY, 0.0);
761 basegfx::B3DPoint aNewCamPos(fViewPointX, fViewPointY, fViewPointZ);
762 rCamera.SetPosAndLookAt( aNewCamPos, _aLookAt );
763 pScene->SetCamera( rCamera );
766 // NbcSetTransform has not updated the scene 2D rectangles.
767 // Idea: Get a bound volume as polygon from bound rectangle of shape without 2D
768 // transformations. Calculate its projection to the XY-plane. Then calculate the bounding
769 // rectangle of the projection and convert this rectangle back to absolute 2D coordinates.
770 // Set that as 2D rectangle of the scene.
771 const tools::Polygon aPolygon(aBoundRect2d); // y-up
772 basegfx::B3DPolygon aPolygonBoundVolume; // y-down, scene coordinates
773 for (sal_uInt16 i = 0; i < 4; i++ )
775 aPolygonBoundVolume.append(basegfx::B3DPoint(aPolygon[i].X(), -aPolygon[i].Y(), 0));
777 for (sal_uInt16 i = 0; i < 4; i++ )
779 aPolygonBoundVolume.append(basegfx::B3DPoint(aPolygon[i].X(), -aPolygon[i].Y(), fDepth));
781 aPolygonBoundVolume.transform(aNewTransform);
783 // projection
784 tools::Polygon a2DProjectionResult(8); // in fact 3D points with z=0
785 for (sal_uInt16 i = 0; i < 8; i++ )
787 const basegfx::B3DPoint aPoint3D(aPolygonBoundVolume.getB3DPoint(i));
789 if (eProjectionMode == drawing::ProjectionMode_PARALLEL)
791 a2DProjectionResult[i].setX(aPoint3D.getX());
792 a2DProjectionResult[i].setY(aPoint3D.getY());
794 else
796 // skip point if line from viewpoint to point is parallel to xy-plane
797 if (double fDiv = aPoint3D.getZ() - fViewPointZ; fDiv != 0.0)
799 double f = (- fViewPointZ) / fDiv;
800 double fX = (aPoint3D.getX() - fViewPointX) * f + fViewPointX;
801 double fY = (aPoint3D.getY() - fViewPointY) * f + fViewPointY;;
802 a2DProjectionResult[i].setX(static_cast<sal_Int32>(fX));
803 a2DProjectionResult[i].setY(static_cast<sal_Int32>(fY));
807 // Convert to y-axis down
808 for (sal_uInt16 i = 0; i < 8; i++ )
810 a2DProjectionResult[i].setY(- a2DProjectionResult[i].Y());
812 // Shift back to shape center
813 a2DProjectionResult.Translate(aCenter);
815 pScene->SetLogicRect(a2DProjectionResult.GetBoundRect());
818 // light and material
820 // "LightFace" has nothing corresponding in 3D rendering engine.
821 /* bool bLightFace = */ GetBool(rGeometryItem, u"LightFace"_ustr, true); // default in ODF
823 // Light directions
825 drawing::Direction3D aFirstLightDirectionDefault(50000.0, 0.0, 10000.0);
826 drawing::Direction3D aFirstLightDirection(GetDirection3D( rGeometryItem, u"FirstLightDirection"_ustr, aFirstLightDirectionDefault));
827 if (aFirstLightDirection.DirectionX == 0.0 && aFirstLightDirection.DirectionY == 0.0
828 && aFirstLightDirection.DirectionZ == 0.0)
829 aFirstLightDirection.DirectionZ = 1.0;
830 basegfx::B3DVector aLight1Vector(aFirstLightDirection.DirectionX, -aFirstLightDirection.DirectionY, aFirstLightDirection.DirectionZ);
831 aLight1Vector.normalize();
833 drawing::Direction3D aSecondLightDirectionDefault(-50000.0, 0.0, 10000.0);
834 drawing::Direction3D aSecondLightDirection(GetDirection3D( rGeometryItem, u"SecondLightDirection"_ustr, aSecondLightDirectionDefault));
835 if (aSecondLightDirection.DirectionX == 0.0 && aSecondLightDirection.DirectionY == 0.0
836 && aSecondLightDirection.DirectionZ == 0.0)
837 aSecondLightDirection.DirectionZ = 1.0;
838 basegfx::B3DVector aLight2Vector(aSecondLightDirection.DirectionX, -aSecondLightDirection.DirectionY, aSecondLightDirection.DirectionZ);
839 aLight2Vector.normalize();
841 // tdf#160421 a single flip inverts the light directions currently (March 2024). So invert
842 // their directions here for rendering.
843 if (bIsMirroredX != bIsMirroredY)
845 aLight1Vector *= -1.0;
846 aLight2Vector *= -1.0;
849 // Light Intensity
851 // For "FirstLight" the 3D-Scene light "1" is regularly used. In case of surface "Matte"
852 // the light 4 is used instead. For "SecondLight" the 3D-Scene light "2" is regularly used.
853 // In case first or second light is not harsh, the lights 5 to 8 are used in addition
854 // to get a soft light appearance.
855 // The 3D-Scene light "3" is currently not used.
857 // ODF default 66%. MS Office default 38000/65536=0.579 is set in import filter.
858 double fLight1Intensity = GetDouble(rGeometryItem, u"FirstLightLevel"_ustr, 66) / 100.0;
859 // ODF and MS Office have both default 'true'.
860 bool bFirstLightHarsh = GetBool(rGeometryItem, u"FirstLightHarsh"_ustr, true);
861 // ODF default 66%. MS Office default 38000/65536=0.579 is set in import filter
862 double fLight2Intensity = GetDouble(rGeometryItem, u"SecondLightLevel"_ustr, 66) / 100.0;
863 // ODF has default 'true'. MS Office default 'false' is set in import.
864 bool bSecondLightHarsh = GetBool(rGeometryItem, u"SecondLightHarsh"_ustr, true);
866 // ODF default 33%. MS Office default 20000/65536=0.305 is set in import filter.
867 double fAmbientIntensity = GetDouble(rGeometryItem, u"Brightness"_ustr, 33) / 100.0;
869 double fLight1IntensityForSpecular(fLight1Intensity); // remember original value
870 if (!bFirstLightHarsh || !bSecondLightHarsh) // might need softing lights
872 bool bNeedSoftLights(false); // catch case of lights with zero intensity.
873 basegfx::B3DVector aLight5Vector;
874 basegfx::B3DVector aLight6Vector;
875 basegfx::B3DVector aLight7Vector;
876 basegfx::B3DVector aLight8Vector;
877 // The needed light intensities depend on the angle between regular light and
878 // additional lights, currently for 60deg.
879 Color aHoriSoftLightColor;
880 Color aVertSoftLightColor;
882 if (!bSecondLightHarsh && fLight2Intensity > 0.0
883 && (bFirstLightHarsh || fLight1Intensity == 0.0)) // only second light soft
885 // That is default for shapes generated in the UI, for LO and MS Office as well.
886 bNeedSoftLights = true;
887 double fLight2SoftIntensity = fLight2Intensity * 0.40;
888 aHoriSoftLightColor = Color(basegfx::BColor(fLight2SoftIntensity).clamp());
889 aVertSoftLightColor = aHoriSoftLightColor;
890 fLight2Intensity *= 0.2;
892 lcl_SoftLightsDirection(aLight2Vector, aLight5Vector, aLight6Vector,
893 aLight7Vector, aLight8Vector);
895 else if (!bFirstLightHarsh && fLight1Intensity > 0.0
896 && (bSecondLightHarsh || fLight2Intensity == 0.0)) // only first light soft
898 bNeedSoftLights = true;
899 double fLight1SoftIntensity = fLight1Intensity * 0.40;
900 aHoriSoftLightColor = Color(basegfx::BColor(fLight1SoftIntensity).clamp());
901 aVertSoftLightColor = aHoriSoftLightColor;
902 fLight1Intensity *= 0.2;
904 lcl_SoftLightsDirection(aLight1Vector, aLight5Vector, aLight6Vector,
905 aLight7Vector, aLight8Vector);
907 else if (!bFirstLightHarsh && fLight1Intensity > 0.0 && !bSecondLightHarsh
908 && fLight2Intensity > 0.0) // both lights soft
910 bNeedSoftLights = true;
911 // We do not hat enough lights. We use two soft lights for FirstLight and two for
912 // SecondLight and double intensity.
913 double fLight1SoftIntensity = fLight1Intensity * 0.8;
914 fLight1Intensity *= 0.4;
915 aHoriSoftLightColor = Color(basegfx::BColor(fLight1SoftIntensity).clamp());
916 basegfx::B3DVector aDummy1, aDummy2;
917 lcl_SoftLightsDirection(aLight1Vector, aDummy1, aDummy2, aLight7Vector,
918 aLight8Vector);
920 double fLight2SoftIntensity = fLight2Intensity * 0.8;
921 aVertSoftLightColor = Color(basegfx::BColor(fLight2SoftIntensity).clamp());
922 fLight2Intensity *= 0.4;
923 lcl_SoftLightsDirection(aLight2Vector, aLight5Vector, aLight6Vector, aDummy1,
924 aDummy2);
927 if (bNeedSoftLights)
929 pScene->GetProperties().SetObjectItem(
930 makeSvx3DLightDirection5Item(aLight5Vector));
931 pScene->GetProperties().SetObjectItem(
932 makeSvx3DLightcolor5Item(aVertSoftLightColor));
933 pScene->GetProperties().SetObjectItem(makeSvx3DLightOnOff5Item(true));
934 pScene->GetProperties().SetObjectItem(
935 makeSvx3DLightDirection6Item(aLight6Vector));
936 pScene->GetProperties().SetObjectItem(
937 makeSvx3DLightcolor6Item(aVertSoftLightColor));
938 pScene->GetProperties().SetObjectItem(makeSvx3DLightOnOff6Item(true));
939 pScene->GetProperties().SetObjectItem(
940 makeSvx3DLightDirection7Item(aLight7Vector));
941 pScene->GetProperties().SetObjectItem(
942 makeSvx3DLightcolor7Item(aHoriSoftLightColor));
943 pScene->GetProperties().SetObjectItem(makeSvx3DLightOnOff7Item(true));
944 pScene->GetProperties().SetObjectItem(
945 makeSvx3DLightDirection8Item(aLight8Vector));
946 pScene->GetProperties().SetObjectItem(
947 makeSvx3DLightcolor8Item(aHoriSoftLightColor));
948 pScene->GetProperties().SetObjectItem(makeSvx3DLightOnOff8Item(true));
952 // ToDo: MSO seems to add half of the surplus to ambient color. ODF restricts value to <1.
953 if (fLight1Intensity > 1.0)
955 fAmbientIntensity += (fLight1Intensity - 1.0) / 2.0;
958 // ToDo: How to handle fAmbientIntensity larger 1.0 ? Perhaps lighten object color?
960 // Now set the regularly 3D-scene light attributes.
961 Color aAmbientColor(basegfx::BColor(fAmbientIntensity).clamp());
962 pScene->GetProperties().SetObjectItem(makeSvx3DAmbientcolorItem(aAmbientColor));
964 pScene->GetProperties().SetObjectItem(makeSvx3DLightDirection1Item(aLight1Vector));
965 pScene->GetProperties().SetObjectItem(makeSvx3DLightOnOff1Item(fLight1Intensity > 0.0));
966 Color aLight1Color(basegfx::BColor(fLight1Intensity).clamp());
967 pScene->GetProperties().SetObjectItem(makeSvx3DLightcolor1Item(aLight1Color));
969 pScene->GetProperties().SetObjectItem(makeSvx3DLightDirection2Item(aLight2Vector));
970 pScene->GetProperties().SetObjectItem(makeSvx3DLightOnOff2Item(fLight2Intensity > 0.0));
971 Color aLight2Color(basegfx::BColor(fLight2Intensity).clamp());
972 pScene->GetProperties().SetObjectItem(makeSvx3DLightcolor2Item(aLight2Color));
974 // Object reactions on light
975 // Diffusion, Specular-Color and -Intensity are object properties, not scene properties.
976 // Surface flag "Metal" is an object property too.
978 // Property "Diffusion" would correspond to style attribute "drd3:diffuse-color".
979 // But that is not implemented. We cannot ignore the attribute because MS Office sets
980 // attribute c3DDiffuseAmt to 43712 (Type Fixed 16.16, approx 66,9%) instead of MSO
981 // default 65536 (100%), if the user sets surface 'Metal' in the UI of MS Office.
982 // We will change the material color of the 3D object as ersatz.
983 // ODF data type is percent with default 0%. MSO default is set in import filter.
984 double fDiffusion = GetDouble(rGeometryItem, u"Diffusion"_ustr, 0.0) / 100.0;
986 // ODF standard specifies for value true: "the specular color for the shading of an
987 // extruded shape is gray (red, green and blue values of 200) instead of white and 15% is
988 // added to the specularity."
989 // Neither 'specularity' nor 'specular color' is clearly defined in the standard. ODF term
990 // 'specularity' seems to correspond to UI field 'Specular Intensity' for 3D scenes.
991 // MS Office uses current material color in case 'Metal' is set. To detect, whether
992 // rendering similar to MS Office has to be used the property 'MetalType' is used. It is
993 // set on import and in the extrusion bar.
994 bool bMetal = GetBool(rGeometryItem, u"Metal"_ustr, false);
995 sal_Int16 eMetalType(
996 GetMetalType(rGeometryItem, drawing::EnhancedCustomShapeMetalType::MetalODF));
997 bool bMetalMSCompatible
998 = eMetalType == drawing::EnhancedCustomShapeMetalType::MetalMSCompatible;
1000 // Property "Specularity" corresponds to 3D object style attribute dr3d:specular-color.
1001 double fSpecularity = GetDouble(rGeometryItem, u"Specularity"_ustr, 0) / 100.0;
1003 if (bMetal && !bMetalMSCompatible)
1005 fSpecularity *= 200.0 / 255.0;
1008 // MS Office seems to render as if 'Specular Color' = Specularity * Light1Intensity.
1009 double fShadingFactor = fLight1IntensityForSpecular * fSpecularity;
1010 Color aSpecularCol(basegfx::BColor(fShadingFactor).clamp());
1011 // In case of bMetalMSCompatible the color will be recalculated in the below loop.
1013 // Shininess ODF default 50 (unit %). MS Office default 5, import filter makes *10.
1014 // Shininess corresponds to "Specular Intensity" with the nonlinear relationship
1015 // "Specular Intensity" = 2^c3DShininess = 2^("Shininess" / 10)
1016 double fShininess = GetDouble(rGeometryItem, u"Shininess"_ustr, 50) / 10.0;
1017 fShininess = std::clamp<double>(pow(2, fShininess), 0.0, 100.0);
1018 sal_uInt16 nIntensity = static_cast<sal_uInt16>(basegfx::fround(fShininess));
1019 if (bMetal && !bMetalMSCompatible)
1021 nIntensity += 15; // as specified in ODF
1022 nIntensity = std::clamp<sal_uInt16>(nIntensity, 0, 100);
1025 SdrObjListIter aSceneIter(*pScene, SdrIterMode::DeepNoGroups);
1026 while (aSceneIter.IsMore())
1028 const SdrObject* pNext = aSceneIter.Next();
1030 // Change material color as ersatz for missing style attribute "drd3:diffuse-color".
1031 // For this ersatz we exclude case fDiffusion == 0.0, because for older documents this
1032 // attribute is not written out to draw:extrusion-diffusion and ODF default 0 would
1033 // produce black objects.
1034 const Color& rMatColor
1035 = pNext->GetProperties().GetItem(XATTR_FILLCOLOR).GetColorValue();
1036 Color aOldMatColor(rMatColor);
1037 if (fDiffusion > 0.0 && !basegfx::fTools::equalZero(fDiffusion)
1038 && !basegfx::fTools::equal(fDiffusion, 1.0))
1040 // Occurs e.g. with MS surface preset 'Metal'.
1041 sal_uInt16 nHue;
1042 sal_uInt16 nSaturation;
1043 sal_uInt16 nBrightness;
1044 rMatColor.RGBtoHSB(nHue, nSaturation, nBrightness);
1045 nBrightness
1046 = static_cast<sal_uInt16>(static_cast<double>(nBrightness) * fDiffusion);
1047 nBrightness = std::clamp<sal_uInt16>(nBrightness, 0, 100);
1048 Color aNewMatColor = Color::HSBtoRGB(nHue, nSaturation, nBrightness);
1049 pNext->GetProperties().SetObjectItem(XFillColorItem(u""_ustr, aNewMatColor));
1052 // Using material color instead of gray in case of MS Office compatible rendering.
1053 if (bMetal && bMetalMSCompatible)
1055 sal_uInt16 nHue;
1056 sal_uInt16 nSaturation;
1057 sal_uInt16 nBrightness;
1058 aOldMatColor.RGBtoHSB(nHue, nSaturation, nBrightness);
1059 nBrightness = static_cast<sal_uInt16>(static_cast<double>(nBrightness)
1060 * fShadingFactor);
1061 nBrightness = std::clamp<sal_uInt16>(nBrightness, 0, 100);
1062 aSpecularCol = Color::HSBtoRGB(nHue, nSaturation, nBrightness);
1065 pNext->GetProperties().SetObjectItem(makeSvx3DMaterialSpecularItem(aSpecularCol));
1066 pNext->GetProperties().SetObjectItem(
1067 makeSvx3DMaterialSpecularIntensityItem(nIntensity));
1070 // fSpecularity = 0 is used to indicate surface preset "Matte".
1071 if (basegfx::fTools::equalZero(fSpecularity))
1073 // First light in LO 3D engine is always specular, all other lights are never specular.
1074 // We copy light1 values to light4 and use it instead of light1 in the 3D scene.
1075 pScene->GetProperties().SetObjectItem(makeSvx3DLightOnOff1Item(false));
1076 pScene->GetProperties().SetObjectItem(makeSvx3DLightOnOff4Item(true));
1077 pScene->GetProperties().SetObjectItem(makeSvx3DLightcolor4Item(aLight1Color));
1078 pScene->GetProperties().SetObjectItem(makeSvx3DLightDirection4Item(aLight1Vector));
1081 // removing placeholder objects
1082 for (E3dCompoundObject* pTemp : aPlaceholderObjectList)
1084 pScene->RemoveObject( pTemp->GetOrdNum() );
1088 return pRet;
1091 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */