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