update dev300-m58
[ooovba.git] / drawinglayer / source / processor3d / defaultprocessor3d.cxx
blob33a2e1639f5a058f6daee673f0bc8f2fd397307e
1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
5 * $RCSfile: defaultprocessor3d.cxx,v $
7 * $Revision: 1.13 $
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/defaultprocessor3d.hxx>
40 #include <drawinglayer/primitive3d/textureprimitive3d.hxx>
41 #include <drawinglayer/texture/texture.hxx>
42 #include <drawinglayer/texture/texture3d.hxx>
43 #include <drawinglayer/primitive3d/hatchtextureprimitive3d.hxx>
44 #include <drawinglayer/primitive3d/modifiedcolorprimitive3d.hxx>
45 #include <drawinglayer/primitive3d/polygonprimitive3d.hxx>
46 #include <basegfx/polygon/b3dpolygontools.hxx>
47 #include <drawinglayer/attribute/materialattribute3d.hxx>
48 #include <drawinglayer/primitive3d/polypolygonprimitive3d.hxx>
49 #include <basegfx/polygon/b3dpolypolygontools.hxx>
50 #include <com/sun/star/drawing/ShadeMode.hpp>
51 #include <drawinglayer/attribute/sdrattribute3d.hxx>
52 #include <drawinglayer/primitive3d/transformprimitive3d.hxx>
53 #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
55 //////////////////////////////////////////////////////////////////////////////
57 using namespace com::sun::star;
59 //////////////////////////////////////////////////////////////////////////////
61 namespace drawinglayer
63 namespace processor3d
65 void DefaultProcessor3D::impRenderGradientTexturePrimitive3D(const primitive3d::GradientTexturePrimitive3D& rPrimitive, bool bTransparence)
67 const primitive3d::Primitive3DSequence& rSubSequence = rPrimitive.getChildren();
69 if(rSubSequence.hasElements())
71 // rescue values
72 const bool bOldModulate(getModulate()); mbModulate = rPrimitive.getModulate();
73 const bool bOldFilter(getFilter()); mbFilter = rPrimitive.getFilter();
74 const bool bOldSimpleTextureActive(mbSimpleTextureActive);
75 texture::GeoTexSvx* pOldTex = (bTransparence) ? mpTransparenceGeoTexSvx : mpGeoTexSvx;
77 // create texture
78 const attribute::FillGradientAttribute& rFillGradient = rPrimitive.getGradient();
79 const basegfx::B2DRange aOutlineRange(0.0, 0.0, rPrimitive.getTextureSize().getX(), rPrimitive.getTextureSize().getY());
80 const attribute::GradientStyle aGradientStyle(rFillGradient.getStyle());
81 sal_uInt32 nSteps(rFillGradient.getSteps());
82 const basegfx::BColor aStart(rFillGradient.getStartColor());
83 const basegfx::BColor aEnd(rFillGradient.getEndColor());
84 const sal_uInt32 nMaxSteps(sal_uInt32((aStart.getMaximumDistance(aEnd) * 127.5) + 0.5));
85 texture::GeoTexSvx* pNewTex = 0L;
87 if(nMaxSteps)
89 // there IS a color distance
90 if(nSteps == 0L)
92 nSteps = nMaxSteps;
95 if(nSteps < 2L)
97 nSteps = 2L;
100 if(nSteps > nMaxSteps)
102 nSteps = nMaxSteps;
105 switch(aGradientStyle)
107 case attribute::GRADIENTSTYLE_LINEAR:
109 pNewTex = new texture::GeoTexSvxGradientLinear(aOutlineRange, aStart, aEnd, nSteps, rFillGradient.getBorder(), -rFillGradient.getAngle());
110 break;
112 case attribute::GRADIENTSTYLE_AXIAL:
114 pNewTex = new texture::GeoTexSvxGradientAxial(aOutlineRange, aStart, aEnd, nSteps, rFillGradient.getBorder(), -rFillGradient.getAngle());
115 break;
117 case attribute::GRADIENTSTYLE_RADIAL:
119 pNewTex = new texture::GeoTexSvxGradientRadial(aOutlineRange, aStart, aEnd, nSteps, rFillGradient.getBorder(), rFillGradient.getOffsetX(), rFillGradient.getOffsetY());
120 break;
122 case attribute::GRADIENTSTYLE_ELLIPTICAL:
124 pNewTex = new texture::GeoTexSvxGradientElliptical(aOutlineRange, aStart, aEnd, nSteps, rFillGradient.getBorder(), rFillGradient.getOffsetX(), rFillGradient.getOffsetY(), -rFillGradient.getAngle());
125 break;
127 case attribute::GRADIENTSTYLE_SQUARE:
129 pNewTex = new texture::GeoTexSvxGradientSquare(aOutlineRange, aStart, aEnd, nSteps, rFillGradient.getBorder(), rFillGradient.getOffsetX(), rFillGradient.getOffsetY(), -rFillGradient.getAngle());
130 break;
132 case attribute::GRADIENTSTYLE_RECT:
134 pNewTex = new texture::GeoTexSvxGradientRect(aOutlineRange, aStart, aEnd, nSteps, rFillGradient.getBorder(), rFillGradient.getOffsetX(), rFillGradient.getOffsetY(), -rFillGradient.getAngle());
135 break;
139 mbSimpleTextureActive = false;
141 else
143 // no color distance -> same color, use simple texture
144 pNewTex = new texture::GeoTexSvxMono(aStart, 1.0 - aStart.luminance());
145 mbSimpleTextureActive = true;
148 // set created texture
149 if(bTransparence)
151 mpTransparenceGeoTexSvx = pNewTex;
153 else
155 mpGeoTexSvx = pNewTex;
158 // process sub-list
159 process(rSubSequence);
161 // delete texture
162 delete pNewTex;
164 // restore values
165 mbModulate = bOldModulate;
166 mbFilter = bOldFilter;
167 mbSimpleTextureActive = bOldSimpleTextureActive;
169 if(bTransparence)
171 mpTransparenceGeoTexSvx = pOldTex;
173 else
175 mpGeoTexSvx = pOldTex;
180 void DefaultProcessor3D::impRenderHatchTexturePrimitive3D(const primitive3d::HatchTexturePrimitive3D& rPrimitive)
182 const primitive3d::Primitive3DSequence& rSubSequence = rPrimitive.getChildren();
184 if(rSubSequence.hasElements())
186 // rescue values
187 const bool bOldModulate(getModulate()); mbModulate = rPrimitive.getModulate();
188 const bool bOldFilter(getFilter()); mbFilter = rPrimitive.getFilter();
189 texture::GeoTexSvx* pOldTex = 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(fLogicTexSizeX > fLogicTexSizeY ? fLogicTexSizeX : fLogicTexSizeY);
208 // create texture and set
209 texture::GeoTexSvxMultiHatch* pNewTex = new texture::GeoTexSvxMultiHatch(rPrimitive, fLogicTexSize);
210 mpGeoTexSvx = pNewTex;
212 // process sub-list
213 process(rSubSequence);
215 // delete texture
216 delete mpGeoTexSvx;
218 // restore values
219 mbModulate = bOldModulate;
220 mbFilter = bOldFilter;
221 mpGeoTexSvx = pOldTex;
225 void DefaultProcessor3D::impRenderBitmapTexturePrimitive3D(const primitive3d::BitmapTexturePrimitive3D& rPrimitive)
227 const primitive3d::Primitive3DSequence& rSubSequence = rPrimitive.getChildren();
229 if(rSubSequence.hasElements())
231 // rescue values
232 const bool bOldModulate(getModulate()); mbModulate = rPrimitive.getModulate();
233 const bool bOldFilter(getFilter()); mbFilter = rPrimitive.getFilter();
234 texture::GeoTexSvx* pOldTex = mpGeoTexSvx;
236 // create texture
237 const attribute::FillBitmapAttribute& rFillBitmapAttribute = rPrimitive.getBitmap();
239 if(rFillBitmapAttribute.getTiling())
241 mpGeoTexSvx = new texture::GeoTexSvxBitmapTiled(
242 rFillBitmapAttribute.getBitmap(),
243 rFillBitmapAttribute.getTopLeft() * rPrimitive.getTextureSize(),
244 rFillBitmapAttribute.getSize() * rPrimitive.getTextureSize());
246 else
248 mpGeoTexSvx = new texture::GeoTexSvxBitmap(
249 rFillBitmapAttribute.getBitmap(),
250 rFillBitmapAttribute.getTopLeft() * rPrimitive.getTextureSize(),
251 rFillBitmapAttribute.getSize() * rPrimitive.getTextureSize());
254 // process sub-list
255 process(rSubSequence);
257 // delete texture
258 delete mpGeoTexSvx;
260 // restore values
261 mbModulate = bOldModulate;
262 mbFilter = bOldFilter;
263 mpGeoTexSvx = pOldTex;
267 void DefaultProcessor3D::impRenderModifiedColorPrimitive3D(const primitive3d::ModifiedColorPrimitive3D& rModifiedCandidate)
269 const primitive3d::Primitive3DSequence& rSubSequence = rModifiedCandidate.getChildren();
271 if(rSubSequence.hasElements())
273 // put modifier on stack
274 maBColorModifierStack.push(rModifiedCandidate.getColorModifier());
276 // process sub-list
277 process(rModifiedCandidate.getChildren());
279 // remove modifier from stack
280 maBColorModifierStack.pop();
284 void DefaultProcessor3D::impRenderPolygonHairlinePrimitive3D(const primitive3d::PolygonHairlinePrimitive3D& rPrimitive)
286 basegfx::B3DPolygon aHairline(rPrimitive.getB3DPolygon());
288 if(aHairline.count())
290 // hairlines need no extra data, clear it
291 aHairline.clearTextureCoordinates();
292 aHairline.clearNormals();
293 aHairline.clearBColors();
295 // transform to device coordinates (-1.0 .. 1.0) and check for visibility
296 aHairline.transform(getViewInformation3D().getObjectToView());
297 const basegfx::B3DRange a3DRange(basegfx::tools::getRange(aHairline));
298 const basegfx::B2DRange a2DRange(a3DRange.getMinX(), a3DRange.getMinY(), a3DRange.getMaxX(), a3DRange.getMaxY());
300 if(a2DRange.overlaps(maRasterRange))
302 const attribute::MaterialAttribute3D aMaterial(maBColorModifierStack.getModifiedColor(rPrimitive.getBColor()));
304 rasterconvertB3DPolygon(aMaterial, aHairline);
309 void DefaultProcessor3D::impRenderPolyPolygonMaterialPrimitive3D(const primitive3d::PolyPolygonMaterialPrimitive3D& rPrimitive)
311 basegfx::B3DPolyPolygon aFill(rPrimitive.getB3DPolyPolygon());
312 basegfx::BColor aObjectColor(rPrimitive.getMaterial().getColor());
313 bool bPaintIt(aFill.count());
315 // #i98295# get ShadeMode. Correct early when only flat is possible due to missing normals
316 const ::com::sun::star::drawing::ShadeMode aShadeMode(
317 aFill.areNormalsUsed() ?
318 getSdrSceneAttribute().getShadeMode() : ::com::sun::star::drawing::ShadeMode_FLAT);
320 if(bPaintIt)
322 // get rid of texture coordinates if there is no texture
323 if(aFill.areTextureCoordinatesUsed() && !getGeoTexSvx() && !getTransparenceGeoTexSvx())
325 aFill.clearTextureCoordinates();
328 // #i98295# get rid of normals and color early when not needed
329 if(::com::sun::star::drawing::ShadeMode_FLAT == aShadeMode)
331 aFill.clearNormals();
332 aFill.clearBColors();
335 // transform to device coordinates (-1.0 .. 1.0) and check for visibility
336 aFill.transform(getViewInformation3D().getObjectToView());
337 const basegfx::B3DRange a3DRange(basegfx::tools::getRange(aFill));
338 const basegfx::B2DRange a2DRange(a3DRange.getMinX(), a3DRange.getMinY(), a3DRange.getMaxX(), a3DRange.getMaxY());
340 bPaintIt = a2DRange.overlaps(maRasterRange);
343 // check if it shall be painted regarding hiding of normals (backface culling)
344 if(bPaintIt && !rPrimitive.getDoubleSided())
346 // get plane normal of polygon in view coordinates (with ZBuffer values),
347 // left-handed coordinate system
348 const basegfx::B3DVector aPlaneNormal(aFill.getB3DPolygon(0L).getNormal());
350 if(aPlaneNormal.getZ() > 0.0)
352 bPaintIt = false;
356 if(bPaintIt)
358 // prepare ObjectToEye in NormalTransform
359 basegfx::B3DHomMatrix aNormalTransform(getViewInformation3D().getOrientation() * getViewInformation3D().getObjectTransformation());
361 if(getSdrSceneAttribute().getTwoSidedLighting())
363 // get plane normal of polygon in view coordinates (with ZBuffer values),
364 // left-handed coordinate system
365 const basegfx::B3DVector aPlaneNormal(aFill.getB3DPolygon(0L).getNormal());
367 if(aPlaneNormal.getZ() > 0.0)
369 // mirror normals
370 aNormalTransform.scale(-1.0, -1.0, -1.0);
374 switch(aShadeMode)
376 case ::com::sun::star::drawing::ShadeMode_PHONG:
378 // phong shading. Transform normals to eye coor
379 aFill.transformNormals(aNormalTransform);
380 break;
382 case ::com::sun::star::drawing::ShadeMode_SMOOTH:
384 // gouraud shading. Transform normals to eye coor
385 aFill.transformNormals(aNormalTransform);
387 // prepare color model parameters, evtl. use blend color
388 const basegfx::BColor aColor(getModulate() ? basegfx::BColor(1.0, 1.0, 1.0) : rPrimitive.getMaterial().getColor());
389 const basegfx::BColor& rSpecular(rPrimitive.getMaterial().getSpecular());
390 const basegfx::BColor& rEmission(rPrimitive.getMaterial().getEmission());
391 const sal_uInt16 nSpecularIntensity(rPrimitive.getMaterial().getSpecularIntensity());
393 // solve color model for each normal vector, set colors at points. Clear normals.
394 for(sal_uInt32 a(0L); a < aFill.count(); a++)
396 basegfx::B3DPolygon aPartFill(aFill.getB3DPolygon(a));
398 for(sal_uInt32 b(0L); b < aPartFill.count(); b++)
400 // solve color model. Transform normal to eye coor
401 const basegfx::B3DVector aNormal(aPartFill.getNormal(b));
402 const basegfx::BColor aSolvedColor(getSdrLightingAttribute().solveColorModel(aNormal, aColor, rSpecular, rEmission, nSpecularIntensity));
403 aPartFill.setBColor(b, aSolvedColor);
406 // clear normals on this part polygon and write it back
407 aPartFill.clearNormals();
408 aFill.setB3DPolygon(a, aPartFill);
410 break;
412 case ::com::sun::star::drawing::ShadeMode_FLAT:
414 // flat shading. Get plane vector in eye coordinates
415 const basegfx::B3DVector aPlaneEyeNormal(aNormalTransform * rPrimitive.getB3DPolyPolygon().getB3DPolygon(0L).getNormal());
417 // prepare color model parameters, evtl. use blend color
418 const basegfx::BColor aColor(getModulate() ? basegfx::BColor(1.0, 1.0, 1.0) : rPrimitive.getMaterial().getColor());
419 const basegfx::BColor& rSpecular(rPrimitive.getMaterial().getSpecular());
420 const basegfx::BColor& rEmission(rPrimitive.getMaterial().getEmission());
421 const sal_uInt16 nSpecularIntensity(rPrimitive.getMaterial().getSpecularIntensity());
423 // solve color model for plane vector and use that color for whole plane
424 aObjectColor = getSdrLightingAttribute().solveColorModel(aPlaneEyeNormal, aColor, rSpecular, rEmission, nSpecularIntensity);
425 break;
427 default: // case ::com::sun::star::drawing::ShadeMode_DRAFT:
429 // draft, just use object color which is already set. Delete all other infos
430 aFill.clearNormals();
431 aFill.clearBColors();
432 break;
436 // draw it to ZBuffer
437 const attribute::MaterialAttribute3D aMaterial(
438 maBColorModifierStack.getModifiedColor(aObjectColor),
439 rPrimitive.getMaterial().getSpecular(),
440 rPrimitive.getMaterial().getEmission(),
441 rPrimitive.getMaterial().getSpecularIntensity());
443 rasterconvertB3DPolyPolygon(aMaterial, aFill);
447 void DefaultProcessor3D::impRenderTransformPrimitive3D(const primitive3d::TransformPrimitive3D& rTransformCandidate)
449 // transform group. Remember current transformations
450 const geometry::ViewInformation3D aLastViewInformation3D(getViewInformation3D());
452 // create new transformation; add new object transform from right side
453 const geometry::ViewInformation3D aNewViewInformation3D(
454 aLastViewInformation3D.getObjectTransformation() * rTransformCandidate.getTransformation(),
455 aLastViewInformation3D.getOrientation(),
456 aLastViewInformation3D.getProjection(),
457 aLastViewInformation3D.getDeviceToView(),
458 aLastViewInformation3D.getViewTime(),
459 aLastViewInformation3D.getExtendedInformationSequence());
460 updateViewInformation(aNewViewInformation3D);
462 // let break down recursively
463 process(rTransformCandidate.getChildren());
465 // restore transformations
466 updateViewInformation(aLastViewInformation3D);
469 void DefaultProcessor3D::processBasePrimitive3D(const primitive3d::BasePrimitive3D& rBasePrimitive)
471 // it is a BasePrimitive3D implementation, use getPrimitiveID() call for switch
472 switch(rBasePrimitive.getPrimitiveID())
474 case PRIMITIVE3D_ID_GRADIENTTEXTUREPRIMITIVE3D :
476 // GradientTexturePrimitive3D
477 const primitive3d::GradientTexturePrimitive3D& rPrimitive = static_cast< const primitive3d::GradientTexturePrimitive3D& >(rBasePrimitive);
478 impRenderGradientTexturePrimitive3D(rPrimitive, false);
479 break;
481 case PRIMITIVE3D_ID_HATCHTEXTUREPRIMITIVE3D :
483 // HatchTexturePrimitive3D
484 static bool bDoHatchDecomposition(false);
486 if(bDoHatchDecomposition)
488 // let break down
489 process(rBasePrimitive.get3DDecomposition(getViewInformation3D()));
491 else
493 // hatchTexturePrimitive3D
494 const primitive3d::HatchTexturePrimitive3D& rPrimitive = static_cast< const primitive3d::HatchTexturePrimitive3D& >(rBasePrimitive);
495 impRenderHatchTexturePrimitive3D(rPrimitive);
497 break;
499 case PRIMITIVE3D_ID_BITMAPTEXTUREPRIMITIVE3D :
501 // BitmapTexturePrimitive3D
502 const primitive3d::BitmapTexturePrimitive3D& rPrimitive = static_cast< const primitive3d::BitmapTexturePrimitive3D& >(rBasePrimitive);
503 impRenderBitmapTexturePrimitive3D(rPrimitive);
504 break;
506 case PRIMITIVE3D_ID_ALPHATEXTUREPRIMITIVE3D :
508 // AlphaTexturePrimitive3D
509 const primitive3d::AlphaTexturePrimitive3D& rPrimitive = static_cast< const primitive3d::AlphaTexturePrimitive3D& >(rBasePrimitive);
510 impRenderGradientTexturePrimitive3D(rPrimitive, true);
511 break;
513 case PRIMITIVE3D_ID_MODIFIEDCOLORPRIMITIVE3D :
515 // ModifiedColorPrimitive3D
516 // Force output to unified color.
517 const primitive3d::ModifiedColorPrimitive3D& rPrimitive = static_cast< const primitive3d::ModifiedColorPrimitive3D& >(rBasePrimitive);
518 impRenderModifiedColorPrimitive3D(rPrimitive);
519 break;
521 case PRIMITIVE3D_ID_POLYGONHAIRLINEPRIMITIVE3D :
523 // directdraw of PolygonHairlinePrimitive3D
524 const primitive3d::PolygonHairlinePrimitive3D& rPrimitive = static_cast< const primitive3d::PolygonHairlinePrimitive3D& >(rBasePrimitive);
525 impRenderPolygonHairlinePrimitive3D(rPrimitive);
526 break;
528 case PRIMITIVE3D_ID_POLYPOLYGONMATERIALPRIMITIVE3D :
530 // directdraw of PolyPolygonMaterialPrimitive3D
531 const primitive3d::PolyPolygonMaterialPrimitive3D& rPrimitive = static_cast< const primitive3d::PolyPolygonMaterialPrimitive3D& >(rBasePrimitive);
532 impRenderPolyPolygonMaterialPrimitive3D(rPrimitive);
533 break;
535 case PRIMITIVE3D_ID_TRANSFORMPRIMITIVE3D :
537 // transform group (TransformPrimitive3D)
538 impRenderTransformPrimitive3D(static_cast< const primitive3d::TransformPrimitive3D& >(rBasePrimitive));
539 break;
541 default:
543 // process recursively
544 process(rBasePrimitive.get3DDecomposition(getViewInformation3D()));
545 break;
550 DefaultProcessor3D::DefaultProcessor3D(
551 const geometry::ViewInformation3D& rViewInformation,
552 const attribute::SdrSceneAttribute& rSdrSceneAttribute,
553 const attribute::SdrLightingAttribute& rSdrLightingAttribute)
554 : BaseProcessor3D(rViewInformation),
555 mrSdrSceneAttribute(rSdrSceneAttribute),
556 mrSdrLightingAttribute(rSdrLightingAttribute),
557 maRasterRange(),
558 maBColorModifierStack(),
559 mpGeoTexSvx(0),
560 mpTransparenceGeoTexSvx(0),
561 mbModulate(false),
562 mbFilter(false),
563 mbSimpleTextureActive(false)
565 // a derivation has to set maRasterRange which is used in the basic render methods.
566 // Setting to default here ([0.0 .. 1.0] in X,Y) to avoid problems
567 maRasterRange.expand(basegfx::B2DTuple(0.0, 0.0));
568 maRasterRange.expand(basegfx::B2DTuple(1.0, 1.0));
571 DefaultProcessor3D::~DefaultProcessor3D()
574 } // end of namespace processor3d
575 } // end of namespace drawinglayer
577 //////////////////////////////////////////////////////////////////////////////
578 // eof