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