Axiom.Core.PatchSurface.InterpolateVertexData C# (CSharp) Method

InterpolateVertexData() protected method

protected InterpolateVertexData ( IntPtr lockedBuffer, int leftIndex, int rightIndex, int destIndex ) : void
lockedBuffer System.IntPtr
leftIndex int
rightIndex int
destIndex int
return void
		protected unsafe void InterpolateVertexData( IntPtr lockedBuffer, int leftIndex, int rightIndex, int destIndex )
		{
			int vertexSize = declaration.GetVertexSize( 0 );
			VertexElement elemPos = declaration.FindElementBySemantic( VertexElementSemantic.Position );
			VertexElement elemNorm = declaration.FindElementBySemantic( VertexElementSemantic.Normal );
			VertexElement elemDiffuse = declaration.FindElementBySemantic( VertexElementSemantic.Diffuse );
			VertexElement elemTex0 = declaration.FindElementBySemantic( VertexElementSemantic.TexCoords, 0 );
			VertexElement elemTex1 = declaration.FindElementBySemantic( VertexElementSemantic.TexCoords, 1 );

			float* pDestReal, pLeftReal, pRightReal;
			byte* pDestChar, pLeftChar, pRightChar;
			byte* pDest, pLeft, pRight;

			// Set up pointers & interpolate
			pDest = ( (byte*)lockedBuffer + ( vertexSize * destIndex ) );
			pLeft = ( (byte*)lockedBuffer + ( vertexSize * leftIndex ) );
			pRight = ( (byte*)lockedBuffer + ( vertexSize * rightIndex ) );

			// Position
			pDestReal = (float*)( (byte*)pDest + elemPos.Offset );
			pLeftReal = (float*)( (byte*)pLeft + elemPos.Offset );
			pRightReal = (float*)( (byte*)pRight + elemPos.Offset );

			*pDestReal++ = ( *pLeftReal++ + *pRightReal++ ) * 0.5f;
			*pDestReal++ = ( *pLeftReal++ + *pRightReal++ ) * 0.5f;
			*pDestReal++ = ( *pLeftReal++ + *pRightReal++ ) * 0.5f;

			if ( elemNorm != null )
			{
				// Normals
				pDestReal = (float*)( (byte*)pDest + elemNorm.Offset );
				pLeftReal = (float*)( (byte*)pLeft + elemNorm.Offset );
				pRightReal = (float*)( (byte*)pRight + elemNorm.Offset );

				Vector3 norm = Vector3.Zero;
				norm.x = ( *pLeftReal++ + *pRightReal++ ) * 0.5f;
				norm.y = ( *pLeftReal++ + *pRightReal++ ) * 0.5f;
				norm.z = ( *pLeftReal++ + *pRightReal++ ) * 0.5f;
				norm.Normalize();

				*pDestReal++ = norm.x;
				*pDestReal++ = norm.y;
				*pDestReal++ = norm.z;
			}
			if ( elemDiffuse != null )
			{
				// Blend each byte individually
				pDestChar = (byte*)( pDest + elemDiffuse.Offset );
				pLeftChar = (byte*)( pLeft + elemDiffuse.Offset );
				pRightChar = (byte*)( pRight + elemDiffuse.Offset );

				// 4 bytes to RGBA
				*pDestChar++ = (byte)( ( ( *pLeftChar++ ) + ( *pRightChar++ ) ) * 0.5f );
				*pDestChar++ = (byte)( ( ( *pLeftChar++ ) + ( *pRightChar++ ) ) * 0.5f );
				*pDestChar++ = (byte)( ( ( *pLeftChar++ ) + ( *pRightChar++ ) ) * 0.5f );
				*pDestChar++ = (byte)( ( ( *pLeftChar++ ) + ( *pRightChar++ ) ) * 0.5f );
			}
			if ( elemTex0 != null )
			{
				// Blend each byte individually
				pDestReal = (float*)( (byte*)pDest + elemTex0.Offset );
				pLeftReal = (float*)( (byte*)pLeft + elemTex0.Offset );
				pRightReal = (float*)( (byte*)pRight + elemTex0.Offset );

				for ( int dim = 0; dim < VertexElement.GetTypeCount( elemTex0.Type ); dim++ )
				{
					*pDestReal++ = ( ( *pLeftReal++ ) + ( *pRightReal++ ) ) * 0.5f;
				}
			}
			if ( elemTex1 != null )
			{
				// Blend each byte individually
				pDestReal = (float*)( (byte*)pDest + elemTex1.Offset );
				pLeftReal = (float*)( (byte*)pLeft + elemTex1.Offset );
				pRightReal = (float*)( (byte*)pRight + elemTex1.Offset );

				for ( int dim = 0; dim < VertexElement.GetTypeCount( elemTex1.Type ); dim++ )
				{
					*pDestReal++ = ( ( *pLeftReal++ ) + ( *pRightReal++ ) ) * 0.5f;
				}
			}
		}