LCOV - code coverage report
Current view: top level - libreoffice/drawinglayer/source/processor3d - zbufferprocessor3d.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 318 0.0 %
Date: 2012-12-27 Functions: 0 30 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include <drawinglayer/processor3d/zbufferprocessor3d.hxx>
      21             : #include <basegfx/raster/bpixelraster.hxx>
      22             : #include <vcl/bmpacc.hxx>
      23             : #include <basegfx/raster/rasterconvert3d.hxx>
      24             : #include <basegfx/raster/bzpixelraster.hxx>
      25             : #include <drawinglayer/attribute/materialattribute3d.hxx>
      26             : #include <drawinglayer/texture/texture.hxx>
      27             : #include <drawinglayer/primitive3d/drawinglayer_primitivetypes3d.hxx>
      28             : #include <drawinglayer/primitive3d/textureprimitive3d.hxx>
      29             : #include <drawinglayer/primitive3d/polygonprimitive3d.hxx>
      30             : #include <drawinglayer/primitive3d/polypolygonprimitive3d.hxx>
      31             : #include <drawinglayer/geometry/viewinformation2d.hxx>
      32             : #include <basegfx/polygon/b3dpolygontools.hxx>
      33             : #include <basegfx/polygon/b3dpolypolygontools.hxx>
      34             : #include <drawinglayer/attribute/sdrlightingattribute3d.hxx>
      35             : 
      36             : //////////////////////////////////////////////////////////////////////////////
      37             : 
      38             : using namespace com::sun::star;
      39             : 
      40             : //////////////////////////////////////////////////////////////////////////////
      41             : 
      42             : namespace
      43             : {
      44           0 :     BitmapEx BPixelRasterToBitmapEx(const basegfx::BPixelRaster& rRaster, sal_uInt16 mnAntiAlialize)
      45             :     {
      46           0 :         BitmapEx aRetval;
      47           0 :         const sal_uInt32 nWidth(mnAntiAlialize ? rRaster.getWidth()/mnAntiAlialize : rRaster.getWidth());
      48           0 :         const sal_uInt32 nHeight(mnAntiAlialize ? rRaster.getHeight()/mnAntiAlialize : rRaster.getHeight());
      49             : 
      50           0 :         if(nWidth && nHeight)
      51             :         {
      52           0 :             const Size aDestSize(nWidth, nHeight);
      53           0 :             sal_uInt8 nInitAlpha(255);
      54           0 :             Bitmap aContent(aDestSize, 24);
      55           0 :             AlphaMask aAlpha(aDestSize, &nInitAlpha);
      56           0 :             BitmapWriteAccess* pContent = aContent.AcquireWriteAccess();
      57           0 :             BitmapWriteAccess* pAlpha = aAlpha.AcquireWriteAccess();
      58             : 
      59           0 :             if(pContent && pAlpha)
      60             :             {
      61           0 :                 if(mnAntiAlialize)
      62             :                 {
      63           0 :                     const sal_uInt16 nDivisor(mnAntiAlialize * mnAntiAlialize);
      64             : 
      65           0 :                     for(sal_uInt32 y(0L); y < nHeight; y++)
      66             :                     {
      67           0 :                         for(sal_uInt32 x(0L); x < nWidth; x++)
      68             :                         {
      69           0 :                             sal_uInt16 nRed(0);
      70           0 :                             sal_uInt16 nGreen(0);
      71           0 :                             sal_uInt16 nBlue(0);
      72           0 :                             sal_uInt16 nOpacity(0);
      73           0 :                             sal_uInt32 nIndex(rRaster.getIndexFromXY(x * mnAntiAlialize, y * mnAntiAlialize));
      74             : 
      75           0 :                             for(sal_uInt32 c(0); c < mnAntiAlialize; c++)
      76             :                             {
      77           0 :                                 for(sal_uInt32 d(0); d < mnAntiAlialize; d++)
      78             :                                 {
      79           0 :                                     const basegfx::BPixel& rPixel(rRaster.getBPixel(nIndex++));
      80           0 :                                     nRed = nRed + rPixel.getRed();
      81           0 :                                     nGreen = nGreen + rPixel.getGreen();
      82           0 :                                     nBlue = nBlue + rPixel.getBlue();
      83           0 :                                     nOpacity = nOpacity + rPixel.getOpacity();
      84             :                                 }
      85             : 
      86           0 :                                 nIndex += rRaster.getWidth() - mnAntiAlialize;
      87             :                             }
      88             : 
      89           0 :                             nOpacity = nOpacity / nDivisor;
      90             : 
      91           0 :                             if(nOpacity)
      92             :                             {
      93             :                                 pContent->SetPixel(y, x, BitmapColor(
      94             :                                     (sal_uInt8)(nRed / nDivisor),
      95             :                                     (sal_uInt8)(nGreen / nDivisor),
      96           0 :                                     (sal_uInt8)(nBlue / nDivisor)));
      97           0 :                                 pAlpha->SetPixel(y, x, BitmapColor(255 - (sal_uInt8)nOpacity));
      98             :                             }
      99             :                         }
     100             :                     }
     101             :                 }
     102             :                 else
     103             :                 {
     104           0 :                     sal_uInt32 nIndex(0L);
     105             : 
     106           0 :                     for(sal_uInt32 y(0L); y < nHeight; y++)
     107             :                     {
     108           0 :                         for(sal_uInt32 x(0L); x < nWidth; x++)
     109             :                         {
     110           0 :                             const basegfx::BPixel& rPixel(rRaster.getBPixel(nIndex++));
     111             : 
     112           0 :                             if(rPixel.getOpacity())
     113             :                             {
     114           0 :                                 pContent->SetPixel(y, x, BitmapColor(rPixel.getRed(), rPixel.getGreen(), rPixel.getBlue()));
     115           0 :                                 pAlpha->SetPixel(y, x, BitmapColor(255 - rPixel.getOpacity()));
     116             :                             }
     117             :                         }
     118             :                     }
     119             :                 }
     120             : 
     121           0 :                 aContent.ReleaseAccess(pContent);
     122           0 :                 aAlpha.ReleaseAccess(pAlpha);
     123             :             }
     124             : 
     125           0 :             aRetval = BitmapEx(aContent, aAlpha);
     126             : 
     127             :             // #i101811# set PrefMapMode and PrefSize at newly created Bitmap
     128           0 :             aRetval.SetPrefMapMode(MAP_PIXEL);
     129           0 :             aRetval.SetPrefSize(Size(nWidth, nHeight));
     130             :         }
     131             : 
     132           0 :         return aRetval;
     133             :     }
     134             : } // end of anonymous namespace
     135             : 
     136             : //////////////////////////////////////////////////////////////////////////////
     137             : 
     138           0 : class ZBufferRasterConverter3D : public basegfx::RasterConverter3D
     139             : {
     140             : private:
     141             :     const drawinglayer::processor3d::DefaultProcessor3D&    mrProcessor;
     142             :     basegfx::BZPixelRaster&                                 mrBuffer;
     143             : 
     144             :     // interpolators for a single line span
     145             :     basegfx::ip_single                                      maIntZ;
     146             :     basegfx::ip_triple                                      maIntColor;
     147             :     basegfx::ip_triple                                      maIntNormal;
     148             :     basegfx::ip_double                                      maIntTexture;
     149             :     basegfx::ip_triple                                      maIntInvTexture;
     150             : 
     151             :     // current material to use for ratsreconversion
     152             :     const drawinglayer::attribute::MaterialAttribute3D*     mpCurrentMaterial;
     153             : 
     154             :     // bitfield
     155             :     // some boolean flags for line span interpolator usages
     156             :     unsigned                                                mbModifyColor : 1;
     157             :     unsigned                                                mbUseTex : 1;
     158             :     unsigned                                                mbHasTexCoor : 1;
     159             :     unsigned                                                mbHasInvTexCoor : 1;
     160             :     unsigned                                                mbUseNrm : 1;
     161             :     unsigned                                                mbUseCol : 1;
     162             : 
     163           0 :     void getTextureCoor(basegfx::B2DPoint& rTarget) const
     164             :     {
     165           0 :         if(mbHasTexCoor)
     166             :         {
     167           0 :             rTarget.setX(maIntTexture.getX().getVal());
     168           0 :             rTarget.setY(maIntTexture.getY().getVal());
     169             :         }
     170           0 :         else if(mbHasInvTexCoor)
     171             :         {
     172           0 :             const double fZFactor(maIntInvTexture.getZ().getVal());
     173           0 :             const double fInvZFactor(basegfx::fTools::equalZero(fZFactor) ? 1.0 : 1.0 / fZFactor);
     174           0 :             rTarget.setX(maIntInvTexture.getX().getVal() * fInvZFactor);
     175           0 :             rTarget.setY(maIntInvTexture.getY().getVal() * fInvZFactor);
     176             :         }
     177           0 :     }
     178             : 
     179           0 :     void incrementLineSpanInterpolators(double fStep)
     180             :     {
     181           0 :         maIntZ.increment(fStep);
     182             : 
     183           0 :         if(mbUseTex)
     184             :         {
     185           0 :             if(mbHasTexCoor)
     186             :             {
     187           0 :                 maIntTexture.increment(fStep);
     188             :             }
     189           0 :             else if(mbHasInvTexCoor)
     190             :             {
     191           0 :                 maIntInvTexture.increment(fStep);
     192             :             }
     193             :         }
     194             : 
     195           0 :         if(mbUseNrm)
     196             :         {
     197           0 :             maIntNormal.increment(fStep);
     198             :         }
     199             : 
     200           0 :         if(mbUseCol)
     201             :         {
     202           0 :             maIntColor.increment(fStep);
     203             :         }
     204           0 :     }
     205             : 
     206           0 :     double decideColorAndOpacity(basegfx::BColor& rColor)
     207             :     {
     208             :         // init values with full opacity and material color
     209             :         OSL_ENSURE(0 != mpCurrentMaterial, "CurrentMaterial not set (!)");
     210           0 :         double fOpacity(1.0);
     211           0 :         rColor = mpCurrentMaterial->getColor();
     212             : 
     213           0 :         if(mbUseTex)
     214             :         {
     215           0 :             basegfx::B2DPoint aTexCoor(0.0, 0.0);
     216           0 :             getTextureCoor(aTexCoor);
     217             : 
     218           0 :             if(mrProcessor.getGeoTexSvx().get())
     219             :             {
     220             :                 // calc color in spot. This may also set to invisible already when
     221             :                 // e.g. bitmap textures have transparent parts
     222           0 :                 mrProcessor.getGeoTexSvx()->modifyBColor(aTexCoor, rColor, fOpacity);
     223             :             }
     224             : 
     225           0 :             if(basegfx::fTools::more(fOpacity, 0.0) && mrProcessor.getTransparenceGeoTexSvx().get())
     226             :             {
     227             :                 // calc opacity. Object has a 2nd texture, a transparence texture
     228           0 :                 mrProcessor.getTransparenceGeoTexSvx()->modifyOpacity(aTexCoor, fOpacity);
     229           0 :             }
     230             :         }
     231             : 
     232           0 :         if(basegfx::fTools::more(fOpacity, 0.0))
     233             :         {
     234           0 :             if(mrProcessor.getGeoTexSvx().get())
     235             :             {
     236           0 :                 if(mbUseNrm)
     237             :                 {
     238             :                     // blend texture with phong
     239           0 :                     rColor = mrProcessor.getSdrLightingAttribute().solveColorModel(
     240           0 :                         basegfx::B3DVector(maIntNormal.getX().getVal(), maIntNormal.getY().getVal(), maIntNormal.getZ().getVal()),
     241             :                         rColor,
     242           0 :                         mpCurrentMaterial->getSpecular(),
     243           0 :                         mpCurrentMaterial->getEmission(),
     244           0 :                         mpCurrentMaterial->getSpecularIntensity());
     245             :                 }
     246           0 :                 else if(mbUseCol)
     247             :                 {
     248             :                     // blend texture with gouraud
     249           0 :                     basegfx::BColor aBlendColor(maIntColor.getX().getVal(), maIntColor.getY().getVal(), maIntColor.getZ().getVal());
     250           0 :                     rColor *= aBlendColor;
     251             :                 }
     252           0 :                 else if(mrProcessor.getModulate())
     253             :                 {
     254             :                     // blend texture with single material color
     255           0 :                     rColor *= mpCurrentMaterial->getColor();
     256             :                 }
     257             :             }
     258             :             else
     259             :             {
     260           0 :                 if(mbUseNrm)
     261             :                 {
     262             :                     // modify color with phong
     263           0 :                     rColor = mrProcessor.getSdrLightingAttribute().solveColorModel(
     264           0 :                         basegfx::B3DVector(maIntNormal.getX().getVal(), maIntNormal.getY().getVal(), maIntNormal.getZ().getVal()),
     265             :                         rColor,
     266           0 :                         mpCurrentMaterial->getSpecular(),
     267           0 :                         mpCurrentMaterial->getEmission(),
     268           0 :                         mpCurrentMaterial->getSpecularIntensity());
     269             :                 }
     270           0 :                 else if(mbUseCol)
     271             :                 {
     272             :                     // modify color with gouraud
     273           0 :                     rColor.setRed(maIntColor.getX().getVal());
     274           0 :                     rColor.setGreen(maIntColor.getY().getVal());
     275           0 :                     rColor.setBlue(maIntColor.getZ().getVal());
     276             :                 }
     277             :             }
     278             : 
     279           0 :             if(mbModifyColor)
     280             :             {
     281           0 :                 rColor = mrProcessor.getBColorModifierStack().getModifiedColor(rColor);
     282             :             }
     283             :         }
     284             : 
     285           0 :         return fOpacity;
     286             :     }
     287             : 
     288           0 :     void setupLineSpanInterpolators(const basegfx::RasterConversionLineEntry3D& rA, const basegfx::RasterConversionLineEntry3D& rB)
     289             :     {
     290             :         // get inverse XDelta
     291           0 :         const double xInvDelta(1.0 / (rB.getX().getVal() - rA.getX().getVal()));
     292             : 
     293             :         // prepare Z-interpolator
     294           0 :         const double fZA(rA.getZ().getVal());
     295           0 :         const double fZB(rB.getZ().getVal());
     296           0 :         maIntZ = basegfx::ip_single(fZA, (fZB - fZA) * xInvDelta);
     297             : 
     298             :         // get bools and init other interpolators on demand accordingly
     299           0 :         mbModifyColor = mrProcessor.getBColorModifierStack().count();
     300           0 :         mbHasTexCoor = SCANLINE_EMPTY_INDEX != rA.getTextureIndex() && SCANLINE_EMPTY_INDEX != rB.getTextureIndex();
     301           0 :         mbHasInvTexCoor = SCANLINE_EMPTY_INDEX != rA.getInverseTextureIndex() && SCANLINE_EMPTY_INDEX != rB.getInverseTextureIndex();
     302           0 :         const bool bTextureActive(mrProcessor.getGeoTexSvx().get() || mrProcessor.getTransparenceGeoTexSvx().get());
     303           0 :         mbUseTex = bTextureActive && (mbHasTexCoor || mbHasInvTexCoor || mrProcessor.getSimpleTextureActive());
     304           0 :         const bool bUseColorTex(mbUseTex && mrProcessor.getGeoTexSvx().get());
     305           0 :         const bool bNeedNrmOrCol(!bUseColorTex || (bUseColorTex && mrProcessor.getModulate()));
     306           0 :         mbUseNrm = bNeedNrmOrCol && SCANLINE_EMPTY_INDEX != rA.getNormalIndex() && SCANLINE_EMPTY_INDEX != rB.getNormalIndex();
     307           0 :         mbUseCol = !mbUseNrm && bNeedNrmOrCol && SCANLINE_EMPTY_INDEX != rA.getColorIndex() && SCANLINE_EMPTY_INDEX != rB.getColorIndex();
     308             : 
     309           0 :         if(mbUseTex)
     310             :         {
     311           0 :             if(mbHasTexCoor)
     312             :             {
     313           0 :                 const basegfx::ip_double& rTA(getTextureInterpolators()[rA.getTextureIndex()]);
     314           0 :                 const basegfx::ip_double& rTB(getTextureInterpolators()[rB.getTextureIndex()]);
     315             :                 maIntTexture = basegfx::ip_double(
     316           0 :                     rTA.getX().getVal(), (rTB.getX().getVal() - rTA.getX().getVal()) * xInvDelta,
     317           0 :                     rTA.getY().getVal(), (rTB.getY().getVal() - rTA.getY().getVal()) * xInvDelta);
     318             :             }
     319           0 :             else if(mbHasInvTexCoor)
     320             :             {
     321           0 :                 const basegfx::ip_triple& rITA(getInverseTextureInterpolators()[rA.getInverseTextureIndex()]);
     322           0 :                 const basegfx::ip_triple& rITB(getInverseTextureInterpolators()[rB.getInverseTextureIndex()]);
     323             :                 maIntInvTexture = basegfx::ip_triple(
     324           0 :                     rITA.getX().getVal(), (rITB.getX().getVal() - rITA.getX().getVal()) * xInvDelta,
     325           0 :                     rITA.getY().getVal(), (rITB.getY().getVal() - rITA.getY().getVal()) * xInvDelta,
     326           0 :                     rITA.getZ().getVal(), (rITB.getZ().getVal() - rITA.getZ().getVal()) * xInvDelta);
     327             :             }
     328             :         }
     329             : 
     330           0 :         if(mbUseNrm)
     331             :         {
     332           0 :             const basegfx::ip_triple& rNA(getNormalInterpolators()[rA.getNormalIndex()]);
     333           0 :             const basegfx::ip_triple& rNB(getNormalInterpolators()[rB.getNormalIndex()]);
     334             :             maIntNormal = basegfx::ip_triple(
     335           0 :                 rNA.getX().getVal(), (rNB.getX().getVal() - rNA.getX().getVal()) * xInvDelta,
     336           0 :                 rNA.getY().getVal(), (rNB.getY().getVal() - rNA.getY().getVal()) * xInvDelta,
     337           0 :                 rNA.getZ().getVal(), (rNB.getZ().getVal() - rNA.getZ().getVal()) * xInvDelta);
     338             :         }
     339             : 
     340           0 :         if(mbUseCol)
     341             :         {
     342           0 :             const basegfx::ip_triple& rCA(getColorInterpolators()[rA.getColorIndex()]);
     343           0 :             const basegfx::ip_triple& rCB(getColorInterpolators()[rB.getColorIndex()]);
     344             :             maIntColor = basegfx::ip_triple(
     345           0 :                 rCA.getX().getVal(), (rCB.getX().getVal() - rCA.getX().getVal()) * xInvDelta,
     346           0 :                 rCA.getY().getVal(), (rCB.getY().getVal() - rCA.getY().getVal()) * xInvDelta,
     347           0 :                 rCA.getZ().getVal(), (rCB.getZ().getVal() - rCA.getZ().getVal()) * xInvDelta);
     348             :         }
     349           0 :     }
     350             : 
     351             :     virtual void processLineSpan(const basegfx::RasterConversionLineEntry3D& rA, const basegfx::RasterConversionLineEntry3D& rB, sal_Int32 nLine, sal_uInt32 nSpanCount);
     352             : 
     353             : public:
     354           0 :     ZBufferRasterConverter3D(basegfx::BZPixelRaster& rBuffer, const drawinglayer::processor3d::ZBufferProcessor3D& rProcessor)
     355             :     :   basegfx::RasterConverter3D(),
     356             :         mrProcessor(rProcessor),
     357             :         mrBuffer(rBuffer),
     358             :         maIntZ(),
     359             :         maIntColor(),
     360             :         maIntNormal(),
     361             :         maIntTexture(),
     362             :         maIntInvTexture(),
     363             :         mpCurrentMaterial(0),
     364             :         mbModifyColor(false),
     365             :         mbUseTex(false),
     366             :         mbHasTexCoor(false),
     367             :         mbHasInvTexCoor(false),
     368             :         mbUseNrm(false),
     369           0 :         mbUseCol(false)
     370           0 :     {}
     371             : 
     372           0 :     void setCurrentMaterial(const drawinglayer::attribute::MaterialAttribute3D& rMaterial)
     373             :     {
     374           0 :         mpCurrentMaterial = &rMaterial;
     375           0 :     }
     376             : };
     377             : 
     378           0 : void ZBufferRasterConverter3D::processLineSpan(const basegfx::RasterConversionLineEntry3D& rA, const basegfx::RasterConversionLineEntry3D& rB, sal_Int32 nLine, sal_uInt32 nSpanCount)
     379             : {
     380           0 :     if(!(nSpanCount & 0x0001))
     381             :     {
     382           0 :         if(nLine >= 0 && nLine < (sal_Int32)mrBuffer.getHeight())
     383             :         {
     384           0 :             sal_uInt32 nXA(::std::min(mrBuffer.getWidth(), (sal_uInt32)::std::max((sal_Int32)0, basegfx::fround(rA.getX().getVal()))));
     385           0 :             const sal_uInt32 nXB(::std::min(mrBuffer.getWidth(), (sal_uInt32)::std::max((sal_Int32)0, basegfx::fround(rB.getX().getVal()))));
     386             : 
     387           0 :             if(nXA < nXB)
     388             :             {
     389             :                 // prepare the span interpolators
     390           0 :                 setupLineSpanInterpolators(rA, rB);
     391             : 
     392             :                 // bring span interpolators to start condition by incrementing with the possible difference of
     393             :                 // clamped and non-clamped XStart. Interpolators are setup relying on double precision
     394             :                 // X-values, so that difference is the correct value to compensate for possible clampings
     395           0 :                 incrementLineSpanInterpolators(static_cast<double>(nXA) - rA.getX().getVal());
     396             : 
     397             :                 // prepare scanline index
     398           0 :                 sal_uInt32 nScanlineIndex(mrBuffer.getIndexFromXY(nXA, static_cast<sal_uInt32>(nLine)));
     399           0 :                 basegfx::BColor aNewColor;
     400             : 
     401           0 :                 while(nXA < nXB)
     402             :                 {
     403             :                     // early-test Z values if we need to do anything at all
     404           0 :                     const double fNewZ(::std::max(0.0, ::std::min((double)0xffff, maIntZ.getVal())));
     405           0 :                     const sal_uInt16 nNewZ(static_cast< sal_uInt16 >(fNewZ));
     406           0 :                     sal_uInt16& rOldZ(mrBuffer.getZ(nScanlineIndex));
     407             : 
     408           0 :                     if(nNewZ > rOldZ)
     409             :                     {
     410             :                         // detect color and opacity for this pixel
     411           0 :                         const sal_uInt16 nOpacity(::std::max((sal_Int16)0, static_cast< sal_Int16 >(decideColorAndOpacity(aNewColor) * 255.0)));
     412             : 
     413           0 :                         if(nOpacity > 0)
     414             :                         {
     415             :                             // avoid color overrun
     416           0 :                             aNewColor.clamp();
     417             : 
     418           0 :                             if(nOpacity >= 0x00ff)
     419             :                             {
     420             :                                 // full opacity (not transparent), set z and color
     421           0 :                                 rOldZ = nNewZ;
     422           0 :                                 mrBuffer.getBPixel(nScanlineIndex) = basegfx::BPixel(aNewColor, 0xff);
     423             :                             }
     424             :                             else
     425             :                             {
     426           0 :                                 basegfx::BPixel& rDest = mrBuffer.getBPixel(nScanlineIndex);
     427             : 
     428           0 :                                 if(rDest.getOpacity())
     429             :                                 {
     430             :                                     // mix new color by using
     431             :                                     // color' = color * (1 - opacity) + newcolor * opacity
     432           0 :                                     const sal_uInt16 nTransparence(0x0100 - nOpacity);
     433           0 :                                     rDest.setRed((sal_uInt8)(((rDest.getRed() * nTransparence) + ((sal_uInt16)(255.0 * aNewColor.getRed()) * nOpacity)) >> 8));
     434           0 :                                     rDest.setGreen((sal_uInt8)(((rDest.getGreen() * nTransparence) + ((sal_uInt16)(255.0 * aNewColor.getGreen()) * nOpacity)) >> 8));
     435           0 :                                     rDest.setBlue((sal_uInt8)(((rDest.getBlue() * nTransparence) + ((sal_uInt16)(255.0 * aNewColor.getBlue()) * nOpacity)) >> 8));
     436             : 
     437           0 :                                     if(0xff != rDest.getOpacity())
     438             :                                     {
     439             :                                         // both are transparent, mix new opacity by using
     440             :                                         // opacity = newopacity * (1 - oldopacity) + oldopacity
     441           0 :                                         rDest.setOpacity(((sal_uInt8)((nOpacity * (0x0100 - rDest.getOpacity())) >> 8)) + rDest.getOpacity());
     442             :                                     }
     443             :                                 }
     444             :                                 else
     445             :                                 {
     446             :                                     // dest is unused, set color
     447           0 :                                     rDest = basegfx::BPixel(aNewColor, (sal_uInt8)nOpacity);
     448             :                                 }
     449             :                             }
     450             :                         }
     451             :                     }
     452             : 
     453             :                     // increments
     454           0 :                     nScanlineIndex++;
     455           0 :                     nXA++;
     456           0 :                     incrementLineSpanInterpolators(1.0);
     457           0 :                 }
     458             :             }
     459             :         }
     460             :     }
     461           0 : }
     462             : 
     463             : //////////////////////////////////////////////////////////////////////////////
     464             : // helper class to buffer output for transparent rasterprimitives (filled areas
     465             : // and lines) until the end of processing. To ensure correct transparent
     466             : // visualisation, ZBuffers require to not set Z and to mix with the transparent
     467             : // color. If transparent rasterprimitives overlap, it gets necessary to
     468             : // paint transparent rasterprimitives from back to front to ensure that the
     469             : // mixing happens from back to front. For that purpose, transparent
     470             : // rasterprimitives are held in this class during the processing run, remember
     471             : // all data and will be rendered
     472             : 
     473           0 : class RasterPrimitive3D
     474             : {
     475             : private:
     476             :     boost::shared_ptr< drawinglayer::texture::GeoTexSvx >     mpGeoTexSvx;
     477             :     boost::shared_ptr< drawinglayer::texture::GeoTexSvx >     mpTransparenceGeoTexSvx;
     478             :     drawinglayer::attribute::MaterialAttribute3D              maMaterial;
     479             :     basegfx::B3DPolyPolygon                                   maPolyPolygon;
     480             :     double                                                    mfCenterZ;
     481             : 
     482             :     // bitfield
     483             :     bool                                                      mbModulate : 1;
     484             :     bool                                                      mbFilter : 1;
     485             :     bool                                                      mbSimpleTextureActive : 1;
     486             :     bool                                                      mbIsLine : 1;
     487             : 
     488             : public:
     489           0 :     RasterPrimitive3D(
     490             :         const boost::shared_ptr< drawinglayer::texture::GeoTexSvx >& pGeoTexSvx,
     491             :         const boost::shared_ptr< drawinglayer::texture::GeoTexSvx >& pTransparenceGeoTexSvx,
     492             :         const drawinglayer::attribute::MaterialAttribute3D& rMaterial,
     493             :         const basegfx::B3DPolyPolygon& rPolyPolygon,
     494             :         bool bModulate,
     495             :         bool bFilter,
     496             :         bool bSimpleTextureActive,
     497             :         bool bIsLine)
     498             :     :   mpGeoTexSvx(pGeoTexSvx),
     499             :         mpTransparenceGeoTexSvx(pTransparenceGeoTexSvx),
     500             :         maMaterial(rMaterial),
     501             :         maPolyPolygon(rPolyPolygon),
     502           0 :         mfCenterZ(basegfx::tools::getRange(rPolyPolygon).getCenter().getZ()),
     503             :         mbModulate(bModulate),
     504             :         mbFilter(bFilter),
     505             :         mbSimpleTextureActive(bSimpleTextureActive),
     506           0 :         mbIsLine(bIsLine)
     507             :     {
     508           0 :     }
     509             : 
     510           0 :     RasterPrimitive3D& operator=(const RasterPrimitive3D& rComp)
     511             :     {
     512           0 :         mpGeoTexSvx = rComp.mpGeoTexSvx;
     513           0 :         mpTransparenceGeoTexSvx = rComp.mpTransparenceGeoTexSvx;
     514           0 :         maMaterial = rComp.maMaterial;
     515           0 :         maPolyPolygon = rComp.maPolyPolygon;
     516           0 :         mfCenterZ = rComp.mfCenterZ;
     517           0 :         mbModulate = rComp.mbModulate;
     518           0 :         mbFilter = rComp.mbFilter;
     519           0 :         mbSimpleTextureActive = rComp.mbSimpleTextureActive;
     520           0 :         mbIsLine = rComp.mbIsLine;
     521             : 
     522           0 :         return *this;
     523             :     }
     524             : 
     525           0 :     bool operator<(const RasterPrimitive3D& rComp) const
     526             :     {
     527           0 :         return mfCenterZ < rComp.mfCenterZ;
     528             :     }
     529             : 
     530           0 :     const boost::shared_ptr< drawinglayer::texture::GeoTexSvx >& getGeoTexSvx() const { return mpGeoTexSvx; }
     531           0 :     const boost::shared_ptr< drawinglayer::texture::GeoTexSvx >& getTransparenceGeoTexSvx() const { return mpTransparenceGeoTexSvx; }
     532           0 :     const drawinglayer::attribute::MaterialAttribute3D& getMaterial() const { return maMaterial; }
     533           0 :     const basegfx::B3DPolyPolygon& getPolyPolygon() const { return maPolyPolygon; }
     534           0 :     bool getModulate() const { return mbModulate; }
     535           0 :     bool getFilter() const { return mbFilter; }
     536           0 :     bool getSimpleTextureActive() const { return mbSimpleTextureActive; }
     537           0 :     bool getIsLine() const { return mbIsLine; }
     538             : };
     539             : 
     540             : //////////////////////////////////////////////////////////////////////////////
     541             : 
     542             : namespace drawinglayer
     543             : {
     544             :     namespace processor3d
     545             :     {
     546           0 :         void ZBufferProcessor3D::rasterconvertB3DPolygon(const attribute::MaterialAttribute3D& rMaterial, const basegfx::B3DPolygon& rHairline) const
     547             :         {
     548           0 :             if(mpBZPixelRaster)
     549             :             {
     550           0 :                 if(getTransparenceCounter())
     551             :                 {
     552             :                     // transparent output; record for later sorting and painting from
     553             :                     // back to front
     554           0 :                     if(!mpRasterPrimitive3Ds)
     555             :                     {
     556           0 :                         const_cast< ZBufferProcessor3D* >(this)->mpRasterPrimitive3Ds = new std::vector< RasterPrimitive3D >;
     557             :                     }
     558             : 
     559             :                     mpRasterPrimitive3Ds->push_back(RasterPrimitive3D(
     560           0 :                         getGeoTexSvx(),
     561           0 :                         getTransparenceGeoTexSvx(),
     562             :                         rMaterial,
     563             :                         basegfx::B3DPolyPolygon(rHairline),
     564           0 :                         getModulate(),
     565           0 :                         getFilter(),
     566           0 :                         getSimpleTextureActive(),
     567           0 :                         true));
     568             :                 }
     569             :                 else
     570             :                 {
     571             :                     // do rasterconversion
     572           0 :                     mpZBufferRasterConverter3D->setCurrentMaterial(rMaterial);
     573             : 
     574           0 :                     if(mnAntiAlialize > 1)
     575             :                     {
     576           0 :                         const bool bForceLineSnap(getOptionsDrawinglayer().IsAntiAliasing() && getOptionsDrawinglayer().IsSnapHorVerLinesToDiscrete());
     577             : 
     578           0 :                         if(bForceLineSnap)
     579             :                         {
     580           0 :                             basegfx::B3DHomMatrix aTransform;
     581           0 :                             basegfx::B3DPolygon aSnappedHairline(rHairline);
     582           0 :                             const double fScaleDown(1.0 / mnAntiAlialize);
     583           0 :                             const double fScaleUp(mnAntiAlialize);
     584             : 
     585             :                             // take oversampling out
     586           0 :                             aTransform.scale(fScaleDown, fScaleDown, 1.0);
     587           0 :                             aSnappedHairline.transform(aTransform);
     588             : 
     589             :                             // snap to integer
     590           0 :                             aSnappedHairline = basegfx::tools::snapPointsOfHorizontalOrVerticalEdges(aSnappedHairline);
     591             : 
     592             :                             // add oversampling again
     593           0 :                             aTransform.identity();
     594           0 :                             aTransform.scale(fScaleUp, fScaleUp, 1.0);
     595             : 
     596           0 :                             aSnappedHairline.transform(aTransform);
     597             : 
     598           0 :                             mpZBufferRasterConverter3D->rasterconvertB3DPolygon(aSnappedHairline, 0, mpBZPixelRaster->getHeight(), mnAntiAlialize);
     599             :                         }
     600             :                         else
     601             :                         {
     602           0 :                             mpZBufferRasterConverter3D->rasterconvertB3DPolygon(rHairline, 0, mpBZPixelRaster->getHeight(), mnAntiAlialize);
     603             :                         }
     604             :                     }
     605             :                     else
     606             :                     {
     607           0 :                         mpZBufferRasterConverter3D->rasterconvertB3DPolygon(rHairline, 0, mpBZPixelRaster->getHeight(), 1);
     608             :                     }
     609             :                 }
     610             :             }
     611           0 :         }
     612             : 
     613           0 :         void ZBufferProcessor3D::rasterconvertB3DPolyPolygon(const attribute::MaterialAttribute3D& rMaterial, const basegfx::B3DPolyPolygon& rFill) const
     614             :         {
     615           0 :             if(mpBZPixelRaster)
     616             :             {
     617           0 :                 if(getTransparenceCounter())
     618             :                 {
     619             :                     // transparent output; record for later sorting and painting from
     620             :                     // back to front
     621           0 :                     if(!mpRasterPrimitive3Ds)
     622             :                     {
     623           0 :                         const_cast< ZBufferProcessor3D* >(this)->mpRasterPrimitive3Ds = new std::vector< RasterPrimitive3D >;
     624             :                     }
     625             : 
     626             :                     mpRasterPrimitive3Ds->push_back(RasterPrimitive3D(
     627           0 :                         getGeoTexSvx(),
     628           0 :                         getTransparenceGeoTexSvx(),
     629             :                         rMaterial,
     630             :                         rFill,
     631           0 :                         getModulate(),
     632           0 :                         getFilter(),
     633           0 :                         getSimpleTextureActive(),
     634           0 :                         false));
     635             :                 }
     636             :                 else
     637             :                 {
     638           0 :                     mpZBufferRasterConverter3D->setCurrentMaterial(rMaterial);
     639           0 :                     mpZBufferRasterConverter3D->rasterconvertB3DPolyPolygon(rFill, &maInvEyeToView, 0, mpBZPixelRaster->getHeight());
     640             :                 }
     641             :             }
     642           0 :         }
     643             : 
     644           0 :         ZBufferProcessor3D::ZBufferProcessor3D(
     645             :             const geometry::ViewInformation3D& rViewInformation3D,
     646             :             const geometry::ViewInformation2D& rViewInformation2D,
     647             :             const attribute::SdrSceneAttribute& rSdrSceneAttribute,
     648             :             const attribute::SdrLightingAttribute& rSdrLightingAttribute,
     649             :             double fSizeX,
     650             :             double fSizeY,
     651             :             const basegfx::B2DRange& rVisiblePart,
     652             :             sal_uInt16 nAntiAlialize)
     653             :         :   DefaultProcessor3D(rViewInformation3D, rSdrSceneAttribute, rSdrLightingAttribute),
     654             :             mpBZPixelRaster(0),
     655             :             maInvEyeToView(),
     656             :             mpZBufferRasterConverter3D(0),
     657             :             mnAntiAlialize(nAntiAlialize),
     658           0 :             mpRasterPrimitive3Ds(0)
     659             :         {
     660             :             // generate ViewSizes
     661           0 :             const double fFullViewSizeX((rViewInformation2D.getObjectToViewTransformation() * basegfx::B2DVector(fSizeX, 0.0)).getLength());
     662           0 :             const double fFullViewSizeY((rViewInformation2D.getObjectToViewTransformation() * basegfx::B2DVector(0.0, fSizeY)).getLength());
     663           0 :             const double fViewSizeX(fFullViewSizeX * rVisiblePart.getWidth());
     664           0 :             const double fViewSizeY(fFullViewSizeY * rVisiblePart.getHeight());
     665             : 
     666             :             // generate RasterWidth and RasterHeight
     667           0 :             const sal_uInt32 nRasterWidth((sal_uInt32)basegfx::fround(fViewSizeX) + 1);
     668           0 :             const sal_uInt32 nRasterHeight((sal_uInt32)basegfx::fround(fViewSizeY) + 1);
     669             : 
     670           0 :             if(nRasterWidth && nRasterHeight)
     671             :             {
     672             :                 // create view unit buffer
     673             :                 mpBZPixelRaster = new basegfx::BZPixelRaster(
     674             :                     mnAntiAlialize ? nRasterWidth * mnAntiAlialize : nRasterWidth,
     675           0 :                     mnAntiAlialize ? nRasterHeight * mnAntiAlialize : nRasterHeight);
     676             :                 OSL_ENSURE(mpBZPixelRaster, "ZBufferProcessor3D: Could not allocate basegfx::BZPixelRaster (!)");
     677             : 
     678             :                 // create DeviceToView for Z-Buffer renderer since Z is handled
     679             :                 // different from standard 3D transformations (Z is mirrored). Also
     680             :                 // the transformation includes the step from unit device coordinates
     681             :                 // to discrete units ([-1.0 .. 1.0] -> [minDiscrete .. maxDiscrete]
     682             : 
     683           0 :                 basegfx::B3DHomMatrix aDeviceToView;
     684             : 
     685             :                 {
     686             :                     // step one:
     687             :                     //
     688             :                     // bring from [-1.0 .. 1.0] in X,Y and Z to [0.0 .. 1.0]. Also
     689             :                     // necessary to
     690             :                     // - flip Y due to screen orientation
     691             :                     // - flip Z due to Z-Buffer orientation from back to front
     692             : 
     693           0 :                     aDeviceToView.scale(0.5, -0.5, -0.5);
     694           0 :                     aDeviceToView.translate(0.5, 0.5, 0.5);
     695             :                 }
     696             : 
     697             :                 {
     698             :                     // step two:
     699             :                     //
     700             :                     // bring from [0.0 .. 1.0] in X,Y and Z to view cordinates
     701             :                     //
     702             :                     // #i102611#
     703             :                     // also: scale Z to [1.5 .. 65534.5]. Normally, a range of [0.0 .. 65535.0]
     704             :                     // could be used, but a 'unused' value is needed, so '0' is used what reduces
     705             :                     // the range to [1.0 .. 65535.0]. It has also shown that small numerical errors
     706             :                     // (smaller as basegfx::fTools::mfSmallValue, which is 0.000000001) happen.
     707             :                     // Instead of checking those by basegfx::fTools methods which would cost
     708             :                     // runtime, just add another 0.5 tolerance to the start and end of the Z-Buffer
     709             :                     // range, thus resulting in [1.5 .. 65534.5]
     710           0 :                     const double fMaxZDepth(65533.0);
     711           0 :                     aDeviceToView.translate(-rVisiblePart.getMinX(), -rVisiblePart.getMinY(), 0.0);
     712             : 
     713           0 :                     if(mnAntiAlialize)
     714           0 :                         aDeviceToView.scale(fFullViewSizeX * mnAntiAlialize, fFullViewSizeY * mnAntiAlialize, fMaxZDepth);
     715             :                     else
     716           0 :                         aDeviceToView.scale(fFullViewSizeX, fFullViewSizeY, fMaxZDepth);
     717             : 
     718           0 :                     aDeviceToView.translate(0.0, 0.0, 1.5);
     719             :                 }
     720             : 
     721             :                 // update local ViewInformation3D with own DeviceToView
     722             :                 const geometry::ViewInformation3D aNewViewInformation3D(
     723           0 :                     getViewInformation3D().getObjectTransformation(),
     724           0 :                     getViewInformation3D().getOrientation(),
     725           0 :                     getViewInformation3D().getProjection(),
     726             :                     aDeviceToView,
     727           0 :                     getViewInformation3D().getViewTime(),
     728           0 :                     getViewInformation3D().getExtendedInformationSequence());
     729           0 :                 updateViewInformation(aNewViewInformation3D);
     730             : 
     731             :                 // prepare inverse EyeToView transformation. This can be done in constructor
     732             :                 // since changes in object transformations when processing TransformPrimitive3Ds
     733             :                 // do not influence this prepared partial transformation
     734           0 :                 maInvEyeToView = getViewInformation3D().getDeviceToView() * getViewInformation3D().getProjection();
     735           0 :                 maInvEyeToView.invert();
     736             : 
     737             :                 // prepare maRasterRange
     738           0 :                 maRasterRange.reset();
     739           0 :                 maRasterRange.expand(basegfx::B2DPoint(0.0, 0.0));
     740           0 :                 maRasterRange.expand(basegfx::B2DPoint(mpBZPixelRaster->getWidth(), mpBZPixelRaster->getHeight()));
     741             : 
     742             :                 // create the raster converter
     743           0 :                 mpZBufferRasterConverter3D = new ZBufferRasterConverter3D(*mpBZPixelRaster, *this);
     744             :             }
     745           0 :         }
     746             : 
     747           0 :         ZBufferProcessor3D::~ZBufferProcessor3D()
     748             :         {
     749           0 :             if(mpBZPixelRaster)
     750             :             {
     751           0 :                 delete mpZBufferRasterConverter3D;
     752           0 :                 delete mpBZPixelRaster;
     753             :             }
     754             : 
     755           0 :             if(mpRasterPrimitive3Ds)
     756             :             {
     757             :                 OSL_FAIL("ZBufferProcessor3D: destructed, but there are unrendered transparent geometries. Use ZBufferProcessor3D::finish() to render these (!)");
     758           0 :                 delete mpRasterPrimitive3Ds;
     759             :             }
     760           0 :         }
     761             : 
     762           0 :         void ZBufferProcessor3D::finish()
     763             :         {
     764           0 :             if(mpRasterPrimitive3Ds)
     765             :             {
     766             :                 // there are transparent rasterprimitives
     767           0 :                 const sal_uInt32 nSize(mpRasterPrimitive3Ds->size());
     768             : 
     769           0 :                 if(nSize > 1)
     770             :                 {
     771             :                     // sort them from back to front
     772           0 :                     std::sort(mpRasterPrimitive3Ds->begin(), mpRasterPrimitive3Ds->end());
     773             :                 }
     774             : 
     775           0 :                 for(sal_uInt32 a(0); a < nSize; a++)
     776             :                 {
     777             :                     // paint each one by setting the remembered data and calling
     778             :                     // the render method
     779           0 :                     const RasterPrimitive3D& rCandidate = (*mpRasterPrimitive3Ds)[a];
     780             : 
     781           0 :                     mpGeoTexSvx = rCandidate.getGeoTexSvx();
     782           0 :                     mpTransparenceGeoTexSvx = rCandidate.getTransparenceGeoTexSvx();
     783           0 :                     mbModulate = rCandidate.getModulate();
     784           0 :                     mbFilter = rCandidate.getFilter();
     785           0 :                     mbSimpleTextureActive = rCandidate.getSimpleTextureActive();
     786             : 
     787           0 :                     if(rCandidate.getIsLine())
     788             :                     {
     789             :                         rasterconvertB3DPolygon(
     790           0 :                             rCandidate.getMaterial(),
     791           0 :                             rCandidate.getPolyPolygon().getB3DPolygon(0));
     792             :                     }
     793             :                     else
     794             :                     {
     795             :                         rasterconvertB3DPolyPolygon(
     796           0 :                             rCandidate.getMaterial(),
     797           0 :                             rCandidate.getPolyPolygon());
     798             :                     }
     799             :                 }
     800             : 
     801             :                 // delete them to signal the destructor that all is done and
     802             :                 // to allow asserting there
     803           0 :                 delete mpRasterPrimitive3Ds;
     804           0 :                 mpRasterPrimitive3Ds = 0;
     805             :             }
     806           0 :         }
     807             : 
     808           0 :         BitmapEx ZBufferProcessor3D::getBitmapEx() const
     809             :         {
     810           0 :             if(mpBZPixelRaster)
     811             :             {
     812           0 :                 return BPixelRasterToBitmapEx(*mpBZPixelRaster, mnAntiAlialize);
     813             :             }
     814             : 
     815           0 :             return BitmapEx();
     816             :         }
     817             :     } // end of namespace processor3d
     818             : } // end of namespace drawinglayer
     819             : 
     820             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10