cool#10630 lok doc sign: fix Impress sign line when picking a certificate
[LibreOffice.git] / drawinglayer / source / processor3d / defaultprocessor3d.cxx
blob7331ed8421658a93790ef2744ffe8f7500168530
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 <processor3d/defaultprocessor3d.hxx>
21 #include <primitive3d/textureprimitive3d.hxx>
22 #include <texture/texture.hxx>
23 #include <texture/texture3d.hxx>
24 #include <primitive3d/hatchtextureprimitive3d.hxx>
25 #include <drawinglayer/primitive3d/modifiedcolorprimitive3d.hxx>
26 #include <drawinglayer/primitive3d/polygonprimitive3d.hxx>
27 #include <basegfx/polygon/b3dpolygontools.hxx>
28 #include <drawinglayer/attribute/materialattribute3d.hxx>
29 #include <drawinglayer/primitive3d/polypolygonprimitive3d.hxx>
30 #include <basegfx/polygon/b3dpolypolygontools.hxx>
31 #include <com/sun/star/drawing/ShadeMode.hpp>
32 #include <drawinglayer/primitive3d/transformprimitive3d.hxx>
33 #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
34 #include <vcl/bitmapex.hxx>
35 #include <drawinglayer/attribute/sdrsceneattribute3d.hxx>
36 #include <drawinglayer/attribute/sdrlightingattribute3d.hxx>
37 #include <vcl/graph.hxx>
38 #include <basegfx/matrix/b2dhommatrixtools.hxx>
41 using namespace com::sun::star;
44 namespace drawinglayer::processor3d
46 void DefaultProcessor3D::impRenderGradientTexturePrimitive3D(const primitive3d::GradientTexturePrimitive3D& rPrimitive, bool bTransparence)
48 const primitive3d::Primitive3DContainer& rSubSequence = rPrimitive.getChildren();
50 if(rSubSequence.empty())
51 return;
53 // rescue values
54 const bool bOldModulate(getModulate()); mbModulate = rPrimitive.getModulate();
55 const bool bOldFilter(getFilter()); mbFilter = rPrimitive.getFilter();
56 const bool bOldSimpleTextureActive(getSimpleTextureActive());
57 std::shared_ptr< texture::GeoTexSvx > pOldTex = bTransparence ? mpTransparenceGeoTexSvx : mpGeoTexSvx;
59 // create texture
60 const attribute::FillGradientAttribute& rFillGradient = rPrimitive.getGradient();
61 const basegfx::B2DRange aOutlineRange(0.0, 0.0, rPrimitive.getTextureSize().getX(), rPrimitive.getTextureSize().getY());
62 const css::awt::GradientStyle aGradientStyle(rFillGradient.getStyle());
63 std::shared_ptr< texture::GeoTexSvx > pNewTex;
64 basegfx::BColor aSingleColor;
66 if (!rFillGradient.getColorStops().isSingleColor(aSingleColor))
68 switch(aGradientStyle)
70 default: // GradientStyle_MAKE_FIXED_SIZE
71 case css::awt::GradientStyle_LINEAR:
73 pNewTex = std::make_shared<texture::GeoTexSvxGradientLinear>(
74 aOutlineRange,
75 aOutlineRange,
76 rFillGradient.getSteps(),
77 rFillGradient.getColorStops(),
78 rFillGradient.getBorder(),
79 rFillGradient.getAngle());
80 break;
82 case css::awt::GradientStyle_AXIAL:
84 pNewTex = std::make_shared<texture::GeoTexSvxGradientAxial>(
85 aOutlineRange,
86 aOutlineRange,
87 rFillGradient.getSteps(),
88 rFillGradient.getColorStops(),
89 rFillGradient.getBorder(),
90 rFillGradient.getAngle());
91 break;
93 case css::awt::GradientStyle_RADIAL:
95 pNewTex =
96 std::make_shared<texture::GeoTexSvxGradientRadial>(
97 aOutlineRange,
98 rFillGradient.getSteps(),
99 rFillGradient.getColorStops(),
100 rFillGradient.getBorder(),
101 rFillGradient.getOffsetX(),
102 rFillGradient.getOffsetY());
103 break;
105 case css::awt::GradientStyle_ELLIPTICAL:
107 pNewTex =
108 std::make_shared<texture::GeoTexSvxGradientElliptical>(
109 aOutlineRange,
110 rFillGradient.getSteps(),
111 rFillGradient.getColorStops(),
112 rFillGradient.getBorder(),
113 rFillGradient.getOffsetX(),
114 rFillGradient.getOffsetY(),
115 rFillGradient.getAngle());
116 break;
118 case css::awt::GradientStyle_SQUARE:
120 pNewTex =
121 std::make_shared<texture::GeoTexSvxGradientSquare>(
122 aOutlineRange,
123 rFillGradient.getSteps(),
124 rFillGradient.getColorStops(),
125 rFillGradient.getBorder(),
126 rFillGradient.getOffsetX(),
127 rFillGradient.getOffsetY(),
128 rFillGradient.getAngle());
129 break;
131 case css::awt::GradientStyle_RECT:
133 pNewTex =
134 std::make_shared<texture::GeoTexSvxGradientRect>(
135 aOutlineRange,
136 rFillGradient.getSteps(),
137 rFillGradient.getColorStops(),
138 rFillGradient.getBorder(),
139 rFillGradient.getOffsetX(),
140 rFillGradient.getOffsetY(),
141 rFillGradient.getAngle());
142 break;
146 mbSimpleTextureActive = false;
148 else
150 // only one color, so no real gradient -> use simple texture
151 pNewTex = std::make_shared<texture::GeoTexSvxMono>(aSingleColor, 1.0 - aSingleColor.luminance());
152 mbSimpleTextureActive = true;
155 // set created texture
156 if(bTransparence)
157 mpTransparenceGeoTexSvx = std::move(pNewTex);
158 else
159 mpGeoTexSvx = std::move(pNewTex);
161 // process sub-list
162 process(rSubSequence);
164 // restore values
165 mbModulate = bOldModulate;
166 mbFilter = bOldFilter;
167 mbSimpleTextureActive = bOldSimpleTextureActive;
169 if(bTransparence)
171 mpTransparenceGeoTexSvx = std::move(pOldTex);
173 else
175 mpGeoTexSvx = std::move(pOldTex);
179 void DefaultProcessor3D::impRenderHatchTexturePrimitive3D(const primitive3d::HatchTexturePrimitive3D& rPrimitive)
181 const primitive3d::Primitive3DContainer& rSubSequence = rPrimitive.getChildren();
183 if(rSubSequence.empty())
184 return;
186 // rescue values
187 const bool bOldModulate(getModulate()); mbModulate = rPrimitive.getModulate();
188 const bool bOldFilter(getFilter()); mbFilter = rPrimitive.getFilter();
189 std::shared_ptr<texture::GeoTexSvx> xOldTex(mpGeoTexSvx);
191 // calculate logic pixel size in object coordinates. Create transformation view
192 // to object by inverting ObjectToView
193 basegfx::B3DHomMatrix aInvObjectToView(getViewInformation3D().getObjectToView());
194 aInvObjectToView.invert();
196 // back-project discrete coordinates to object coordinates and extract
197 // maximum distance
198 const basegfx::B3DPoint aZero(aInvObjectToView * basegfx::B3DPoint(0.0, 0.0, 0.0));
199 const basegfx::B3DPoint aOne(aInvObjectToView * basegfx::B3DPoint(1.0, 1.0, 1.0));
200 const basegfx::B3DVector aLogicPixel(aOne - aZero);
201 double fLogicPixelSizeWorld(std::max(std::max(fabs(aLogicPixel.getX()), fabs(aLogicPixel.getY())), fabs(aLogicPixel.getZ())));
203 // calculate logic pixel size in texture coordinates
204 const double fLogicTexSizeX(fLogicPixelSizeWorld / rPrimitive.getTextureSize().getX());
205 const double fLogicTexSizeY(fLogicPixelSizeWorld / rPrimitive.getTextureSize().getY());
206 const double fLogicTexSize(std::max(fLogicTexSizeX, fLogicTexSizeY));
208 // create texture and set
209 mpGeoTexSvx = std::make_shared<texture::GeoTexSvxMultiHatch>(rPrimitive, fLogicTexSize);
211 // process sub-list
212 process(rSubSequence);
214 // restore values
215 mbModulate = bOldModulate;
216 mbFilter = bOldFilter;
217 mpGeoTexSvx = std::move(xOldTex);
220 void DefaultProcessor3D::impRenderBitmapTexturePrimitive3D(const primitive3d::BitmapTexturePrimitive3D& rPrimitive)
222 const primitive3d::Primitive3DContainer& rSubSequence = rPrimitive.getChildren();
224 if(rSubSequence.empty())
225 return;
227 // rescue values
228 const bool bOldModulate(getModulate()); mbModulate = rPrimitive.getModulate();
229 const bool bOldFilter(getFilter()); mbFilter = rPrimitive.getFilter();
230 std::shared_ptr< texture::GeoTexSvx > pOldTex = mpGeoTexSvx;
232 // create texture
233 const attribute::FillGraphicAttribute& rFillGraphicAttribute = rPrimitive.getFillGraphicAttribute();
235 // #121194# For 3D texture we will use the BitmapRex representation
236 const BitmapEx aBitmapEx(rFillGraphicAttribute.getGraphic().GetBitmapEx());
238 // create range scaled by texture size
239 basegfx::B2DRange aGraphicRange(rFillGraphicAttribute.getGraphicRange());
241 aGraphicRange.transform(
242 basegfx::utils::createScaleB2DHomMatrix(
243 rPrimitive.getTextureSize()));
245 if(rFillGraphicAttribute.getTiling())
247 mpGeoTexSvx =
248 std::make_shared<texture::GeoTexSvxBitmapExTiled>(
249 aBitmapEx,
250 aGraphicRange,
251 rFillGraphicAttribute.getOffsetX(),
252 rFillGraphicAttribute.getOffsetY());
254 else
256 mpGeoTexSvx =
257 std::make_shared<texture::GeoTexSvxBitmapEx>(
258 aBitmapEx,
259 aGraphicRange);
262 // process sub-list
263 process(rSubSequence);
265 // restore values
266 mbModulate = bOldModulate;
267 mbFilter = bOldFilter;
268 mpGeoTexSvx = std::move(pOldTex);
271 void DefaultProcessor3D::impRenderModifiedColorPrimitive3D(const primitive3d::ModifiedColorPrimitive3D& rModifiedCandidate)
273 const primitive3d::Primitive3DContainer& rSubSequence = rModifiedCandidate.getChildren();
275 if(!rSubSequence.empty())
277 // put modifier on stack
278 maBColorModifierStack.push(rModifiedCandidate.getColorModifier());
280 // process sub-list
281 process(rModifiedCandidate.getChildren());
283 // remove modifier from stack
284 maBColorModifierStack.pop();
288 void DefaultProcessor3D::impRenderPolygonHairlinePrimitive3D(const primitive3d::PolygonHairlinePrimitive3D& rPrimitive) const
290 basegfx::B3DPolygon aHairline(rPrimitive.getB3DPolygon());
292 if(!aHairline.count())
293 return;
295 // hairlines need no extra data, clear it
296 aHairline.clearTextureCoordinates();
297 aHairline.clearNormals();
298 aHairline.clearBColors();
300 // transform to device coordinates (-1.0 .. 1.0) and check for visibility
301 aHairline.transform(getViewInformation3D().getObjectToView());
302 const basegfx::B3DRange a3DRange(basegfx::utils::getRange(aHairline));
303 const basegfx::B2DRange a2DRange(a3DRange.getMinX(), a3DRange.getMinY(), a3DRange.getMaxX(), a3DRange.getMaxY());
305 if(a2DRange.overlaps(maRasterRange))
307 const attribute::MaterialAttribute3D aMaterial(maBColorModifierStack.getModifiedColor(rPrimitive.getBColor()));
309 rasterconvertB3DPolygon(aMaterial, aHairline);
313 void DefaultProcessor3D::impRenderPolyPolygonMaterialPrimitive3D(const primitive3d::PolyPolygonMaterialPrimitive3D& rPrimitive) const
315 basegfx::B3DPolyPolygon aFill(rPrimitive.getB3DPolyPolygon());
316 basegfx::BColor aObjectColor(rPrimitive.getMaterial().getColor());
317 bool bPaintIt(aFill.count());
319 // #i98295# get ShadeMode. Correct early when only flat is possible due to missing normals
320 const css::drawing::ShadeMode aShadeMode(
321 aFill.areNormalsUsed() ?
322 getSdrSceneAttribute().getShadeMode() : css::drawing::ShadeMode_FLAT);
324 if(bPaintIt)
326 // get rid of texture coordinates if there is no texture
327 if(aFill.areTextureCoordinatesUsed() && !getGeoTexSvx() && !getTransparenceGeoTexSvx())
329 aFill.clearTextureCoordinates();
332 // #i98295# get rid of normals and color early when not needed
333 if(css::drawing::ShadeMode_FLAT == aShadeMode)
335 aFill.clearNormals();
336 aFill.clearBColors();
339 // transform to device coordinates (-1.0 .. 1.0) and check for visibility
340 aFill.transform(getViewInformation3D().getObjectToView());
341 const basegfx::B3DRange a3DRange(basegfx::utils::getRange(aFill));
342 const basegfx::B2DRange a2DRange(a3DRange.getMinX(), a3DRange.getMinY(), a3DRange.getMaxX(), a3DRange.getMaxY());
344 bPaintIt = a2DRange.overlaps(maRasterRange);
347 // check if it shall be painted regarding hiding of normals (backface culling)
348 if(bPaintIt && !rPrimitive.getDoubleSided())
350 // get plane normal of polygon in view coordinates (with ZBuffer values),
351 // left-handed coordinate system
352 const basegfx::B3DVector aPlaneNormal(aFill.getB3DPolygon(0).getNormal());
354 if(aPlaneNormal.getZ() > 0.0)
356 bPaintIt = false;
360 if(!bPaintIt)
361 return;
363 // prepare ObjectToEye in NormalTransform
364 basegfx::B3DHomMatrix aNormalTransform(getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation());
366 if(getSdrSceneAttribute().getTwoSidedLighting())
368 // get plane normal of polygon in view coordinates (with ZBuffer values),
369 // left-handed coordinate system
370 const basegfx::B3DVector aPlaneNormal(aFill.getB3DPolygon(0).getNormal());
372 if(aPlaneNormal.getZ() > 0.0)
374 // mirror normals
375 aNormalTransform.scale(-1.0, -1.0, -1.0);
379 switch(aShadeMode)
381 case css::drawing::ShadeMode_PHONG:
383 // phong shading. Transform normals to eye coor
384 aFill.transformNormals(aNormalTransform);
385 break;
387 case css::drawing::ShadeMode_SMOOTH:
389 // gouraud shading. Transform normals to eye coor
390 aFill.transformNormals(aNormalTransform);
392 // prepare color model parameters, evtl. use blend color
393 const basegfx::BColor aColor(getModulate() ? basegfx::BColor(1.0, 1.0, 1.0) : rPrimitive.getMaterial().getColor());
394 const basegfx::BColor& rSpecular(rPrimitive.getMaterial().getSpecular());
395 const basegfx::BColor& rEmission(rPrimitive.getMaterial().getEmission());
396 const sal_uInt16 nSpecularIntensity(rPrimitive.getMaterial().getSpecularIntensity());
398 // solve color model for each normal vector, set colors at points. Clear normals.
399 for(sal_uInt32 a(0); a < aFill.count(); a++)
401 basegfx::B3DPolygon aPartFill(aFill.getB3DPolygon(a));
403 for(sal_uInt32 b(0); b < aPartFill.count(); b++)
405 // solve color model. Transform normal to eye coor
406 const basegfx::B3DVector aNormal(aPartFill.getNormal(b));
407 const basegfx::BColor aSolvedColor(getSdrLightingAttribute().solveColorModel(aNormal, aColor, rSpecular, rEmission, nSpecularIntensity));
408 aPartFill.setBColor(b, aSolvedColor);
411 // clear normals on this part polygon and write it back
412 aPartFill.clearNormals();
413 aFill.setB3DPolygon(a, aPartFill);
415 break;
417 case css::drawing::ShadeMode_FLAT:
419 // flat shading. Get plane vector in eye coordinates
420 const basegfx::B3DVector aPlaneEyeNormal(aNormalTransform * rPrimitive.getB3DPolyPolygon().getB3DPolygon(0).getNormal());
422 // prepare color model parameters, evtl. use blend color
423 const basegfx::BColor aColor(getModulate() ? basegfx::BColor(1.0, 1.0, 1.0) : rPrimitive.getMaterial().getColor());
424 const basegfx::BColor& rSpecular(rPrimitive.getMaterial().getSpecular());
425 const basegfx::BColor& rEmission(rPrimitive.getMaterial().getEmission());
426 const sal_uInt16 nSpecularIntensity(rPrimitive.getMaterial().getSpecularIntensity());
428 // solve color model for plane vector and use that color for whole plane
429 aObjectColor = getSdrLightingAttribute().solveColorModel(aPlaneEyeNormal, aColor, rSpecular, rEmission, nSpecularIntensity);
430 break;
432 default: // case css::drawing::ShadeMode_DRAFT:
434 // draft, just use object color which is already set. Delete all other infos
435 aFill.clearNormals();
436 aFill.clearBColors();
437 break;
441 // draw it to ZBuffer
442 const attribute::MaterialAttribute3D aMaterial(
443 maBColorModifierStack.getModifiedColor(aObjectColor),
444 rPrimitive.getMaterial().getSpecular(),
445 rPrimitive.getMaterial().getEmission(),
446 rPrimitive.getMaterial().getSpecularIntensity());
448 rasterconvertB3DPolyPolygon(aMaterial, aFill);
451 void DefaultProcessor3D::impRenderTransformPrimitive3D(const primitive3d::TransformPrimitive3D& rTransformCandidate)
453 // transform group. Remember current transformations
454 const geometry::ViewInformation3D aLastViewInformation3D(getViewInformation3D());
456 // create new transformation; add new object transform from right side
457 const geometry::ViewInformation3D aNewViewInformation3D(
458 aLastViewInformation3D.getObjectTransformation() * rTransformCandidate.getTransformation(),
459 aLastViewInformation3D.getOrientation(),
460 aLastViewInformation3D.getProjection(),
461 aLastViewInformation3D.getDeviceToView(),
462 aLastViewInformation3D.getViewTime(),
463 aLastViewInformation3D.getExtendedInformationSequence());
464 updateViewInformation(aNewViewInformation3D);
466 // let break down recursively
467 process(rTransformCandidate.getChildren());
469 // restore transformations
470 updateViewInformation(aLastViewInformation3D);
473 void DefaultProcessor3D::processBasePrimitive3D(const primitive3d::BasePrimitive3D& rBasePrimitive)
475 // it is a BasePrimitive3D implementation, use getPrimitive3DID() call for switch
476 switch(rBasePrimitive.getPrimitive3DID())
478 case PRIMITIVE3D_ID_GRADIENTTEXTUREPRIMITIVE3D :
480 // GradientTexturePrimitive3D
481 const primitive3d::GradientTexturePrimitive3D& rPrimitive = static_cast< const primitive3d::GradientTexturePrimitive3D& >(rBasePrimitive);
482 impRenderGradientTexturePrimitive3D(rPrimitive, false);
483 break;
485 case PRIMITIVE3D_ID_HATCHTEXTUREPRIMITIVE3D :
487 // hatchTexturePrimitive3D
488 const primitive3d::HatchTexturePrimitive3D& rPrimitive = static_cast< const primitive3d::HatchTexturePrimitive3D& >(rBasePrimitive);
489 impRenderHatchTexturePrimitive3D(rPrimitive);
490 break;
492 case PRIMITIVE3D_ID_BITMAPTEXTUREPRIMITIVE3D :
494 // BitmapTexturePrimitive3D
495 const primitive3d::BitmapTexturePrimitive3D& rPrimitive = static_cast< const primitive3d::BitmapTexturePrimitive3D& >(rBasePrimitive);
496 impRenderBitmapTexturePrimitive3D(rPrimitive);
497 break;
499 case PRIMITIVE3D_ID_TRANSPARENCETEXTUREPRIMITIVE3D :
501 // TransparenceTexturePrimitive3D
502 const primitive3d::TransparenceTexturePrimitive3D& rPrimitive = static_cast< const primitive3d::TransparenceTexturePrimitive3D& >(rBasePrimitive);
503 mnTransparenceCounter++;
504 impRenderGradientTexturePrimitive3D(rPrimitive, true);
505 mnTransparenceCounter--;
506 break;
508 case PRIMITIVE3D_ID_MODIFIEDCOLORPRIMITIVE3D :
510 // ModifiedColorPrimitive3D
511 // Force output to unified color.
512 const primitive3d::ModifiedColorPrimitive3D& rPrimitive = static_cast< const primitive3d::ModifiedColorPrimitive3D& >(rBasePrimitive);
513 impRenderModifiedColorPrimitive3D(rPrimitive);
514 break;
516 case PRIMITIVE3D_ID_POLYGONHAIRLINEPRIMITIVE3D :
518 // directdraw of PolygonHairlinePrimitive3D
519 const primitive3d::PolygonHairlinePrimitive3D& rPrimitive = static_cast< const primitive3d::PolygonHairlinePrimitive3D& >(rBasePrimitive);
520 impRenderPolygonHairlinePrimitive3D(rPrimitive);
521 break;
523 case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D :
525 // directdraw of PolyPolygonMaterialPrimitive3D
526 const primitive3d::PolyPolygonMaterialPrimitive3D& rPrimitive = static_cast< const primitive3d::PolyPolygonMaterialPrimitive3D& >(rBasePrimitive);
527 impRenderPolyPolygonMaterialPrimitive3D(rPrimitive);
528 break;
530 case PRIMITIVE3D_ID_TRANSFORMPRIMITIVE3D :
532 // transform group (TransformPrimitive3D)
533 impRenderTransformPrimitive3D(static_cast< const primitive3d::TransformPrimitive3D& >(rBasePrimitive));
534 break;
536 default:
538 // process recursively
539 process(rBasePrimitive.get3DDecomposition(getViewInformation3D()));
540 break;
545 DefaultProcessor3D::DefaultProcessor3D(
546 const geometry::ViewInformation3D& rViewInformation,
547 const attribute::SdrSceneAttribute& rSdrSceneAttribute,
548 const attribute::SdrLightingAttribute& rSdrLightingAttribute)
549 : BaseProcessor3D(rViewInformation),
550 mrSdrSceneAttribute(rSdrSceneAttribute),
551 mrSdrLightingAttribute(rSdrLightingAttribute),
552 maBColorModifierStack(),
553 mnTransparenceCounter(0),
554 mbModulate(false),
555 mbFilter(false),
556 mbSimpleTextureActive(false)
558 // a derivation has to set maRasterRange which is used in the basic render methods.
559 // Setting to default here ([0.0 .. 1.0] in X,Y) to avoid problems
560 maRasterRange.expand(basegfx::B2DTuple(0.0, 0.0));
561 maRasterRange.expand(basegfx::B2DTuple(1.0, 1.0));
564 DefaultProcessor3D::~DefaultProcessor3D()
568 } // end of namespace
570 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */