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/primitive3d/sdrsphereprimitive3d.hxx>
21 #include <basegfx/polygon/b3dpolypolygontools.hxx>
22 #include <basegfx/matrix/b2dhommatrix.hxx>
23 #include <primitive3d/sdrdecompositiontools3d.hxx>
24 #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
25 #include <drawinglayer/attribute/sdrfillattribute.hxx>
26 #include <drawinglayer/attribute/sdrlineattribute.hxx>
27 #include <drawinglayer/attribute/sdrshadowattribute.hxx>
30 using namespace com::sun::star
;
33 namespace drawinglayer::primitive3d
35 Primitive3DContainer
SdrSpherePrimitive3D::create3DDecomposition(const geometry::ViewInformation3D
& /*rViewInformation*/) const
37 Primitive3DContainer aRetval
;
38 const basegfx::B3DRange
aUnitRange(0.0, 0.0, 0.0, 1.0, 1.0, 1.0);
39 const bool bCreateNormals(css::drawing::NormalsKind_SPECIFIC
== getSdr3DObjectAttribute().getNormalsKind()
40 || css::drawing::NormalsKind_SPHERE
== getSdr3DObjectAttribute().getNormalsKind());
42 // create unit geometry
43 basegfx::B3DPolyPolygon
aFill(basegfx::utils::createSphereFillPolyPolygonFromB3DRange(aUnitRange
,
44 getHorizontalSegments(), getVerticalSegments(), bCreateNormals
));
47 if(!getSdrLFSAttribute().getFill().isDefault()
49 && getSdr3DObjectAttribute().getNormalsInvert()
50 && aFill
.areNormalsUsed())
53 aFill
= basegfx::utils::invertNormals(aFill
);
56 // texture coordinates
57 if(!getSdrLFSAttribute().getFill().isDefault())
59 // handle texture coordinates X
60 const bool bParallelX(css::drawing::TextureProjectionMode_PARALLEL
== getSdr3DObjectAttribute().getTextureProjectionX());
61 const bool bObjectSpecificX(css::drawing::TextureProjectionMode_OBJECTSPECIFIC
== getSdr3DObjectAttribute().getTextureProjectionX());
62 const bool bSphereX(css::drawing::TextureProjectionMode_SPHERE
== getSdr3DObjectAttribute().getTextureProjectionX());
64 // handle texture coordinates Y
65 const bool bParallelY(css::drawing::TextureProjectionMode_PARALLEL
== getSdr3DObjectAttribute().getTextureProjectionY());
66 const bool bObjectSpecificY(css::drawing::TextureProjectionMode_OBJECTSPECIFIC
== getSdr3DObjectAttribute().getTextureProjectionY());
67 const bool bSphereY(css::drawing::TextureProjectionMode_SPHERE
== getSdr3DObjectAttribute().getTextureProjectionY());
69 if(bParallelX
|| bParallelY
)
71 // apply parallel texture coordinates in X and/or Y
72 const basegfx::B3DRange
aRange(basegfx::utils::getRange(aFill
));
73 aFill
= basegfx::utils::applyDefaultTextureCoordinatesParallel(aFill
, aRange
, bParallelX
, bParallelY
);
76 if(bSphereX
|| bObjectSpecificX
|| bSphereY
|| bObjectSpecificY
)
78 double fRelativeAngle(0.0);
82 // Since the texture coordinates are (for historical reasons)
83 // different from forced to sphere texture coordinates,
84 // create a old version from it by rotating to old state before applying
85 // the texture coordinates to emulate old behaviour
86 fRelativeAngle
= 2 * M_PI
* (static_cast<double>((getHorizontalSegments() >> 1) - 1) / static_cast<double>(getHorizontalSegments()));
87 basegfx::B3DHomMatrix aRot
;
88 aRot
.rotate(0.0, fRelativeAngle
, 0.0);
89 aFill
.transform(aRot
);
92 // apply spherical texture coordinates in X and/or Y
93 const basegfx::B3DRange
aRange(basegfx::utils::getRange(aFill
));
94 const basegfx::B3DPoint
aCenter(aRange
.getCenter());
95 aFill
= basegfx::utils::applyDefaultTextureCoordinatesSphere(aFill
, aCenter
,
96 bSphereX
|| bObjectSpecificX
, bSphereY
|| bObjectSpecificY
);
101 basegfx::B3DHomMatrix aRot
;
102 aRot
.rotate(0.0, -fRelativeAngle
, 0.0);
103 aFill
.transform(aRot
);
107 // transform texture coordinates to texture size
108 basegfx::B2DHomMatrix aTexMatrix
;
109 aTexMatrix
.scale(getTextureSize().getX(), getTextureSize().getY());
110 aFill
.transformTextureCoordinates(aTexMatrix
);
113 // build vector of PolyPolygons
114 std::vector
< basegfx::B3DPolyPolygon
> a3DPolyPolygonVector
;
116 for(sal_uInt32
a(0); a
< aFill
.count(); a
++)
118 a3DPolyPolygonVector
.emplace_back(aFill
.getB3DPolygon(a
));
121 if(!getSdrLFSAttribute().getFill().isDefault())
124 aRetval
= create3DPolyPolygonFillPrimitives(
125 a3DPolyPolygonVector
,
128 getSdr3DObjectAttribute(),
129 getSdrLFSAttribute().getFill(),
130 getSdrLFSAttribute().getFillFloatTransGradient());
134 // create simplified 3d hit test geometry
135 aRetval
= createHiddenGeometryPrimitives3D(
136 a3DPolyPolygonVector
,
139 getSdr3DObjectAttribute());
143 if(!getSdrLFSAttribute().getLine().isDefault())
145 basegfx::B3DPolyPolygon
aSphere(basegfx::utils::createSpherePolyPolygonFromB3DRange(aUnitRange
, getHorizontalSegments(), getVerticalSegments()));
146 const Primitive3DContainer
aLines(create3DPolyPolygonLinePrimitives(
147 aSphere
, getTransform(), getSdrLFSAttribute().getLine()));
148 aRetval
.append(aLines
);
152 if(!getSdrLFSAttribute().getShadow().isDefault()
155 const Primitive3DContainer
aShadow(createShadowPrimitive3D(
156 aRetval
, getSdrLFSAttribute().getShadow(), getSdr3DObjectAttribute().getShadow3D()));
157 aRetval
.append(aShadow
);
163 SdrSpherePrimitive3D::SdrSpherePrimitive3D(
164 const basegfx::B3DHomMatrix
& rTransform
,
165 const basegfx::B2DVector
& rTextureSize
,
166 const attribute::SdrLineFillShadowAttribute3D
& rSdrLFSAttribute
,
167 const attribute::Sdr3DObjectAttribute
& rSdr3DObjectAttribute
,
168 sal_uInt32 nHorizontalSegments
,
169 sal_uInt32 nVerticalSegments
)
170 : SdrPrimitive3D(rTransform
, rTextureSize
, rSdrLFSAttribute
, rSdr3DObjectAttribute
),
171 mnHorizontalSegments(nHorizontalSegments
),
172 mnVerticalSegments(nVerticalSegments
)
176 bool SdrSpherePrimitive3D::operator==(const BasePrimitive3D
& rPrimitive
) const
178 if(SdrPrimitive3D::operator==(rPrimitive
))
180 const SdrSpherePrimitive3D
& rCompare
= static_cast< const SdrSpherePrimitive3D
& >(rPrimitive
);
182 return (getHorizontalSegments() == rCompare
.getHorizontalSegments()
183 && getVerticalSegments() == rCompare
.getVerticalSegments());
189 basegfx::B3DRange
SdrSpherePrimitive3D::getB3DRange(const geometry::ViewInformation3D
& /*rViewInformation*/) const
191 // use default from sdrPrimitive3D which uses transformation expanded by line width/2
192 // The parent implementation which uses the ranges of the decomposition would be more
193 // correct, but for historical reasons it is necessary to do the old method: To get
194 // the range of the non-transformed geometry and transform it then. This leads to different
195 // ranges where the new method is more correct, but the need to keep the old behaviour
196 // has priority here.
197 return getStandard3DRange();
201 ImplPrimitive3DIDBlock(SdrSpherePrimitive3D
, PRIMITIVE3D_ID_SDRSPHEREPRIMITIVE3D
)
203 } // end of namespace
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */