update dev300-m58
[ooovba.git] / drawinglayer / source / processor3d / shadow3dextractor.cxx
blobdaf3df495b099eccdff961497e961b308d7c1658
1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
5 * $RCSfile: shadow3dextractor.cxx,v $
7 * $Revision: 1.9 $
9 * last change: $Author: aw $ $Date: 2008-06-24 15:31:09 $
11 * The Contents of this file are made available subject to
12 * the terms of GNU Lesser General Public License Version 2.1.
15 * GNU Lesser General Public License Version 2.1
16 * =============================================
17 * Copyright 2005 by Sun Microsystems, Inc.
18 * 901 San Antonio Road, Palo Alto, CA 94303, USA
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License version 2.1, as published by the Free Software Foundation.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * MA 02111-1307 USA
34 ************************************************************************/
36 // MARKER(update_precomp.py): autogen include statement, do not remove
37 #include "precompiled_drawinglayer.hxx"
39 #include <drawinglayer/processor3d/shadow3dextractor.hxx>
40 #include <drawinglayer/primitive3d/shadowprimitive3d.hxx>
41 #include <drawinglayer/primitive2d/shadowprimitive2d.hxx>
42 #include <drawinglayer/primitive2d/unifiedalphaprimitive2d.hxx>
43 #include <drawinglayer/primitive3d/transformprimitive3d.hxx>
44 #include <drawinglayer/primitive3d/polygonprimitive3d.hxx>
45 #include <basegfx/polygon/b2dpolygontools.hxx>
46 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
47 #include <drawinglayer/primitive3d/polypolygonprimitive3d.hxx>
48 #include <basegfx/polygon/b2dpolypolygontools.hxx>
49 #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
50 #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
52 //////////////////////////////////////////////////////////////////////////////
54 using namespace com::sun::star;
56 //////////////////////////////////////////////////////////////////////////////
58 namespace drawinglayer
60 namespace processor3d
62 // as tooling, the process() implementation takes over API handling and calls this
63 // virtual render method when the primitive implementation is BasePrimitive3D-based.
64 void Shadow3DExtractingProcessor::processBasePrimitive3D(const primitive3d::BasePrimitive3D& rCandidate)
66 // it is a BasePrimitive3D implementation, use getPrimitiveID() call for switch
67 switch(rCandidate.getPrimitiveID())
69 case PRIMITIVE3D_ID_SHADOWPRIMITIVE3D :
71 // shadow3d object. Call recursive with content and start conversion
72 const primitive3d::ShadowPrimitive3D& rPrimitive = static_cast< const primitive3d::ShadowPrimitive3D& >(rCandidate);
74 // set new target
75 primitive2d::Primitive2DSequence aNewSubList;
76 primitive2d::Primitive2DSequence* pLastTargetSequence = mpPrimitive2DSequence;
77 mpPrimitive2DSequence = &aNewSubList;
79 // activate convert
80 const bool bLastConvert(mbConvert);
81 mbConvert = true;
83 // set projection flag
84 const bool bLastUseProjection(mbUseProjection);
85 mbUseProjection = rPrimitive.getShadow3D();
87 // process content
88 process(rPrimitive.getChildren());
90 // restore values
91 mbUseProjection = bLastUseProjection;
92 mbConvert = bLastConvert;
93 mpPrimitive2DSequence = pLastTargetSequence;
95 // create 2d shadow primitive with result
96 const primitive2d::Primitive2DReference xRef(new primitive2d::ShadowPrimitive2D(rPrimitive.getShadowTransform(), rPrimitive.getShadowColor(), aNewSubList));
98 if(basegfx::fTools::more(rPrimitive.getShadowTransparence(), 0.0))
100 // create simpleTransparencePrimitive, add created primitives
101 const primitive2d::Primitive2DSequence aNewTransPrimitiveVector(&xRef, 1L);
102 const primitive2d::Primitive2DReference xRef2(new primitive2d::UnifiedAlphaPrimitive2D(aNewTransPrimitiveVector, rPrimitive.getShadowTransparence()));
103 primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(*mpPrimitive2DSequence, xRef2);
105 else
107 // add directly
108 primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(*mpPrimitive2DSequence, xRef);
110 break;
112 case PRIMITIVE3D_ID_TRANSFORMPRIMITIVE3D :
114 // transform group. Remember current transformations
115 const primitive3d::TransformPrimitive3D& rPrimitive = static_cast< const primitive3d::TransformPrimitive3D& >(rCandidate);
116 const geometry::ViewInformation3D aLastViewInformation3D(getViewInformation3D());
118 // create new transformation; add new object transform from right side
119 const geometry::ViewInformation3D aNewViewInformation3D(
120 aLastViewInformation3D.getObjectTransformation() * rPrimitive.getTransformation(),
121 aLastViewInformation3D.getOrientation(),
122 aLastViewInformation3D.getProjection(),
123 aLastViewInformation3D.getDeviceToView(),
124 aLastViewInformation3D.getViewTime(),
125 aLastViewInformation3D.getExtendedInformationSequence());
126 updateViewInformation(aNewViewInformation3D);
128 if(mbShadowProjectionIsValid)
130 // update buffered WorldToEye and EyeToView
131 maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
132 maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
135 // let break down
136 process(rPrimitive.getChildren());
138 // restore transformations
139 updateViewInformation(aLastViewInformation3D);
141 if(mbShadowProjectionIsValid)
143 // update buffered WorldToEye and EyeToView
144 maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
145 maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
147 break;
149 case PRIMITIVE3D_ID_POLYGONHAIRLINEPRIMITIVE3D :
151 // PolygonHairlinePrimitive3D
152 if(mbConvert)
154 const primitive3d::PolygonHairlinePrimitive3D& rPrimitive = static_cast< const primitive3d::PolygonHairlinePrimitive3D& >(rCandidate);
155 basegfx::B2DPolygon a2DHairline;
157 if(mbUseProjection)
159 if(mbShadowProjectionIsValid)
161 a2DHairline = impDoShadowProjection(rPrimitive.getB3DPolygon());
164 else
166 a2DHairline = basegfx::tools::createB2DPolygonFromB3DPolygon(rPrimitive.getB3DPolygon(), getViewInformation3D().getObjectToView());
169 if(a2DHairline.count())
171 a2DHairline.transform(getObjectTransformation());
172 const primitive2d::Primitive2DReference xRef(new primitive2d::PolygonHairlinePrimitive2D(a2DHairline, maPrimitiveColor));
173 primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(*mpPrimitive2DSequence, xRef);
176 break;
178 case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D :
180 // PolyPolygonMaterialPrimitive3D
181 if(mbConvert)
183 const primitive3d::PolyPolygonMaterialPrimitive3D& rPrimitive = static_cast< const primitive3d::PolyPolygonMaterialPrimitive3D& >(rCandidate);
184 basegfx::B2DPolyPolygon a2DFill;
186 if(mbUseProjection)
188 if(mbShadowProjectionIsValid)
190 a2DFill = impDoShadowProjection(rPrimitive.getB3DPolyPolygon());
193 else
195 a2DFill = basegfx::tools::createB2DPolyPolygonFromB3DPolyPolygon(rPrimitive.getB3DPolyPolygon(), getViewInformation3D().getObjectToView());
198 if(a2DFill.count())
200 a2DFill.transform(getObjectTransformation());
201 const primitive2d::Primitive2DReference xRef(new primitive2d::PolyPolygonColorPrimitive2D(a2DFill, maPrimitiveColor));
202 primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(*mpPrimitive2DSequence, xRef);
205 break;
207 default :
209 // process recursively
210 process(rCandidate.get3DDecomposition(getViewInformation3D()));
211 break;
216 Shadow3DExtractingProcessor::Shadow3DExtractingProcessor(
217 const geometry::ViewInformation3D& rViewInformation,
218 const basegfx::B2DHomMatrix& rObjectTransformation,
219 const basegfx::B3DVector& rLightNormal,
220 double fShadowSlant,
221 const basegfx::B3DRange& rContained3DRange)
222 : BaseProcessor3D(rViewInformation),
223 maPrimitive2DSequence(),
224 mpPrimitive2DSequence(&maPrimitive2DSequence),
225 maObjectTransformation(rObjectTransformation),
226 maWorldToEye(),
227 maEyeToView(),
228 maLightNormal(rLightNormal),
229 maShadowPlaneNormal(),
230 maPlanePoint(),
231 mfLightPlaneScalar(0.0),
232 maPrimitiveColor(),
233 mbShadowProjectionIsValid(false),
234 mbConvert(false),
235 mbUseProjection(false)
237 // normalize light normal, get and normalize shadow plane normal and calculate scalar from it
238 maLightNormal.normalize();
239 maShadowPlaneNormal = basegfx::B3DVector(0.0, sin(fShadowSlant), cos(fShadowSlant));
240 maShadowPlaneNormal.normalize();
241 mfLightPlaneScalar = maLightNormal.scalar(maShadowPlaneNormal);
243 // use only when scalar is > 0.0, so the light is in front of the object
244 if(basegfx::fTools::more(mfLightPlaneScalar, 0.0))
246 // prepare buffered WorldToEye and EyeToView
247 maWorldToEye = getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation();
248 maEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
250 // calculate range to get front edge around which to rotate the shadow's projection
251 basegfx::B3DRange aContained3DRange(rContained3DRange);
252 aContained3DRange.transform(getWorldToEye());
253 maPlanePoint.setX(maShadowPlaneNormal.getX() < 0.0 ? aContained3DRange.getMinX() : aContained3DRange.getMaxX());
254 maPlanePoint.setY(maShadowPlaneNormal.getY() > 0.0 ? aContained3DRange.getMinY() : aContained3DRange.getMaxY());
255 maPlanePoint.setZ(aContained3DRange.getMinZ() - (aContained3DRange.getDepth() / 8.0));
257 // set flag that shadow projection is prepared and allowed
258 mbShadowProjectionIsValid = true;
262 basegfx::B2DPolygon Shadow3DExtractingProcessor::impDoShadowProjection(const basegfx::B3DPolygon& rSource)
264 basegfx::B2DPolygon aRetval;
266 for(sal_uInt32 a(0L); a < rSource.count(); a++)
268 // get point, transform to eye coordinate system
269 basegfx::B3DPoint aCandidate(rSource.getB3DPoint(a));
270 aCandidate *= getWorldToEye();
272 // we are in eye coordinates
273 // ray is (aCandidate + fCut * maLightNormal)
274 // plane is (maPlanePoint, maShadowPlaneNormal)
275 // maLightNormal.scalar(maShadowPlaneNormal) is already in mfLightPlaneScalar and > 0.0
276 // get cut point of ray with shadow plane
277 const double fCut(basegfx::B3DVector(maPlanePoint - aCandidate).scalar(maShadowPlaneNormal) / mfLightPlaneScalar);
278 aCandidate += maLightNormal * fCut;
280 // transform to view, use 2d coordinates
281 aCandidate *= getEyeToView();
282 aRetval.append(basegfx::B2DPoint(aCandidate.getX(), aCandidate.getY()));
285 // copy closed flag
286 aRetval.setClosed(rSource.isClosed());
288 return aRetval;
291 basegfx::B2DPolyPolygon Shadow3DExtractingProcessor::impDoShadowProjection(const basegfx::B3DPolyPolygon& rSource)
293 basegfx::B2DPolyPolygon aRetval;
295 for(sal_uInt32 a(0L); a < rSource.count(); a++)
297 aRetval.append(impDoShadowProjection(rSource.getB3DPolygon(a)));
300 return aRetval;
302 } // end of namespace processor3d
303 } // end of namespace drawinglayer
305 //////////////////////////////////////////////////////////////////////////////
306 // eof