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/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())
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
;
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
>(
76 rFillGradient
.getSteps(),
77 rFillGradient
.getColorStops(),
78 rFillGradient
.getBorder(),
79 rFillGradient
.getAngle());
82 case css::awt::GradientStyle_AXIAL
:
84 pNewTex
= std::make_shared
<texture::GeoTexSvxGradientAxial
>(
87 rFillGradient
.getSteps(),
88 rFillGradient
.getColorStops(),
89 rFillGradient
.getBorder(),
90 rFillGradient
.getAngle());
93 case css::awt::GradientStyle_RADIAL
:
96 std::make_shared
<texture::GeoTexSvxGradientRadial
>(
98 rFillGradient
.getSteps(),
99 rFillGradient
.getColorStops(),
100 rFillGradient
.getBorder(),
101 rFillGradient
.getOffsetX(),
102 rFillGradient
.getOffsetY());
105 case css::awt::GradientStyle_ELLIPTICAL
:
108 std::make_shared
<texture::GeoTexSvxGradientElliptical
>(
110 rFillGradient
.getSteps(),
111 rFillGradient
.getColorStops(),
112 rFillGradient
.getBorder(),
113 rFillGradient
.getOffsetX(),
114 rFillGradient
.getOffsetY(),
115 rFillGradient
.getAngle());
118 case css::awt::GradientStyle_SQUARE
:
121 std::make_shared
<texture::GeoTexSvxGradientSquare
>(
123 rFillGradient
.getSteps(),
124 rFillGradient
.getColorStops(),
125 rFillGradient
.getBorder(),
126 rFillGradient
.getOffsetX(),
127 rFillGradient
.getOffsetY(),
128 rFillGradient
.getAngle());
131 case css::awt::GradientStyle_RECT
:
134 std::make_shared
<texture::GeoTexSvxGradientRect
>(
136 rFillGradient
.getSteps(),
137 rFillGradient
.getColorStops(),
138 rFillGradient
.getBorder(),
139 rFillGradient
.getOffsetX(),
140 rFillGradient
.getOffsetY(),
141 rFillGradient
.getAngle());
146 mbSimpleTextureActive
= false;
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
157 mpTransparenceGeoTexSvx
= std::move(pNewTex
);
159 mpGeoTexSvx
= std::move(pNewTex
);
162 process(rSubSequence
);
165 mbModulate
= bOldModulate
;
166 mbFilter
= bOldFilter
;
167 mbSimpleTextureActive
= bOldSimpleTextureActive
;
171 mpTransparenceGeoTexSvx
= std::move(pOldTex
);
175 mpGeoTexSvx
= std::move(pOldTex
);
179 void DefaultProcessor3D::impRenderHatchTexturePrimitive3D(const primitive3d::HatchTexturePrimitive3D
& rPrimitive
)
181 const primitive3d::Primitive3DContainer
& rSubSequence
= rPrimitive
.getChildren();
183 if(rSubSequence
.empty())
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
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
);
212 process(rSubSequence
);
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())
228 const bool bOldModulate(getModulate()); mbModulate
= rPrimitive
.getModulate();
229 const bool bOldFilter(getFilter()); mbFilter
= rPrimitive
.getFilter();
230 std::shared_ptr
< texture::GeoTexSvx
> pOldTex
= mpGeoTexSvx
;
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())
248 std::make_shared
<texture::GeoTexSvxBitmapExTiled
>(
251 rFillGraphicAttribute
.getOffsetX(),
252 rFillGraphicAttribute
.getOffsetY());
257 std::make_shared
<texture::GeoTexSvxBitmapEx
>(
263 process(rSubSequence
);
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());
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())
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
);
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)
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)
375 aNormalTransform
.scale(-1.0, -1.0, -1.0);
381 case css::drawing::ShadeMode_PHONG
:
383 // phong shading. Transform normals to eye coor
384 aFill
.transformNormals(aNormalTransform
);
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
);
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
);
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();
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);
485 case PRIMITIVE3D_ID_HATCHTEXTUREPRIMITIVE3D
:
487 // hatchTexturePrimitive3D
488 const primitive3d::HatchTexturePrimitive3D
& rPrimitive
= static_cast< const primitive3d::HatchTexturePrimitive3D
& >(rBasePrimitive
);
489 impRenderHatchTexturePrimitive3D(rPrimitive
);
492 case PRIMITIVE3D_ID_BITMAPTEXTUREPRIMITIVE3D
:
494 // BitmapTexturePrimitive3D
495 const primitive3d::BitmapTexturePrimitive3D
& rPrimitive
= static_cast< const primitive3d::BitmapTexturePrimitive3D
& >(rBasePrimitive
);
496 impRenderBitmapTexturePrimitive3D(rPrimitive
);
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
--;
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
);
516 case PRIMITIVE3D_ID_POLYGONHAIRLINEPRIMITIVE3D
:
518 // directdraw of PolygonHairlinePrimitive3D
519 const primitive3d::PolygonHairlinePrimitive3D
& rPrimitive
= static_cast< const primitive3d::PolygonHairlinePrimitive3D
& >(rBasePrimitive
);
520 impRenderPolygonHairlinePrimitive3D(rPrimitive
);
523 case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D
:
525 // directdraw of PolyPolygonMaterialPrimitive3D
526 const primitive3d::PolyPolygonMaterialPrimitive3D
& rPrimitive
= static_cast< const primitive3d::PolyPolygonMaterialPrimitive3D
& >(rBasePrimitive
);
527 impRenderPolyPolygonMaterialPrimitive3D(rPrimitive
);
530 case PRIMITIVE3D_ID_TRANSFORMPRIMITIVE3D
:
532 // transform group (TransformPrimitive3D)
533 impRenderTransformPrimitive3D(static_cast< const primitive3d::TransformPrimitive3D
& >(rBasePrimitive
));
538 // process recursively
539 process(rBasePrimitive
.get3DDecomposition(getViewInformation3D()));
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),
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: */