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
158 mpTransparenceGeoTexSvx
= pNewTex
;
162 mpGeoTexSvx
= pNewTex
;
166 process(rSubSequence
);
169 mbModulate
= bOldModulate
;
170 mbFilter
= bOldFilter
;
171 mbSimpleTextureActive
= bOldSimpleTextureActive
;
175 mpTransparenceGeoTexSvx
= pOldTex
;
179 mpGeoTexSvx
= pOldTex
;
183 void DefaultProcessor3D::impRenderHatchTexturePrimitive3D(const primitive3d::HatchTexturePrimitive3D
& rPrimitive
)
185 const primitive3d::Primitive3DContainer
& rSubSequence
= rPrimitive
.getChildren();
187 if(rSubSequence
.empty())
191 const bool bOldModulate(getModulate()); mbModulate
= rPrimitive
.getModulate();
192 const bool bOldFilter(getFilter()); mbFilter
= rPrimitive
.getFilter();
193 std::shared_ptr
< texture::GeoTexSvx
> pOldTex
= mpGeoTexSvx
;
195 // calculate logic pixel size in object coordinates. Create transformation view
196 // to object by inverting ObjectToView
197 basegfx::B3DHomMatrix
aInvObjectToView(getViewInformation3D().getObjectToView());
198 aInvObjectToView
.invert();
200 // back-project discrete coordinates to object coordinates and extract
202 const basegfx::B3DPoint
aZero(aInvObjectToView
* basegfx::B3DPoint(0.0, 0.0, 0.0));
203 const basegfx::B3DPoint
aOne(aInvObjectToView
* basegfx::B3DPoint(1.0, 1.0, 1.0));
204 const basegfx::B3DVector
aLogicPixel(aOne
- aZero
);
205 double fLogicPixelSizeWorld(std::max(std::max(fabs(aLogicPixel
.getX()), fabs(aLogicPixel
.getY())), fabs(aLogicPixel
.getZ())));
207 // calculate logic pixel size in texture coordinates
208 const double fLogicTexSizeX(fLogicPixelSizeWorld
/ rPrimitive
.getTextureSize().getX());
209 const double fLogicTexSizeY(fLogicPixelSizeWorld
/ rPrimitive
.getTextureSize().getY());
210 const double fLogicTexSize(std::max(fLogicTexSizeX
, fLogicTexSizeY
));
212 // create texture and set
213 mpGeoTexSvx
= std::make_shared
<texture::GeoTexSvxMultiHatch
>(rPrimitive
, fLogicTexSize
);
216 process(rSubSequence
);
219 mbModulate
= bOldModulate
;
220 mbFilter
= bOldFilter
;
221 mpGeoTexSvx
= pOldTex
;
224 void DefaultProcessor3D::impRenderBitmapTexturePrimitive3D(const primitive3d::BitmapTexturePrimitive3D
& rPrimitive
)
226 const primitive3d::Primitive3DContainer
& rSubSequence
= rPrimitive
.getChildren();
228 if(rSubSequence
.empty())
232 const bool bOldModulate(getModulate()); mbModulate
= rPrimitive
.getModulate();
233 const bool bOldFilter(getFilter()); mbFilter
= rPrimitive
.getFilter();
234 std::shared_ptr
< texture::GeoTexSvx
> pOldTex
= mpGeoTexSvx
;
237 const attribute::FillGraphicAttribute
& rFillGraphicAttribute
= rPrimitive
.getFillGraphicAttribute();
239 // #121194# For 3D texture we will use the BitmapRex representation
240 const BitmapEx
aBitmapEx(rFillGraphicAttribute
.getGraphic().GetBitmapEx());
242 // create range scaled by texture size
243 basegfx::B2DRange
aGraphicRange(rFillGraphicAttribute
.getGraphicRange());
245 aGraphicRange
.transform(
246 basegfx::utils::createScaleB2DHomMatrix(
247 rPrimitive
.getTextureSize()));
249 if(rFillGraphicAttribute
.getTiling())
252 std::make_shared
<texture::GeoTexSvxBitmapExTiled
>(
255 rFillGraphicAttribute
.getOffsetX(),
256 rFillGraphicAttribute
.getOffsetY());
261 std::make_shared
<texture::GeoTexSvxBitmapEx
>(
267 process(rSubSequence
);
270 mbModulate
= bOldModulate
;
271 mbFilter
= bOldFilter
;
272 mpGeoTexSvx
= pOldTex
;
275 void DefaultProcessor3D::impRenderModifiedColorPrimitive3D(const primitive3d::ModifiedColorPrimitive3D
& rModifiedCandidate
)
277 const primitive3d::Primitive3DContainer
& rSubSequence
= rModifiedCandidate
.getChildren();
279 if(!rSubSequence
.empty())
281 // put modifier on stack
282 maBColorModifierStack
.push(rModifiedCandidate
.getColorModifier());
285 process(rModifiedCandidate
.getChildren());
287 // remove modifier from stack
288 maBColorModifierStack
.pop();
292 void DefaultProcessor3D::impRenderPolygonHairlinePrimitive3D(const primitive3d::PolygonHairlinePrimitive3D
& rPrimitive
) const
294 basegfx::B3DPolygon
aHairline(rPrimitive
.getB3DPolygon());
296 if(!aHairline
.count())
299 // hairlines need no extra data, clear it
300 aHairline
.clearTextureCoordinates();
301 aHairline
.clearNormals();
302 aHairline
.clearBColors();
304 // transform to device coordinates (-1.0 .. 1.0) and check for visibility
305 aHairline
.transform(getViewInformation3D().getObjectToView());
306 const basegfx::B3DRange
a3DRange(basegfx::utils::getRange(aHairline
));
307 const basegfx::B2DRange
a2DRange(a3DRange
.getMinX(), a3DRange
.getMinY(), a3DRange
.getMaxX(), a3DRange
.getMaxY());
309 if(a2DRange
.overlaps(maRasterRange
))
311 const attribute::MaterialAttribute3D
aMaterial(maBColorModifierStack
.getModifiedColor(rPrimitive
.getBColor()));
313 rasterconvertB3DPolygon(aMaterial
, aHairline
);
317 void DefaultProcessor3D::impRenderPolyPolygonMaterialPrimitive3D(const primitive3d::PolyPolygonMaterialPrimitive3D
& rPrimitive
) const
319 basegfx::B3DPolyPolygon
aFill(rPrimitive
.getB3DPolyPolygon());
320 basegfx::BColor
aObjectColor(rPrimitive
.getMaterial().getColor());
321 bool bPaintIt(aFill
.count());
323 // #i98295# get ShadeMode. Correct early when only flat is possible due to missing normals
324 const css::drawing::ShadeMode
aShadeMode(
325 aFill
.areNormalsUsed() ?
326 getSdrSceneAttribute().getShadeMode() : css::drawing::ShadeMode_FLAT
);
330 // get rid of texture coordinates if there is no texture
331 if(aFill
.areTextureCoordinatesUsed() && !getGeoTexSvx() && !getTransparenceGeoTexSvx())
333 aFill
.clearTextureCoordinates();
336 // #i98295# get rid of normals and color early when not needed
337 if(css::drawing::ShadeMode_FLAT
== aShadeMode
)
339 aFill
.clearNormals();
340 aFill
.clearBColors();
343 // transform to device coordinates (-1.0 .. 1.0) and check for visibility
344 aFill
.transform(getViewInformation3D().getObjectToView());
345 const basegfx::B3DRange
a3DRange(basegfx::utils::getRange(aFill
));
346 const basegfx::B2DRange
a2DRange(a3DRange
.getMinX(), a3DRange
.getMinY(), a3DRange
.getMaxX(), a3DRange
.getMaxY());
348 bPaintIt
= a2DRange
.overlaps(maRasterRange
);
351 // check if it shall be painted regarding hiding of normals (backface culling)
352 if(bPaintIt
&& !rPrimitive
.getDoubleSided())
354 // get plane normal of polygon in view coordinates (with ZBuffer values),
355 // left-handed coordinate system
356 const basegfx::B3DVector
aPlaneNormal(aFill
.getB3DPolygon(0).getNormal());
358 if(aPlaneNormal
.getZ() > 0.0)
367 // prepare ObjectToEye in NormalTransform
368 basegfx::B3DHomMatrix
aNormalTransform(getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation());
370 if(getSdrSceneAttribute().getTwoSidedLighting())
372 // get plane normal of polygon in view coordinates (with ZBuffer values),
373 // left-handed coordinate system
374 const basegfx::B3DVector
aPlaneNormal(aFill
.getB3DPolygon(0).getNormal());
376 if(aPlaneNormal
.getZ() > 0.0)
379 aNormalTransform
.scale(-1.0, -1.0, -1.0);
385 case css::drawing::ShadeMode_PHONG
:
387 // phong shading. Transform normals to eye coor
388 aFill
.transformNormals(aNormalTransform
);
391 case css::drawing::ShadeMode_SMOOTH
:
393 // gouraud shading. Transform normals to eye coor
394 aFill
.transformNormals(aNormalTransform
);
396 // prepare color model parameters, evtl. use blend color
397 const basegfx::BColor
aColor(getModulate() ? basegfx::BColor(1.0, 1.0, 1.0) : rPrimitive
.getMaterial().getColor());
398 const basegfx::BColor
& rSpecular(rPrimitive
.getMaterial().getSpecular());
399 const basegfx::BColor
& rEmission(rPrimitive
.getMaterial().getEmission());
400 const sal_uInt16
nSpecularIntensity(rPrimitive
.getMaterial().getSpecularIntensity());
402 // solve color model for each normal vector, set colors at points. Clear normals.
403 for(sal_uInt32
a(0); a
< aFill
.count(); a
++)
405 basegfx::B3DPolygon
aPartFill(aFill
.getB3DPolygon(a
));
407 for(sal_uInt32
b(0); b
< aPartFill
.count(); b
++)
409 // solve color model. Transform normal to eye coor
410 const basegfx::B3DVector
aNormal(aPartFill
.getNormal(b
));
411 const basegfx::BColor
aSolvedColor(getSdrLightingAttribute().solveColorModel(aNormal
, aColor
, rSpecular
, rEmission
, nSpecularIntensity
));
412 aPartFill
.setBColor(b
, aSolvedColor
);
415 // clear normals on this part polygon and write it back
416 aPartFill
.clearNormals();
417 aFill
.setB3DPolygon(a
, aPartFill
);
421 case css::drawing::ShadeMode_FLAT
:
423 // flat shading. Get plane vector in eye coordinates
424 const basegfx::B3DVector
aPlaneEyeNormal(aNormalTransform
* rPrimitive
.getB3DPolyPolygon().getB3DPolygon(0).getNormal());
426 // prepare color model parameters, evtl. use blend color
427 const basegfx::BColor
aColor(getModulate() ? basegfx::BColor(1.0, 1.0, 1.0) : rPrimitive
.getMaterial().getColor());
428 const basegfx::BColor
& rSpecular(rPrimitive
.getMaterial().getSpecular());
429 const basegfx::BColor
& rEmission(rPrimitive
.getMaterial().getEmission());
430 const sal_uInt16
nSpecularIntensity(rPrimitive
.getMaterial().getSpecularIntensity());
432 // solve color model for plane vector and use that color for whole plane
433 aObjectColor
= getSdrLightingAttribute().solveColorModel(aPlaneEyeNormal
, aColor
, rSpecular
, rEmission
, nSpecularIntensity
);
436 default: // case css::drawing::ShadeMode_DRAFT:
438 // draft, just use object color which is already set. Delete all other infos
439 aFill
.clearNormals();
440 aFill
.clearBColors();
445 // draw it to ZBuffer
446 const attribute::MaterialAttribute3D
aMaterial(
447 maBColorModifierStack
.getModifiedColor(aObjectColor
),
448 rPrimitive
.getMaterial().getSpecular(),
449 rPrimitive
.getMaterial().getEmission(),
450 rPrimitive
.getMaterial().getSpecularIntensity());
452 rasterconvertB3DPolyPolygon(aMaterial
, aFill
);
455 void DefaultProcessor3D::impRenderTransformPrimitive3D(const primitive3d::TransformPrimitive3D
& rTransformCandidate
)
457 // transform group. Remember current transformations
458 const geometry::ViewInformation3D
aLastViewInformation3D(getViewInformation3D());
460 // create new transformation; add new object transform from right side
461 const geometry::ViewInformation3D
aNewViewInformation3D(
462 aLastViewInformation3D
.getObjectTransformation() * rTransformCandidate
.getTransformation(),
463 aLastViewInformation3D
.getOrientation(),
464 aLastViewInformation3D
.getProjection(),
465 aLastViewInformation3D
.getDeviceToView(),
466 aLastViewInformation3D
.getViewTime(),
467 aLastViewInformation3D
.getExtendedInformationSequence());
468 updateViewInformation(aNewViewInformation3D
);
470 // let break down recursively
471 process(rTransformCandidate
.getChildren());
473 // restore transformations
474 updateViewInformation(aLastViewInformation3D
);
477 void DefaultProcessor3D::processBasePrimitive3D(const primitive3d::BasePrimitive3D
& rBasePrimitive
)
479 // it is a BasePrimitive3D implementation, use getPrimitive3DID() call for switch
480 switch(rBasePrimitive
.getPrimitive3DID())
482 case PRIMITIVE3D_ID_GRADIENTTEXTUREPRIMITIVE3D
:
484 // GradientTexturePrimitive3D
485 const primitive3d::GradientTexturePrimitive3D
& rPrimitive
= static_cast< const primitive3d::GradientTexturePrimitive3D
& >(rBasePrimitive
);
486 impRenderGradientTexturePrimitive3D(rPrimitive
, false);
489 case PRIMITIVE3D_ID_HATCHTEXTUREPRIMITIVE3D
:
491 // hatchTexturePrimitive3D
492 const primitive3d::HatchTexturePrimitive3D
& rPrimitive
= static_cast< const primitive3d::HatchTexturePrimitive3D
& >(rBasePrimitive
);
493 impRenderHatchTexturePrimitive3D(rPrimitive
);
496 case PRIMITIVE3D_ID_BITMAPTEXTUREPRIMITIVE3D
:
498 // BitmapTexturePrimitive3D
499 const primitive3d::BitmapTexturePrimitive3D
& rPrimitive
= static_cast< const primitive3d::BitmapTexturePrimitive3D
& >(rBasePrimitive
);
500 impRenderBitmapTexturePrimitive3D(rPrimitive
);
503 case PRIMITIVE3D_ID_TRANSPARENCETEXTUREPRIMITIVE3D
:
505 // TransparenceTexturePrimitive3D
506 const primitive3d::TransparenceTexturePrimitive3D
& rPrimitive
= static_cast< const primitive3d::TransparenceTexturePrimitive3D
& >(rBasePrimitive
);
507 mnTransparenceCounter
++;
508 impRenderGradientTexturePrimitive3D(rPrimitive
, true);
509 mnTransparenceCounter
--;
512 case PRIMITIVE3D_ID_MODIFIEDCOLORPRIMITIVE3D
:
514 // ModifiedColorPrimitive3D
515 // Force output to unified color.
516 const primitive3d::ModifiedColorPrimitive3D
& rPrimitive
= static_cast< const primitive3d::ModifiedColorPrimitive3D
& >(rBasePrimitive
);
517 impRenderModifiedColorPrimitive3D(rPrimitive
);
520 case PRIMITIVE3D_ID_POLYGONHAIRLINEPRIMITIVE3D
:
522 // directdraw of PolygonHairlinePrimitive3D
523 const primitive3d::PolygonHairlinePrimitive3D
& rPrimitive
= static_cast< const primitive3d::PolygonHairlinePrimitive3D
& >(rBasePrimitive
);
524 impRenderPolygonHairlinePrimitive3D(rPrimitive
);
527 case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D
:
529 // directdraw of PolyPolygonMaterialPrimitive3D
530 const primitive3d::PolyPolygonMaterialPrimitive3D
& rPrimitive
= static_cast< const primitive3d::PolyPolygonMaterialPrimitive3D
& >(rBasePrimitive
);
531 impRenderPolyPolygonMaterialPrimitive3D(rPrimitive
);
534 case PRIMITIVE3D_ID_TRANSFORMPRIMITIVE3D
:
536 // transform group (TransformPrimitive3D)
537 impRenderTransformPrimitive3D(static_cast< const primitive3d::TransformPrimitive3D
& >(rBasePrimitive
));
542 // process recursively
543 process(rBasePrimitive
.get3DDecomposition(getViewInformation3D()));
549 DefaultProcessor3D::DefaultProcessor3D(
550 const geometry::ViewInformation3D
& rViewInformation
,
551 const attribute::SdrSceneAttribute
& rSdrSceneAttribute
,
552 const attribute::SdrLightingAttribute
& rSdrLightingAttribute
)
553 : BaseProcessor3D(rViewInformation
),
554 mrSdrSceneAttribute(rSdrSceneAttribute
),
555 mrSdrLightingAttribute(rSdrLightingAttribute
),
556 maBColorModifierStack(),
557 mnTransparenceCounter(0),
560 mbSimpleTextureActive(false)
562 // a derivation has to set maRasterRange which is used in the basic render methods.
563 // Setting to default here ([0.0 .. 1.0] in X,Y) to avoid problems
564 maRasterRange
.expand(basegfx::B2DTuple(0.0, 0.0));
565 maRasterRange
.expand(basegfx::B2DTuple(1.0, 1.0));
568 DefaultProcessor3D::~DefaultProcessor3D()
572 } // end of namespace
574 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */