Esempio n. 1
0
		public void Gouraud(IBuffers buffer, Span span)
		{
			var spreadCount = span.XEnd - span.XStart;
			GouraudInterpolator.SetPoint(0, span.ZStart, span.ZEnd);
			GouraudInterpolator.SetPoint(1, span.ColorStart.Red, span.ColorEnd.Red);
			GouraudInterpolator.SetPoint(2, span.ColorStart.Green, span.ColorEnd.Green);
			GouraudInterpolator.SetPoint(3, span.ColorStart.Blue, span.ColorEnd.Blue);
			GouraudInterpolator.SetPoint(4, span.ColorStart.Alpha, span.ColorEnd.Alpha);

			var yOffset = buffer.FrameBuffer.Stride * span.Y;
			var rOffset = buffer.FrameBuffer.RedPosition;
			var gOffset = buffer.FrameBuffer.GreenPosition;
			var bOffset = buffer.FrameBuffer.BluePosition;
			var aOffset = buffer.FrameBuffer.AlphaPosition;
			var bufferOffset = yOffset + span.XStart;
			var depthBufferOffset = (buffer.Width*span.Y)+span.XStart;
			GouraudInterpolator.Interpolate(spreadCount);

			var xOffset = span.XStart;

			for (var index = 0; index < spreadCount; index++)
			{
				if (xOffset >= 0 && xOffset < buffer.Width)
				{

					var z = GouraudInterpolator.Points[0].InterpolatedValues[index];
					var bufferZ = (UInt32) (z*(float) UInt32.MaxValue);


					if (bufferZ < buffer.DepthBuffer[depthBufferOffset] &&
					    z >= 0f &&
					    z < 1f
						)
					{
						buffer.DepthBuffer[depthBufferOffset] = bufferZ;

						ColorAsVector.Red = GouraudInterpolator.Points[1].InterpolatedValues[index];
						ColorAsVector.Green = GouraudInterpolator.Points[2].InterpolatedValues[index];
						ColorAsVector.Blue = GouraudInterpolator.Points[3].InterpolatedValues[index];
						ColorAsVector.Alpha = GouraudInterpolator.Points[4].InterpolatedValues[index];
						var color = (int)ColorAsVector.ToColor().ToUInt32();
						buffer.FrameBuffer.Pixels[bufferOffset] = color;
					}
				}

				xOffset++;
				bufferOffset ++;

				depthBufferOffset++;
			}
		}
Esempio n. 2
0
		public void Flat(IBuffers buffer, Span span, Color color)
		{
			var spreadCount = span.Length; //span.XEnd - span.XStart;
			DepthInterpolator.SetPoint(0, span.ZStart, span.ZEnd);
			var yOffset = span.Y*buffer.FrameBuffer.Stride;
			var rOffset = buffer.FrameBuffer.RedPosition;
			var gOffset = buffer.FrameBuffer.GreenPosition;
			var bOffset = buffer.FrameBuffer.BluePosition;
			var aOffset = buffer.FrameBuffer.AlphaPosition;
			var bufferOffset = yOffset + span.XStart;
			var depthBufferOffset = (buffer.Width*span.Y) + span.XStart;
			DepthInterpolator.Interpolate(spreadCount);

			var colorAsInt = (int)color.ToUInt32();

			var xOffset = span.XStart;
			for( var index=0; index<spreadCount; index++ )
			{
				if (xOffset >= 0 && xOffset < buffer.Width)
				{
					var z = DepthInterpolator.Points[0].InterpolatedValues[index];
					var bufferZ = (UInt32) (z*(float) UInt32.MaxValue);

					if (bufferZ < buffer.DepthBuffer[depthBufferOffset] &&
					    z >= 0f &&
					    z < 1f
						)
					{
						buffer.FrameBuffer.Pixels[bufferOffset] = colorAsInt;
						buffer.DepthBuffer[depthBufferOffset] = bufferZ;
					}
					else
					{
						int i = 0;
						i++;
					}
				}

				xOffset++;
				bufferOffset++;
				depthBufferOffset++;
			}
		}
Esempio n. 3
0
        private static Span? GetSpan(int interpolationIndex, Interpolator interpolator, Vertex vertexA, bool useTexture)
        {
            var y = ((int)vertexA.TranslatedScreenCoordinates.Y) + interpolationIndex;
            var xstart = interpolator.Points[0].InterpolatedValues[interpolationIndex];
            var xend = interpolator.Points[1].InterpolatedValues[interpolationIndex];

            var ustart = 0f;
            var vstart = 0f;
            var uend = 0f;
            var vend = 0f;

            if (useTexture)
            {
                ustart = interpolator.Points[4].InterpolatedValues[interpolationIndex];
                uend = interpolator.Points[5].InterpolatedValues[interpolationIndex];
                vstart = interpolator.Points[6].InterpolatedValues[interpolationIndex];
                vend = interpolator.Points[7].InterpolatedValues[interpolationIndex];
            }

            var swap = false;

            if (xstart > xend)
            {
                var temp = xstart;
                xstart = xend;
                xend = temp;
                swap = true;

                temp = ustart;
                ustart = uend;
                uend = temp;

                temp = vstart;
                vstart = vend;
                vend = temp;
            }

            xend += 0.5f;
            var length = xend - xstart;

            var absLength = (int)System.Math.Round((double)length + 0.5);

            if (absLength >= 1)
            {
                var span = new Span
                            {
                                Y = y,
                                XStart = (int)xstart,
                                XEnd = (int)xend,
                                UStart = ustart,
                                UEnd = uend,
                                VStart = vstart,
                                VEnd = vend,
                                Length = absLength,
                                Swap = swap
                            };
                return span;
            }

            return null;
        }
Esempio n. 4
0
		public void Texture(IBuffers buffer, Span span, Image image, ImageContext texture)
		{
			var spreadCount = span.XEnd - span.XStart;
			TextureInterpolator.SetPoint(0, span.ZStart, span.ZEnd);
			TextureInterpolator.SetPoint(1, span.UStart, span.UEnd);
			TextureInterpolator.SetPoint(2, span.VStart, span.VEnd);
			var yOffset = span.Y * buffer.FrameBuffer.Stride;
			var rOffset = buffer.FrameBuffer.RedPosition;
			var gOffset = buffer.FrameBuffer.GreenPosition;
			var bOffset = buffer.FrameBuffer.BluePosition;
			var aOffset = buffer.FrameBuffer.AlphaPosition;
			var bufferOffset = yOffset + span.XStart;
			var depthBufferOffset = (buffer.Width * span.Y) + span.XStart;
			TextureInterpolator.Interpolate(spreadCount);

			var xOffset = span.XStart;

			for (var index = 0; index < spreadCount; index++)
			{
				if (xOffset >= 0 && xOffset < buffer.Width)
				{
					var z = TextureInterpolator.Points[0].InterpolatedValues[index];
					var bufferZ = (UInt32) (z*(float) UInt32.MaxValue);


					var u = TextureInterpolator.Points[1].InterpolatedValues[index];
					var v = TextureInterpolator.Points[2].InterpolatedValues[index];

					var intu = (int) (u*image.Width) & (image.Width - 1);
					var intv = (int) (v*image.Height) & (image.Height - 1);

					var texel = ((intv*image.Width) + intu);


					
					if (bufferZ < buffer.DepthBuffer[depthBufferOffset] &&
					    z >= 0f &&
					    z < 1f
						)
					{
						buffer.FrameBuffer.Pixels[bufferOffset] = texture.Pixels[texel];

						buffer.DepthBuffer[depthBufferOffset] = bufferZ;
					}
				}

				bufferOffset ++;
				depthBufferOffset++;
				xOffset++;
			}
		}