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 <processor3d/shadow3dextractor.hxx>
21 #include <primitive3d/shadowprimitive3d.hxx>
22 #include <drawinglayer/primitive2d/shadowprimitive2d.hxx>
23 #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
24 #include <drawinglayer/primitive3d/transformprimitive3d.hxx>
25 #include <drawinglayer/primitive3d/polygonprimitive3d.hxx>
26 #include <basegfx/polygon/b2dpolygontools.hxx>
27 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
28 #include <drawinglayer/primitive3d/polypolygonprimitive3d.hxx>
29 #include <basegfx/polygon/b2dpolypolygontools.hxx>
30 #include <drawinglayer/primitive2d/PolyPolygonColorPrimitive2D.hxx>
31 #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
34 using namespace com::sun::star
;
37 namespace drawinglayer::processor3d
39 // as tooling, the process() implementation takes over API handling and calls this
40 // virtual render method when the primitive implementation is BasePrimitive3D-based.
41 void Shadow3DExtractingProcessor::processBasePrimitive3D(const primitive3d::BasePrimitive3D
& rCandidate
)
43 // it is a BasePrimitive3D implementation, use getPrimitive3DID() call for switch
44 switch(rCandidate
.getPrimitive3DID())
46 case PRIMITIVE3D_ID_SHADOWPRIMITIVE3D
:
48 // shadow3d object. Call recursive with content and start conversion
49 const primitive3d::ShadowPrimitive3D
& rPrimitive
= static_cast< const primitive3d::ShadowPrimitive3D
& >(rCandidate
);
52 primitive2d::Primitive2DContainer aNewSubList
;
53 primitive2d::Primitive2DContainer
* pLastTargetSequence
= mpPrimitive2DSequence
;
54 mpPrimitive2DSequence
= &aNewSubList
;
57 const bool bLastConvert(mbConvert
);
60 // set projection flag
61 const bool bLastUseProjection(mbUseProjection
);
62 mbUseProjection
= rPrimitive
.getShadow3D();
65 process(rPrimitive
.getChildren());
68 mbUseProjection
= bLastUseProjection
;
69 mbConvert
= bLastConvert
;
70 mpPrimitive2DSequence
= pLastTargetSequence
;
72 // create 2d shadow primitive with result. This also fetches all entries
73 // from aNewSubList, so there is no need to delete them
74 primitive2d::BasePrimitive2D
* pNew
= new primitive2d::ShadowPrimitive2D(
75 rPrimitive
.getShadowTransform(),
76 rPrimitive
.getShadowColor(),
77 0, // shadow3d doesn't have rPrimitive.getShadowBlur() yet.
80 if(basegfx::fTools::more(rPrimitive
.getShadowTransparence(), 0.0))
82 // create simpleTransparencePrimitive, add created primitives
83 const primitive2d::Primitive2DReference
xRef(pNew
);
84 const primitive2d::Primitive2DContainer aNewTransPrimitiveVector
{ xRef
};
86 pNew
= new primitive2d::UnifiedTransparencePrimitive2D(
87 aNewTransPrimitiveVector
,
88 rPrimitive
.getShadowTransparence());
91 mpPrimitive2DSequence
->push_back(pNew
);
95 case PRIMITIVE3D_ID_TRANSFORMPRIMITIVE3D
:
97 // transform group. Remember current transformations
98 const primitive3d::TransformPrimitive3D
& rPrimitive
= static_cast< const primitive3d::TransformPrimitive3D
& >(rCandidate
);
99 const geometry::ViewInformation3D
aLastViewInformation3D(getViewInformation3D());
101 // create new transformation; add new object transform from right side
102 const geometry::ViewInformation3D
aNewViewInformation3D(
103 aLastViewInformation3D
.getObjectTransformation() * rPrimitive
.getTransformation(),
104 aLastViewInformation3D
.getOrientation(),
105 aLastViewInformation3D
.getProjection(),
106 aLastViewInformation3D
.getDeviceToView(),
107 aLastViewInformation3D
.getViewTime(),
108 aLastViewInformation3D
.getExtendedInformationSequence());
109 updateViewInformation(aNewViewInformation3D
);
111 if(mbShadowProjectionIsValid
)
113 // update buffered WorldToEye and EyeToView
114 maWorldToEye
= getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
115 maEyeToView
= getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
119 process(rPrimitive
.getChildren());
121 // restore transformations
122 updateViewInformation(aLastViewInformation3D
);
124 if(mbShadowProjectionIsValid
)
126 // update buffered WorldToEye and EyeToView
127 maWorldToEye
= getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
128 maEyeToView
= getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
132 case PRIMITIVE3D_ID_POLYGONHAIRLINEPRIMITIVE3D
:
134 // PolygonHairlinePrimitive3D
137 const primitive3d::PolygonHairlinePrimitive3D
& rPrimitive
= static_cast< const primitive3d::PolygonHairlinePrimitive3D
& >(rCandidate
);
138 basegfx::B2DPolygon a2DHairline
;
142 if(mbShadowProjectionIsValid
)
144 a2DHairline
= impDoShadowProjection(rPrimitive
.getB3DPolygon());
149 a2DHairline
= basegfx::utils::createB2DPolygonFromB3DPolygon(rPrimitive
.getB3DPolygon(), getViewInformation3D().getObjectToView());
152 if(a2DHairline
.count())
154 a2DHairline
.transform(getObjectTransformation());
155 mpPrimitive2DSequence
->push_back(
156 new primitive2d::PolygonHairlinePrimitive2D(
163 case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D
:
165 // PolyPolygonMaterialPrimitive3D
168 const primitive3d::PolyPolygonMaterialPrimitive3D
& rPrimitive
= static_cast< const primitive3d::PolyPolygonMaterialPrimitive3D
& >(rCandidate
);
169 basegfx::B2DPolyPolygon a2DFill
;
173 if(mbShadowProjectionIsValid
)
175 a2DFill
= impDoShadowProjection(rPrimitive
.getB3DPolyPolygon());
180 a2DFill
= basegfx::utils::createB2DPolyPolygonFromB3DPolyPolygon(rPrimitive
.getB3DPolyPolygon(), getViewInformation3D().getObjectToView());
185 a2DFill
.transform(getObjectTransformation());
186 mpPrimitive2DSequence
->push_back(
187 new primitive2d::PolyPolygonColorPrimitive2D(
196 // process recursively
197 process(rCandidate
.get3DDecomposition(getViewInformation3D()));
203 Shadow3DExtractingProcessor::Shadow3DExtractingProcessor(
204 const geometry::ViewInformation3D
& rViewInformation
,
205 const basegfx::B2DHomMatrix
& rObjectTransformation
,
206 const basegfx::B3DVector
& rLightNormal
,
208 const basegfx::B3DRange
& rContained3DRange
)
209 : BaseProcessor3D(rViewInformation
),
210 maPrimitive2DSequence(),
211 mpPrimitive2DSequence(&maPrimitive2DSequence
),
212 maObjectTransformation(rObjectTransformation
),
215 maLightNormal(rLightNormal
),
216 maShadowPlaneNormal(),
218 mfLightPlaneScalar(0.0),
219 mbShadowProjectionIsValid(false),
221 mbUseProjection(false)
223 // normalize light normal, get and normalize shadow plane normal and calculate scalar from it
224 maLightNormal
.normalize();
225 maShadowPlaneNormal
= basegfx::B3DVector(0.0, sin(fShadowSlant
), cos(fShadowSlant
));
226 maShadowPlaneNormal
.normalize();
227 mfLightPlaneScalar
= maLightNormal
.scalar(maShadowPlaneNormal
);
229 // use only when scalar is > 0.0, so the light is in front of the object
230 if(!basegfx::fTools::more(mfLightPlaneScalar
, 0.0))
233 // prepare buffered WorldToEye and EyeToView
234 maWorldToEye
= getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
235 maEyeToView
= getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
237 // calculate range to get front edge around which to rotate the shadow's projection
238 basegfx::B3DRange
aContained3DRange(rContained3DRange
);
239 aContained3DRange
.transform(getWorldToEye());
240 maPlanePoint
.setX(maShadowPlaneNormal
.getX() < 0.0 ? aContained3DRange
.getMinX() : aContained3DRange
.getMaxX());
241 maPlanePoint
.setY(maShadowPlaneNormal
.getY() > 0.0 ? aContained3DRange
.getMinY() : aContained3DRange
.getMaxY());
242 maPlanePoint
.setZ(aContained3DRange
.getMinZ() - (aContained3DRange
.getDepth() / 8.0));
244 // set flag that shadow projection is prepared and allowed
245 mbShadowProjectionIsValid
= true;
248 Shadow3DExtractingProcessor::~Shadow3DExtractingProcessor()
250 OSL_ENSURE(maPrimitive2DSequence
.empty(),
251 "OOps, someone used Shadow3DExtractingProcessor, but did not fetch the results (!)");
254 basegfx::B2DPolygon
Shadow3DExtractingProcessor::impDoShadowProjection(const basegfx::B3DPolygon
& rSource
)
256 basegfx::B2DPolygon aRetval
;
258 for(sal_uInt32
a(0); a
< rSource
.count(); a
++)
260 // get point, transform to eye coordinate system
261 basegfx::B3DPoint
aCandidate(rSource
.getB3DPoint(a
));
262 aCandidate
*= getWorldToEye();
264 // we are in eye coordinates
265 // ray is (aCandidate + fCut * maLightNormal)
266 // plane is (maPlanePoint, maShadowPlaneNormal)
267 // maLightNormal.scalar(maShadowPlaneNormal) is already in mfLightPlaneScalar and > 0.0
268 // get cut point of ray with shadow plane
269 const double fCut(basegfx::B3DVector(maPlanePoint
- aCandidate
).scalar(maShadowPlaneNormal
) / mfLightPlaneScalar
);
270 aCandidate
+= maLightNormal
* fCut
;
272 // transform to view, use 2d coordinates
273 aCandidate
*= maEyeToView
;
274 aRetval
.append(basegfx::B2DPoint(aCandidate
.getX(), aCandidate
.getY()));
278 aRetval
.setClosed(rSource
.isClosed());
283 basegfx::B2DPolyPolygon
Shadow3DExtractingProcessor::impDoShadowProjection(const basegfx::B3DPolyPolygon
& rSource
)
285 basegfx::B2DPolyPolygon aRetval
;
287 for(sal_uInt32
a(0); a
< rSource
.count(); a
++)
289 aRetval
.append(impDoShadowProjection(rSource
.getB3DPolygon(a
)));
295 const primitive2d::Primitive2DContainer
& Shadow3DExtractingProcessor::getPrimitive2DSequence() const
297 return maPrimitive2DSequence
;
300 } // end of namespace
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */